Codeforces 2016 ACM Amman Collegiate Programming Contest B. The Little Match Girl(贪心)
Description
Using at most 7 matchsticks, you can draw any of the 10 digits as in the following picture:
![](http://espresso.codeforces.com/253fe585d8f6d0b7200e946867f9996fc070012f.png)
The picture shows how many sticks you need to draw each of the digits.
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?
Input
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.
Output
For each test case, print on a single line the maximum number Zaytoonah can get.
Sample Input
3
1 3
3 512
3 079
Sample Output
5
977
997
思路
题意:
给出0-9十个数字,每个数字用固定的火柴棍搭成,给出由N个字符的构成数字串,只能移动火柴并且移动后数字的个数也是N个,问怎么移动火柴使数字值最大
题解:
每个数字的火柴根数固定,并且数字“1”所需要的火柴是十个数字中最少的,因此假定一开始全部都是由“1”组成N个数字,然后用剩下的火柴棍从前往后扫尽量使之最大。
#include<bits/stdc++.h> using namespace std; const int maxn = 100005; int main() { int T; int num[] = {6,2,5,5,4,5,6,3,7,6}; scanf("%d",&T); while (T--) { int N,sum = 0; char a[maxn]; scanf("%d %s",&N,a); for (int i = 0;i < N;i++) sum += num[a[i]-'0']; sum -= 2*N; for (int i = 0;i < N;i++) { if (sum >= 4) { sum -= 4; a[i] = '9'; } else if (sum > 0 && sum < 4) { for (int j = 9;j >= 0;j--) { if ((num[j] <= sum + 2 && i != N - 1) || num[j] == sum + 2) { sum -= num[j] - 2; a[i] = j + '0'; break; } } } else { a[i] = '1'; } } for (int i = N - 1;i >= 0 && sum > 0;i--) { a[i] = '8'; sum--; } printf("%s\n",a); } return 0; }
- Codeforces 2016 ACM Amman Collegiate Programming Contest B. The Little Match Girl(贪心)
- 【Codeforces】2015-2016 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2015) A Adjoin the Netwo
- Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)
- Gym 101102A Coins -- 2016 ACM Amman Collegiate Programming Contest(01背包变形)
- 2015-2016 ACM-ICPC Nordic Collegiate Programming Contest ---E题Entertainment Box(有点变化的贪心)
- 2016 ACM Amman Collegiate Programming Contest 训练赛部分题解
- 2015-2016 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2015) Adjoin the Networks (树的直径)
- 【Codeforces】2015-2016 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2015) E 线段树+离散化
- Gym - 101102B B. The Little Match Girl 贪心、数论、分步
- 2016 浙江省赛 The 13th Zhejiang Provincial Collegiate Programming Contest K题 SPFA
- Codeforces Gym 2015 ACM Amman Collegiate Programming Contest
- 2015 ACM Amman Collegiate Programming Contest D.Alternating Strings【Dp】
- 2015-2016 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2015) Disastrous Downtime(前缀和)
- 【ZOJ3941 The 13th Zhejiang Provincial Collegiate Programming ContestF】【贪心 暴力】Kpop Music Party n段part
- 2015-2016 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2015) Goblin Garden Guards (数论)
- 2015 ACM Amman Collegiate Programming Contest I.Bahosain and Digits【思维+暴力枚举】
- The 37th ACM/ICPC Asia Regional ChangChun Site Contest - C The Little Girl who Picks Mushrooms
- CF the little match girl (贪心!!!)
- 2016-2017 ACM-ICPC, Egyptian Collegiate Programming Contest (ECPC 16)
- GYM 101102 B.The Little Match Girl (贪心)