精神AC合集 2018.4.3

UESTC炸了,先把看似十分OK(只是过了样例)的代码贴上,修复好后再交上去

594

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#define rep(i,j,k) for(register int i=j;i<=k;i++)
#define rrep(i,j,k) for(register int i=j;i>=k;i--)
#define erep(i,u) for(register int i=head[u];~i;i=nxt[i])
#define iin(a) scanf("%d",&a)
#define lin(a) scanf("%lld",&a)
#define din(a) scanf("%lf",&a)
#define s0(a) scanf("%s",a)
#define s1(a) scanf("%s",a+1)
#define print(a) printf("%lld",(ll)a)
#define enter putchar('\n')
#define blank putchar(' ')
#define println(a) printf("%lld\n",(ll)a)
#define IOS ios::sync_with_stdio(0)
using namespace std;
const int MAXN = 5e4+11;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
typedef long long ll;
// jjjlo
const ll MOD = 1e9+7; 
ll read(){
    ll x=0,f=1;register 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;
}
ll best[MAXN][103];
int a[MAXN],n,C;
int main(){
    while(cin>>n>>C){
        memset(best,0x3f,sizeof best);
        deque<int> que1,que2;
        rep(i,1,n) a[i]=read();
        rep(i,1,n) best[1][i]=(i>=a[1]?(i-a[1])*(i-a[1]):INF);
        rep(i,2,n){
            rep(j,1,a[i]){//ai || deng 
                if(best[i-1][j]==INF) continue;
                while(!que1.empty()&&best[i-1][que1.front()]-C*que1.front()>best[i-1][j]-C*j) que1.pop_front();
                que1.push_front(j);
            }
            rep(j,a[i]+1,100){//gao
                if(best[i-1][j]==INF) continue;
                while(!que2.empty()&&best[i-1][que2.front()]+C*que2.front()>best[i-1][j]+C*j) que2.pop_front();
                que2.push_front(j);
            }
            rep(j,a[i],100){
                if(!que1.empty()) best[i][j]=best[i-1][que1.back()]+C*(j-que1.back())+(j-a[i])*(j-a[i]);
                if(!que2.empty()) best[i][j]=min(best[i][j],best[i-1][que2.back()]+C*(que2.back()-j)+(j-a[i])*(j-a[i]));
                while(!que2.empty()&&que2.back()<=j+1){
                    int tmp=que2.back();que2.pop_back();
                    while(!que1.empty()&&best[i-1][que2.front()]-C*que1.front()>best[i-1][tmp]-C*tmp) que1.pop_front();
                    que1.push_front(tmp);
                }
            }
            while(!que1.empty()) que1.pop_back();
            while(!que2.empty()) que2.pop_back();
        }
        ll ans=INF;
        rep(i,1,100) if(a[n]<=i) ans=min(ans,best[n][i]);
        println(ans);
    }
    return 0;
}

1651

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#define rep(i,j,k) for(register int i=j;i<=k;i++)
#define rrep(i,j,k) for(register int i=j;i>=k;i--)
#define erep(i,u) for(register int i=head[u];~i;i=nxt[i])
#define iin(a) scanf("%d",&a)
#define lin(a) scanf("%lld",&a)
#define din(a) scanf("%lf",&a)
#define s0(a) scanf("%s",a)
#define s1(a) scanf("%s",a+1)
#define print(a) printf("%lld",(ll)a)
#define enter putchar('\n')
#define blank putchar(' ')
#define println(a) printf("%lld\n",(ll)a)
#define IOS ios::sync_with_stdio(0)
using namespace std;
const int MAXN = 1e3+11;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
typedef long long ll;
const ll MOD = 1e9+7; 
ll read(){
    ll x=0,f=1;register 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;
}
int G[MAXN][MAXN],n,m;
ll best[2][MAXN][MAXN][2];
int main(){
    while(cin>>n>>m){
        rep(i,1,n)rep(j,1,m) G[i][j]=read();
        memset(best,0,sizeof best);
        rep(i,1,n) rep(j,1,m){
            best[0][i][j][0]=max(best[0][i][j][0],best[0][i-1][j][0]+G[i][j]);
            best[0][i][j][0]=max(best[0][i][j][0],best[0][i][j-1][0]+G[i][j]);
        }
        rrep(i,n,1) rrep(j,m,1){
            best[0][i][j][1]=max(best[0][i][j][1],best[0][i+1][j][1]+G[i][j]);
            best[0][i][j][1]=max(best[0][i][j][1],best[0][i][j+1][1]+G[i][j]);
        }
        rrep(i,n,1) rep(j,1,m){
            best[1][i][j][0]=max(best[1][i][j][0],best[1][i+1][j][0]+G[i][j]);
            best[1][i][j][0]=max(best[1][i][j][0],best[1][i][j-1][0]+G[i][j]);
        }
        rep(i,1,n) rrep(j,m,1){
            best[1][i][j][1]=max(best[1][i][j][1],best[1][i-1][j][1]+G[i][j]);
            best[1][i][j][1]=max(best[1][i][j][1],best[1][i][j+1][1]+G[i][j]);
        }
        ll ans=-1;
        rep(i,1,n) rep(j,1,m){
            ll tmp1=best[0][i][j-1][0];//from left
            ll tmp2=best[0][i-1][j][0];//from top
            ll tmp3=best[0][i][j+1][1];//to right
            ll tmp4=best[0][i+1][j][1];// to bottom
            // ll t1=max(tmp1+tmp3,tmp1+tmp4);
            // ll t2=max(tmp2+tmp3,tmp2+tmp4);
            // ll res1=max(t1,t2);
            ll rtmp1=best[1][i][j-1][0];//from left
            ll rtmp2=best[1][i+1][j][0];//from bottom
            ll rtmp3=best[1][i][j+1][1];//to right
            ll rtmp4=best[1][i-1][j][1];//to top
            // ll rt1=max(rtmp1+rtmp3,rtmp1+rtmp4);
            // ll rt2=max(rtmp2+rtmp3,rtmp2+rtmp4);
            // ll res2=max(rt1,rt2);
            // ans=max(ans,res1+res2);
            ll t1=tmp1+tmp3+rtmp2+rtmp4;
            ll t2=rtmp1+rtmp3+tmp2+tmp4;//可通过终点的方案(未考虑边界/能否接触<-必能接触,不是强制同步)
            if(i==1)continue;//专家1非法
            if(j==1)continue;//专家0非法
            ans=max(ans,max(t1,t2));
        }
        println(ans);
    }
    return 0;
}

1608

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#define rep(i,j,k) for(register int i=j;i<=k;i++)
#define rrep(i,j,k) for(register int i=j;i>=k;i--)
#define erep(i,u) for(register int i=head[u];~i;i=nxt[i])
#define iin(a) scanf("%d",&a)
#define lin(a) scanf("%lld",&a)
#define din(a) scanf("%lf",&a)
#define s0(a) scanf("%s",a)
#define s1(a) scanf("%s",a+1)
#define print(a) printf("%lld",(ll)a)
#define enter putchar('\n')
#define blank putchar(' ')
#define println(a) printf("%lld\n",(ll)a)
#define IOS ios::sync_with_stdio(0)
using namespace std;
const int MAXN = 1e3+11;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
typedef long long ll;
const ll MOD = 1e9+7; 
ll read(){
    ll x=0,f=1;register 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;
}
int P[22][22][22],n,x,y,z,zz;
ll dp[1<<21|1];
void get(int t){
    x=y=z=zz=-1;
    for(int i=0;i<23;i++) if((t>>i)&1){
        if(x==-1) x=i;
        else if(y==-1) y=i;
        else if(z==-1) z=i;
        else if(zz==-1) zz=i;
    }
}
int main(){
    while(cin>>n){
        int m=n*(n-1)*(n-2)/6;
        rep(i,1,m){
            int a=read();
            int b=read();
            int c=read();
            int t=read();
            P[a][b][c]=t;
        }
        memset(dp,0,sizeof dp);
        for(int S=0;S<=(1<<n)-1;S++){
            get(S);
            // cout<<x<<" "<<y<<" "<<z<<endl;
            if(z==-1)continue;
            for(int S0=S;S0;S0=S&(S0-1)){
                get(S0);
                if(z==-1)continue;
                if(zz!=-1)continue;
                dp[S]=max(dp[S],dp[S^S0]+P[x+1][y+1][z+1]);
            }
        }
        println(dp[(1<<n)-1]);
    }
    return 0;
}

优化一下

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#define rep(i,j,k) for(register int i=j;i<=k;i++)
#define rrep(i,j,k) for(register int i=j;i>=k;i--)
#define erep(i,u) for(register int i=head[u];~i;i=nxt[i])
#define iin(a) scanf("%d",&a)
#define lin(a) scanf("%lld",&a)
#define din(a) scanf("%lf",&a)
#define s0(a) scanf("%s",a)
#define s1(a) scanf("%s",a+1)
#define print(a) printf("%lld",(ll)a)
#define enter putchar('\n')
#define blank putchar(' ')
#define println(a) printf("%lld\n",(ll)a)
#define IOS ios::sync_with_stdio(0)
using namespace std;
const int MAXN = 1e3+11;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
typedef long long ll;
const ll MOD = 1e9+7; 
ll read(){
    ll x=0,f=1;register 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;
}
int P[22][22][22],n,x,y,z,zz;
bool vis[22];
ll dp[1<<21|1];
void get(int t){
    x=y=z=zz=-1;
    for(int i=0;i<23;i++) if((t>>i)&1){
        if(x==-1) x=i;
        else if(y==-1) y=i;
        else if(z==-1) z=i;
        else if(zz==-1) zz=i;
    }
}
ll DP(int S){
    if(~dp[S]) return dp[S];
    ll ans=0;
    int i,flag=0;
    for(i=0;i<n-2;i++) if((S>>i)&1){
        flag=1;break;
    }
    if(!flag)return dp[S]=0;
    // for(int i=0;i<n-2;i++){
        // if((S>>i)&1){
            for(int j=i+1;j<n-1;j++){
                if((S>>j)&1){
                    for(int k=j+1;k<n;k++){
                        if((S>>k)&1){
                            ans=max(ans,DP(S^(1<<i)^(1<<j)^(1<<k))+P[i+1][j+1][k+1]);
                        }
                    }
                }
            }
        // }
    // }
    return dp[S]=ans;
}
int main(){
    while(cin>>n){
        int m=n*(n-1)*(n-2)/6;
        rep(i,1,m){
            int a=read();
            int b=read();
            int c=read();
            int t=read();
            P[a][b][c]=t;
        }
        // memset(dp,0,sizeof dp);
        // for(int S=0;S<=(1<<n)-1;S++){
        //     get(S);
        //     // cout<<x<<" "<<y<<" "<<z<<endl;
        //     if(z==-1)continue;
        //     for(int S0=S;S0;S0=S&(S0-1)){
        //         get(S0);
        //         if(z==-1)continue;
        //         if(zz!=-1)continue;
        //         dp[S]=max(dp[S],dp[S^S0]+P[x+1][y+1][z+1]);
        //     }
        // } //太慢了
        memset(dp,-1,sizeof dp);
        println(DP((1<<n)-1));
    }
    return 0;
}

1134

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#define rep(i,j,k) for(register int i=j;i<=k;i++)
#define rrep(i,j,k) for(register int i=j;i>=k;i--)
#define erep(i,u) for(register int i=head[u];~i;i=nxt[i])
#define iin(a) scanf("%d",&a)
#define lin(a) scanf("%lld",&a)
#define din(a) scanf("%lf",&a)
#define s0(a) scanf("%s",a)
#define s1(a) scanf("%s",a+1)
#define print(a) printf("%lld",(ll)a)
#define enter putchar('\n')
#define blank putchar(' ')
#define println(a) printf("%lld\n",(ll)a)
#define IOS ios::sync_with_stdio(0)
using namespace std;
const int MAXN = 5e2+11;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
typedef long long ll;
const ll MOD = 1e9+7; 
ll read(){
    ll x=0,f=1;register 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;
}
ll dp[11][11][1<<11|1];
int G[11][11];
int main(){
	rep(i,1,10) rep(j,1,10) G[i][j]=read();
	memset(dp,0x3f,sizeof dp);
//	rep(i,0,(1<<10)-1) dp[1][0][i]=dp[0][0][i]=dp[0][1][i]=0;
	dp[1][1][1<<G[1][1]]=G[1][1];
	rep(i,1,10) rep(j,1,10){
		for(int S=0;S<=(1<<10)-1;S++){
			int t=1<<G[i][j];
			if((S>>G[i][j])&1){
				dp[i][j][S]=min(dp[i][j][S],min(dp[i-1][j][S^t],dp[i][j-1][S^t])+G[i][j]);//que 
				dp[i][j][S]=min(dp[i][j][S],min(dp[i-1][j][S],dp[i][j-1][S])+G[i][j]);//buque
			}
			else{
				dp[i][j][S^t]=min(dp[i][j][S^t], min(dp[i-1][j][S],dp[i][j-1][S])+G[i][j] );//que
			}
		}
	}
	println(dp[10][10][(1<<10)-1]);
	return 0;
} 
posted @ 2018-04-03 17:31  Caturra  阅读(205)  评论(0)    收藏  举报