Codeforces Gym - 101102B - The Little Match Girl
1 second
256 megabytes
standard input
standard output
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?
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.
3
1 3
3 512
3 079
5
977
997
题意:略。
方法:贪心,先求出总的火柴数,再从最大数字开始贪,最后一个单独输出。
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1e5+;
int a[]={
,,,,,,,,,
};
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
char s[N];
int sum=;
scanf("%d %s",&n,s);
for(int i=;i<n;i++)
{
sum+=a[s[i]-''];
}
for(int i=;i<n;i++)
{
for(int j=;j>=;j--)
{
int temp=sum-a[j];
if(temp>=*(n-i)&&temp<=*(n-i))
{
cout<<j;
sum-=a[j];
break;
}
}
}
for(int i=;i>=;i--)
{
if(sum==a[i])
{
cout<<i<<endl;
break;
}
}
}
return ;
}
Codeforces Gym - 101102B - The Little Match Girl的更多相关文章
- Gym 101102B The Little Match Girl(贪心+规律)
这个题目的做法不止一种,solve1:每个数字使用的火柴棒都在2~7之间,而且是连续的,就是2-7之前没有空着的数.这样就有一个结论,在下界为l,上界为r的情况下,假设有n个数,那么火柴棒总数一定在n ...
- Codeforces Gym 101252D&&floyd判圈算法学习笔记
一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...
- Codeforces Gym 101190M Mole Tunnels - 费用流
题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...
- Codeforces Gym 101623A - 动态规划
题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...
- 【Codeforces Gym 100725K】Key Insertion
Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...
- Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】
2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...
- codeforces gym 100553I
codeforces gym 100553I solution 令a[i]表示位置i的船的编号 研究可以发现,应是从中间开始,往两边跳.... 于是就是一个点往两边的最长下降子序列之和减一 魔改树状数 ...
- CodeForces Gym 100213F Counterfeit Money
CodeForces Gym题目页面传送门 有\(1\)个\(n1\times m1\)的字符矩阵\(a\)和\(1\)个\(n2\times m2\)的字符矩阵\(b\),求\(a,b\)的最大公共 ...
- Codeforces GYM 100876 J - Buying roads 题解
Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...
随机推荐
- oracle No more data to read from socket之ora-07445排查解决
今天下午,原来一个部门的同事找过来,说有个即将上线的环境偶尔会出现 No more data to read from socket错误,版本是oracle 11.2.0.1,如下: 经查,这个问题原 ...
- 【题解】Luogu P4344 [SHOI2015]脑洞治疗仪
原题传送门:P4344 [SHOI2015]脑洞治疗仪 前置芝士:珂朵莉树 窝博客里对珂朵莉树的介绍 没什么好说的自己看看吧 珂朵莉树好题啊 我一开始一直Re65 后来重构代码就ac了,或许是rp问题 ...
- Python学习基础(三)——装饰器,列表生成器,斐波那契数列
装饰器——闭包 # 装饰器 闭包 ''' 如果一个内部函数对外部(非全局)的变量进行了引用,那么内部函数被认为是闭包 闭包 = 函数块 + 定义时的函数环境 ''' def f(): x = 100 ...
- amqp 抓包
1. wireshark 2. tcpick -yR -r file.name
- Android - Resource 之 String 小结
简单的string: <?xml version="1.0" encoding="utf-8"?> <resources> <st ...
- Python中的对象行为与特殊方法(二)类型检查与抽象基类
类型检查 创建类的实例时,该实例的类型为类本身: class Foo(object): pass f = Foo() 要测试实例是否属于某个类,可以使用type()内置函数: >>> ...
- 深刻理解Python中的元类(metaclass)以及元类实现单例模式
在理解元类之前,你需要先掌握Python中的类.Python中类的概念借鉴于Smalltalk,这显得有些奇特.在大多数编程语言中,类就是一组用来描述如何生成一个对象的代码段.在Python中这一点仍 ...
- Python 装饰器实例
retry 偶然看到一篇文章,想到了前几天的一个需求,git pull性能不稳,需要加入重试机制,正好这个装饰器的实例符合这样的场景. # coding:utf-8 import time impor ...
- FFmpeg:视频转码、剪切、合并、播放速调整
原文:https://fzheng.me/2016/01/08/ffmpeg/ FFmpeg:视频转码.剪切.合并.播放速调整 2016-01-08 前阵子帮导师处理项目 ppt,因为插入视频的格式问 ...
- Spring Security原理与应用
Spring Security是什么 Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在Spring应用上下文中配置 ...