1804A - Lame King - CodeForces Solution


greedy math

Please click on ads to support us..

C++ Code:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <stack>
#include <cmath>
#include <complex>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <vector>
using namespace std;
//================================================================
// يا رب
/*بسم الله الرحمن الرحيم
اللهم صلي علي سيدنا محمد
 */
/*
  سَبِّحِ اسْمَ رَبِّكَ الأَعْلَى
  الَّذِي خَلَقَ فَسَوَّى
  وَالَّذِي قَدَّرَ فَهَدَ
  وَالَّذِي أَخْرَجَ الْمَرْعَى
  فَجَعَلَهُ غُثَاءً أَحْوَى
  سَنُقْرِؤُكَ فَلا تَنسَى
  إِلاَّ مَا شَاء اللَّهُ إِنَّهُ يَعْلَمُ الْجَهْرَ وَمَا يَخْفَى
  وَنُيَسِّرُكَ لِلْيُسْرَى
فَذَكِّرْ إِن نَّفَعَتِ الذِّكْرَى
*/
//================================================================
//OTHERS
#define Ones(n) __builtin_popcount(n)
#define _Nader cin.tie(0), cout.tie(0), cin.sync_with_stdio(0), cout.sync_with_stdio(0);
#define END return 0;
#define e '\n'
#define inarr(n, arr) for(int ax = 0; ax<(n); ax++)cin>>(arr)[ax];
#define E '\n'
#define yes cout<<"YES"<<e;
#define no cout<<"NO"<<e;
#define YES cout<<"YES"<<e;
#define NO cout<<"NO"<<e;
#define GO continue;
#define ll long long
#define lll __int128_t
#define ld long double
//================================================================
// pairs
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<ld, ld>;
#define mp make_pair
#define f first
#define s second
//================================================================
// vectors
#define tcT template <class T
tcT > using V = vector<T>;
using vi = V<int>;
using vb = V<bool>;
using vl = V<ll>;
using vd = V<ld>;
using vs = V<string>;
using vpi = V<pi>;
using vpl = V<pl>;
using vpd = V<pd>;
using mll = map<ll,ll>;
using pll = pair<ll,ll>;

//================================================================
//LOOPS
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define f(i, a, b)  for(int i = a; i < b; i++)
#define R0F(i, a) ROF(i, 0, a)
#define rep(a) F0R(_, a)
#define each(a, x) for (auto &a : x)
//================================================================
//OPERATIONS
#define sz(x) int((x).size())
#define all(x) begin(x), end(x)
#define rall(x) x.rbegin(), x.rend()

#define pb push_back
#define PB push_back
#define eb emplace_back
#define SZ(x)  (x.size())
//================================================================
//CONSTANTS
const int OO = 0x3f3f3f3f;
const ll LOO = 0x3f3f3f3f3f3f3f3f;
const unsigned ll mod = 1000000007;
const ll MOD = 998244353;
const int N = 3e5+5;
const long double EPS = 1e-8;
//const ll N = 1'000'000'000'000L;
ll dx[] = {+0, +0, -1, +1, +1, +1, -1, -1};
ll dy[] = {-1, +1, +0, +0, +1, -1, +1, -1};
// string strpi = "3141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067";

//================================================================
void _GOAT_() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
#endif
}
//==================================================================
//Reminders
//partial_sum(v.begin(), v.end(), v.begin());
//     priority_queue<ll> pq;  reversed - sorted (lg n)
//    priority_queue<ll,vl,greater<ll>> pq(all(a)); only sorted

//struct X{
//    ll l,r,c;
//};
//==================================================================

void testCase(ll tc) {
    ll a,b;
    cin>>a>>b;
    ll ans = (abs(a)+abs(b))+max(abs(abs(a)-abs(b))-1,0LL);
    cout << ans <<e;

}


int main() {
    _Nader
    ll tt = 1;
  cin >> tt;
    ll tc = 0;
    while (tt--) {
        testCase(++tc);
    }
    END
}


Comments

Submit
0 Comments
More Questions

232. Implement Queue using Stacks
844. Backspace String Compare
20. Valid Parentheses
746. Min Cost Climbing Stairs
392. Is Subsequence
70. Climbing Stairs
53. Maximum Subarray
1527A. And Then There Were K
1689. Partitioning Into Minimum Number Of Deci-Binary Numbers
318. Maximum Product of Word Lengths
448. Find All Numbers Disappeared in an Array
1155. Number of Dice Rolls With Target Sum
415. Add Strings
22. Generate Parentheses
13. Roman to Integer
2. Add Two Numbers
515. Find Largest Value in Each Tree Row
345. Reverse Vowels of a String
628. Maximum Product of Three Numbers
1526A - Mean Inequality
1526B - I Hate 1111
1881. Maximum Value after Insertion
237. Delete Node in a Linked List
27. Remove Element
39. Combination Sum
378. Kth Smallest Element in a Sorted Matrix
162. Find Peak Element
1529A - Eshag Loves Big Arrays
19. Remove Nth Node From End of List
925. Long Pressed Name