标签:
Orz AK爷faebdc
Growin要跟全部的n个人握手共2n杯香槟,再加上每对关系的两杯香槟,直接统计邻接矩阵中1的个数,再加2n就是answer
1 //BestCoder 42 A 2 #include<vector> 3 #include<cstdio> 4 #include<cstring> 5 #include<cstdlib> 6 #include<iostream> 7 #include<algorithm> 8 #define rep(i,n) for(int i=0;i<n;++i) 9 #define F(i,j,n) for(int i=j;i<=n;++i) 10 #define D(i,j,n) for(int i=j;i>=n;--i) 11 #define pb push_back 12 using namespace std; 13 inline int getint(){ 14 int v=0,sign=1; char ch=getchar(); 15 while(ch<‘0‘||ch>‘9‘){ if (ch==‘-‘) sign=-1; ch=getchar();} 16 while(ch>=‘0‘&&ch<=‘9‘){ v=v*10+ch-‘0‘; ch=getchar();} 17 return v*sign; 18 } 19 const int N=1e5+10,INF=~0u>>2; 20 typedef long long LL; 21 /******************tamplate*********************/ 22 int n,a[100][100]; 23 int main(){ 24 #ifndef ONLINE_JUDGE 25 freopen("A.in","r",stdin); 26 freopen("A.out","w",stdout); 27 #endif 28 while(scanf("%d",&n)!=EOF){ 29 memset(a,0,sizeof a); 30 LL cnt=0; 31 F(i,1,n) F(j,1,n){ 32 a[i][j]=getint(); 33 if (a[i][j]) cnt++; 34 } 35 printf("%lld\n",cnt+2*n); 36 } 37 return 0; 38 }
我一开始写了个以height为第一关键字,下标为第二关键字的set……每次直接lower_bound……然而TLE了
后来改了改离散化+链表模拟……结果FST了
查了半天想不出算法哪里错了……然后把10W的数组改成100W……过了
以后这种空间复杂度为线性的题目……在允许的情况下还是尽量开大点好了……
1 //BestCoder 42 B 2 #include<vector> 3 #include<cstdio> 4 #include<cstdlib> 5 #include<cstring> 6 #include<iostream> 7 #include<algorithm> 8 #define rep(i,n) for(int i=0;i<n;++i) 9 #define F(i,j,n) for(int i=j;i<=n;++i) 10 #define D(i,j,n) for(int i=j;i>=n;--i) 11 using namespace std; 12 13 int getint(){ 14 int v=0,sign=1; char ch=getchar(); 15 while(ch<‘0‘||ch>‘9‘) {if (ch==‘-‘) sign=-1; ch=getchar();} 16 while(ch>=‘0‘&&ch<=‘9‘) {v=v*10+ch-‘0‘; ch=getchar();} 17 return v*sign; 18 } 19 typedef long long LL; 20 const int N=1000100,INF=~0u>>2; 21 /*******************template********************/ 22 int n,m,h[N],q[N],c[N<<1],head[N],v[N],nxt[N],cnt; 23 void add(int x,int y){ 24 v[++cnt]=y; nxt[cnt]=head[x]; head[x]=cnt; 25 } 26 int main(){ 27 #ifndef ONLINE_JUDGE 28 freopen("B.in","r",stdin); 29 // freopen("output.txt","w",stdout); 30 #endif 31 while(scanf("%d%d",&n,&m)!=EOF){ 32 F(i,1,n) c[i]=h[i]=getint(); 33 F(i,1,m) c[n+i]=q[i]=getint(); 34 memset(head,0,sizeof head); cnt=0; 35 sort(c+1,c+n+m+1); 36 int num=unique(c+1,c+n+m+1)-c-1; 37 D(i,n,1){ 38 h[i]=lower_bound(c+1,c+num+1,h[i])-c; 39 add(h[i],i); 40 } 41 F(i,1,m){ 42 q[i]=lower_bound(c+1,c+num+1,q[i])-c; 43 if (head[q[i]]==0) puts("-1"); 44 else{ 45 printf("%d\n",v[head[q[i]]]); 46 head[q[i]]=nxt[head[q[i]]]; 47 } 48 } 49 } 50 return 0; 51 }
直接背包DP吧……本来还想:会不会有人直接把ans的初值设为w[1][1]呢?然而w[1][1]>k?是不是可以hack一下……然而看了样例我发现我想多了……这场比赛的hack果然很少……
1 //BestCoder 42 C 2 #include<queue> 3 #include<set> 4 #include<vector> 5 #include<cstdio> 6 #include<cstring> 7 #include<cstdlib> 8 #include<iostream> 9 #include<algorithm> 10 #define rep(i,n) for(int i=0;i<n;++i) 11 #define F(i,j,n) for(int i=j;i<=n;++i) 12 #define D(i,j,n) for(int i=j;i>=n;--i) 13 #define pb push_back 14 using namespace std; 15 inline int getint(){ 16 int v=0,sign=1; char ch=getchar(); 17 while(ch<‘0‘||ch>‘9‘){ if (ch==‘-‘) sign=-1; ch=getchar();} 18 while(ch>=‘0‘&&ch<=‘9‘){ v=v*10+ch-‘0‘; ch=getchar();} 19 return v*sign; 20 } 21 const int N=110,INF=~0u>>2; 22 typedef long long LL; 23 /******************tamplate*********************/ 24 int n,m,K,w[N][N]; 25 bool f[N][N][N]; 26 struct node{ 27 int x,y,v; 28 }; 29 queue<node>Q; 30 int main(){ 31 #ifndef ONLINE_JUDGE 32 freopen("C.in","r",stdin); 33 freopen("C.out","w",stdout); 34 #endif 35 while(scanf("%d%d%d",&n,&m,&K)!=EOF){ 36 memset(w,0,sizeof w); 37 memset(f,0,sizeof f); 38 F(i,1,n) F(j,1,m) w[i][j]=getint(); 39 Q.push((node){1,1,0}); 40 int ans=0; 41 if (w[1][1]<=K){ 42 ans=w[1][1]; 43 Q.push((node){1,1,w[1][1]}); 44 } 45 while(!Q.empty()){ 46 int x=Q.front().x,y=Q.front().y,v=Q.front().v; Q.pop(); 47 if (x<n){ 48 if (!f[x+1][y][v]){ 49 f[x+1][y][v]=1; 50 Q.push((node){x+1,y,v}); 51 } 52 if (v+w[x+1][y]<=K && !f[x+1][y][v+w[x+1][y]]){ 53 f[x+1][y][v+w[x+1][y]]=1; 54 Q.push((node){x+1,y,v+w[x+1][y]}); 55 ans=max(ans,v+w[x+1][y]); 56 } 57 } 58 if (y<m){ 59 if (!f[x][y+1][v]){ 60 f[x][y+1][v]=1; 61 Q.push((node){x,y+1,v}); 62 } 63 if (v+w[x][y+1]<=K && !f[x][y+1][v+w[x][y+1]]){ 64 f[x][y+1][v+w[x][y+1]]=1; 65 Q.push((node){x,y+1,v+w[x][y+1]}); 66 ans=max(ans,v+w[x][y+1]); 67 } 68 } 69 } 70 printf("%d\n",ans); 71 } 72 return 0; 73 }
标签:
原文地址:http://www.cnblogs.com/Tunix/p/4525047.html