ACM/ICPC NAQ 2017. C. Canonical Coin System(背包dp)

写于大年二十八

继2018高中毕业前夕之后第一个在家过的春节,恍如隔世一般

说到春节,就有诸如“摄沙山脊”,“安克雷奇”之类的航班地图会涌上大脑,再没有比这些名词更奇怪的东西了,回家的时候那么令人期盼,离开时又那么冰凉,感不到一丝温度

这样想来,和在Nashville自闭比起俩,大年初一打比赛好像没什么大不了了

先搞一波NAQ吧,这道题17年的,思路很裸,贪心和dp解法一不一样,那就是无解,否则输出canonical

这题就是跑一遍完全背包,然后暴力算出贪心解,因为输入范围在1e6,可证,在2e6之内必然有解(mod)

然后这题难度也就这样吧,为什么只有一队写出来了?

#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace std;
#define limit (4000000 + 5)//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f
#define lowbit(i) i&(-i)//一步两步
#define EPS 1e-6
#define FASTIO  ios::sync_with_stdio(false);cin.tie(0);
#define ff(a) printf("%d\n",a );
#define pi(a,b) pair<a,b>
#define rep(i, a, b) for(ll i = a; i <= b ; ++i)
#define per(i, a, b) for(ll i = b ; i >= a  ; --i)
#define MOD 998244353
#define traverse(u) for(int i = head[u]; ~i ; i = edge[i].next)
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\data.txt", "rt", stdin)
#define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\dabiao.txt", "wt", stdout)
#define debug(x) cout<<x<<endl
typedef long long ll;
typedef unsigned long long ull;
char buf[1<<23],*p1=buf,*p2=buf,obuf[1<<23],*O=obuf;
inline ll read(){
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
    ll sign = 1, x = 0;char s = getchar();
    while(s > '9' || s < '0' ){if(s == '-')sign = -1;s = getchar();}
    while(s >= '0' && s <= '9'){x = (x << 3) + (x << 1) + s - '0';s = getchar();}
    return x * sign;
#undef getchar
}//快读
void print(ll x) {
    if(x/ 10) print(x / 10);
    *O++=x % 10+'0';
}
void write(ll x) {
    if(x < 0)putchar('-'),x = -x;
    print(x);
    fwrite(obuf,O-obuf,1,stdout);
    O = obuf;
}

int kase;
int a[limit];
int n,k;
int dp1[limit],dp2[limit];
int greedy(int x){
    int ans = 0;
    rep(i,1,n){
        if(!x)break;
        ans += x / a[i];
        x %= a[i];
    }
    return ans;
}

void solve(){
    cin>>n;
    rep(i,1,n){
        cin>>a[i];
    }
    sort(a + 1 , a + 1 + n,greater<>());
    memset(dp2, INF, sizeof(dp2));
    dp2[0] = 0;
    rep(i,1,n){
        rep(j,a[i],2e6 + 1){
            dp2[j] = min(dp2[j - a[i]] + 1, dp2[j]);
        }
    }
    rep(i,1,2e6 + 1){
        if(greedy(i) != dp2[i]){
            cout<<"non-canonical"<<endl;
            //cout<<i<<" "<<greedy(i)<<' '<<dp2[i]<<endl;
            return;
        }
    }
    cout<<"canonical"<<endl;
}
int main() {
#ifdef LOCAL
    FOPEN;
    //FOUT;
#endif
    FASTIO
    //cin>>kase;
    //while (kase--)
    solve();
    cerr << "Time elapsed: " << 1.0*clock()/CLOCKS_PER_SEC << "s\n";
    return 0;
}

 

posted @ 2021-02-09 21:14  tiany7  阅读(68)  评论(0编辑  收藏  举报