标签:class print ems room main using ons register 图片
倒着跑最短路就行
没仔细看题凉凉
code:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define F(i,a,b) for(register int i=(a);i<=(b);i++)
using namespace std;
int rd() {
int x=0,f=1; char c=getchar();
while(!isdigit(c)) {if(c==‘-‘)f=-f; c=getchar();}
while(isdigit(c)) x=(x<<1)+(x<<3)+c-48,c=getchar();
return x*f;
}
const int N=5010;
int n,cnt,tot;
int nxt[N],head[N],to[N],qu[N<<3],dp[N],vis[N];
void bfs(int x) {//SPFA
int h=0,t=1;qu[h]=x;
while(h<=t) {
int u=qu[h++],ret=0;
vis[u]=0;
if(u<27) {
ret=dp[u]/19;
if(ret*19!=dp[u]) ret++;
} else ret=1;
for(int i=head[u];i;i=nxt[i]) {
int v=to[i];
if(dp[v]>dp[u]+ret) {
dp[v]=dp[u]+ret;
if(!vis[dp[v]]) qu[t++]=v,vis[v]++;
}
}
}
}
#define add(a,b) nxt[++cnt]=head[a],to[cnt]=b,head[a]=cnt
#define insert(a,b) add(a,b),add(b,a)
int main() {
freopen("toll.in","r",stdin);
freopen("toll.out","w",stdout);
while(scanf("%d",&n),n!=-1) {
cnt=0;tot++;
memset(vis,0,sizeof(vis));
memset(dp,0x3f,sizeof(dp));
memset(head,0,sizeof(head));
char a,b;
F(i,1,n) {
scanf(" %c %c",&a,&b);
insert(a-‘A‘+1,b-‘A‘+1);
}
getchar();
int ret; scanf("%d ",&ret);
scanf("%c %c",&a,&b);
int T=a-‘A‘+1,S=b-‘A‘+1;
dp[S]=ret; bfs(S);
printf("Case %d: %d\n",tot,dp[T]);//考试时直接输出dp[T]QAQ凉凉
}
return 0;
}
求没有逆序对的最长序列——不就是LIS么? ——高 Orz
code:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int rd() {
int x=0,f=1; char c=getchar();
while(!isdigit(c)) {if(c==‘-‘)f=-f; c=getchar();}
while(isdigit(c)) x=(x<<1)+(x<<3)+c-48,c=getchar();
return x*f;
}
const int N=100010;
int n,top,da[N],dp[N];
int main() {
freopen("sort.in","r",stdin);
freopen("sort.out","w",stdout);
n=rd();
for(int i=1;i<=n;i++) da[i]=rd();
for(int i=1;i<=n;i++) if(dp[top]<da[i]) dp[++top]=da[i];
else dp[lower_bound(dp+1,dp+1+top,da[i])-dp]=da[i];
printf("%d",top);
return 0;
}
比较水的状压
dp[i][j] i表示状态 j表示不同颜色钥匙数量
code:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define F(i,a,b) for(register int i=(a);i<=(b);i++)
using namespace std;
int rd() {
int x=0,f=1; char c=getchar();
while(!isdigit(c)) {if(c==‘-‘)f=-f; c=getchar();}
while(isdigit(c)) x=(x<<1)+(x<<3)+c-48,c=getchar();
return x*f;
}
int n,re,gr,wi,ans,mx;
int rek[15],grk[15],wik[15],red[15],grd[15];
int dp[20000][3];
bool jud(int a,int b) {
int sum=0;
int ret=dp[a-(1<<b)][0]-red[b+1]; if(ret<0) sum+=ret;
ret=dp[a-(1<<b)][1]-grd[b+1]; if(ret<0) sum+=ret;
return sum+dp[a-(1<<b)][2]>=0;
}
int main() {
freopen("room.in","r",stdin);
freopen("room.out","w",stdout);
n=rd();
F(i,1,n) red[i]=rd();
F(i,1,n) grd[i]=rd();
F(i,1,n) rek[i]=rd();
F(i,1,n) grk[i]=rd();
F(i,1,n) wik[i]=rd();
dp[0][0]=rd(),dp[0][1]=rd(),dp[0][2]=rd();
ans=dp[0][0]+dp[0][1]+dp[0][2];
for(int i=0;i<(1<<n);i++) {
for(int j=0;(i>>j);j++)
if(((i>>j)&1)&&jud(i,j)) {
dp[i][2]=dp[i-(1<<j)][2]+wik[j+1];
if(dp[i-(1<<j)][0]-red[j+1]<0) {
dp[i][2]+=dp[i-(1<<j)][0]-red[j+1];
dp[i][0]=rek[j+1];
} else dp[i][0]=dp[i-(1<<j)][0]-red[j+1]+rek[j+1];
if(dp[i-(1<<j)][1]-grd[j+1]<0) {
dp[i][2]+=dp[i-(1<<j)][1]-grd[j+1];
dp[i][1]=grk[j+1];
} else dp[i][1]=dp[i-(1<<j)][1]-grd[j+1]+grk[j+1];
ans=max(ans,dp[i][0]+dp[i][1]+dp[i][2]);
}
}
printf("%d",ans);
return 0;
}
标签:class print ems room main using ons register 图片
原文地址:https://www.cnblogs.com/Menteur-Hxy/p/9268967.html