time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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?

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.

Example
input
3
1 3
3 512
3 079
output
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的更多相关文章

  1. Gym 101102B The Little Match Girl(贪心+规律)

    这个题目的做法不止一种,solve1:每个数字使用的火柴棒都在2~7之间,而且是连续的,就是2-7之前没有空着的数.这样就有一个结论,在下界为l,上界为r的情况下,假设有n个数,那么火柴棒总数一定在n ...

  2. Codeforces Gym 101252D&&floyd判圈算法学习笔记

    一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...

  3. Codeforces Gym 101190M Mole Tunnels - 费用流

    题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...

  4. Codeforces Gym 101623A - 动态规划

    题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...

  5. 【Codeforces Gym 100725K】Key Insertion

    Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...

  6. 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 ...

  7. codeforces gym 100553I

    codeforces gym 100553I solution 令a[i]表示位置i的船的编号 研究可以发现,应是从中间开始,往两边跳.... 于是就是一个点往两边的最长下降子序列之和减一 魔改树状数 ...

  8. CodeForces Gym 100213F Counterfeit Money

    CodeForces Gym题目页面传送门 有\(1\)个\(n1\times m1\)的字符矩阵\(a\)和\(1\)个\(n2\times m2\)的字符矩阵\(b\),求\(a,b\)的最大公共 ...

  9. Codeforces GYM 100876 J - Buying roads 题解

    Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...

随机推荐

  1. 把源码放到服务器部署webservice调用出错 MVC C#

    C# WebService在本地调用正常,同样的代码布睹到服务器上总是调用报如下错误 Server Error in '/' Application. The resource cannot be f ...

  2. 如何利用好github的问题

    github对我来说真的是一个超好的平台,不过之前只是把它仓库来使用, 后来在大佬告诉我应该怎么使用github,今天就来总结下如何利用好github,让它发挥最大的威力. 1.把github当做百科 ...

  3. Auth组件,Forms组件

    一.Auth组件默认auth_user表常用操作 #1.配置settings,使django与数据库连接 DATABASES = { 'default': { 'ENGINE': 'django.db ...

  4. 基础选择器,长度与颜色,标签display,嵌套关系,盒模型,盒模型布局

    css基础选择器 # *(统配选择器): 控制html, body,以及body下所有用于显示的标签 # div(标签选择器): 该标签名对应的所有该标签 # .(class选择器)(eg: .div ...

  5. Loader

    1.定义 可以把Loader当做一个占位符,即占有屏幕的某一个空间,当加载了组件之后,这个空间就能显示相应的图形了.所以可以给Loader设置anchor布局 2.加载组件 source:加载QML文 ...

  6. oracle a:=100 和 b=:c 区别

    a:=100  是赋值语句 b=:c  :c是一个整体,表示一个参数.

  7. Python3 tkinter基础 Label 显示的文字换行

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  8. Docker部署微服务

    部署时需要注!意!: 打开防火墙对应的应用端口!!用于外部访问!!内部互访问则不需要. 和对应数据库,缓存,消息中间件服务等的端口(当然这些服务必须先开启,它们也可使用docker部署开启) ,用于容 ...

  9. 【控制分片分配】控制Elasticsearch分片和副本的分配

    ES集群中索引可能由多个分片构成,并且每个分片可以拥有多个副本.通过将一个单独的索引分为多个分片,我们可以处理不能在一个单一的服务器上面运行的大型索引,简单的说就是索引的大小过大,导致效率问题.不能运 ...

  10. [bootstrapValidator] - bootstrap的验证工具

    翻了下之前和同事做的一个验证 <!--bootstrapValidator--> <script type="text/javascript" th:inline ...