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:
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
31 33 5123 079
Sample Output
5977997
思路
题意:
给出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 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)
传送门 Description Hasan and Bahosain want to buy a new video game, they want to share the expenses. Ha ...
- Gym 101102A Coins -- 2016 ACM Amman Collegiate Programming Contest(01背包变形)
A - Coins Time Limit:3000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Descript ...
- 2016 ACM Amman Collegiate Programming Contest D Rectangles
Rectangles time limit per test 5 seconds memory limit per test 256 megabytes input standard input ou ...
- 18春季训练01-3/11 2015 ACM Amman Collegiate Programming Contest
Solved A Gym 100712A Who Is The Winner Solved B Gym 100712B Rock-Paper-Scissors Solved C Gym 100712C ...
- ACM Amman Collegiate Programming Contest(7.22随机组队娱乐赛)
题目链接 https://vjudge.net/contest/240074#overview 只写一下自己做的几个题吧 /* D n^2的暴力dp怎么搞都可以的 这里先预处理 i到j的串时候合法 转 ...
- 2015 ACM Amman Collegiate Programming Contest 题解
[题目链接] A - Who Is The Winner 模拟. #include <bits/stdc++.h> using namespace std; int T; int n; s ...
- 2017 ACM Amman Collegiate Programming Contest 题解
[题目链接] A - Watching TV 模拟.统计一下哪个数字最多即可. #include <bits/stdc++.h> using namespace std; const in ...
- 2017 ACM Amman Collegiate Programming Contest
A - Watching TV /* 题意:求出出现次数最多的数字 */ #include <cstdio> #include <algorithm> #include < ...
- gym100712 ACM Amman Collegiate Programming Contest
非常水的手速赛,大部分题都是没有算法的.巨慢手速,老年思维.2个小时的时候看了下榜,和正常人差了3题(,最后还没写完跑去吃饭了.. A 水 Sort 比大小 /** @Date : 2017-09-0 ...
随机推荐
- vim插件管理vundle备忘
转自:http://blog.csdn.net/jiaolongdy/article/details/17889787/ http://www.cnblogs.com/xia520pi/archive ...
- yii2缓存的介绍和使用
作者:白狼 出处:http://www.manks.top/yii2_cache.html 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律 ...
- Ant :Property
Property Ant 内置的Property 系统属性 Ant附加的属性 自定义Property Ant :Property properties是由key-value组成的集合,就是Java中 ...
- sshfs 实现普通用户可读写
先实现普通用户免密码host1可登入host2host1# yum install fuse sshfs; 如果装不上,需要安装epep源 --enablerepo=epelhost1# su - e ...
- 设计模式C#实现(十五)——命令模式
意图 0 适用性 1 结构 2 实现 3 效果 4 参考 5 意图 将请求封装成一个对象,客户接受请求参数:可以对请求排队或者记录请求日志,以及可以支持撤销操作 适用性 抽象出待执行的动作以参数化某对 ...
- Entity Framework Code First反向生成代码
那些年我们生成的代码 早年,笨点的方法通常都是使用DbFirst先生成cs,然后把CS复制出来做些修改 后台基本上就自己使用T4来写,但是一直也没时间完善成通用的版本 MS官方 提供了EntityFr ...
- 解决开启SQL Server sql Always on Group 事务日志增大的问题
配置了Alwayson之后,因为没有只能使用完全恢复模式,不能使用简单或大容量日志模式,所以日志不断增长,不能使用改变恢复模式的方式清空日志 手动操作收缩或截断日志也无效 读了一些文章后发现,有人使用 ...
- php使用curl 检测socks5 代理的可用性
少废话 直接粘代码 <?php define('PROXY_CONF', 'ip:port'); define('PROXY_CHECK_URL', 'http://www.bai ...
- Java程序设计之扑克牌
这段代码的主要实现功能扑克牌的洗牌和发牌功能,一副牌,红桃,黑桃,梅花,方片,A~K,不含大小王. 构造一个class. 首先是声明花色: private String[] sign={"方 ...
- Intellij IDEA 快捷键整理(TonyCody)
[常规] Ctrl+Shift + Enter,语句完成 "!",否定完成,输入表达式时按 "!"键 Ctrl+E,最近的文件 Ctrl+Shift+E,最近更 ...