2018 ACM 网络选拔赛 南京赛区
A. An Olympian Math Problem
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <time.h> 6 #include <string> 7 #include <set> 8 #include <map> 9 #include <list> 10 #include <stack> 11 #include <queue> 12 #include <vector> 13 #include <bitset> 14 #include <ext/rope> 15 #include <algorithm> 16 #include <iostream> 17 using namespace std; 18 #define ll long long 19 #define minv 1e-6 20 #define inf 1e9 21 #define pi 3.1415926536 22 #define nl 2.7182818284 23 const ll mod=1e9+7;//998244353 24 const int maxn=1e5+10; 25 26 27 int main() 28 { 29 int t; 30 ll n; 31 scanf("%d",&t); 32 while (t--) 33 { 34 scanf("%lld",&n); 35 printf("%lld\n",n-1); 36 } 37 return 0; 38 }
B. The writing on the wall
与 https://leetcode.com/problems/maximal-rectangle/description/ 这道题很像
1 #include <bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 const ll mod=1e9+7; 5 const int maxn=1e5+10; 6 const int maxm=1e2+10; 7 8 int a[maxn][maxm],c[maxn][maxm]; 9 int qx[maxm],qy[maxm]; 10 11 int main() 12 { 13 ll sum=0; 14 int t,T,n,m,g,x,y,i,j,k; 15 bool vis; 16 scanf("%d",&t); 17 for (T=1;T<=t;T++) 18 { 19 memset(a,0,sizeof(a)); 20 scanf("%d%d%d",&n,&m,&g); 21 while (g--) 22 { 23 scanf("%d%d",&x,&y); 24 a[x][y]=1; 25 } 26 for (j=1;j<=m;j++) 27 for (i=1;i<=n;i++) 28 c[i][j]=(a[i][j]==1)?0:c[i-1][j]+1; 29 sum=0; 30 for (i=1;i<=n;i++) 31 { 32 ///以a[i][j]作为右下方 33 g=0; 34 for (j=1;j<=m;j++) 35 { 36 if (g==0 || c[i][j]>qx[g]) 37 vis=1; 38 else 39 vis=0; 40 sum+=c[i][j]; 41 qy[g+1]=j; 42 43 while (qx[g]>c[i][j]) 44 sum+=1ll*(qy[g+1]-qy[g])*c[i][j],g--; 45 k=g; 46 while (k) 47 sum+=1ll*(qy[k+1]-qy[k])*qx[k],k--; 48 if (g==0 || qx[g]!=c[i][j]) 49 g++; 50 if (vis) 51 qy[g]=j; 52 qx[g]=c[i][j]; 53 } 54 } 55 printf("Case #%d: %lld\n",T,sum); 56 } 57 return 0; 58 } 59 /* 60 100 61 3 3 3 62 1 1 63 1 2 64 2 1 65 66 3 3 4 67 1 1 68 1 2 69 2 1 70 2 2 71 72 2 3 0 73 74 100000 100 0 75 */
C. GDY
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <time.h> 6 #include <string> 7 #include <set> 8 #include <map> 9 #include <list> 10 #include <stack> 11 #include <queue> 12 #include <vector> 13 #include <bitset> 14 #include <ext/rope> 15 #include <algorithm> 16 #include <iostream> 17 using namespace std; 18 #define ll long long 19 #define minv 1e-6 20 #define inf 1e9 21 #define pi 3.1415926536 22 #define nl 2.7182818284 23 const ll mod=1e9+7;//998244353 24 const int maxn=2e2+10; 25 const int maxm=2e4; 26 27 int s[maxm],a[maxn][maxm],g[maxn]; 28 int value[14]={0,12,13,1,2,3,4,5,6,7,8,9,10,11}; 29 30 int cmp(int x,int y) 31 { 32 return value[x]>value[y]; 33 } 34 35 int main() 36 { 37 int t,T,n,m,num,i,j,sum; 38 int x,y,z; 39 scanf("%d",&t); 40 for (T=1;T<=t;T++) 41 { 42 memset(g,0,sizeof(g)); 43 44 scanf("%d%d",&n,&m); 45 for (i=0;i<m;i++) 46 scanf("%d",&s[i]); 47 num=0; 48 for (i=0;i<n;i++) 49 { 50 for (j=0;j<5;j++) 51 { 52 if (num==m) 53 break; 54 a[i][j]=s[num++]; 55 } 56 sort(a[i],a[i]+j,cmp); 57 g[i]=j; 58 } 59 60 z=a[0][g[0]-1];//previous number 61 g[0]--; 62 y=1;//has y persons 63 x=1;//pos 64 while (1) 65 { 66 for (i=g[x]-1;i>=0;i--) 67 if (value[z]+1==value[a[x][i]] || (a[x][i]==2 && z!=2)) 68 break; 69 70 if (i!=-1) 71 { 72 z=a[x][i]; 73 a[x][i]=0; 74 sort(a[x],a[x]+g[x],cmp); 75 g[x]--; 76 if (g[x]==0) 77 break; 78 y=1; 79 } 80 else if (y!=n-1) 81 y++; 82 else 83 { 84 x=(x+1)%n; 85 for (i=x;;i=(i+1)%n) 86 { 87 if (num==m) 88 break; 89 else 90 { 91 a[i][g[i]++]=s[num++]; 92 sort(a[i],a[i]+g[i],cmp); 93 } 94 if (i==(x-1+n)%n) 95 break; 96 } 97 98 z=a[x][g[x]-1]; 99 g[x]--; 100 if (g[x]==0) 101 break; 102 y=1; 103 } 104 105 x=(x+1)%n; 106 } 107 printf("Case #%d:\n",T); 108 for (i=0;i<n;i++) 109 if (i==x) 110 printf("Winner\n"); 111 else 112 { 113 sum=0; 114 for (j=0;j<g[i];j++) 115 sum+=a[i][j]; 116 printf("%d\n",sum); 117 } 118 } 119 return 0; 120 } 121 /* 122 10 123 2 6 124 3 5 7 9 11 4 125 126 3 20 127 2 2 2 2 2 2 2 2 2 2 128 2 2 2 2 2 2 2 2 2 2 129 130 3 19 131 2 2 2 2 2 2 2 2 2 2 132 2 2 2 2 2 2 2 2 2 133 134 3 20 135 3 2 2 2 2 2 2 2 2 2 2 136 2 2 2 2 2 2 2 2 2 137 138 2 10 139 3 4 5 6 7 2 3 4 5 6 140 141 3 15 142 3 4 5 6 7 12 12 12 12 12 13 13 13 13 13 143 144 3 11 145 1 2 3 4 5 6 7 8 9 10 11 146 147 3 13 148 1 2 3 4 5 6 7 8 9 10 11 12 13 149 150 1 151 3 17 152 1 2 3 4 5 6 7 8 9 10 11 12 13 12 11 10 9 153 154 */
E. AC Challenge
#include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <time.h> #include <string> #include <set> #include <map> #include <list> #include <stack> #include <queue> #include <vector> #include <bitset> #include <ext/rope> #include <algorithm> #include <iostream> using namespace std; #define ll long long #define minv 1e-6 #define inf -1e18 #define pi 3.1415926536 #define nl 2.7182818284 const ll mod=1e9+7;//998244353 const int maxn=1e5+10; ll f[1<<20]; struct node { ll value; int pos,t; }; struct cmp1 { bool operator() (node a,node b) { return a.value>b.value; } }; //priority_queue<node>st; priority_queue<node,vector<node>,cmp1>st; int er[21],a[21],b[21],v[21]; int main() { int n,m,s,i,j,value,pos,t; ll r=0,rr; for (i=0;i<20;i++) er[i]=(1<<i); scanf("%d",&n); for (i=0;i<(1<<n);i++) f[i]=inf; for (i=0;i<n;i++) { scanf("%d%d%d",&a[i],&b[i],&m); v[i]=0; while (m--) { scanf("%d",&s); v[i]+=(er[s-1]); } if (v[i]==0) { f[er[i]]=a[i]+b[i]; st.push({a[i]+b[i],er[i],2}); r=max(r,(ll)a[i]+b[i]); } } while (!st.empty()) { value=st.top().value; pos=st.top().pos; t=st.top().t; st.pop(); for (i=0;i<n;i++) if ((pos & er[i])==0 && (pos & v[i])==v[i] && value+1ll*t*a[i]+b[i]>f[pos|er[i]]) { rr=value+1ll*t*a[i]+b[i]; f[pos|er[i]]=rr; r=max(r,rr); st.push({rr,pos|er[i],t+1}); } } printf("%lld",r); return 0; } /* 3 1 2 0 1 5 0 8 1 0 */
G. Lpl and Energy-saving Lamps
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <time.h> 6 #include <string> 7 #include <set> 8 #include <map> 9 #include <list> 10 #include <stack> 11 #include <queue> 12 #include <vector> 13 #include <bitset> 14 #include <ext/rope> 15 #include <algorithm> 16 #include <iostream> 17 using namespace std; 18 #define ll long long 19 #define minv 1e-6 20 #define inf 1e18 21 #define pi 3.1415926536 22 #define nl 2.7182818284 23 const ll mod=1e9+7;//998244353 24 const int maxn=1e5+10; 25 26 struct node 27 { 28 int x,y; 29 }f[maxn],r[maxn]; 30 31 int tag[maxn<<2],a[maxn],x,y; 32 33 int cmp(node a,node b) 34 { 35 return a.x<b.x; 36 } 37 38 void build(int index,int l,int r) 39 { 40 if (l==r) 41 scanf("%d",&tag[index]); 42 else 43 { 44 int m=(l+r)>>1; 45 build(index<<1,l,m); 46 build(index<<1|1,m+1,r); 47 tag[index]=min(tag[index<<1],tag[index<<1|1]); 48 } 49 } 50 51 int query(int index,int l,int r,int v) 52 { 53 if (l==r) 54 { 55 if (tag[index]>v) 56 return 0; 57 58 x++; 59 y-=tag[index]; 60 tag[index]=inf; 61 return l; 62 } 63 else 64 { 65 int m=(l+r)>>1,z; 66 if (tag[index<<1]<=v) 67 z=query(index<<1,l,m,v); 68 69 else 70 z=query(index<<1|1,m+1,r,v); 71 tag[index]=min(tag[index<<1],tag[index<<1|1]); 72 return z; 73 } 74 } 75 76 int main() 77 { 78 int n,m,q,Q,d,index,i,j; 79 scanf("%d%d",&n,&m); 80 build(1,1,n); 81 82 scanf("%d",&q); 83 for (Q=1;Q<=q;Q++) 84 { 85 scanf("%d",&d); 86 f[Q].x=d; 87 f[Q].y=Q; 88 } 89 sort(f+1,f+q+1,cmp); 90 91 index=1; 92 x=0; 93 y=0; 94 for (i=1;i<=f[q].x;i++) 95 { 96 y+=m; 97 j=1; 98 while (j) 99 j=query(1,1,n,y); 100 101 while (f[index].x==i) 102 { 103 r[f[index].y]={x,y}; 104 index++; 105 } 106 } 107 108 for (i=1;i<=q;i++) 109 printf("%d %d\n",r[i].x,r[i].y); 110 return 0; 111 }
J. Sum
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <time.h> 6 #include <string> 7 #include <set> 8 #include <map> 9 #include <list> 10 #include <stack> 11 #include <queue> 12 #include <vector> 13 #include <bitset> 14 #include <ext/rope> 15 #include <algorithm> 16 #include <iostream> 17 using namespace std; 18 #define ll long long 19 #define minv 1e-6 20 #define inf 1e9 21 #define pi 3.1415926536 22 #define nl 2.7182818284 23 const ll mod=1e9+7;//998244353 24 const int maxn=2e7+10; 25 26 int zhi[maxn],f[maxn]; 27 bool vis[maxn],v[maxn]; 28 29 int main() 30 { 31 int t,i,j,k,x=0,y=0,value=2e7; 32 ll n,sum; 33 memset(vis,0,sizeof(vis)); 34 memset(v,0,sizeof(v)); 35 for (i=2;i<=value;i++) 36 { 37 if (!vis[i]) 38 { 39 x++; 40 zhi[x]=i; 41 } 42 for (j=1;j<=x;j++) 43 { 44 k=i*zhi[j]; 45 if (k>value) 46 break; 47 vis[k]=1; 48 v[k]=v[i]; 49 if (i%zhi[j]==0) 50 { 51 v[k]=1; 52 break; 53 } 54 } 55 } 56 for (i=1;i<=value;i++) 57 if (!v[i]) 58 { 59 y++; 60 f[y]=i; 61 } 62 63 // for (i=1;i<=100;i++) 64 // printf("%d ",f[i]); 65 66 scanf("%d",&t); 67 f[0]=0; 68 while (t--) 69 { 70 scanf("%lld",&n); 71 sum=0; 72 j=y; 73 for (i=1;i<=y;i++) 74 { 75 while (1ll*f[i]*f[j]>n) 76 j--; 77 sum+=j; 78 } 79 printf("%lld\n",sum); 80 } 81 return 0; 82 }
L. Magical Girl Haze
1 /* 2 图问题: 3 spfa容易被卡时间复杂度 4 5 而dijkstra是贪心,不会被卡 6 */ 7 #include <cstdio> 8 #include <cstdlib> 9 #include <cmath> 10 #include <cstring> 11 #include <time.h> 12 #include <string> 13 #include <set> 14 #include <map> 15 #include <list> 16 #include <stack> 17 #include <queue> 18 #include <vector> 19 #include <bitset> 20 #include <ext/rope> 21 #include <algorithm> 22 #include <iostream> 23 using namespace std; 24 #define ll long long 25 #define minv 1e-6 26 #define inf 1e18 27 #define pi 3.1415926536 28 #define nl 2.7182818284 29 const ll mod=1e9+7;//998244353 30 const int maxn=1e5+10; 31 32 struct rec 33 { 34 int d,len; 35 rec *to; 36 }*e[maxn]; 37 38 struct node 39 { 40 ll dist; 41 int k,d; 42 }; 43 44 struct cmp1 45 { 46 bool operator() (node a,node b) 47 { 48 if (a.dist==b.dist) 49 return a.k>b.k; 50 else 51 return a.dist>b.dist; 52 } 53 }; 54 55 priority_queue<node,vector<node>,cmp1>st; 56 57 ll f[maxn][12]; 58 59 int main() 60 { 61 rec *p; 62 int t,n,m,k,i,j,d,kk,dd,x,y,z; 63 ll dist,r; 64 scanf("%d",&t); 65 while (t--) 66 { 67 scanf("%d%d%d",&n,&m,&k); 68 for (i=1;i<=n;i++) 69 e[i]=NULL; 70 while (m--) 71 { 72 scanf("%d%d%d",&x,&y,&z); 73 p=(rec*) malloc (sizeof(rec)); 74 p->d=y; 75 p->len=z; 76 p->to=e[x]; 77 e[x]=p; 78 } 79 for (i=1;i<=n;i++) 80 for (j=0;j<=k;j++) 81 f[i][j]=inf; 82 for (j=0;j<=k;j++) 83 { 84 f[1][j]=0; 85 st.push({0,j,1}); 86 } 87 //f[i][j]作为一个状态,100000*10 88 while (!st.empty()) 89 { 90 dist=st.top().dist; 91 d=st.top().d; 92 kk=st.top().k; 93 st.pop(); 94 p=e[d]; 95 while (p) 96 { 97 dd=p->d; 98 if (f[dd][kk]>dist+p->len) 99 { 100 f[dd][kk]=dist+p->len; 101 st.push({f[dd][kk],kk,dd}); 102 } 103 if (kk!=k && f[dd][kk+1]>dist) 104 { 105 f[dd][kk+1]=dist; 106 st.push({f[dd][kk+1],kk+1,dd}); 107 } 108 p=p->to; 109 } 110 } 111 r=inf; 112 for (j=0;j<=k;j++) 113 r=min(r,f[n][j]); 114 printf("%lld\n",r); 115 } 116 return 0; 117 }