Codeforces Round #202 (Div. 2)B. Color the Fence

8956 단어
두 가지 해법, 하나는 dp, 하나는 욕심
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
int a[11];
int dp[1000100];
int pre[1000100];
int b[1000100];

int main(){
    int n;
    int S;
    while(scanf("%d",&S)!=EOF){
        for(int i=1;i<10;i++)
            cin>>a[i];
        int flag=1;
        for(int i=1;i<=9;i++){
            if(S>=a[i])
                flag=0;
        }
        mem0(dp);
        mem0(pre);
        if(flag==1){
            cout<<"-1"<<endl;
            continue;
        }
        for(int i=1;i<=9;i++)
            for(int j=a[i];j<=S;j++){
                    if(dp[j]<=dp[j-a[i]]+1){
                        dp[j]=dp[j-a[i]]+1;
                        pre[j]=i;
                    }
            }
        int num=0;
        while(S&&pre[S]!=0){
            b[num]=pre[S];
            num++;
            S-=a[pre[S]];
        }
        sort(b,b+num);
        for(int i=num-1;i>=0;i--)
            cout<<b[i];
        cout<<endl;
    }
}
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
int a[11];
int dp[100100];
int pre[100100];
int b[100100];

int main(){
    int n;
    int S;
    while(scanf("%d",&S)!=EOF){
        int minv=INF;
        int x;
        for(int i=1;i<10;i++){
            cin>>a[i];
            if(minv>=a[i]){
                x=i;
                minv=a[i];
            }
        }
        if(minv>S){
            cout<<"-1"<<endl;
            continue;
        }
        int count1=S/minv;
        if(S%minv==0){
            for(int i=1;i<=S/minv;i++)
                cout<<x;
            cout<<endl;
        }
        else{
            while(S>0){
                int b=0;
                for(int i=1;i<=9;i++){
                    if(S<a[i])
                        continue;
                    if((S-a[i])/minv==(count1-1)&&i>=x){
                        b=i;
                    }
                }
                if(b!=0){
                    cout<<b;
                    count1--;
                    S-=a[b];
                }
                if(b==0)
                    break;
            }
            cout<<endl;
        }
    }
    return 0;
}

좋은 웹페이지 즐겨찾기