https://vjudge.net/contest/310704 dp+点分治
密码:996996
A - Lighting System Design
这个题目毒的太累了,dp[i]代表选前i个灯最少的消费,先按电压从小到大排,方便后面的转移。
#include<bits/stdc++.h>
#define il inline
#define pb push_back
#define fi first
#define se second
#define ms(_data,v) memset(_data,v,sizeof(_data))
#define sc(n) scanf("%d",&n)
#define SC(n,m) scanf("%d %d",&n,&m)
#define SZ(a) int((a).size())
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define drep(i,a,b) for(int i=a;i>=b;--i)
using namespace std;
typedef long long ll;
const ll inf=0x3f3f3f3f;
const double PI=acos(-1.0);
const double eps=1e-9;
const int maxn=1e3+5;
struct node{
int v,k,c,l;
}a[maxn];
bool cmp(node x,node y){
return x.v<y.v;
}
int n,sum[maxn],dp[maxn];
int main(){
std::ios::sync_with_stdio(0);
while(cin>>n && n){
rep(i,1,n) cin>>a[i].v>>a[i].k>>a[i].c>>a[i].l;
sort(a+1,a+n+1,cmp);
rep(i,1,n) sum[i]=sum[i-1]+a[i].l;
ms(dp,0x3f),dp[0]=0;
for(int i=1;i<=n;++i){
for(int j=1;j<=i;++j){
dp[i]=min(dp[i],dp[j-1]+a[i].k+a[i].c*(sum[i]-sum[j-1]));
}
}
cout<<dp[n]<<endl;
}
return 0;
}
B - Partitioning by Palindromes
dp[i]代表到i字符最多的回文串,转移也很简单,但是要先处理一下i~j是不是回文串,一个简单的区间dp。
#include<bits/stdc++.h>
#define il inline
#define pb push_back
#define fi first
#define se second
#define ms(_data,v) memset(_data,v,sizeof(_data))
#define sc(n) scanf("%d",&n)
#define SC(n,m) scanf("%d %d",&n,&m)
#define SZ(a) int((a).size())
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define drep(i,a,b) for(int i=a;i>=b;--i)
using namespace std;
typedef long long ll;
const ll inf=0x3f3f3f3f;
const double PI=acos(-1.0);
const double eps=1e-9;
const int maxn=1e3+5;
int T,dp[maxn];
bool isp[maxn][maxn];
int main(){
std::ios::sync_with_stdio(0);
cin>>T;
string s;
while(T--){
cin>>s;
s.insert(s.begin(),'#');
ms(dp,0),ms(isp,0);
int len=SZ(s)-1;
rep(i,1,len) isp[i][i]=1;
for(int le=2;le<=len;++le){
for(int i=1;i+le-1<=len;++i){
int j=i+le-1;
if(s[i]==s[j] && (isp[i+1][j-1]==1 || le==2)) isp[i][j]=1;
}
}
for(int i=1;i<=len;++i){
dp[i]=i;
for(int j=0;j<i;++j){
if(isp[j+1][i]) dp[i]=min(dp[i],dp[j]+1);
}
}
cout<<dp[len]<<endl;
}
return 0;
}
C - Color Length
根本想不到啊,太难了。定义了ad[i][j]为某些已经出现了,但尚未出现的颜色个数,从而方便转移。
#include<bits/stdc++.h>
#define il inline
#define pb push_back
#define fi first
#define se second
#define ms(_data,v) memset(_data,v,sizeof(_data))
#define sc(n) scanf("%d",&n)
#define SC(n,m) scanf("%d %d",&n,&m)
#define SZ(a) int((a).size())
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define drep(i,a,b) for(int i=a;i>=b;--i)
using namespace std;
typedef long long ll;
const ll inf=0x3f3f3f3f;
const double PI=acos(-1.0);
const double eps=1e-9;
const int maxn=5e3+3;
int dp[maxn][maxn],ad[maxn][maxn];
int T,le1[30],ri1[30],le2[30],ri2[30];
int main(){
std::ios::sync_with_stdio(0);
cin>>T;
string s1,s2;
while(T--){
cin>>s1>>s2;
s1.insert(s1.begin(),'#'),s2.insert(s2.begin(),'#');
ms(le1,0x3f),ms(ri1,0),ms(le2,0x3f),ms(ri2,0);
for(int i=1;i<SZ(s1);++i){
if(le1[s1[i]-'A']==inf) le1[s1[i]-'A']=i,ri1[s1[i]-'A']=i;
else ri1[s1[i]-'A']=i;
}
for(int i=1;i<SZ(s2);++i){
if(le2[s2[i]-'A']==inf) le2[s2[i]-'A']=i,ri2[s2[i]-'A']=i;
else ri2[s2[i]-'A']=i;
}
for(int i=0;i<SZ(s1);++i){
for(int j=0;j<SZ(s2);++j){
int sum=0;
for(int k=0;k<26;++k){
if((le1[k]<=i || le2[k]<=j) && (ri1[k]>i || ri2[k]>j)) sum++;
}
ad[i][j]=sum; //某些颜色已经出现了,但尚未出现结束的个数
}
}
dp[0][0]=0;
for(int i=0;i<SZ(s1);++i){
for(int j=0;j<SZ(s2);++j){
if(i==0 && j==0) continue;
if(i==0) dp[i][j]=dp[i][j-1]+ad[i][j];
else if(j==0) dp[i][j]=dp[i-1][j]+ad[i][j];
else dp[i][j]=min(dp[i-1][j],dp[i][j-1])+ad[i][j];
}
}
cout<<dp[SZ(s1)-1][SZ(s2)-1]<<endl;
}
return 0;
}
D - Cutting Sticks
读完题,就会发现其实这就是石子合并,n^3的复杂度,但是有低于n^3复杂度的,但没有思考,懒了
#include<bits/stdc++.h>
#define il inline
#define pb push_back
#define fi first
#define se second
#define ms(_data,v) memset(_data,v,sizeof(_data))
#define sc(n) scanf("%d",&n)
#define SC(n,m) scanf("%d %d",&n,&m)
#define SZ(a) int((a).size())
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define drep(i,a,b) for(int i=a;i>=b;--i)
using namespace std;
typedef long long ll;
const ll inf=0x3f3f3f3f;
const double PI=acos(-1.0);
const double eps=1e-9;
const int maxn=1e3+5;
int L,n,sum[maxn],dp[maxn][maxn];
int a[maxn],x;
int main(){
std::ios::sync_with_stdio(0);
while(cin>>L && L){
cin>>n;
rep(i,1,n){
cin>>a[i];
sum[i]=sum[i-1]+a[i]-a[i-1];
}
sum[n+1]=sum[n]+L-a[n];
ms(dp,0x3f);
for(int i=1;i<=n+1;++i) dp[i][i]=0;
for(int len=2;len<=n+1;++len){
for(int i=1;i+len-1<=n+1;++i){
int j=i+len-1,ss=sum[j]-sum[i-1];
for(int k=i;k<=j-1;++k){
dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]+ss);
}
}
}
cout<<"The minimum cutting is "<<dp[1][n+1]<<"."<<endl;
}
return 0;
}
E - Headmaster's Headache
一道状压dp,学好状压,必须要对位运算有一定的掌握,思路参考这个 参考链接
#include<bits/stdc++.h>
#define il inline
#define pb push_back
#define fi first
#define se second
#define ms(_data,v) memset(_data,v,sizeof(_data))
#define sc(n) scanf("%d",&n)
#define SC(n,m) scanf("%d %d",&n,&m)
#define SZ(a) int((a).size())
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define drep(i,a,b) for(int i=a;i>=b;--i)
using namespace std;
typedef long long ll;
const ll inf=0x3f3f3f3f;
const double PI=acos(-1.0);
const double eps=1e-9;
const int maxn=5e2+5;
int n,m,s,cost[maxn],tea[maxn],dp[maxn][maxn][maxn];
il int run(int cur,int s1,int s2){
if(cur == m+n+1){
if(s2 == (1<<s)-1){
return 0;
}
else return inf;
}
if(dp[cur][s1][s2] != -1) return dp[cur][s1][s2];
dp[cur][s1][s2]=inf;
if(cur >= m+1) dp[cur][s1][s2]=run(cur+1,s1,s2);
int ns2 = s2|(s1 & tea[cur]);
int ns1 = s1|tea[cur];
dp[cur][s1][s2]=min(dp[cur][s1][s2],cost[cur]+run(cur+1,ns1,ns2));
return dp[cur][s1][s2];
}
int main(){
std::ios::sync_with_stdio(0);
while(cin>>s>>m>>n && (n || m || s)){
string ss;
rep(i,1,m+n){
cin>>cost[i];
getline(cin,ss);
stringstream sss(ss);
tea[i]=0;
int x;
while(sss>>x){
tea[i] |= 1<<(x-1);
}
}
ms(dp,-1);
cout<<run(1,0,0)<<endl;
}
return 0;
}
F - Party at Hali-Bula
一道树型dp,//dp[i][0]表示不选i点得到的最大人数,dp[i][1]则为选该点//f[i][0]代表不选这种的方案是否唯一(1,0区分),f[i][1]同 。原型是树的最大独立集,但是这个加了方案是否唯一。参考链接
#include<bits/stdc++.h>
#define il inline
#define pb push_back
#define fi first
#define se second
#define ms(_data,v) memset(_data,v,sizeof(_data))
#define sc(n) scanf("%d",&n)
#define SC(n,m) scanf("%d %d",&n,&m)
#define SZ(a) int((a).size())
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define drep(i,a,b) for(int i=a;i>=b;--i)
using namespace std;
typedef long long ll;
const ll inf=0x3f3f3f3f;
const double PI=acos(-1.0);
const double eps=1e-9;
const int maxn=2e5+5;
unordered_map<string,int> id;
int n,cnt,dp[maxn][2],f[maxn][2];
//dp[i][0]表示不选i点得到的最大人数,dp[i][1]则为选该点
//f[i][0]代表不选这种的方案是否唯一(1,0区分),f[i][1]同
vector<int> eg[maxn];
il int getid(string s){
if(id.count(s)) return id[s];
else return id[s]=cnt++;
}
il int run(int u,int op){
if(dp[u][op]!=0) return dp[u][op];
f[u][op]=true,dp[u][op]=op;
for(int i=0;i<SZ(eg[u]);++i){
int son=eg[u][i];
if(op){
dp[u][op] += run(son,0);
if(!f[son][0]) f[u][op]=false;
}
else{
dp[u][op]+=max(run(son,0),run(son,1));
if(dp[son][0] == dp[son][1]) f[u][op]=false;
else if(dp[son][0] > dp[son][1] && !f[son][0]) f[u][op]=false;
else if(dp[son][1] > dp[son][0] && !f[son][1]) f[u][op]=false;
}
}
// cout<<u<<" "<<op<<" "<<dp[u][op]<<endl;
return dp[u][op];
}
int main(){
std::ios::sync_with_stdio(0);
while(cin>>n && n){
cnt=0;
string x,y;
cin>>x,getid(x);
rep(i,1,n-1){
cin>>x>>y;
eg[getid(y)].pb(getid(x));
}
cout<<max(run(0,1),run(0,0))<<" ";
if(dp[0][1] == dp[0][0]) cout<<"No"<<endl;
else if(dp[0][1] > dp[0][0] && !f[0][1]) cout<<"No"<<endl;
else if(dp[0][0] > dp[0][1] && !f[0][0]) cout<<"No"<<endl;
else cout<<"Yes"<<endl;
ms(dp,0),id.clear();
rep(i,0,n) eg[i].clear();
}
return 0;
}
G - Tree
点分治裸题,之前也是作过的。传送
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<cmath>
#include<sstream>
//#include<bits/stdc++.h>
#define il inline
#define pb push_back
#define fi first
#define se second
#define ms(_data,v) memset(_data,v,sizeof(_data))
#define sc(n) scanf("%d",&n)
#define SC(n,m) scanf("%d %d",&n,&m)
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define drep(i,a,b) for(int i=a;i>=b;--i)
using namespace std;
typedef long long ll;
const ll inf=0x3f3f3f3f;
const double PI=acos(-1.0);
const double eps=1e-9;
const int maxn=1e4+5;
int n,k,sz[maxn],root,mx,all,l,r;
ll dis[maxn],q[maxn],ans;
bool vis[maxn];
struct node{
int to,w;
};
vector<node> mp[maxn];
il void getroot(int x,int fa){ //获取重心
sz[x]=1;
int num=0;
for(int i=0;i<(int)(mp[x].size());++i){
int nto=mp[x][i].to;
if(vis[nto] || fa==nto) continue;
getroot(nto,x);
sz[x]+=sz[nto];
num=max(num,sz[nto]);
}
num=max(num,all-num);
if(num<mx){
mx=num;
root=x;
}
}
il void getdis(int x,int fa){ //源点到该分支各点的距离
q[++r]=dis[x];
for(int i=0;i<(int)(mp[x].size());++i){
int nto=mp[x][i].to,nw=mp[x][i].w;
if(nto==fa || vis[nto]) continue;
dis[nto]=dis[x]+nw;
getdis(nto,x);
}
}
il ll calc(int x,int v){ //计算满足的个数
r=0,l=1,dis[x]=v;
getdis(x,0);
ll sum=0;
sort(q+1,q+r+1);
while(l<r){
if(q[l]+q[r]<=k) sum+=r-l,l++;
else r--;
}
return sum;
}
il void dfs(int x){
ans+=calc(x,0);
vis[x]=1;
for(int i=0;i<(int)(mp[x].size());++i){
int nto=mp[x][i].to,nw=mp[x][i].w;
if(vis[nto]) continue;
ans-=calc(nto,nw); //减去不和条件的
all=sz[x];
mx=inf;
getroot(nto,0);
dfs(root);
}
}
int main(){
while(SC(n,k)!=EOF){
if(n==0 && k==0) break;
all=n,ans=0;
int x,y,z;
for(int i=1;i<=n-1;++i){
sc(x),sc(y),sc(z);
mp[x].pb(node{y,z});
mp[y].pb(node{x,z});
}
mx=inf;
getroot(1,0);
dfs(root);
cout<<ans<<endl;
for(int i=0;i<=n+5;++i){
mp[i].clear();
vis[i]=0;
}
}
return 0;
}