贴一下比赛的代码, 其中 I 题代码源于final大神 ok_again
http://acm.hust.edu.cn/vjudge/contest/127444#overview
I
1 /************************************************************************* 2 > File Name: a.cpp 3 > Author: ok_again 4 ************************************************************************/ 5 6 #include<algorithm> 7 #include<iostream> 8 #include<cstring> 9 #include<vector> 10 #include<cstdio> 11 12 #define CLR(a, b) memset(a, b, sizeof(a)) 13 using namespace std; 14 15 const int MOD = 1000000007; 16 const int maxn = 2020; 17 18 struct Node { 19 int a, b, id; 20 Node() { 21 22 } 23 Node(int a, int b, int id) 24 :a(a), b(b), id(id) { 25 26 } 27 bool operator < (const Node& rhs) const { 28 return a > rhs.a || (a == rhs.a && id < rhs.id); 29 } 30 }; 31 32 vector<Node> lvl[111]; 33 vector<int> out; 34 35 int dp[111][maxn][22]; 36 pair<int, int> last[111][maxn][22]; 37 38 void getout(int st, int c, int k) { 39 if(k == 0 || st == 0) return ; 40 int l = last[st][c][k].first, j = last[st][c][k].second; 41 for(int i = 0; i < j; i ++) { 42 out.push_back(lvl[l][i].id); 43 } 44 getout(st - 1, c - l * j, k - j); 45 } 46 47 int main() { 48 int n, k; 49 while(scanf("%d%d", &n, &k) != EOF) { 50 for(int i = 0; i <= 100; i ++) 51 lvl[i].clear(); 52 for(int i = 1; i <= n; i ++) { 53 int a, b; 54 scanf("%d%d", &a, &b); 55 lvl[a - b + 50].push_back(Node(a, b, i)); 56 } 57 for(int i = 0; i <= 100; i ++) 58 sort(lvl[i].begin(), lvl[i].end());//, printf("%d -- ", i); 59 CLR(dp, -1); 60 CLR(last, -1); 61 dp[0][0][0] = 0; 62 for(int i = 0; i <= 100; i ++) { 63 int sum = 0; 64 for(int j = 0; j <= min(k, (int)lvl[i].size()); j ++) { 65 //printf("%d -- %d ", i, j); 66 int c = j * i; 67 if(j) sum += lvl[i][j - 1].a; 68 for(int s = 2000; s >= c; s --) { 69 for(int t = k; t >= j; t --) { 70 if(dp[i][s - c][t - j] == -1) continue; 71 if(dp[i + 1][s][t] < dp[i][s - c][t - j] + sum) { 72 dp[i + 1][s][t] = dp[i][s - c][t - j] + sum; 73 last[i + 1][s][t] = make_pair(i, j); 74 } 75 } 76 } 77 } 78 } 79 int A, B; 80 out.clear(); 81 for(int i = 0; ; i ++) { 82 int c = k * 50; 83 if(dp[101][c + i][k] != -1) { 84 A = dp[101][c + i][k]; 85 B = A - i; 86 if(dp[101][c - i][k] == -1) { 87 getout(101, c + i, k); 88 break; 89 } else { 90 if(dp[101][c - i][k] * 2 + i > A * 2 - i) { 91 A = dp[101][c - i][k]; 92 B = A + i; 93 getout(101, c - i, k); 94 break; 95 } 96 getout(101, c + i, k); 97 break; 98 } 99 } else if(dp[101][c - i][k] != -1) { 100 A = dp[101][c - i][k]; 101 B = A + i; 102 getout(101, c - i, k); 103 break; 104 } 105 } 106 sort(out.begin(), out.end()); 107 printf("%d %d ", A, B); 108 for(int i = 0; i < out.size(); i ++) 109 printf("%d%c", out[i], i == k - 1 ? ' ' : ' '); 110 } 111 return 0; 112 }
A
1 //#define txtout 2 //#define debug 3 #include<bits/stdc++.h> 4 #define mt(a,b) memset(a,b,sizeof(a)) 5 using namespace std; 6 typedef long long LL; 7 const double pi=acos(-1.0); 8 const double eps=1e-8; 9 const int inf=0x3f3f3f3f; 10 const int M=1e5+10; 11 int n; 12 int a[M]; 13 int b[M]; 14 int c[M]; 15 int d[M]; 16 int get(int s[]){ 17 sort(s,s+3); 18 return s[1]; 19 } 20 int solve(){ 21 d[0]=get(a); 22 d[1]=get(b); 23 d[2]=get(c); 24 return get(d); 25 } 26 int main(){ 27 #ifdef txtout 28 freopen("in.txt","r",stdin); 29 freopen("out.txt","w",stdout); 30 #endif // txtout 31 while(~scanf("%d",&a[0])){ 32 for(int i=1;i<3;i++){ 33 scanf("%d",&a[i]); 34 } 35 for(int i=0;i<3;i++){ 36 scanf("%d",&b[i]); 37 } 38 for(int i=0;i<3;i++){ 39 scanf("%d",&c[i]); 40 } 41 printf("%d ",solve()); 42 } 43 return 0; 44 }
B
1 //#define txtout 2 //#define debug 3 #include<bits/stdc++.h> 4 #define mt(a,b) memset(a,b,sizeof(a)) 5 using namespace std; 6 typedef long long LL; 7 const double pi=acos(-1.0); 8 const double eps=1e-8; 9 const int inf=0x3f3f3f3f; 10 const int M=1e5+10; 11 int n; 12 char a[32][32]; 13 vector<int> answer; 14 int to[8]; 15 int area[8]; 16 void solve(){ 17 for(int i=0;i<4;i++){ 18 to[i]=i; 19 area[i]=0; 20 } 21 answer.clear(); 22 for(int i=0;i<n;i++){ 23 if(a[i][0]=='w'){ 24 char c='a'; 25 for(int j=0;a[i][j];j++){ 26 if(a[i][j]!='(') continue; 27 c=a[i][j+1]; 28 break; 29 } 30 answer.push_back(area[to[c-'a']]); 31 continue; 32 } 33 if(isdigit(a[i][4])){ 34 area[to[a[i][0]-'a']]=a[i][4]-'0'; 35 continue; 36 } 37 if(a[i][1]==':'&&a[i][2]=='='){ 38 to[a[i][0]-'a']=to[a[i][3]-'a']; 39 continue; 40 } 41 int x=a[i][0]-'a'; 42 int y=a[i][4]-'a'; 43 area[to[x]]=area[to[y]]; 44 } 45 } 46 int main(){ 47 #ifdef txtout 48 freopen("in.txt","r",stdin); 49 freopen("out.txt","w",stdout); 50 #endif // txtout 51 while(~scanf("%d",&n)){ 52 for(int i=0;i<n;i++){ 53 scanf("%s",a[i]); 54 } 55 solve(); 56 for(int i=0;i<answer.size();i++){ 57 printf("%d ",answer[i]); 58 } 59 } 60 return 0; 61 }
E
1 //#define txtout 2 //#define debug 3 #include<bits/stdc++.h> 4 #define mt(a,b) memset(a,b,sizeof(a)) 5 using namespace std; 6 typedef long long LL; 7 const double pi=acos(-1.0); 8 const double eps=1e-8; 9 const int inf=0x3f3f3f3f; 10 const int M=1e1+10; 11 int n,m,sx,sy; 12 char answer[M*M]; 13 bool tabu[M][M]; 14 int dx[4]={1,0,-1,0}; 15 int dy[4]={0,-1,0,1}; 16 char type[8]="DLUR"; 17 bool inside(int x,int y){ 18 return x>=1&&x<=n&&y>=1&&y<=m; 19 } 20 void solve(){ 21 mt(tabu,false); 22 int step=0; 23 tabu[sx][sy]=true; 24 while(true){ 25 bool cango=false; 26 for(int i=0;i<4;i++){ 27 int tx=sx+dx[i]; 28 int ty=sy+dy[i]; 29 if(!inside(tx,ty)) continue; 30 if(tabu[tx][ty]) continue; 31 cango=true; 32 tabu[tx][ty]=true; 33 sx=tx; 34 sy=ty; 35 answer[step++]=type[i]; 36 break; 37 } 38 if(cango) continue; 39 answer[step]=0; 40 return ; 41 } 42 } 43 int main(){ 44 #ifdef txtout 45 freopen("in.txt","r",stdin); 46 freopen("out.txt","w",stdout); 47 #endif // txtout 48 while(~scanf("%d%d%d%d",&n,&m,&sx,&sy)){ 49 solve(); 50 puts(answer); 51 } 52 return 0; 53 }
H
1 //#define txtout 2 //#define debug 3 #include<bits/stdc++.h> 4 #define mt(a,b) memset(a,b,sizeof(a)) 5 using namespace std; 6 typedef long long LL; 7 const double pi=acos(-1.0); 8 const double eps=1e-8; 9 const int inf=0x3f3f3f3f; 10 const int M=1e5+10; 11 int n; 12 int a[M]; 13 LL dp[21][10][2]; 14 LL solve(){ 15 mt(dp,0); 16 for(int i=0;i<=9;i++){ 17 dp[0][i][0]=1; 18 } 19 for(int i=0;i<n;i++){ 20 for(int j=0;j<=9;j++){ 21 for(int k=0;k<2;k++){ 22 if(dp[i][j][k]==0) continue; 23 if(!k){ 24 for(int x=0;x<=9;x++){ 25 if(x>=j){ 26 dp[i+1][x][0]+=dp[i][j][k]; 27 } 28 else{ 29 dp[i+1][x][1]+=dp[i][j][k]; 30 } 31 } 32 continue; 33 } 34 for(int x=0;x<=j;x++){ 35 dp[i+1][x][1]+=dp[i][j][k]; 36 } 37 } 38 } 39 } 40 LL sum=0; 41 for(int j=0;j<=9;j++){ 42 for(int k=0;k<2;k++){ 43 sum+=dp[n-1][j][k]; 44 } 45 } 46 return sum; 47 } 48 int main(){ 49 #ifdef txtout 50 freopen("in.txt","r",stdin); 51 freopen("out.txt","w",stdout); 52 #endif // txtout 53 while(~scanf("%d",&n)){ 54 printf("%lld ",solve()); 55 } 56 return 0; 57 }
F
1 //#define txtout 2 //#define debug 3 #include<bits/stdc++.h> 4 #define mt(a,b) memset(a,b,sizeof(a)) 5 using namespace std; 6 typedef long long LL; 7 const double pi=acos(-1.0); 8 const double eps=1e-8; 9 const int inf=0x3f3f3f3f; 10 const int M=1e2+10; 11 int n,m; 12 struct P{ 13 int k,t,qid,left; 14 }people[M]; 15 struct Q{ 16 int pid,need; 17 }node; 18 queue<Q> q[M]; 19 void init(){ 20 for(int i=1;i<=m;i++){ 21 while(!q[i].empty()) q[i].pop(); 22 } 23 } 24 void solve(){ 25 init(); 26 int peopleID=0; 27 for(int time=1;;time++){ 28 bool had=false; 29 for(int i=1;i<=m;i++){ 30 if(q[i].empty()) continue; 31 had=true; 32 q[i].front().need--; 33 if(q[i].front().need==0){ 34 people[q[i].front().pid].left=time; 35 q[i].pop(); 36 } 37 } 38 if(!had&&peopleID>=n) break; 39 while(peopleID<n){ 40 if(people[peopleID].k>time) break; 41 int id=1,number=q[1].size(); 42 for(int i=2;i<=m;i++){ 43 if(number>q[i].size()){ 44 number=q[i].size(); 45 id=i; 46 } 47 } 48 people[peopleID].qid=id; 49 node.pid=peopleID; 50 node.need=people[peopleID].t; 51 q[id].push(node); 52 peopleID++; 53 } 54 } 55 } 56 int main(){ 57 #ifdef txtout 58 freopen("in.txt","r",stdin); 59 freopen("out.txt","w",stdout); 60 #endif // txtout 61 while(~scanf("%d%d",&n,&m)){ 62 for(int i=0;i<n;i++){ 63 scanf("%d%d",&people[i].k,&people[i].t); 64 } 65 solve(); 66 for(int i=0;i<n;i++){ 67 printf("%d %d ",people[i].qid,people[i].left); 68 } 69 } 70 return 0; 71 }
D
1 //#define txtout 2 //#define debug 3 #include<bits/stdc++.h> 4 #define mt(a,b) memset(a,b,sizeof(a)) 5 using namespace std; 6 typedef long long LL; 7 const double pi=acos(-1.0); 8 const double eps=1e-8; 9 const int inf=0x3f3f3f3f; 10 const int M=1e3+10; 11 int n,m; 12 char a[M][M]; 13 char answer[M][M]; 14 bool inside(int x,int y){ 15 return x>=0&&x<M&&y>=0&&y<M; 16 } 17 void init(){ 18 for(int i=0;i<M;i++){ 19 for(int j=0;j<M;j++){ 20 a[i][j]='0'; 21 } 22 } 23 for(int i=0;i<M;i+=3){ 24 int sx=i; 25 int sy=0; 26 while(inside(sx,sy)){ 27 a[sx][sy]='#'; 28 sx--; 29 sy++; 30 } 31 } 32 } 33 int get_sum(int sx,int sy){ 34 int sum=0; 35 for(int i=0;i<n;i++){ 36 for(int j=0;j<m;j++){ 37 if(a[sx+i][sy+j]=='#') sum++; 38 } 39 } 40 return sum; 41 } 42 void solve(){ 43 int sx=0,sy=0; 44 int sum=get_sum(sx,sy); 45 int tx=0,ty=1; 46 int tsum=get_sum(tx,ty); 47 if(tsum<sum){ 48 sum=tsum; 49 sx=tx; 50 sy=ty; 51 } 52 tx=0,ty=2; 53 tsum=get_sum(tx,ty); 54 if(tsum<sum){ 55 sum=tsum; 56 sx=tx; 57 sy=ty; 58 } 59 for(int i=0;i<n;i++){ 60 for(int j=0;j<m;j++){ 61 answer[i][j]=a[sx+i][sy+j]; 62 } 63 } 64 } 65 int main(){ 66 #ifdef txtout 67 freopen("in.txt","r",stdin); 68 freopen("out.txt","w",stdout); 69 #endif // txtout 70 init(); 71 while(~scanf("%d%d",&n,&m)){ 72 solve(); 73 for(int i=0;i<n;i++){ 74 for(int j=0;j<m;j++){ 75 putchar(answer[i][j]); 76 } 77 puts(""); 78 } 79 } 80 return 0; 81 }
J
1 //#define txtout 2 //#define debug 3 #include<bits/stdc++.h> 4 #define mt(a,b) memset(a,b,sizeof(a)) 5 using namespace std; 6 typedef long long LL; 7 const double pi=acos(-1.0); 8 const double eps=1e-8; 9 const int inf=0x3f3f3f3f; 10 const int M=1e4+10; 11 int n,m,t; 12 int a[M]; 13 struct E{ 14 int u,v; 15 }e[M]; 16 struct A{ 17 vector<int> id; 18 LL t; 19 void init(){ 20 t=0; 21 id.clear(); 22 } 23 }answer; 24 struct Q{ 25 int need,id; 26 bool operator <(const Q &b) const { 27 return need>b.need; 28 } 29 }node; 30 priority_queue<Q> q; 31 vector<int> g[M]; 32 int degree[M]; 33 void init(){ 34 answer.init(); 35 for(int i=1;i<=n;i++){ 36 g[i].clear(); 37 degree[i]=0; 38 } 39 for(int i=0;i<m;i++){ 40 int u=e[i].u; 41 int v=e[i].v; 42 g[u].push_back(v); 43 degree[v]++; 44 } 45 } 46 void solve(){ 47 init(); 48 while(!q.empty()) q.pop(); 49 for(int i=1;i<=n;i++){ 50 if(degree[i]==0){ 51 node.need=a[i]; 52 node.id=i; 53 q.push(node); 54 } 55 } 56 LL time=0; 57 while(!q.empty()){ 58 node=q.top(); 59 q.pop(); 60 if(time+node.need>t) break; 61 time+=node.need; 62 int u=node.id; 63 answer.id.push_back(u); 64 for(int i=0;i<g[u].size();i++){ 65 int v=g[u][i]; 66 degree[v]--; 67 if(degree[v]==0){ 68 node.id=v; 69 node.need=a[v]; 70 q.push(node); 71 } 72 } 73 } 74 time=0; 75 for(int i=0;i<answer.id.size();i++){ 76 int u=answer.id[i]; 77 answer.t+=time+a[u]; 78 time+=a[u]; 79 } 80 } 81 int main(){ 82 #ifdef txtout 83 freopen("in.txt","r",stdin); 84 freopen("out.txt","w",stdout); 85 #endif // txtout 86 while(~scanf("%d%d",&n,&t)){ 87 for(int i=1;i<=n;i++){ 88 scanf("%d",&a[i]); 89 } 90 scanf("%d",&m); 91 for(int i=0;i<m;i++){ 92 scanf("%d%d",&e[i].u,&e[i].v); 93 } 94 solve(); 95 printf("%d %I64d ",answer.id.size(),answer.t); 96 for(int i=0;i<answer.id.size();i++){ 97 printf("%d%c",answer.id[i],i==answer.id.size()-1?' ':' '); 98 } 99 } 100 return 0; 101 }
end