CF834D. The Bakery(线段树优化dp 决策单调性优化dp)
// Problem: D. The Bakery// Contest: Codeforces - Codeforces Round #426 (Div. 2)// URL: Memory Limit: 256 MB// Time Limit: 2500 ms// // Powered by CP Editor (namespace std;typedef long long ll;typedef pairPII;inline ll read(){ll x = 0, f = 1;char ch = getchar();while(ch < '0' || ch > '9'){if(ch == '-')f = -1;ch = getchar();}while(ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}return x * f;}inline void write(ll x){if (x < 0) x = ~x + 1, putchar('-');if (x > 9) write(x / 10);putchar(x % 10 + '0');}#define rep(i,a,b) for(int i=(a);i<=(b);i++)#define per(i,a,b) for(int i=(a);i>=(b);i--)ll ksm(ll a, ll b,ll mod){ll res = 1;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;}#define read read()#define debug(x) cout<<#x<<":"<mid) update(u<<1|1,mid+1,r,ql,qr); pushup(u);}int query(int u,int l,int r,int ql,int qr){ if(ql<=l&&r<=qr){ return tr[u].maxx; } pushdown(u); int mid=(l+r)/2; int ans=0; if(ql<=mid) ans=max(ans,query(u<<1,l,mid,ql,qr)); if(qr>mid) ans=max(ans,query(u<<1|1,mid+1,r,ql,qr)); return ans;}int main(){ n=read,m=read; rep(i,1,n){ a[i]=read; pre[i]=pos[a[i]]+1; pos[a[i]]=i; } for(int j=1;j<=m;j++){ build(1,1,n,j); for(int i=1;i<=n;i++){ update(1,1,n,pre[i],i); if(j-1<=i) dp[i][j]=query(1,1,n,j-1,i); } } write(dp[n][m]); return 0;}
思路2:
再用莫队维护某段区间的不同数的个数。
代码:
// Problem: D. The Bakery// Contest: Codeforces - Codeforces Round #426 (Div. 2)// URL: Memory Limit: 256 MB// Time Limit: 2500 ms// // Powered by CP Editor (namespace std;typedef long long ll;typedef unsigned long long ull;typedef pairPLL;typedef pairPII;typedef pairPDD;typedef pairPSS;#define I_int llinline ll read(){ll x = 0, f = 1;char ch = getchar();while(ch < '0' || ch > '9'){if(ch == '-')f = -1;ch = getchar();}while(ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}return x * f;} inline void write(ll x){if (x < 0) x = ~x + 1, putchar('-');if (x > 9) write(x / 10);putchar(x % 10 + '0');} #define read read()#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)#define multiCase int T;cin>>T;for(int t=1;t<=T;t++)#define rep(i,a,b) for(int i=(a);i<=(b);i++)#define repp(i,a,b) for(int i=(a);i<(b);i++)#define per(i,a,b) for(int i=(a);i>=(b);i--)ll ksm(ll a, ll b,ll mod){ll res = 1;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;} const int maxn=35050,mod=1e9+7;const double pi = acos(-1);int dp[maxn][50],a[maxn],n,m;int cnt[maxn],ans,L=1,R;void add(int x){ cnt[x]++; if(cnt[x]==1) ans++;}void del(int x){ cnt[x]--; if(!cnt[x]) ans--;}int cul(int l,int r){ while(Ll) add(a[--L]); while(Rr) del(a[R--]); return ans;}void solve(int l,int r,int ql,int qr,int tot){ if(l>r) return ; int mid=(l+r)/2,qmid=ql; for(int i=ql;i<=min(qr,mid);i++){ int now=dp[i-1][tot-1]+cul(i,mid); if(now>dp[mid][tot]){ dp[mid][tot]=now;qmid=i; } } solve(l,mid-1,ql,qmid,tot); solve(mid+1,r,qmid,qr,tot);}int main(){ n=read,m=read; rep(i,1,n) a[i]=read; // memset(dp,0x3f,sizeof dp); dp[0][0]=0; for(int i=1;i<=m;i++) solve(1,n,1,n,i); cout<
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~