您的位置:首页 > 其它

The Little Match Girl Gym - 101102B 【贪心, 上下界规律】

2017-08-17 21:22 591 查看
B. The Little Match Girl

time limit per test
1.0 s

memory limit per test
256 MB

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


1和2 两块代码思路相同,其实你列举出对应关系后会发现。用火柴棍摆出的每一种数字所用的火柴棍数都在2~7之间。

数               消耗

9                   6

8                  7

7                  3

5                  5

4                  4

1                  2

按这个顺序进行贪心,枚举就可以了。

需要注意的是你贪心的条件是    如果放了这个数字  剩余木棍数 >= 剩余位数×2   && 剩余木棍数 <= 剩余位数×7

因为每一位消耗都是大于等于2 小于等于7的。

1.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;
int p[10]={9,8,7,5,4,1};
int v[10]={6,7,3,5,4,2};
int a[20]={6,2,5,5,4,5,6,3,7,6};
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
char s[112345];
scanf("%d%s",&n,s);
int sum=0,len = strlen(s);
for(int i=0;i<len;i++)
{
sum += a[s[i]-'0'];
}
for(int i=0;i<len;i++)
{
int j=0;
while(sum-v[j]<(n-i-1)*2||sum-v[j]>(n-i-1)*7) j++;
sum -= v[j];
printf("%d",p[j]);
}
printf("\n");
}
return 0;
}


2.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;
int p[10]={9,8,7,5,4,1};
int v[10]={6,7,3,5,4,2};
int a[20]={6,2,5,5,4,5,6,3,7,6};
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
char s[112345];
scanf("%d%s",&n,s);
int sum=0,len = strlen(s);
for(int i=0;i<len;i++)
{
sum += a[s[i]-'0'];
}
for(int i=0;i<len;i++)
{
for(int j=0;j<6;j++)
{
if(sum-v[j]>=2*(n-1-i) && sum-v[j]<=7*(n-1-i))
{
sum-=v[j];
printf("%d",p[j]);
break;
}
}
}
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: