贪心 Codeforces Round #173 (Div. 2) B. Painting Eggs

本文介绍了一个基于贪心策略的算法解决方案,用于解决 Codeforces Round #173 (Div.2) B.Painting Eggs 问题。通过每次选择能够最小化两个累计值之间差异的操作来构造答案,确保最终结果的绝对差值不超过500。

 

题目传送门

 1 /*
 2     题意:给出一种方案使得abs (A - G) <= 500,否则输出-1
 3     贪心:每次选取使他们相差最小的,然而并没有-1:)
 4 */
 5 #include <cstdio>
 6 #include <cstring>
 7 #include <iostream>
 8 #include <cmath>
 9 #include <algorithm>
10 using namespace std;
11 
12 const int MAXN = 1e6 + 10;
13 const int INF = 0x3f3f3f3f;
14 
15 int main(void)        //Codeforces Round #173 (Div. 2) B. Painting Eggs
16 {
17 //    freopen ("B.in", "r", stdin);
18 
19     int n;
20     while (scanf ("%d", &n) == 1)
21     {
22         string ans = "";    int sum_a = 0, sum_g = 0;
23         for (int i=1; i<=n; ++i)
24         {
25             int x, y;
26             scanf ("%d%d", &x, &y);
27             if (abs (sum_a + x - sum_g) <= abs (sum_g + y - sum_a))    {ans += "A";    sum_a += x;}
28             else    {ans += 'G';    sum_g += y;}
29         }
30 
31         cout << ans << endl;
32     }
33 
34     return 0;
35 }

 

转载于:https://www.cnblogs.com/Running-Time/p/4566365.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值