CF Round 4 div2B. Before an Exam(DP, 背包)
回复手感的第一天
怎么正解都是贪心啊,这题明明dp可以做(
首先题目问给定若干个区间,每个区间选择一个数字,要求构造一种情况使得和为k,并且给出方案,否则输出NO
看了下数据不是很大,似乎可以用背包做,然后记录转移路径就好
然后发现内存超了,我索性把map当作数组用,结果水过去了2333
#include <bits/stdc++.h> using namespace std; #define limit (200000 + 5)//防止溢出 #define INF 0x3f3f3f3f #define inf 0x3f3f3f3f3f #define lowbit(i) i&(-i)//一步两步 #define EPS 1e-9 #define FASTIO ios::sync_with_stdio(false);cin.tie(0),cout.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) 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, char c = 't') { if(x < 0)putchar('-'),x = -x; print(x); if(!isalpha(c))*O++ = c; fwrite(obuf,O-obuf,1,stdout); O = obuf; } int kase; int n, m,k; int a[limit]; int l[limit], r[limit]; map<int, map<int, int>> dp, inc; int tot; void solve(){ cin>>n>>k; rep(i,1,n){ cin>>l[i]>>r[i]; } rep(i,l[1],r[1]){ dp[1][i] = 1; inc[1][i] = i; } rep(i,2,n){ rep(j,l[i],r[i]){ per(h, j, k){ if(dp[i][h])continue; dp[i][h] = dp[i - 1][h - j]; if(dp[i][h]){ inc[i][h] = j; } } } } if(!dp[n][k]){ cout<<"NO"<<endl; }else{ int target = k; deque<int>ans; per(i,1,n){ // cout<<dp[i][target]<<" "<<inc[i][target]<<" "<<target<<endl; ans.push_front(inc[i][target]); // cout<<i<<" "<<target<<endl; target -= inc[i][target]; } cout<<"YES"<<endl; for(auto it : ans){ cout<<it<<" "; } cout<<endl; } } int32_t 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; }
天才选手zerol的主页:https://zerol.me/
|
WeepingDemon的个人主页:https://weepingdemon.gitee.io/blog/