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
Output
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;
}
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;
}
相关文章推荐
- Gym - 101102B B. The Little Match Girl 贪心、数论、分步
- GYM 101102 B.The Little Match Girl (贪心)
- B - The Little Match Girl Gym - 101102B 枚举技巧 火柴棒问题
- Codeforces 2016 ACM Amman Collegiate Programming Contest B. The Little Match Girl(贪心)
- Codeforces 2016 ACM Amman Collegiate Programming Contest B. The Little Match Girl(贪心)
- CF the little match girl (贪心!!!)
- hdu 4422 The Little Girl who Picks Mushrooms【贪心+分类讨论】
- ACM刷题之codeforces————The Little Match Girl
- 【瞎搞】 HDU 4422 The Little Girl who Picks Mushrooms
- GYM 100971 L.Chess Match(贪心)
- CodeForces 276D – Little Girl and Maximum XOR 贪心
- HDU 4422 The Little Girl who Picks Mushrooms(数学)
- ZOJ 3657 The Little Girl who Picks Mushrooms
- HDU 4422 The Little Girl Who Picks Mushrooms
- ZOJ - 3657-The Little Girl who Picks Mushrooms
- CodeFroces gym 100781 A.Adjoin the Networks(贪心)
- ZOJ 3657 The Little Girl who Picks Mushrooms(12年长春区域赛-C题-枚举)
- C - The Little Girl who Picks Mushrooms HDU - 4422
- The Little Girl who Picks Mushrooms(HDU - 4422)枚举 数学
- HDU 4422 The Little Girl who Picks Mushrooms(简单题)