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 ...
随机推荐
- java HelloWorld 提示“错误: 找不到或无法加载主类 HelloWorld“解决方案
在检查环境变量等前提工作准确无误后,注意要配好CLASSPATH,仍然报“错误: 找不到或无法加载主类 HelloWorld“. 本人工程目录:mygs-maven/src/main/java/hel ...
- sp_addlinkedserver '(null)' is an invalid product name
使用SSMS 2008客户端工具逆向生成了创建链接服务器的脚本时,在测试环境执行是报如下错误:'(null)' is an invalid product name. USE [master] GO ...
- TCP首部解析
TCP首部: TCP数据被封装在一个IP数据报中,如下: TCP首部数据格式: 16位源都口号,16为目的端口号用于寻找发送端和接收端的应用进程,加上IP首部的源端IP及终端IP,唯一的确认一个TCP ...
- SQL Server 2008 R2——以特定符号出现的次数来判断当前内容所在的层次
=================================版权声明================================= 版权声明:原创文章 禁止转载 请通过右侧公告中的“联系邮 ...
- linux shell 比较文件夹内容 diff
diff -ruNa test1 test2 > test12.diff -r 比较子目录中的文件 -u 以合并的方式显示文件的不同 -N 比较目录时,若文件A仅出现在某个目录中,预设 ...
- netty3升netty4一失眼成千古恨
老项目是netty3的,本来想直接改到netty5,但是netty5居然是只支持jdk1.7,很奇怪jdk1.6和jdk1.8都不行..为了兼容jdk1.6加上netty4本来和netty5就差别不大 ...
- .NET 缩略图服务器 ResizingServer
之前写过一篇文章 .NET 响应式自动缩略图服务器 之后对此Image Server又进行了基于ImageResizer的改写 基于.NET 图片服务器 支持缩略图格式 基于 ImageResizer ...
- 基于Zabbix IPMI监控服务器硬件状况
基于Zabbix IPMI监控服务器硬件状况 zabbix ipmi 公司有多个分部,且机房没有专业值班,机房等级不够.在这种情况下,又想实时监控机房环境,于是使用IPMI方式来达到目的.由于之前已经 ...
- 数据结构(Java描述)之线性表
基础概念 数据结构:是相互之间存在一种或多种关系的数据元素的集合. 逻辑结构和物理结构 关于数据结构,我们可以从逻辑结构和物理结构这两个维度去描述 逻辑结构是数据对象中数据元素之间的关系,是从逻辑意义 ...
- 2.一个EJB的小Demo
新建一个java普通项目即可 这里用到了Jboss,需要安装Jboss,然后进入jboss-4.2.3.GA\client目录,拷贝所有的jar包到本项目的lib下. 3个接口分别如下所示: publ ...