Gym - 101102B B. The Little Match Girl 贪心、数论、分步
来源:互联网 发布:mac 安装apache 编辑:程序博客网 时间:2024/06/05 10:30
Using at most 7 matchsticks, you can draw any of the 10 digits as in the following picture:
Zaytoonah has a number that consists of N digits. She wants to move some sticks (zero or more) to maximize the number. Note that she doesn’t want to remove any of the sticks, she will only move them from one place to another within the N digits. She also doesn’t want to add new digits as N is her lucky number.
Can you help Zaytoonah maximize her number?
The first line of input contains a single integer T, the number of test cases.
Each test case contains a single integer N (1 ≤ N ≤ 105), followed by a space, then N digits that represent the number Zaytoonah currently has.
For each test case, print on a single line the maximum number Zaytoonah can get.
31 33 5123 079
5977997
Source
2016 ACM Amman Collegiate Programming Contest
UESTC 2017 Winter Training #1
Gym - 101102B
My Solution
题意:给一串由火柴构成的数字,可以移动火柴改变数字,使得整个数尽可能大,但不能增加或减少数位。
贪心、数论、分步
每个数字的火柴数分别是a[0] = 6, a[1] = 2, a[2] = 5, a[3] = 5, a[4] = 4, a[5] = 5, a[6] = 6, a[7] = 3, a[8] = 7, a[9] = 6;
统计出总火柴数,
优先构造出6,此时要判断if(cnt - 6 * i >= (c - i) * 2 && cnt - 6 * i <= (c - i) * 7 && c)即剩余的火柴必须可以构造至少c-i个1(2),最多构造c-i个8(7),
然后构造8(7),if(cnt - 7 * i >= (c - i) * 2 && c)
再构造7(3),此时要判断if(cnt - 3 * i >= (c - i) * 2 && cnt - 3 * i <= (c - i) * 5 && c) 即剩余的火柴必须最多可以构造c-i个5(5),
然后构造5(5),4(4),1(2)。
复杂度 O(n)
#include <iostream>#include <cstdio>#include <string>#include <cstring>using namespace std;typedef long long LL;const int maxn = 1e6 + 8;string s;int a[10], cnt, ans[10];int main(){ #ifdef LOCAL freopen("b.txt", "r", stdin); //freopen("b.out", "w", stdout); #endif // LOCAL ios::sync_with_stdio(false); cin.tie(0); a[0] = 6, a[1] = 2, a[2] = 5, a[3] = 5, a[4] = 4, a[5] = 5, a[6] = 6, a[7] = 3, a[8] = 7, a[9] = 6; int T, n, cnt, c, i, k; cin >> T; while(T--){ memset(ans, 0, sizeof ans); cnt = c = 0; cin >> n >> s; for(i = 0; i < n; i++){ if(a[s[i] - '0'] == 6){ ans[9]++; } else{ cnt += a[s[i] - '0']; c++; } } k = cnt / 6; //cout << cnt << endl; for(i = min(c, k); i >= 0; i--){ if(cnt - 6 * i >= (c - i) * 2 && cnt - 6 * i <= (c - i) * 7 && c){ if(i == c){ if(cnt - 6 * i == 0){ ans[9] += i; cnt -= 6 * i; c -= i; break; } } else{ ans[9] += i; cnt -= 6 * i; c -= i; break; } } } k = cnt / 7; for(i = min(k, c); i >= 0; i--){ if(cnt - 7 * i >= (c - i) * 2 && c){ if(i == c){ if(cnt - 7 * i == 0){ ans[8] += i; cnt -= 7 * i; c -= i; break; } } else{ ans[8] += i; cnt -= 7 * i; c -= i; break; } } } k = cnt / 3; for(i = min(k, c); i >= 0; i--){ if(cnt - 3 * i >= (c - i) * 2 && cnt - 3 * i <= (c - i) * 5 && c){ if(i == c){ if(cnt - 3 * i == 0){ ans[7] += i; cnt -= 3 * i; c -= i; break; } } else{ ans[7] += i; cnt -= 3 * i; c -= i; break; } } } k = cnt / 5; for(i = min(k, c); i >= 0; i--){ if(cnt - 5 * i >= (c - i) * 2 && c){ if(i == c){ if(cnt - 5 * i == 0){ ans[5] += i; cnt -= 5 * i; c -= i; break; } } else{ ans[5] += i; cnt -= 5 * i; c -= i; break; } } } k = cnt / 4; for(i = min(k, c); i >= 0; i--){ if(cnt - 4 * i >= (c - i) * 2 && c){ if(i == c){ if(cnt - 4 * i == 0){ ans[4] += i; cnt -= 4 * i; c -= i; break; } } else{ ans[4] += i; cnt -= 4 * i; c -= i; break; } } } ans[1] = max(0, c); for(int i = 9; i >= 0; i--){ while(ans[i] > 0){ cout << i; ans[i]--; } } cout << endl; } return 0;}
Thank you!
------from ProLights
- Gym - 101102B B. The Little Match Girl 贪心、数论、分步
- GYM 101102 B.The Little Match Girl (贪心)
- The Little Match Girl Gym
- CF the little match girl (贪心!!!)
- Code Forces 276 B. Little Girl and Game 贪心
- B. Little Girl and Game
- B. Little Robber Girl's Zoo
- B. Little Robber Girl's Zoo(CF#359)
- codeforces 686B Little Robber Girl's Zoo (sort)
- CodeForces 686B Little Robber Girl's Zoo
- CodeForces 686B - Little Robber Girl's Zoo
- codeforces 276D D. Little Girl and Maximum XOR(贪心+dp+数论)
- ACM刷题之codeforces————The Little Match Girl
- Codeforces 403B Upgrading Array [贪心] [数论]
- 【CodeForces】gym-101205B Curvy Little Bottles (2012 ACM-ICPC World Finals B)
- D. Little Girl and Maximum XOR(贪心)
- hdu 4422 The Little Girl who Picks Mushrooms【贪心+分类讨论】
- Codeforces Round #359 (Div. 2) B. Little Robber Girl's Zoo
- 直接插入法排序
- 问题二十五:为什么有时候XnView无法显示PPM图片?
- 图论(三)图的遍历
- 【BS学习】——AJAX基础
- UML之用例图
- Gym - 101102B B. The Little Match Girl 贪心、数论、分步
- Linux--linux tail 命令详解
- 安装python-mysql出现的问题以及解决方法
- shell 编程
- 平面设计大神养成记之PS篇——编辑菜单栏
- M--二分查找
- Plplot绘制随时间变换的动态图
- OAuth2.0忽略state参数引发的CSRF漏洞
- day_06