CF#202 div2 B Color the Fence

来源:互联网 发布:富豪移民 知乎 编辑:程序博客网 时间:2024/05/29 08:20

地址

http://codeforces.com/contest/349/problem/B

题意

给一个颜料的体积V,然后写数字1到9需要消耗的颜料数a[i],问最大能写的数字是多少。

解析

法一:
位数最多的数最大,所以先选择颜料最少的数字,然后写这么多位数字就行了。然而这种方法有错,所以需要在选出位数之后,再每一位和每一个数比较,如果当前比原位大的数能替换,则替换。

法二:
先算出最多的位数,然后对于每一位,从大到小遍历每一个数,如果满足条件:(v - a[i]) / minn == num && v >= a[i],即可。

总结

没考虑到定下最多位数之后,还能取大的数。

代码

法一:

#pragma comment(linker, "/STACK:1677721600")#include <map>#include <set>#include <cmath>#include <queue>#include <stack>#include <vector>#include <cstdio>#include <cstdlib>#include <cstring>#include <climits>#include <cassert>#include <iostream>#include <algorithm>#define pb push_back#define mp make_pair#define LL long long#define lson lo,mi,rt<<1#define rson mi+1,hi,rt<<1|1#define Min(a,b) ((a)<(b)?(a):(b))#define Max(a,b) ((a)>(b)?(a):(b))#define mem(a,b) memset(a,b,sizeof(a))#define FIN freopen("in.txt", "r", stdin)#define FOUT freopen("out.txt", "w", stdout)#define rep(i,a,b) for(int i=(a); i<=(b); i++)#define dec(i,a,b) for(int i=(a); i>=(b); i--)using namespace std;const int mod = 1e9 + 7;const double eps = 1e-8;const double ee = exp(1.0);const int inf = 0x3f3f3f3f;const int maxn = 1e6 + 10;const double pi = acos(-1.0);int readT(){    char c;    int ret = 0,flg = 0;    while(c = getchar(), (c < '0' || c > '9') && c != '-');    if(c == '-') flg = 1; else ret = c ^ 48;    while( c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c ^ 48);    return flg ? - ret : ret;}LL readTL(){    char c;    int flg = 0;    LL ret = 0;    while(c = getchar(), (c < '0' || c > '9') && c != '-');    if(c == '-') flg = 1; else ret = c ^ 48;    while( c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c ^ 48);    return flg ? - ret : ret;}struct Node{    int id, num;    bool operator < (const Node& other) const    {        if (num == other.num)        {            return id > other.id;        }        return num < other.num;    }} a[maxn];char ans[maxn];int cost[20];int main(){    #ifdef LOCAL    FIN;    #endif // LOCAL    int v = readT();    for (int i = 0; i <= 9; i++)    {        if (i == 0)            continue;        a[i].num = readT();        cost[i] = a[i].num;        a[i].id = i;    }    sort(a + 1, a + 10);    int cnt = 0;    for (int i = 1; i <= 9; i++)    {        int c = v / a[i].num;        for (int j = 0; j < c; j++)        {            ans[cnt++] = (a[i].id + '0');        }        v -= c * a[i].num;        if (v <= 0)        {            v = 0;            break;        }    }    if (cnt == 0)        puts("-1");    else    {        sort(ans, ans + cnt);        for (int i = cnt - 1; i >= 0; i--)        {            bool flg = true;            for (int j = 9; j >= 1; j--)            {                if (ans[i] - '0' < j)                {                    if (v + cost[ans[i] - '0'] >= cost[j])                    {                        v = v + cost[ans[i] - '0'] - cost[j];                        flg = false;                        printf("%d", j);                        break;                    }                }            }            if (flg)                printf("%c", ans[i]);        }        puts("");    }    return 0;}

法二:

#pragma comment(linker, "/STACK:1677721600")#include <map>#include <set>#include <cmath>#include <queue>#include <stack>#include <vector>#include <cstdio>#include <cstdlib>#include <cstring>#include <climits>#include <cassert>#include <iostream>#include <algorithm>#define pb push_back#define mp make_pair#define LL long long#define lson lo,mi,rt<<1#define rson mi+1,hi,rt<<1|1#define Min(a,b) ((a)<(b)?(a):(b))#define Max(a,b) ((a)>(b)?(a):(b))#define mem(a,b) memset(a,b,sizeof(a))#define FIN freopen("in.txt", "r", stdin)#define FOUT freopen("out.txt", "w", stdout)#define rep(i,a,b) for(int i=(a); i<=(b); i++)#define dec(i,a,b) for(int i=(a); i>=(b); i--)using namespace std;const int mod = 1e9 + 7;const double eps = 1e-8;const double ee = exp(1.0);const int inf = 0x3f3f3f3f;const int maxn = 1e6 + 10;const double pi = acos(-1.0);int readT(){    char c;    int ret = 0,flg = 0;    while(c = getchar(), (c < '0' || c > '9') && c != '-');    if(c == '-') flg = 1; else ret = c ^ 48;    while( c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c ^ 48);    return flg ? - ret : ret;}LL readTL(){    char c;    int flg = 0;    LL ret = 0;    while(c = getchar(), (c < '0' || c > '9') && c != '-');    if(c == '-') flg = 1; else ret = c ^ 48;    while( c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c ^ 48);    return flg ? - ret : ret;}int a[maxn];int main(){    #ifdef LOCAL    FIN;    #endif // LOCAL    int v = readT();    int minn = inf;    rep(i, 1, 9)    {        a[i] = readT();        minn = Min(minn, a[i]);    }    int num = v / minn;    if (num == 0)    {        puts("-1");    }    else    {        while (num--)        {            dec(i, 9, 1)            {                if ((v - a[i]) / minn == num && v >= a[i])                {                    printf("%d", i);                    v -= a[i];                    break;                }            }        }        puts("");    }    return 0;}
0 0