标签:博客 block pre scan str ons code bre tac
这道题我神之迷惑~~~~链接教练博客(神之迷惑)
#include<bits/stdc++.h> using namespace std; const int N=20; int n,w; int a[N]; int ans; int f[1<<18]; int main(){ scanf("%d%d",&n,&w); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); } memset(f,0x3f,sizeof(f)); int maxn=1<<n; f[0]=0; //f[1]=0; for(ans=1;ans;ans++){ for(int i=0;i<maxn;i++){ if(f[i]<=w)f[i]=0; } for(int i=0;i<maxn;i++){ if(f[i]<w){ for(int j=1;j<=n;j++){ if(!((i>>(j-1))&1)){ f[i|(1<<(j-1))]=min(f[i|(1<<(j-1))],f[i]+a[j]); } } } } if(f[maxn-1]<=w){ printf("%d\n",ans); //return 0; break; } } return 0; }
#include<bits/stdc++.h> using namespace std; const int n=6010; int N,r[n]; int head[n]; int cnt; int rd[n]; struct Node{ int v; int ne; }e[n<<1]; int f[n][2]; void add(int u,int v){ e[++cnt].v=v; e[cnt].ne=head[u]; head[u]=cnt; } void dfs(int rt){ for(int i=head[rt];i;i=e[i].ne){ int v=e[i].v; dfs(v); f[rt][0]+=max(f[v][0],f[v][1]); f[rt][1]+=f[v][0]; } f[rt][1]+=r[rt]; } int k; int main(){ scanf("%d",&N); for(int i=1;i<=N;i++){ scanf("%d",&r[i]); } while(1){ int x,y; scanf("%d%d",&x,&y); rd[x]++; if(x!=0&&y!=0){ add(y,x); } else { break; } } //int rt=0; for(int i=1;i<=N;i++){ if(rd[i]==0)k=i; } dfs(k); printf("%d\n",max(f[k][0],f[k][1])); return 0; }
#include<bits/stdc++.h> using namespace std; const int N=2500+5; int a[N],f[N][N]; int main(){ int n , ans = 0; scanf("%d",&n); for(int i = 1; i <= n; i++) scanf("%d",&a[i]); for(int i = 1; i <= n; i++){ for(int j = 1; j < i; j++){ f[i+1][j]=max(f[i+1][j],f[i][j]+a[i]*a[i+1]); f[i+1][i]=max(f[i+1][i],f[i][j]+a[j]*a[i+1]); } } for(int i = 0; i < n; i++) ans =max(ans, f[n][i] + a[n] * a[i]); printf("%d\n",ans); return 0; }
waiting。。。。。。
标签:博客 block pre scan str ons code bre tac
原文地址:https://www.cnblogs.com/LightyaChoo/p/13225670.html