Codeforces 282B. Painting Eggs

The Bitlandians are quite weird people. They have very peculiar customs.

As is customary, Uncle J. wants to have n eggs painted for Bitruz (an ancient Bitland festival). He has asked G. and A. to do the work.

The kids are excited because just as is customary, they're going to be paid for the job! 

Overall uncle J. has got n eggs. G. named his price for painting each egg. Similarly, A. named his price for painting each egg. It turns out that for each egg the sum of the money both A. and G. want for the painting equals 1000.

Uncle J. wants to distribute the eggs between the children so as to give each egg to exactly one child. Also, Uncle J. wants the total money paid to A. to be different from the total money paid to G. by no more than 500.

Help Uncle J. Find the required distribution of eggs or otherwise say that distributing the eggs in the required manner is impossible.

Input

The first line contains integer n (1 ≤ n ≤ 106) — the number of eggs.

Next n lines contain two integers ai and gi each (0 ≤ ai, gi ≤ 1000; ai + gi = 1000): ai is the price said by A. for the i-th egg and gi is the price said by G. for the i-th egg.

Output

If it is impossible to assign the painting, print "-1" (without quotes).

Otherwise print a string, consisting of n letters "G" and "A". The i-th letter of this string should represent the child who will get the i-th egg in the required distribution. Letter "A" represents A. and letter "G" represents G. If we denote the money Uncle J. must pay A. for the painting as Sa, and the money Uncle J. must pay G. for the painting as Sg, then this inequality must hold: |Sa  -  Sg|  ≤  500. 

If there are several solutions, you are allowed to print any of them.

ai+gi=1000是关键,也就是说,每次你考虑投给a而不是g实际上对二者的总投资额差距都是造成了1000的影响,所以顺序和各个值就不那么重要了

首先全部投给a,然后一个个投给g,每次投sa-=ai,sg+=gi,直到|sa-sg|<=500

ps:还有一种可能就是都投a的,也要考虑

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;

#define SIZE 1000005

struct egg{
  LL a,b;
  LL used;
};

bool cmp(egg e1,egg e2){
  return (e1.b < e2.b);
}
egg e[SIZE];
LL sa = 0,sb = 0,n;
int main(){
  // freopen("test.in","r",stdin);
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i=1;i<=n;i++){
    cin >> e[i].a >> e[i].b;
    e[i].used = 0;
    sa += e[i].a;
  }

  if (abs(sa-sb) <= 500){
    for (int i=1;i<=n;i++){
      cout << "A";
    }
    return 0;
  }

  for (int i=1;i<=n;i++){
    sa -= e[i].a; sb += e[i].b;
    e[i].used = 1;
    if (abs(sa-sb) <= 500){
      break;
    }
  }
  for (int i=1;i<=n;i++){
    if (e[i].used){
      cout << "G";
    }
    else
      cout << "A";
  }
  return 0;
}
View Code
原文地址:https://www.cnblogs.com/ToTOrz/p/7570213.html