2018-2019 ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)
http://codeforces.com/contest/1070/problem/A
A. Find a Number
从高位到低位,避免从低位到高位遇到‘0’的麻烦。
bfs 从0(1)~9,首先找到的解是最小解。
二维布尔数组,余数&数字之和。
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxyu=5e2+10; 4 const int maxans=5e3+10; 5 const int maxtot=5e2*5e3+10; 6 7 bool vis[maxyu][maxans]; 8 pair<int,int> q[maxtot]; 9 int pre[maxtot],num[maxtot],s[10000]; 10 11 int main() 12 { 13 int yu,ans,head=0,tail=1,x,y,xx,yy,z,i; 14 scanf("%d%d",&yu,&ans); 15 q[1]={0,0}; 16 while (head<tail) 17 { 18 head++; 19 xx=q[head].first; 20 yy=q[head].second; 21 z=min(9,ans-yy); 22 for (i=(head==1?1:0);i<=z;i++) 23 { 24 x=(xx*10+i)%yu; 25 y=yy+i; 26 if (!vis[x][y]) 27 { 28 vis[x][y]=1; 29 tail++; 30 q[tail]={x,y}; 31 pre[tail]=head; 32 num[tail]=i; 33 if (x==0 && y==ans) 34 { 35 x=tail; 36 y=0; 37 while (x!=1) 38 { 39 s[++y]=num[x]; 40 x=pre[x]; 41 } 42 for (i=y;i>=1;i--) 43 printf("%d",s[i]); 44 return 0; 45 } 46 } 47 } 48 } 49 printf("-1"); 50 return 0; 51 } 52 /* 53 500 5000 54 */
C. Cloud Computing
study from:
https://blog.csdn.net/Cymbals/article/details/83304720
https://blog.csdn.net/Tawn0000/article/details/83217954
https://blog.csdn.net/sm_545/article/details/83273111
线段树:记录区间[x,y]的和
按照起始时间从小到大,扫描线
O((m+n)*log(p))
1 #include <bits/stdc++.h>
2 using namespace std;
3 #define ll long long
4 const int maxm=2e5+10;
5 const int maxn=1e6+10;
6
7 struct node
8 {
9 int l,r;
10 ll c,p;
11 }f[maxm];
12
13 vector<int>add[maxn],del[maxn];
14 ll tot[maxn<<2],num[maxn<<2];
15
16 ll query(int ind,int l,int r,ll k)
17 {
18 if (l==r)
19 return min(k,num[ind])*l;
20 int m=(l+r)>>1;
21 if (num[ind<<1]>=k)
22 return query(ind<<1,l,m,k);
23 else
24 return tot[ind<<1]+query(ind<<1|1,m+1,r,k-num[ind<<1]);
25 }
26
27 void update(int ind,int l,int r,ll c,ll p)
28 {
29 if (l==r)
30 {
31 tot[ind]+=c*p;
32 num[ind]+=c;
33 return;
34 }
35 int m=(l+r)>>1;
36 if (p<=m)
37 update(ind<<1,l,m,c,p);
38 else
39 update(ind<<1|1,m+1,r,c,p);
40 tot[ind]=tot[ind<<1]+tot[ind<<1|1];
41 num[ind]=num[ind<<1]+num[ind<<1|1];
42 }
43
44 int main()
45 {
46 ll sum=0;
47 int n,m,i;
48 ll k;
49 vector<int>::iterator j;
50 scanf("%d%lld%d",&n,&k,&m);
51 for (i=1;i<=m;i++)
52 {
53 scanf("%d%d%lld%lld",&f[i].l,&f[i].r,&f[i].c,&f[i].p);
54 add[f[i].l].push_back(i);
55 del[f[i].r].push_back(i);
56 }
57
58 for (i=1;i<=n;i++)
59 {
60 for (j=add[i].begin();j!=add[i].end();j++)
61 update(1,1,1e6,f[*j].c,f[*j].p);
62 sum+=query(1,1,1e6,k);
63 for (j=del[i].begin();j!=del[i].end();j++)
64 update(1,1,1e6,-f[*j].c,f[*j].p);
65 }
66 printf("%lld",sum);
67 return 0;
68 }
树状数组:记录区间[1,y]的和
二分求得[1,y']大于某个值
按照起始时间从小到大,扫描线
O(m*log(p)+n*log(p)*log(p))
1 #include <bits/stdc++.h>
2 using namespace std;
3 #define ll long long
4 const int maxm=2e5+10;
5 const int maxn=1e6+10;
6
7 struct node
8 {
9 int l,r;
10 ll c,p;
11 }f[maxm];
12
13 vector<int>add[maxn],del[maxn];
14 ll tot[maxn],num[maxn];
15
16 int main()
17 {
18 int n,m,i,l,r;
19 ll k,c,p,t,q,sum=0;
20 vector<int>::iterator j;
21 scanf("%d%lld%d",&n,&k,&m);
22 for (i=1;i<=m;i++)
23 {
24 scanf("%d%d%lld%lld",&f[i].l,&f[i].r,&f[i].c,&f[i].p);
25 add[f[i].l].push_back(i);
26 del[f[i].r].push_back(i);
27 }
28 for (i=1;i<=n;i++)
29 {
30 for (j=add[i].begin();j!=add[i].end();j++)
31 {
32 c=f[*j].c;
33 p=f[*j].p;
34 t=c*p;
35 while (p<=1e6)
36 {
37 tot[p]+=t;
38 num[p]+=c;
39 p+=p & (-p);
40 }
41 }
42
43 l=1,r=1e6;
44 while (l<=r)
45 {
46 p=(l+r)>>1;
47 q=p;
48 t=0;
49 while (p)
50 {
51 t+=num[p];
52 p-=p & (-p);
53 }
54 if (t>=k)
55 r=q-1;
56 else
57 l=q+1;
58 }
59
60 p=min(l,(int)1e6);
61 t=0;
62 while (p)
63 {
64 t+=num[p];
65 sum+=tot[p];
66 p-=p & (-p);
67 }
68 if (t>k)
69 sum-=(t-k)*l;
70
71 for (j=del[i].begin();j!=del[i].end();j++)
72 {
73 c=f[*j].c;
74 p=f[*j].p;
75 t=c*p;
76 while (p<=1e6)
77 {
78 tot[p]-=t;
79 num[p]-=c;
80 p+=p & (-p);
81 }
82 }
83 }
84 printf("%lld",sum);
85 return 0;
86 }
按照价格从小到大排序,线段树,超时
有大佬使用这个方法通过(第三个网址)
以下是我超时的代码
1 #include <bits/stdc++.h>
2 using namespace std;
3 #define ll long long
4 #define minv 1e-6
5 #define inf 1e9
6 #define pi 3.1415926536
7 #define nl 2.7182818284
8 const ll mod=1e9+7;//998244353
9 const int maxn=1e6+10;
10 const int maxm=2e5+10;
11
12 struct node
13 {
14 int l,r,c,p;
15 }f[maxm];
16 int z,i,ci,need[maxn<<2],del[maxn<<2];
17 ll sum=0;
18
19 int cmp(node a,node b)
20 {
21 return a.p<b.p;
22 }
23
24 void build(int ind,int l,int r)
25 {
26 need[ind]=z;
27 if (l!=r)
28 {
29 int m=(l+r)>>1;
30 build(ind<<1,l,m);
31 build(ind<<1|1,m+1,r);
32 }
33 }
34
35 void push_down(int ind)
36 {
37 del[ind<<1]+=del[ind];
38 del[ind<<1|1]+=del[ind];
39 if (need[ind<<1]!=-1)
40 need[ind<<1]-=del[ind];
41 if (need[ind<<1|1]!=-1)
42 need[ind<<1|1]-=del[ind];
43 del[ind]=0;
44 }
45
46 void update(int ind,int l,int r,int x,int y)
47 {
48 if (need[ind]==0)
49 return;
50 if (x<=l && r<=y &&need[ind]!=-1)
51 {
52 ci=min(f[i].c,need[ind]);
53 sum+=1ll*(r-l+1)*ci*f[i].p;
54 if (need[ind]!=-1)
55 need[ind]-=ci;
56 del[ind]+=ci;
57 return;
58 }
59 if (del[ind]!=0)
60 push_down(ind);
61 int m=(l+r)>>1;
62 if (x<=m)
63 update(ind<<1,l,m,x,y);
64 if (m<y)
65 update(ind<<1|1,m+1,r,x,y);
66
67 if (need[ind<<1]==need[ind<<1|1])
68 need[ind]=need[ind<<1];
69 else
70 need[ind]=-1;
71 }
72
73 int main()
74 {
75 int n,m;
76 scanf("%d%d%d",&n,&z,&m);
77 for (i=1;i<=m;i++)
78 scanf("%d%d%d%d",&f[i].l,&f[i].r,&f[i].c,&f[i].p);
79 sort(f+1,f+m+1,cmp);
80 build(1,1,n);
81 for (i=1;i<=m;i++)
82 update(1,1,n,f[i].l,f[i].r);
83 printf("%lld",sum);
84 return 0;
85 }
D. Garbage Disposal
两个整数的除法(非div)
1 #include <bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 #define minv 1e-6 5 #define inf 1e9 6 #define pi 3.1415926536 7 #define nl 2.7182818284 8 const ll mod=1e9+7;//998244353 9 const int maxn=1e5+10; 10 11 12 int main() 13 { 14 int n,k,a,b,g,i; 15 ll tot=0; 16 scanf("%d%d",&n,&k); 17 scanf("%d",&b); 18 for (i=2;i<=n;i++) 19 { 20 scanf("%d",&a); 21 g=ceil(1.0*b/k); 22 tot+=g; 23 b=max(0,a+b-g*k); 24 } 25 tot+=ceil(1.0*b/k); 26 printf("%lld",tot); 27 return 0; 28 }
E. Getting Deals Done
细节多
1 #include <bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 5 const int maxn=2e5+10; 6 int p[maxn]; 7 8 int main() 9 { 10 int T,n,len,c,l,r,m,i,g,gg; 11 ll t,x,y,xx; 12 scanf("%d",&T); 13 while (T--) 14 { 15 scanf("%d%d%lld",&n,&len,&t); 16 l=1e9; 17 for (i=1;i<=n;i++) 18 scanf("%d",&p[i]),l=min(l,p[i]),r=max(r,p[i]); 19 l++;r=min((ll)r,t); 20 while (l<=r) 21 { 22 m=(l+r)>>1; 23 y=0; 24 x=0; 25 c=0; 26 for (i=1;i<=n;i++) 27 if (p[i]<=m) 28 { 29 x+=p[i]; 30 y+=p[i]; 31 c++; 32 if (c==len) 33 { 34 y+=x; 35 xx=x; 36 x=0; 37 c=0; 38 } 39 } 40 if (c==0) 41 y-=xx; 42 if (y>t) 43 r=m-1; 44 else 45 l=m+1; 46 } 47 if (r==0) 48 r++; 49 50 c=0; 51 y=0; 52 x=0; 53 g=0; 54 for (i=1;i<=n;i++) 55 if (p[i]<=r) 56 { 57 x+=p[i]; 58 y+=p[i]; 59 if (y>t) 60 break; 61 g++; 62 c++; 63 if (c==len) 64 { 65 y+=x; 66 if (y>t) 67 break; 68 xx=x; 69 x=0; 70 c=0; 71 } 72 } 73 gg=g; 74 75 c=0; 76 y=0; 77 x=0; 78 g=0; 79 for (i=1;i<=n;i++) 80 if (p[i]<=r+1) 81 { 82 x+=p[i]; 83 y+=p[i]; 84 if (y>t) 85 break; 86 g++; 87 c++; 88 if (c==len) 89 { 90 y+=x; 91 if (y>t) 92 break; 93 xx=x; 94 x=0; 95 c=0; 96 } 97 } 98 99 if (gg>=g) 100 printf("%d %d\n",gg,r); 101 else 102 printf("%d %d\n",g,r+1); 103 } 104 return 0; 105 } 106 /* 107 1 108 11 1 3 109 6 4 3 7 5 3 4 7 3 5 3 110 111 1 112 11 1 31 113 6 4 3 7 5 3 4 7 3 5 3 114 */
F. Debate
推导为主
1 #include <bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 #define minv 1e-6 5 #define inf 1e9 6 #define pi 3.1415926536 7 #define nl 2.7182818284 8 const ll mod=1e9+7;//998244353 9 const int maxn=4e5+10; 10 11 int a[maxn],b[maxn],c[maxn],d[maxn],mb[maxn],mc[maxn]; 12 13 int cmp(int x,int y) 14 { 15 return x>y; 16 } 17 18 int main() 19 { 20 int n,t1=0,t2=0,t3=0,t4=0,sa=0,sd=0,r=0,i,j,v,diff=0; 21 char s[3]; 22 scanf("%d",&n); 23 for (i=1;i<=n;i++) 24 { 25 scanf("%s%d",s,&v); 26 if (strcmp(s,"00")==0) 27 a[++t1]=v; 28 else if (strcmp(s,"01")==0) 29 b[++t2]=v; 30 else if (strcmp(s,"10")==0) 31 c[++t3]=v; 32 else 33 d[++t4]=v; 34 } 35 sort(a+1,a+t1+1,cmp); 36 sort(b+1,b+t2+1,cmp); 37 sort(c+1,c+t3+1,cmp); 38 sort(d+1,d+t4+1,cmp); 39 for (i=1;i<=t2;i++) 40 mb[i]=mb[i-1]+b[i]; 41 for (i=1;i<=t3;i++) 42 mc[i]=mc[i-1]+c[i]; 43 44 t1=min(t1,t4); 45 for (i=1;i<=t1;i++) 46 sa+=a[i]; 47 for (i=1;i<=t4;i++) 48 sd+=d[i]; 49 50 j=t1; 51 diff=t4-t1; 52 while (j>=0) 53 { 54 if (t3>t2) 55 r=max(r,sa+sd+ mb[t2]+mc[min(t3,t2+diff)]); 56 else 57 r=max(r,sa+sd+ mb[min(t2,t3+diff)]+mc[t3]); 58 diff++; 59 sa-=a[j]; 60 j--; 61 } 62 printf("%d",r); 63 return 0; 64 } 65 /* 66 5 67 00 1 68 01 1 69 10 2 70 11 3 71 11 4 72 */
G. Monsters and Potions
H. BerOS File Suggestion
位数:总的情况+1(最高位的值不能为0)
1 #include <bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 #define minv 1e-6 5 #define inf 1e9 6 #define pi 3.1415926536 7 #define nl 2.7182818284 8 const ll mod=1e9+7;//998244353 9 const int maxn=1e4+10; 10 11 map<int,int>a,b; 12 char s[10],str[maxn][10]; 13 ll d[100]; 14 15 int main() 16 { 17 int n,i,j,l,len,q,g=0; 18 ll k; 19 scanf("%d",&n); 20 for (l=1;l<=n;l++) 21 { 22 scanf("%s",s); 23 strcpy(str[l],s); 24 len=strlen(s); 25 g=0; 26 for (i=0;i<len;i++) 27 { 28 k=0; 29 for (j=i;j<len;j++) 30 { 31 k*=38; 32 if (s[j]=='.') 33 k++; 34 else if (s[j]>='0' && s[j]<='9') 35 k+=s[j]-46; 36 else if (s[j]>='a' && s[j]<='z') 37 k+=s[j]-85; 38 d[++g]=k; 39 // printf("%lld ",k);/// 40 } 41 } 42 sort(d+1,d+g+1); 43 for (i=1;i<=g;i++) 44 if (d[i]!=d[i-1]) 45 { 46 a[d[i]]++; 47 if (a[d[i]]==1) 48 b[d[i]]=l; 49 } 50 // printf("\n");/// 51 } 52 scanf("%d",&q); 53 while (q--) 54 { 55 scanf("%s",s); 56 len=strlen(s); 57 k=0; 58 for (j=0;j<len;j++) 59 { 60 k*=38; 61 if (s[j]=='.') 62 k++; 63 else if (s[j]>='0' && s[j]<='9') 64 k+=s[j]-46; 65 else if (s[j]>='a' && s[j]<='z') 66 k+=s[j]-85; 67 } 68 // printf("-- %lld\n",k);/// 69 if (a.find(k)!=a.end()) 70 printf("%d %s\n",a[k],str[b[k]]); 71 else 72 printf("0 -\n"); 73 } 74 return 0; 75 }
K. Video Posts
1 #include <bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 #define minv 1e-6 5 #define inf 1e9 6 #define pi 3.1415926536 7 #define nl 2.7182818284 8 const ll mod=1e9+7;//998244353 9 const int maxn=1e5+10; 10 11 int a[maxn],b[maxn]; 12 13 int main() 14 { 15 int n,k,tot=0,y=0,j=0,ind=0,i; 16 scanf("%d%d",&n,&k); 17 for (i=1;i<=n;i++) 18 { 19 scanf("%d",&a[i]); 20 tot+=a[i]; 21 } 22 if (tot%k!=0) 23 { 24 printf("No"); 25 return 0; 26 } 27 tot/=k; 28 for (i=1;i<=n;i++) 29 { 30 y+=a[i]; 31 if (y==tot) 32 { 33 b[++j]=i-ind; 34 ind=i; 35 y=0; 36 } 37 else if (y>tot) 38 { 39 printf("No"); 40 return 0; 41 } 42 } 43 printf("Yes\n"); 44 for (i=1;i<=j;i++) 45 printf("%d%c",b[i],i==j?'\n':' '); 46 return 0; 47 }