传送门

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(贪心)的更多相关文章

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

  2. Gym 101102A Coins -- 2016 ACM Amman Collegiate Programming Contest(01背包变形)

    A - Coins Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Descript ...

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

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

  5. ACM Amman Collegiate Programming Contest(7.22随机组队娱乐赛)

    题目链接 https://vjudge.net/contest/240074#overview 只写一下自己做的几个题吧 /* D n^2的暴力dp怎么搞都可以的 这里先预处理 i到j的串时候合法 转 ...

  6. 2015 ACM Amman Collegiate Programming Contest 题解

    [题目链接] A - Who Is The Winner 模拟. #include <bits/stdc++.h> using namespace std; int T; int n; s ...

  7. 2017 ACM Amman Collegiate Programming Contest 题解

    [题目链接] A - Watching TV 模拟.统计一下哪个数字最多即可. #include <bits/stdc++.h> using namespace std; const in ...

  8. 2017 ACM Amman Collegiate Programming Contest

    A - Watching TV /* 题意:求出出现次数最多的数字 */ #include <cstdio> #include <algorithm> #include < ...

  9. gym100712 ACM Amman Collegiate Programming Contest

    非常水的手速赛,大部分题都是没有算法的.巨慢手速,老年思维.2个小时的时候看了下榜,和正常人差了3题(,最后还没写完跑去吃饭了.. A 水 Sort 比大小 /** @Date : 2017-09-0 ...

随机推荐

  1. Linux命令学习总结:date命令

    命令简介: date 根据给定格式显示日期或设置系统日期时间.print or set the system date and time 指令所在路径:/bin/date 命令语法: date [OP ...

  2. MS SQL 统计信息浅析上篇

    统计信息概念 统计信息是一些对象,这些对象包含在表或索引视图中一列或多列中的数据分布有关的统计信息.数据库查询优化器使用这些统计信息来估计查询结果中的基数或行数. 通过这些基数估计,查询优化器可以生成 ...

  3. SSRS 2008 R2 错误:Timeout expired. The timeout period

    今天遇到了Reporting Services(SQL SERVER 2008 R2)的报表执行异常情况,报表加载数据很长时间都没有响应,最后报"An error occurred with ...

  4. SQLite使用(一)&&选择表类型

    在SQLite中,主要有两种表类型,带rowid的表和不带rowid的表.我们利用create table 建一张表,默认都会有一个隐含名字为rowid的主键,暂且称带rowid的表为普通表.如果建表 ...

  5. asp.net获取服务端和客户端信息

    asp.net获取服务端和客户端信息 获取服务器名:Page.Server.ManchineName获取用户信息:Page.User 获取客户端电脑名:Page.Request.UserHostNam ...

  6. [Java入门笔记] Java语言基础(五):数组

    简介 数组可用用于存储存储多个数据,Java的数组要求所有的数组元素具有一种相同的数据类型.一旦数组初始化完成,数组在内存中的空间被固定下来,长度不可改变,即使把数组的元素清空,所占用的空间依然被保留 ...

  7. WPF 依赖属性

    依赖属性,简单的说,在WPF控件应用过程中,界面上直接可以引用的属性 如:<Button Content="aaa"></Button> Content称为 ...

  8. Linux 下编译openjdk

    操作系统ubuntu14.04 openjdk版本 7u4 openjdk7u4可以在https://jdk7.java.net/source.html下载   一.构建编译环境 sudo apt-g ...

  9. 如何在Oracle中复制表结构和表数据

    1. 复制表结构及其数据: create table table_name_new as select * from table_name_old 2. 只复制表结构: create table ta ...

  10. spring项目部署到resin4中的无法注入问题

    碰到个奇葩事啊,一个spring的项目拿到客户现场部署到resin4中,启动后各种报无法注入bean,找不到bean的问题.出现大量下图的错误: nested exception is org.spr ...