{"id":1404,"date":"2013-03-17T13:48:23","date_gmt":"2013-03-17T05:48:23","guid":{"rendered":"https:\/\/blog.rexdf.org\/?p=1404"},"modified":"2013-03-17T13:48:23","modified_gmt":"2013-03-17T05:48:23","slug":"codeforces-round-173-div-2-painting-eggs","status":"publish","type":"post","link":"https:\/\/blog.rexdf.org\/en\/2013\/03\/codeforces-round-173-div-2-painting-eggs\/","title":{"rendered":"Codeforces Round #173 (Div. 2) Painting Eggs"},"content":{"rendered":"<div>\n<div>B. Painting Eggs<\/div>\n<div>\n<div>time limit per test<\/div>\n<p>5 seconds<\/p><\/div>\n<div>\n<div>memory limit per test<\/div>\n<p>256 megabytes<\/p><\/div>\n<div>\n<div>input<\/div>\n<p>standard input<\/p><\/div>\n<div>\n<div>output<\/div>\n<p>standard output<\/p><\/div>\n<\/div>\n<div>\n<p>The Bitlandians are quite weird people. They have very peculiar customs.<\/p>\n<p>As is customary, Uncle J. wants to have\u00a0<i>n<\/i>\u00a0eggs painted for Bitruz (an ancient Bitland festival). He has asked G. and A. to do the work.<\/p>\n<p>The kids are excited because just as is customary, they&#8217;re going to be paid for the job!<\/p>\n<p>Overall uncle J. has got\u00a0<i>n<\/i>\u00a0eggs. 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\u00a01000.<\/p>\n<p>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\u00a0500.<\/p>\n<p>Help Uncle J. Find the required distribution of eggs or otherwise say that distributing the eggs in the required manner is impossible.<\/p>\n<\/div>\n<div>\n<div>Input<\/div>\n<p>The first line contains integer\u00a0<i>n<\/i>\u00a0(1\u2009\u2264\u2009<i>n<\/i>\u2009\u2264\u200910<sup>6<\/sup>)\u00a0\u2014 the number of eggs.<\/p>\n<p>Next\u00a0<i>n<\/i>\u00a0lines contain two integers\u00a0<i>a<\/i><sub><i>i<\/i><\/sub>\u00a0and\u00a0<i>g<\/i><sub><i>i<\/i><\/sub>\u00a0each\u00a0(0\u2009\u2264\u2009<i>a<\/i><sub><i>i<\/i><\/sub>,\u2009<i>g<\/i><sub><i>i<\/i><\/sub>\u2009\u2264\u20091000;\u00a0<i>a<\/i><sub><i>i<\/i><\/sub>\u2009+\u2009<i>g<\/i><sub><i>i<\/i><\/sub>\u2009=\u20091000):\u00a0<i>a<\/i><sub><i>i<\/i><\/sub>\u00a0is the price said by A. for the\u00a0<i>i<\/i>-th egg and\u00a0<i>g<\/i><sub><i>i<\/i><\/sub>\u00a0is the price said by G. for the\u00a0<i>i<\/i>-th egg.<\/p>\n<\/div>\n<div>\n<div>Output<\/div>\n<p>If it is impossible to assign the painting, print &#8220;-1&#8221; (without quotes).<\/p>\n<p>Otherwise print a string, consisting of\u00a0<i>n<\/i>\u00a0letters &#8220;G&#8221; and &#8220;A&#8221;. The\u00a0<i>i<\/i>-th letter of this string should represent the child who will get the\u00a0<i>i<\/i>-th egg in the required distribution. Letter &#8220;A&#8221; represents A. and letter &#8220;G&#8221; represents G. If we denote the money Uncle J. must pay A. for the painting as\u00a0<i>S<\/i><sub><i>a<\/i><\/sub>, and the money Uncle J. must pay G. for the painting as\u00a0<i>S<\/i><sub><i>g<\/i><\/sub>, then this inequality must hold:\u00a0|<i>S<\/i><sub><i>a<\/i><\/sub>\u2009\u2009-\u2009\u2009<i>S<\/i><sub><i>g<\/i><\/sub>|\u2009\u2009\u2264\u2009\u2009500.<\/p>\n<p>If there are several solutions, you are allowed to print any of them.<\/p>\n<\/div>\n<div>\n<div>Sample test(s)<\/div>\n<div>\n<div>\n<div>input<\/div>\n<pre>2 1 999 999 1<\/pre>\n<\/div>\n<div>\n<div>output<\/div>\n<pre>AG<\/pre>\n<\/div>\n<div>\n<div>input<\/div>\n<pre>3 400 600 400 600 400 600<\/pre>\n<\/div>\n<div>\n<div>output<\/div>\n<pre>AGA<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<pre class=\"brush: cpp; gutter: true\">\r\n#include &lt;iostream&gt;\r\n#include &lt;cstdio&gt;\r\n#include &lt;cstdlib&gt;\r\n#include &lt;algorithm&gt;\r\n\r\nusing namespace std;\r\n\r\nconst int MAXN=1000005;\r\n\r\nstruct egg\r\n{\r\n\tint a,g;\r\n\tint id;\r\n\tchar ch;\r\n}eggs[MAXN];\r\nint n;\r\n\r\nbool operator&lt;(const egg a,const egg b)\r\n{\r\n\treturn a.a&lt;b.a;\r\n}\r\n\r\nint cmp(const void *a,const void *b)\r\n{\r\n\treturn ((egg *)a)-&gt;id &lt; ((egg *)b)-&gt;id;\r\n}\r\n\r\nint Abs(int a)\r\n{\r\n\tif(a&lt;0)return -a;\r\n\treturn a;\r\n}\r\n\r\nint main()\r\n{\r\n\tint ans1,ans2,ans;\r\n\tint st,ed;\r\n\tint tmp1,tmp2;\r\n\twhile(cin&gt;&gt;n)\r\n\t{\r\n\t\tfor(int i=0;i&lt;n;i++)\r\n\t\t{\r\n\t\t\tcin&gt;&gt;eggs[i].a&gt;&gt;eggs[i].g;\r\n\t\t\teggs[i].id=i;\r\n\t\t}\r\n\t\tsort(eggs,eggs+n);\r\n\t\tans1=0;ans2=0;\r\n\t\tst=0;ed=n-1;\r\n\t\twhile(st&lt;=ed)\r\n\t\t{\r\n\t\t\ttmp1=Abs(ans1+eggs[st].a-ans2);\r\n\t\t\ttmp2=Abs(ans2+eggs[ed].g-ans1);\r\n\t\t\tif(tmp1&lt;=Abs(ans2-ans1))\r\n\t\t\t{\r\n\t\t\t\tans1+=eggs[st].a;\r\n\t\t\t\teggs[st].ch=&#039;A&#039;;\r\n\t\t\t\tst++;\r\n\t\t\t}\r\n\t\t\telse if(tmp2&lt;=Abs(ans2-ans1))\r\n\t\t\t{\r\n\t\t\t\t\/\/if(ans2+eggs[ed].g)\r\n\t\t\t\tans2+=eggs[ed].g;\r\n\t\t\t\teggs[ed].ch=&#039;G&#039;;\r\n\t\t\t\ted--;\r\n\t\t\t}\r\n\t\t\telse if(tmp1&lt;=tmp2)\r\n\t\t\t{\r\n\t\t\t\tans1+=eggs[st].a;\r\n\t\t\t\teggs[st].ch=&#039;A&#039;;\r\n\t\t\t\tst++;\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\tans2+=eggs[ed].g;\r\n\t\t\t\teggs[ed].ch=&#039;G&#039;;\r\n\t\t\t\ted--;\r\n\t\t\t}\r\n\t\t}\r\n\t\tans=ans1-ans2;\r\n\t\tif(ans&lt;0)ans=-ans;\r\n\t\tif(ans&gt;500)cout&lt;&lt;-1&lt;&lt;endl;\r\n\t\telse\r\n\t\t{\r\n\t\t\tqsort(eggs,n,sizeof(egg),cmp);\r\n\t\t\tfor(int i=0;i&lt;n;i++)\r\n\t\t\t  cout&lt;&lt;eggs[i].ch;\r\n\t\t\tcout&lt;&lt;endl;\r\n\t\t}\r\n\t}\r\n\t\/\/system(&quot;pause&quot;);\r\n\treturn 0;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>B. Painting Eggs time 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 have\u00a0n\u00a0eggs &hellip; <a href=\"https:\/\/blog.rexdf.org\/en\/2013\/03\/codeforces-round-173-div-2-painting-eggs\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[125],"class_list":["post-1404","post","type-post","status-publish","format-standard","hentry","category-algorithm","tag-codeforces"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/blog.rexdf.org\/en\/wp-json\/wp\/v2\/posts\/1404","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.rexdf.org\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.rexdf.org\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.rexdf.org\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.rexdf.org\/en\/wp-json\/wp\/v2\/comments?post=1404"}],"version-history":[{"count":0,"href":"https:\/\/blog.rexdf.org\/en\/wp-json\/wp\/v2\/posts\/1404\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.rexdf.org\/en\/wp-json\/wp\/v2\/media?parent=1404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.rexdf.org\/en\/wp-json\/wp\/v2\/categories?post=1404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.rexdf.org\/en\/wp-json\/wp\/v2\/tags?post=1404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}