B. Painting egstime limit per test
5 seconds
Memory limit per test
256 megabytes
Input
Standard input
Output
Standard output
The Bitlandians are quite weird people. They have very peculiar customs.
As is customary, Uncle J. wants to haveNEggs painted for Bitruz (an alert ent Bitland festival). He has asked G. and A. to do the work.
The kids are excited because just as is Mary, they're going to be paid for the job!
Overall uncle J. has gotNEggs. 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. 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 integerN(1 digit ≤ DigitNLimit ≤ limit 106 )-
The number of eggs.
NextNLines contain two integersAIAndGIEach (0 limit ≤ limitAI, Bytes,GILimit ≤ limit 1000;AIRegion + RegionGILimit = Limit 1000 ):AIIs
The price said by A. forI-Th egg andGIIs
The price said by G. forI-Th egg.
Output
If it is impossible to assign the painting, print "-1" (without quotes ).
Otherwise print a string, consistingNLetters "G"
And "A".I-Th letter
Of this string shoshould represent the child who will getI-Th egg in the required distribution. Letter ""
Represents A. and letter "G" represents G. If we denote the money Uncle J. must pay A. for the paintingSA,
And the money Uncle J. must pay G. for the paintingSG,
Then this inequality must hold: |SAZookeeper-Zookeeper-SG| Commandid ≤ commandid 500.
If there are several solutions, you are allowed to print any of them.
Sample test (s) input
21 999999 1
Output
AG
Input
3400 600400 600400 600
Output
AGA
The general idea is: let you handle n eggs, then A and G give their prices for each serial number of eggs, then let you select an egg from A and G, and select n egg as appropriate, make sure that the absolute value of the total wage difference between the last A and G is less than or equal to 500. Then, select the service or G service for each egg in sequence. (Output can be any result) if the conditional output is not met-1. algorithm: if the data size of this question is 1 E6, it is considered n or n * logn algorithms. We can solve this problem by sorting n * logn. For greedy algorithms (sorting twice), see the code:
# Include <stdio. h> # include <iostream> # include <algorithm> # include <stdlib. h> # include <math. h> using namespace std; struct node {// rank records the input data. The judge record indicates who handles the egg, and a indicates the salary of, B is the wage of G processing int a, B, rank, judge;} egg [1100000]; int cmp (node a, node B) {return. a <B. a;} int cmp1 (node a, node B) {return. rank <B. rank;} int main () {int n; while (scanf ("% d", & n )! = EOF) {int I; for (I = 1; I <= n; I ++) {scanf ("% d", & egg [I]. a, & egg [I]. b); egg [I]. rank = I;} sort (egg + 1, egg + n + 1, cmp); // For A, int ans1 = 0, ans2 = 0, j = n; I = 1; while (I <= j) {// key point determines who the eggs belong to. if (ans1 + egg [I]. a> 500) {egg [j]. judge = 2; ans1-= egg [j]. b; j --;} else {ans1 + = egg [I]. a; egg [I]. judge = 1; I ++; }}if (abs (ans1)> 500) printf ("-1"); else {// select A for output in the input order, G sequence number sort (egg + 1, egg + 1 + n, cmp1); for (I = 1; I <= n; I ++) if (egg [I]. judge = 1) printf ("A"); else printf ("G");} printf ("\ n");} return 0 ;}