Codeforces 349B

349B - Color the Fence

贪心

代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
const int N=1e6+5;
const int INF=0x7f7f7f7f;
int a[10];
int main()
{
    int n;
    int MIN=INF;
    int index=0;
    cin>>n;
    for(int i=1;i<=9;i++)
    {
        cin>>a[i];
        if(a[i]<=MIN)
        {
            MIN=a[i];
            index=i;
        }
    }    
    if(n<MIN)
    {
        cout<<-1<<endl;
        return 0;
    }
    int mod=n%MIN;
    for(int i=0;i<n/MIN;i++)
    {
        for(int j=9;j>=index;j--)
        {
            if(mod+a[index]-a[j]>=0)
            {
                cout<<j;
                mod-=a[j]-a[index];
                break;
            }
        }
    }
    cout<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/widsom/p/7132218.html