282B - Painting Eggs - CodeForces Solution


greedy math *1500

Please click on ads to support us..

Python Code:

ans = []
num = 0
for i in range(int(input())):
    ar = input().split()
    first_to_int = int(ar[0])
    second_to_int = int(ar[1])
    if i == 0:
        if first_to_int>= second_to_int:
            num -= second_to_int
            ans.append("G")
            continue
        else:
            num += first_to_int
            ans.append("A")
            continue
    if abs(num + first_to_int) < abs(num - second_to_int):
        num += first_to_int
        ans.append("A")
    elif abs(num+first_to_int) >= abs(num-second_to_int):
        num -= second_to_int
        ans.append("G")

if num <= 500:
    print("".join(ans))
else:
    print("-1")

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define faster ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define nl cout << "\n"
#define ll long long
#define lli long long int
#define lp(i, a, n) for (ll i =a; i < n; i++)
#define rlp(i, n) for (int i = (int)n - 1; i >= 0; i--)
#define sortAll(x) sort(x.begin(), x.end())
#define sortRall(x) sort(x.rbegin(), x.rend())
#define out(x) cout << x << '\n'
#define pb(x) push_back(x)
#define coutarr(x, n) for (int i = 0; i < (int)n; i++) cout << x[i] << " ";cout << "\n";
#define coutvec(vec) for (ll i = 0; i < vec.size(); i++) cout << vec[i] << " ";cout << "\n";
#define cinarr(x, n) for (int i = 0; i < (int)n; i++) cin >> x[i];
#define cinAndPush(arr, n, vec) for (ll i = 0; i < n; i++) cin >> arr[i], vec.push_back(arr[i]);
#define cinArrSum(arr,n,sum) for(ll i=0; i<n; i++) cin>>arr[i],sum+=arr[i];

ll gcd(ll x, ll y)
{
    if (y == 0)
        return x;
    return gcd(y, x % y);
}

bool check(ll arr[], ll n)
{
}

ll factorial(ll n)
{
    return (n==1 || n==0) ? 1: n * factorial(n - 1);
}

ll ans=0;
void solve(string s)
{
  if(s[1]=='+'){
    ans++;
  }
  else
  {
    ans--;
  }
  
}


int main()
{
  faster;
 ll n; cin>>n;
 ll tol=0;
 string keep;
 while (n--)
 {
  ll sa,sg;
  cin>>sa>>sg;
  if(sa+tol <=500)
  { 
    tol+=sa;
    keep+='A';
  }
  else
  {
    tol-=sg;
    keep+='G';
  }
  
 }
  out(keep);
}  


	  	 	 	      	   			 		 		 	


Comments

Submit
0 Comments
More Questions

HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations
1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array
461. Hamming Distance
1748. Sum of Unique Elements
897. Increasing Order Search Tree
905. Sort Array By Parity
1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time
700. Search in a Binary Search Tree
590. N-ary Tree Postorder Traversal
589. N-ary Tree Preorder Traversal
1299. Replace Elements with Greatest Element on Right Side
1768. Merge Strings Alternately
561. Array Partition I
1374. Generate a String With Characters That Have Odd Counts
1822. Sign of the Product of an Array
1464. Maximum Product of Two Elements in an Array
1323. Maximum 69 Number
832. Flipping an Image
1295. Find Numbers with Even Number of Digits