Gym - 101102B B. The Little Match Girl 贪心、数论、分步

来源:互联网 发布:mac 安装apache 编辑:程序博客网 时间:2024/06/05 10:30

B. The Little Match Girl
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
31 33 5123 079
output
5977997



Source

2016 ACM Amman Collegiate Programming Contest

UESTC 2017 Winter Training #1

Gym - 101102B


My Solution

题意:给一串由火柴构成的数字,可以移动火柴改变数字,使得整个数尽可能大,但不能增加或减少数位。


贪心、数论、分步

每个数字的火柴数分别是a[0] = 6, a[1] = 2, a[2] = 5, a[3] = 5, a[4] = 4, a[5] = 5, a[6] = 6, a[7] = 3, a[8] = 7, a[9] = 6;

统计出总火柴数,

优先构造出6,此时要判断if(cnt - 6 * i >= (c - i) * 2 && cnt - 6 * i <= (c - i) * 7 && c)即剩余的火柴必须可以构造至少c-i个1(2),最多构造c-i个8(7),

然后构造8(7),if(cnt - 7 * i >= (c - i) * 2 && c)

再构造7(3),此时要判断if(cnt - 3 * i >= (c - i) * 2 && cnt - 3 * i <= (c - i) * 5 && c) 即剩余的火柴必须最多可以构造c-i个5(5),

然后构造5(5),4(4),1(2)。

复杂度 O(n)


#include <iostream>#include <cstdio>#include <string>#include <cstring>using namespace std;typedef long long LL;const int maxn = 1e6 + 8;string s;int a[10], cnt, ans[10];int main(){    #ifdef LOCAL    freopen("b.txt", "r", stdin);    //freopen("b.out", "w", stdout);    #endif // LOCAL    ios::sync_with_stdio(false); cin.tie(0);    a[0] = 6, a[1] = 2, a[2] = 5, a[3] = 5, a[4] = 4, a[5] = 5, a[6] = 6, a[7] = 3, a[8] = 7, a[9] = 6;    int T, n, cnt, c, i, k;    cin >> T;    while(T--){        memset(ans, 0, sizeof ans);        cnt = c = 0;        cin >> n >> s;        for(i = 0; i < n; i++){            if(a[s[i] - '0'] == 6){                ans[9]++;            }            else{                cnt += a[s[i] - '0'];                c++;            }        }        k = cnt / 6;        //cout << cnt << endl;        for(i = min(c, k); i >= 0; i--){            if(cnt - 6 * i >= (c - i) * 2 && cnt - 6 * i <= (c - i) * 7 && c){                if(i == c){                    if(cnt - 6 * i == 0){                        ans[9] += i;                        cnt -= 6 * i;                        c -= i;                        break;                    }                }                else{                    ans[9] += i;                    cnt -= 6 * i;                    c -= i;                    break;                }            }        }        k = cnt / 7;        for(i = min(k, c); i >= 0; i--){            if(cnt - 7 * i >= (c - i) * 2 && c){                if(i == c){                    if(cnt - 7 * i == 0){                        ans[8] += i;                        cnt -= 7 * i;                        c -= i;                        break;                    }                }                else{                    ans[8] += i;                    cnt -= 7 * i;                    c -= i;                    break;                }            }        }        k = cnt / 3;        for(i = min(k, c); i >= 0; i--){            if(cnt - 3 * i >= (c - i) * 2 && cnt - 3 * i <= (c - i) * 5 && c){                if(i == c){                    if(cnt - 3 * i == 0){                        ans[7] += i;                        cnt -= 3 * i;                        c -= i;                        break;                    }                }                else{                    ans[7] += i;                    cnt -= 3 * i;                    c -= i;                    break;                }            }        }        k = cnt / 5;        for(i = min(k, c); i >= 0; i--){            if(cnt - 5 * i >= (c  - i) * 2 && c){                if(i == c){                    if(cnt - 5 * i == 0){                        ans[5] += i;                        cnt -= 5 * i;                        c -= i;                        break;                    }                }                else{                    ans[5] += i;                    cnt -= 5 * i;                    c -= i;                    break;                }            }        }        k = cnt / 4;        for(i = min(k, c); i >= 0; i--){            if(cnt - 4 * i >= (c  - i) * 2 && c){                if(i == c){                    if(cnt - 4 * i == 0){                        ans[4] += i;                        cnt -= 4 * i;                        c -= i;                        break;                    }                }                else{                    ans[4] += i;                    cnt -= 4 * i;                    c -= i;                    break;                }            }        }        ans[1] = max(0, c);        for(int i = 9; i >= 0; i--){            while(ans[i] > 0){                cout << i;                ans[i]--;            }        }        cout << endl;    }    return 0;}


  Thank you!

                                                                                                                                               ------from ProLights

0 0