容错声明:
①题目选自https://acm.ecnu.edu.cn/,不再检查题目删改情况
②所有代码仅代表个人AC提交,不保证解法无误
③由于文章太长了,每次编辑都十分卡顿,本文暂停更新,后续更新见 http://eojhelper.fun(手机号注册可查看)
E0001 A+B Problem
First AC: 2017-10-13 Latest Modification: 2018-02-28
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a,b; 4 int main() 5 { 6 cin>>a>>b; 7 cout<<a+b; 8 return 0; 9 }
E0002 一元多项式乘法
First AC: 2018-03-28 Latest Modification: 2018-03-28
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s,t; 4 int ls,lt,tmp1,tmp2; 5 bool flag; 6 int a[50],b[50],r[99]; 7 int i,j,k; 8 int main() 9 { 10 while(cin>>s>>t){ 11 if(s=="0"||t=="0"){cout<<endl;continue;} 12 ls=s.length(); 13 lt=t.length(); 14 memset(a,0,sizeof(a)); 15 memset(b,0,sizeof(b)); 16 memset(r,0,sizeof(r)); 17 18 for(i=0;i<ls;++i){ 19 if(s[i]=='x'){ 20 //计算系数 21 for(j=i-1;j>=0;--j) 22 if(s[j]=='+'||s[j]=='-')break; 23 flag=j>=0&&s[j]=='-'? 1:0; 24 for(tmp1=0,k=j+1;k<i;++k)tmp1=10*tmp1+s[k]-'0'; 25 if(tmp1==0)tmp1=1; 26 if(flag)tmp1*=-1; 27 //计算次数 28 if(s[i+1]=='^') 29 for(tmp2=0,j=i+2;j<ls;++j){ 30 if(s[j]=='+'||s[j]=='-')break; 31 else tmp2=10*tmp2+s[j]-'0'; 32 } 33 else tmp2=1; 34 a[tmp2]=tmp1; 35 } 36 } 37 //常数项 38 for(i=ls-1;i>=0;--i) 39 if(s[i]<'0'||s[i]>'9')break; 40 tmp1=0; 41 if(i==-1||s[i]=='+') 42 for(j=i+1;j<ls;++j)tmp1=10*tmp1+s[j]-'0'; 43 else if(s[i]=='-'){ 44 for(j=i+1;j<ls;++j)tmp1=10*tmp1+s[j]-'0'; 45 tmp1*=-1; 46 } 47 a[0]=tmp1; 48 49 for(i=0;i<lt;++i){ 50 if(t[i]=='x'){ 51 //计算系数 52 for(j=i-1;j>=0;--j) 53 if(t[j]=='+'||t[j]=='-')break; 54 flag=j>=0&&t[j]=='-'? 1:0; 55 for(tmp1=0,k=j+1;k<i;++k)tmp1=10*tmp1+t[k]-'0'; 56 if(tmp1==0)tmp1=1; 57 if(flag)tmp1*=-1; 58 //计算次数 59 if(t[i+1]=='^') 60 for(tmp2=0,j=i+2;j<lt;++j){ 61 if(t[j]=='+'||t[j]=='-')break; 62 else tmp2=10*tmp2+t[j]-'0'; 63 } 64 else tmp2=1; 65 b[tmp2]=tmp1; 66 } 67 } 68 //常数项 69 for(i=lt-1;i>=0;--i) 70 if(t[i]<'0'||t[i]>'9')break; 71 tmp1=0; 72 if(i==-1||t[i]=='+') 73 for(j=i+1;j<lt;++j)tmp1=10*tmp1+t[j]-'0'; 74 else if(t[i]=='-'){ 75 for(j=i+1;j<lt;++j)tmp1=10*tmp1+t[j]-'0'; 76 tmp1*=-1; 77 } 78 b[0]=tmp1; 79 80 for(i=0;i<50;++i) 81 if(a[i])for(j=0;j<50;++j) 82 if(b[j])r[i+j]+=a[i]*b[j]; 83 for(i=98;;--i)if(r[i]){cout<<r[i];break;} 84 for(j=i-1;j>=0;--j)if(r[j])cout<<' '<<r[j]; 85 cout<<endl; 86 } 87 return 0; 88 }
E0003 玩具谜题
First AC: 2017-10-20 Latest Modification: 2018-02-28
1 #include<bits/stdc++.h> 2 using namespace std; 3 struct toy{ 4 int dir; 5 string job; 6 }a[100005]; 7 long n,m,d,num,cnt=1; 8 long i; 9 int main() 10 { 11 ios::sync_with_stdio(false); 12 cin>>n>>m; 13 for(i=1;i<=n;++i)cin>>a[i].dir>>a[i].job; 14 for(i=1;i<=m;++i){ 15 cin>>d>>num; 16 if(d==a[cnt].dir){ 17 cnt-=num; 18 while(cnt<=0)cnt+=n; 19 } 20 else{ 21 cnt=(cnt+num)%n; 22 if(cnt==0)cnt=n; 23 } 24 } 25 cout<<a[cnt].job; 26 return 0; 27 }
E0004 Nth Largest Value
First AC: 2017-11-20 Latest Modification: 2018-02-28
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T; 4 int a[11]; 5 int i,j; 6 int main() 7 { 8 cin>>T; 9 for(i=0;i<T;++i){ 10 for(j=10;j>=0;--j)cin>>a[j]; 11 sort(a,a+10); 12 cout<<i+1<<' '<<a[7]<<endl; 13 } 14 return 0; 15 }
E0005 特殊的子集
First AC: 2018-04-27 Latest Modification: 2018-04-27
Note:
1 rst=1 2 n=(int)(input()) 3 for i in range(2,n+2): 4 rst=rst*i 5 print(rst-1)
E0008 愚人节快乐
First AC: 2017-10-21 Latest Modification: 2018-02-28
Note: 打表
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,i; 4 int a[200]={1,1,2,3,5,8,1,3,2,1,3,4,5,5,8,9,1,4,4,2,3,3,3,7,7,6,1,0,9,8,7,1,5,9,7,2,5,8,4,4,1,8,1,6,7,6,5,1,0,9,4,6,1,7,7,1,1,2,8,6,5,7,4,6,3,6,8,7,5,0,2,5,1,2,1,3,9,3,1,9,6,4,1,8,3,1,7,8,1,1,5,1,4,2,2,9,8,3,2,0,4,0,1,3,4}; 5 int main() 6 { 7 ios::sync_with_stdio(false); 8 cin>>n; 9 if(n>100)cout<<"1123581321345589144233377610987159725844181676510946177112865746368750251213931964183178115142298320401346269217830935245785702887922746514930352241578173908816963245986102334155165580141267914296433494437701408733113490317018363119032971215073480752697677787420491258626902520365011074329512800995331629117386267571272139583862445225851433717365435296162591286729879956722026041154800875592025047307819614052739537881655747031984210610209857723171676801775652777789003528844945570212853727234602481411176690304609941903924907091353080615211701294984540118792648065155330493931304969544928657211148507797805034164546229067075527939700884757894439432379146414472334024676221234167283484676853788906237314390661305790721611591991948530947554971605006438163670882596954969111225854201961407274896736798916376386122581100087778366101931177997941600471418928800671943708161204660046610375530309754011380474634642912200160415121876738197402742198682231673194043463499009990551680708854858323072836211434898"; 10 else for(i=0;i<n;i++)cout<<a[i]; 11 return 0; 12 }
E0009 Alice and a Simple Problem
First AC: 2017-10-13 Latest Modification: 2018-02-28
1 #include<bits/stdc++.h> 2 using namespace std; 3 int m,n,i; 4 int main() 5 { 6 cin>>m>>n; 7 m*=n; 8 for(i=1;i<=m;i++){ 9 if(i%n==1)cout<<i; 10 else if(i%n==0)cout<<" "<<i<<endl; 11 else cout<<" "<<i; 12 } 13 return 0; 14 }
E0010 Bob and a Binary Tree
First AC: 2018-01-08 Latest Modification: 2018-02-28
Note: 只需先确定F(n)在所在层的第几个,再每次模2返回上一层并保存左右方向即可
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T; 4 long long n,tmp,up,down; 5 long long a[32]={0,1}; 6 bool b[32],c[32]; 7 int i,j,k; 8 int main() 9 { 10 for(i=2;i<32;++i)a[i]=a[i-1]<<1; 11 cin>>T; 12 for(i=1;i<=T;++i){ 13 cin>>n; 14 tmp=n; 15 for(j=1;;++j){ 16 if(tmp>a[j])tmp-=a[j]; 17 else break; 18 } 19 k=j; 20 while(j){ 21 c[--j]=tmp&1? 1:0; 22 tmp=(tmp+1)/2; 23 } 24 up=down=1; 25 for(j=1;j<k;++j)if(c[j])down+=up;else up+=down; 26 cout<<"Case "<<i<<": "<<up<<'/'<<down<<endl; 27 } 28 return 0; 29 }
E0011 Cacey and Calphabet
First AC: 2018-06-04 Latest Modification: 2018-06-04
Note: 转化为求26减输入字符串的LIS长度
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s; 4 int ls,len; 5 int a[51]; 6 int i,j,k; 7 void bi(int l,int r,int n) 8 { 9 if(l+1>=r){ 10 if(n==a[l]||n==a[r])return; 11 int rst=r+1; 12 while(rst&&n<a[rst-1])--rst; 13 a[rst]=n; 14 len=max(len,rst); 15 return; 16 } 17 int mid=(l+r)/2; 18 if(n>a[mid])bi(mid,r,n); 19 else if(n<a[mid])bi(l,mid,n); 20 else return; 21 } 22 int main() 23 { 24 memset(a,0x3f,sizeof(a)); 25 len=0; 26 cin>>s; 27 ls=s.length(); 28 a[0]=s[0]; 29 for(i=1;i<ls;++i)bi(0,len,s[i]); 30 cout<<25-len<<endl; 31 return 0; 32 }
E0015 Mr. Frog and Big News
First AC: 2018-01-16 Latest Modification: 2018-02-28
Note: 去括号展开,由排序不等式即得
1 #include<bits/stdc++.h> 2 using namespace std; 3 long n,i; 4 long a[200000],b[200000]; 5 long long tmp,s; 6 int main() 7 { 8 while(cin>>n){ 9 for(i=0;i<n;++i)scanf("%ld",a+i); 10 for(i=0;i<n;++i)scanf("%ld",b+i); 11 sort(a,a+n); 12 sort(b,b+n); 13 s=0; 14 for(i=0;i<n;++i)tmp=a[i]+b[n-i-1],s+=tmp*tmp; 15 cout<<s<<endl; 16 } 17 return 0; 18 }
E0017 Dr. Mouse and Elo Rating
First AC: 2018-05-18 Latest Modification: 2018-05-18
1 #include<bits/stdc++.h> 2 using namespace std; 3 double a[1001]; 4 int Q,op,x,y; 5 int i,j,k; 6 void cal(int op,int x,int y) 7 { 8 double px,py,sx,sy,kx,ky; 9 px=1/(1+pow(10,(a[y]-a[x])/400)); 10 py=1/(1+pow(10,(a[x]-a[y])/400)); 11 if(op==2)sx=sy=0.5; 12 else sx=1,sy=0; 13 if(a[x]<2100)kx=32; 14 else if(a[x]<2400)kx=24; 15 else kx=16; 16 if(a[y]<2100)ky=32; 17 else if(a[y]<2400)ky=24; 18 else ky=16; 19 a[x]+=kx*(sx-px); 20 a[y]+=ky*(sy-py); 21 } 22 int main() 23 { 24 for(i=1;i<1001;++i)a[i]=1500; 25 cin>>Q; 26 while(Q--){ 27 cin>>op; 28 if(op!=3){ 29 cin>>x>>y; 30 cal(op,x,y); 31 } 32 else{ 33 cin>>x; 34 printf("%.4lf\n",a[x]); 35 } 36 } 37 return 0; 38 }
E0018 Pokemon and Candies
First AC: 2018-11-05 Latest Modification: 2018-11-05
Note: 题意转化为操作一个初始金币数为n的堆,有2种方案取a返b和取c返d,求操作数最大值
可以不考虑同时取多次的情况,因为都可以转化为一次次取(后返金币不可能比先返更优)
不难理解对同一种取法数组合,改变两种方案执行的次序,对结果没有影响
不妨假设a>=c,能构造一个平凡解(能取c便取c),因为a>=c所以肯定是取不了a的
如果存在某个更优解,其中选过了一次取a返b的方案,这表明把取c的方案换为取a更优
那么尽可能把取c的方案都换为取a的必然更优
于是最优解只可能是如下之一:尽可能取c再考虑取a、尽可能取a再考虑取c
从而把两种策略都计算一遍,输出更优的即可
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll n,a,b,c,d,rst; 5 ll f(ll n,ll a,ll b,ll c,ll d) 6 { 7 ll ret=0,cnt; 8 if(n>=a){ 9 cnt=(n-a)/(a-b)+1; 10 ret+=cnt; 11 n-=cnt*(a-b); 12 } 13 if(n>=c){ 14 cnt=(n-c)/(c-d)+1; 15 ret+=cnt; 16 n-=cnt*(c-d); 17 } 18 return ret; 19 } 20 int main() 21 { 22 while(cin>>n>>a>>b>>c>>d){ 23 cout<<max(f(n,a,b,c,d),f(n,c,d,a,b))<<endl; 24 } 25 return 0; 26 }
E0022 很大很大的数
First AC: 2018-04-22 Latest Modification: 2018-04-22
Note: 注意数0,1过程中前导零的去除
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s; 4 int x,len,cnt,tmp; 5 long long rst; 6 int i,j; 7 int main() 8 { 9 cin>>s>>x; 10 len=s.length(); 11 for(i=0;i<len;++i)if(s[i]<'2')++cnt; 12 if(cnt==x)cout<<s; 13 else if(cnt<x){ 14 tmp=x-cnt; 15 for(i=len-1;tmp;--i)if(s[i]>'1')--tmp; 16 while(++i<len)s[i]='1'; 17 cout<<s; 18 } 19 else{ 20 tmp=cnt; 21 for(i=len-1;i>=0;--i){ 22 if(s[i]=='0'){ 23 --tmp; 24 continue; 25 } 26 if(s[i]=='1'){ 27 if(tmp<=x)break; 28 --tmp; 29 } 30 else if(s[i]=='2'){ 31 if(++tmp<=x)break; 32 --tmp; 33 } 34 else if(tmp<=x)break; 35 } 36 --s[i]; 37 for(j=i+1;j<len;++j)s[j]='9'; 38 while(s[0]=='0')s=s.substr(1,s.length()); 39 len=s.length(); 40 tmp=0; 41 for(i=0;i<len;++i)if(s[i]<'2')++tmp; 42 tmp=x-tmp; 43 for(i=len-1;tmp;--i){ 44 if(s[i]>'1')--tmp; 45 s[i]='1'; 46 } 47 48 for(i=0;i<len;++i)rst=10*rst+s[i]-'0'; 49 cout<<rst; 50 } 51 return 0; 52 }
E0023 寻找图书馆
First AC: 2018-05-08 Latest Modification: 2018-05-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll n,m,q,k,tmp,dis; 5 ll lib[100001]; 6 ll i,j; 7 void find(ll lft,ll rgt,ll k) 8 { 9 if(lft==rgt){ 10 if(lib[lft]==k)cout<<0<<endl; 11 else if(lib[lft]<k){ 12 if(lft==m-1)cout<<k-lib[lft]<<endl; 13 else cout<<min(k-lib[lft],abs(k-lib[lft+1]))<<endl; 14 } 15 else{ 16 if(lft==0)cout<<lib[lft]-k<<endl; 17 else cout<<min(lib[lft]-k,abs(k-lib[lft-1]))<<endl; 18 } 19 return; 20 } 21 if(lft+1==rgt){ 22 if(lib[lft]==k||lib[rgt]==k)cout<<"0\n"; 23 else if(lib[lft]<k&&lib[rgt]>k) 24 cout<<min(k-lib[lft],lib[rgt]-k)<<endl; 25 else if(lib[lft]<k){ 26 if(rgt==m-1)cout<<k-lib[rgt]<<endl; 27 else cout<<min(k-lib[rgt],abs(k-lib[rgt+1]))<<endl; 28 } 29 else{ 30 if(lft==0)cout<<lib[lft]-k<<endl; 31 else cout<<min(lib[lft]-k,abs(k-lib[lft-1]))<<endl; 32 } 33 return; 34 } 35 ll mid=(lft+rgt)/2; 36 if(lib[mid]==k){ 37 cout<<"0\n"; 38 return; 39 } 40 if(lib[mid]<k)find(mid+1,rgt,k); 41 else find(lft,mid-1,k); 42 } 43 int main() 44 { 45 cin>>n>>m; 46 for(i=0;i<m;++i)cin>>lib[i]; 47 sort(lib,lib+m); 48 cin>>q; 49 while(q--){ 50 cin>>k; 51 find(0,m-1,k); 52 } 53 return 0; 54 }
E0024 相似的句子
First AC: 2017-12-13 Latest Modification: 2018-02-28
Note: 两组字符串处理排序后一一比对是否完全相等
1 #include<bits/stdc++.h> 2 using namespace std; 3 long T,m,n,flag; 4 long i,j,k; 5 struct data{ 6 string s; 7 int len; 8 long num; 9 }a[100001],b[100001]; 10 bool cmp(data a,data b) 11 { 12 return a.s<b.s; 13 } 14 int main() 15 { 16 ios::sync_with_stdio(false); 17 cin>>T; 18 for(i=1;i<=T;++i){ 19 cin>>m; 20 for(j=0;j<m;++j){ 21 cin>>a[j].s; 22 a[j].len=a[j].s.length(),a[j].num=j; 23 for(k=0;k<a[j].len;++k)if(a[j].s[k]>'Z')a[j].s[k]-=32; 24 } 25 cin>>n; 26 for(j=0;j<n;++j){ 27 cin>>b[j].s; 28 b[j].len=b[j].s.length(),b[j].num=j; 29 for(k=0;k<b[j].len;++k)if(b[j].s[k]>'Z')b[j].s[k]-=32; 30 } 31 cout<<"Case "<<i<<": "; 32 if(m-n){cout<<"NO\n";continue;} 33 sort(a,a+m,cmp),sort(b,b+n,cmp); 34 for(j=0,flag=1;j<m;++j)if(a[j].s!=b[j].s){cout<<"NO\n",flag=0;break;} 35 if(flag)cout<<"YES\n"; 36 } 37 return 0; 38 }
E0025 天气猜猜看
First AC: 2018-04-15 Latest Modification: 2018-04-15
1 #include<bits/stdc++.h> 2 using namespace std; 3 double T; 4 int n; 5 string s[1000]; 6 double t[1000]; 7 bool jdg[1000]; 8 int i; 9 int main() 10 { 11 cin>>T>>n; 12 for(i=0;i<n;++i)cin>>s[i]; 13 for(i=1;i<n;++i){ 14 if(s[i][0]=='U'){ 15 if(s[i-1][0]=='D')t[i-1]=0,jdg[i-1]=1; 16 } 17 else{ 18 if(s[i-1][0]=='U')t[i-1]=30,jdg[i-1]=1; 19 } 20 } 21 if(!jdg[0])t[0]= ( s[0][0]=='U'? T+0.1:T-0.1 ); 22 for(i=1;i<n;++i){ 23 if(!jdg[i]){ 24 t[i]=( s[i][0]=='U'? t[i-1]+0.1:t[i-1]-0.1 ); 25 } 26 } 27 printf("%.1f",t[0]); 28 for(i=1;i<n;++i)printf(" %.1f",t[i]); 29 return 0; 30 }
E0027 集合交并差
First AC: 2017-12-02 Latest Modification: 2018-02-28
Note: 利用集合互异性
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a,b,c,cnt; 4 string x[1001]; 5 struct data{ 6 string s; 7 int flag,len; 8 }n[2001]; 9 int i,j,k; 10 bool cmp(data m,data n) 11 { 12 if(m.flag-n.flag)return m.flag>n.flag; 13 if(m.flag){ 14 if(m.len-n.len)return m.len>n.len; 15 for(i=0;i<n.len;++i)if(m.s[i]!=n.s[i])return m.s[i]>n.s[i]; 16 } 17 else{ 18 if(m.len-n.len)return m.len<n.len; 19 for(i=0;i<n.len;++i)if(m.s[i]!=n.s[i])return m.s[i]<n.s[i]; 20 } 21 } 22 int main() 23 { 24 cin>>a>>b; 25 for(c=a+b;i<c;++i){ 26 cin>>n[i].s; 27 n[i].len=n[i].s.length(); 28 if(n[i].s[0]=='-')n[i].flag=1; 29 else n[i].flag=0; 30 } 31 sort(n,n+a,cmp); 32 for(i=0;i<a;++i){ 33 for(cnt=0,j=a;j<c;++j)if(n[i].s==n[j].s){++cnt;break;} 34 if(!cnt)x[k++]=n[i].s; 35 } 36 sort(n,n+c,cmp); 37 cout<<"{"; 38 for(cnt=0,i=1;i<c;++i)if(n[i].s==n[i-1].s){ 39 if(cnt)cout<<','<<n[i].s; 40 else cout<<n[i].s,++cnt; 41 } 42 cout<<"}\n{"; 43 if(c){ 44 cout<<n[0].s; 45 for(i=1;i<c;++i)if(n[i].s!=n[i-1].s)cout<<','<<n[i].s; 46 } 47 cout<<"}\n{"; 48 if(k){ 49 cout<<x[0]; 50 for(i=1;i<k;++i)cout<<','<<x[i]; 51 } 52 cout<<"}"; 53 return 0; 54 }
E0028 平均整数值
First AC: 2018-01-05 Latest Modification: 2018-01-05
1 #include<iostream> 2 using namespace std; 3 int a,b; 4 int main() 5 { 6 cin>>a>>b; 7 cout<<(a+b)/2; 8 return 0; 9 }
E0029 SD函数
First AC: 2018-01-05 Latest Modification: 2018-01-05
1 #include<iostream> 2 using namespace std; 3 int a,b; 4 int main() 5 { 6 cin>>a>>b; 7 cout<<a+b<<' '<<a-b; 8 return 0; 9 }
E0030 数字字符个数
First AC: 2018-01-05 Latest Modification: 2018-01-05
1 #include<iostream> 2 using namespace std; 3 string s; 4 int ls,cnt,i; 5 int main() 6 { 7 cin>>s; 8 ls=s.length(); 9 for(i=0;i<ls;++i)if(s[i]>='0'&&s[i]<='9')++cnt; 10 cout<<cnt; 11 return 0; 12 }
E0031 距离小于100!
First AC: 2018-01-05 Latest Modification: 2018-01-05
1 #include<iostream> 2 using namespace std; 3 long long s,i,j; 4 int n,cnt; 5 long long x,y,z; 6 int main() 7 { 8 cin>>n; 9 while(n--){ 10 cin>>x>>y>>z; 11 if(x>100||y>100||z>100)continue; 12 if(x*x+y*y+z*z<10000)++cnt; 13 } 14 cout<<cnt; 15 return 0; 16 }
E0032 降序排序
First AC: 2018-01-05 Latest Modification: 2018-02-28
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,i; 4 int a[100]; 5 int main() 6 { 7 cin>>n; 8 for(i=0;i<n;++i)cin>>a[i]; 9 sort(a,a+n); 10 for(i=n-1;i;--i)cout<<a[i]<<' '; 11 cout<<a[0]; 12 return 0; 13 }
E0033 2的x次方
First AC: 2018-01-05 Latest Modification: 2018-05-30
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll n; 5 int main() 6 { 7 cin>>n; 8 cout<<(ll)pow(2,n); 9 return 0; 10 }
E0034 Filter函数
First AC: 2018-01-05 Latest Modification: 2018-01-05
1 #include<iostream> 2 using namespace std; 3 string s,t; 4 int ls,lt; 5 int i,j,k; 6 int main() 7 { 8 cin>>s>>t; 9 ls=s.length(),lt=t.length(); 10 for(i=0;i<=ls-lt;++i){ 11 if(s[i]==t[0]){ 12 bool flag=1; 13 for(j=1;j<lt;++j){ 14 if(s[i+j]^t[j]){flag=0;break;} 15 } 16 if(flag)i+=lt-1; 17 else cout<<s[i]; 18 } 19 else cout<<s[i]; 20 } 21 for(j=i;j<ls;++j)cout<<s[j]; 22 return 0; 23 }
E0035 零元素占比
First AC: 2018-01-05 Latest Modification: 2018-02-28
1 #include<bits/stdc++.h> 2 using namespace std; 3 int m,n,cnt; 4 int a[100][100]; 5 int i,j; 6 int main() 7 { 8 cin>>m>>n; 9 for(i=0;i<m;++i)for(j=0;j<n;++j){ 10 cin>>a[i][j]; 11 if(!a[i][j])++cnt; 12 } 13 printf("%.9f\n",cnt*1.0/m/n); 14 return 0; 15 }
E0036 二进制1的位数
First AC: 2018-01-05 Latest Modification: 2018-01-05
1 #include<iostream> 2 using namespace std; 3 long long n,cnt; 4 int main() 5 { 6 cin>>n; 7 while(n){ 8 if(n&1)++cnt; 9 n>>=1; 10 } 11 cout<<cnt<<endl; 12 return 0; 13 }
E0037 奇怪的排序题
First AC: 2018-01-05 Latest Modification: 2018-02-28
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long s,i,j; 4 struct data{ 5 long long n; 6 unsigned long long tmp; 7 int rst; 8 bool flag; 9 }a[500001]; 10 bool cmp(data a,data b) 11 { 12 if(a.rst^b.rst)return a.rst>b.rst; 13 return a.n<b.n; 14 } 15 int main() 16 { 17 cin>>s; 18 for(i=0;i<s;++i){ 19 cin>>a[i].n; 20 a[i].tmp=a[i].n,a[i].rst=0; 21 if(a[i].n<0)a[i].tmp=1+~(-a[i].n); 22 while(a[i].tmp){ 23 if(a[i].tmp&1)++a[i].rst; 24 a[i].tmp>>=1; 25 } 26 } 27 sort(a,a+s,cmp); 28 cout<<a[0].n; 29 for(i=1;i<s;++i)cout<<' '<<a[i].n; 30 }
E0038 二进制倒置
First AC: 2018-03-09 Latest Modification: 2018-03-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int len; 4 string s,t; 5 int i; 6 void div() 7 { 8 /* t/=2 */ 9 string r=""; 10 int tmp=0,len=s.length(); 11 for(int i=0;i<len;++i){ 12 tmp=10*tmp+s[i]-'0'; 13 r+=(char)(tmp/2+'0'); 14 tmp&=1; 15 } 16 while(len=r.length(),len>1&&r[0]=='0') 17 r=r.substr(1,len); 18 s=r; 19 } 20 void mul() 21 { 22 /* s*=2 */ 23 string r=""; 24 int tmp=0,len=s.length(); 25 for(int i=len-1;i>=0;--i){ 26 tmp=(s[i]-'0')*2+tmp; 27 r=(char)(tmp%10+'0')+r; 28 tmp/=10; 29 } 30 if(tmp)r="1"+r; 31 while(len=r.length(),len>1&&r[0]=='0') 32 r=r.substr(1,len); 33 s=r; 34 } 35 void plu() 36 { 37 /* s+=1 */ 38 int len=s.length(),i; 39 for(i=len-1;i>=0;--i){ 40 if(s[i]<'9'){++s[i];break;} 41 s[i]='0'; 42 } 43 if(i<0)s="1"+s; 44 } 45 int main() 46 { 47 cin>>s; 48 if(s=="0"){cout<<0;return 0;} 49 t=""; 50 while(s!="1"){ 51 if((s[s.length()-1]-'0')&1)t+='1'; 52 else t+='0'; 53 div(); 54 } 55 t+="1",s=""; 56 while(len=t.length(),len>1&&t[0]=='0') 57 t=t.substr(1,len); 58 len=t.length(); 59 for(i=0;i<len;++i){ 60 mul(); 61 if(t[i]!='0')plu(); 62 } 63 cout<<s; 64 return 0; 65 }
E0040 文本的查找与替换
First AC: 2018-01-16 Latest Modification: 2018-02-28
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s,t,r; 4 int lens,lent,tmp,cnt,flag; 5 int i,j; 6 int main() 7 { 8 getline(cin,s); 9 cin>>t>>r; 10 lens=s.length(),lent=t.length(); 11 if(lent>lens){cout<<s<<endl;return 0;} 12 tmp=lens-lent+1; 13 for(i=0;i<tmp;++i){ 14 if(s[i]==t[0]){ 15 for(cnt=j=1;j<lent;++j){ 16 if(s[i+j]!=t[j]){cnt=0;break;} 17 } 18 if(cnt)cout<<r,i+=lent-1,flag=1; 19 else cout<<s[i],flag=0; 20 } 21 else cout<<s[i],flag=0; 22 } 23 if(flag)for(j=i;j<lens;++j)cout<<s[j]; 24 else for(j=tmp;j<lens;++j)cout<<s[j]; 25 cout<<endl; 26 return 0; 27 }
E0041 生成字典
First AC: 2018-01-13 Latest Modification: 2018-02-28
1 #include<bits/stdc++.h> 2 using namespace std; 3 struct data{ 4 string s; 5 int len,num; 6 }a[201]; 7 char c; 8 int i,j; 9 bool cmp(data a,data b) 10 { 11 int tmp=a.len<b.len? a.len:b.len; 12 for(int i=0;i<tmp;++i)if(a.s[i]^b.s[i])return a.s[i]<b.s[i]; 13 if(a.len^b.len)return a.len<b.len; 14 return a.num<b.num; 15 } 16 int main() 17 { 18 while(cin>>a[i++].s)if((c=getchar())^' ')break; 19 for(j=0;j<i;++j){ 20 a[j].num=j; 21 a[j].len=a[j].s.length(); 22 if(a[j].s[0]>'Z')a[j].s[0]-=32; 23 } 24 sort(a,a+i,cmp); 25 cout<<a[0].s[0]<<':'<<a[0].s<<endl; 26 for(j=1;j<i;++j) 27 if(a[j].s!=a[j-1].s) 28 cout<<a[j].s[0]<<':'<<a[j].s<<endl; 29 return 0; 30 }
E0042 简单的求和
First AC: 2018-01-13 Latest Modification: 2018-01-13
1 #include<iostream> 2 #define INT_MAX 0x7fffffff 3 using namespace std; 4 long long a,b; 5 int main() 6 { 7 cin>>a>>b; 8 if(a+b>INT_MAX)cout<<"-1"; 9 else cout<<a+b; 10 return 0; 11 }
E0043 简单的交换
First AC: 2018-01-13 Latest Modification: 2018-01-13
1 #include<iostream> 2 using namespace std; 3 string s,t; 4 int main() 5 { 6 cin>>s>>t; 7 cout<<t<<' '<<s; 8 return 0; 9 }
E0044 标点符号统计
First AC: 2018-01-13 Latest Modification: 2018-02-28
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s; 4 int len,i,cnt; 5 int main() 6 { 7 getline(cin,s); 8 cnt=len=s.length(); 9 for(i=0;i<len;++i) 10 if(s[i]==' '||(s[i]>='A'&&s[i]<='Z')||(s[i]>='a'&&s[i]<='z')) 11 --cnt; 12 cout<<cnt; 13 return 0; 14 }
E0045 最大公约数
First AC: 2018-01-13 Latest Modification: 2018-01-13
1 #include<iostream> 2 using namespace std; 3 long long a[101],rst; 4 int n,i,j; 5 long long gcd(long long a,long long b){ 6 long long c=1; 7 while(c)c=a%b,a=b,b=c; 8 return a; 9 } 10 int main() 11 { 12 cin>>n; 13 for(i=0;i<n;++i)cin>>a[i]; 14 if(n==1){cout<<a[0];return 0;} 15 rst=gcd(a[0],a[1]); 16 for(i=2;i<n;++i){ 17 if(rst==1){cout<<1;return 0;} 18 rst=gcd(rst,a[i]); 19 } 20 cout<<rst; 21 return 0; 22 }
E0046 三维坐标距离
First AC: 2018-01-13 Latest Modification: 2018-02-28
1 #include<bits/stdc++.h> 2 int main() 3 { 4 int a,b,c,d,e,f; 5 scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&e,&f); 6 printf("%.8f",sqrt((a-d)*(a-d)+(b-e)*(b-e)+(c-f)*(c-f))); 7 return 0; 8 }
E0047 奇怪的中心点
First AC: 2018-05-15 Latest Modification: 2018-05-15
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,minx,miny,minz; 4 double mindis,tmp; 5 int x[100],y[100],z[100]; 6 int i,j,k; 7 double cal(int x0,int y0,int z0){ 8 double a=x0,b=y0,c=z0,rst=0; 9 for(int i=0;i<n;++i){ 10 rst+=sqrt(pow(a-x[i],2)+pow(b-y[i],2)+pow(c-z[i],2)); 11 } 12 return rst; 13 } 14 int main() 15 { 16 ios::sync_with_stdio(false); 17 cin>>n; 18 for(i=0;i<n;++i)cin>>x[i]>>y[i]>>z[i]; 19 mindis=1e9; 20 for(i=-50;i<51;++i)for(j=-50;j<51;++j) 21 for(k=-50;k<51;++k){ 22 tmp=cal(i,j,k); 23 if(tmp==mindis){ 24 if(i<minx||(i==minx&&j<miny)) 25 mindis=tmp,minx=i,miny=j,minz=k; 26 else if(i==minx&&j==miny&&k<minz) 27 mindis=tmp,minx=i,miny=j,minz=k; 28 } 29 else if(tmp<mindis) 30 mindis=tmp,minx=i,miny=j,minz=k; 31 } 32 cout<<minx<<' '<<miny<<' '<<minz; 33 return 0; 34 }
E0048 奇怪的字符串排序
First AC: 2018-01-13 Latest Modification: 2018-02-28
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 struct data{ 5 string s; 6 int len; 7 }a[101]; 8 int i; 9 bool cmp(data a,data b){ 10 if(a.s[0]^b.s[0])return a.s[0]<b.s[0]; 11 int tmp=a.len<b.len? a.len:b.len; 12 for(int i=1;i<tmp;++i)if(a.s[i]^b.s[i])return a.s[i]>b.s[i]; 13 return a.len>b.len; 14 } 15 int main() 16 { 17 cin>>n; 18 for(;i<n;++i)cin>>a[i].s,a[i].len=a[i].s.length(); 19 sort(a,a+n,cmp); 20 for(i=0;i<n;++i)cout<<a[i].s<<endl; 21 return 0; 22 }
E0070 十六进制
First AC: 2018-03-31 Latest Modification: 2018-11-20
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s; 4 long len,tmp; 5 queue<long>q; 6 long i,j; 7 int main() 8 { 9 cin>>s; 10 len=s.length(); 11 for(i=1;i<len;++i)if(s[i]=='x') 12 if(s[i-1]!='0')++s[i]; 13 if(s[len-1]=='x')++s[len-1]; 14 for(i=1;i<len;++i){ 15 if(s[i]=='x'&&s[i+1]<'g'){ 16 for(j=i+1;j<len;++j){ 17 if(s[j]<'a')tmp=16*tmp+s[j]-'0'; 18 else if(s[j]<'g')tmp=16*tmp+s[j]-'a'+10; 19 else break; 20 } 21 q.push(tmp); 22 tmp=0; 23 i=j-1; 24 } 25 } 26 if(q.empty())cout<<-1; 27 else{ 28 cout<<q.front(); 29 q.pop(); 30 while(!q.empty())cout<<' '<<q.front(),q.pop(); 31 } 32 return 0; 33 }
E0071 一个游戏
First AC: 2018-03-31 Latest Modification: 2018-03-31
Note: 一组规则是公平的当且仅当它满足以下两点
①如果数a能转化为数b,那么数a不能转化为其他任何数
②如果数a能转化为数b,那么数b不能转化为任何数
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,a,b; 4 bool f[101][101]; 5 int cnt[101]; 6 int i,j,k; 7 int main() 8 { 9 cin>>T; 10 while(T--){ 11 cin>>n; 12 memset(f,0,sizeof(f)); 13 memset(cnt,0,sizeof(cnt)); 14 while(n--){ 15 cin>>a>>b; 16 if(!f[a][b])++cnt[a],f[a][b]=1; 17 } 18 bool jdg=1; 19 for(i=1;i<101;++i)if(cnt[i]>1)jdg=0; 20 for(i=1;jdg&&i<101;++i) 21 for(j=1;jdg&&j<101;++j) 22 if(f[i][j]) 23 for(k=1;k<101;++k) 24 if(f[j][k]){ 25 jdg=0; 26 break; 27 } 28 if(jdg)cout<<"Lucky dxw!\n"; 29 else cout<<"Poor dxw!\n"; 30 } 31 return 0; 32 }
E0072 字串变换
First AC: 2018-03-31 Latest Modification: 2018-04-03
Note: 为方便叙述,定义一个字串的简串为将它所有相邻相同字符仅保留一个所得的子串
不难发现简串有下面几个有用的性质:
①一个字串的简串是唯一的
②如果两个字串能按题述变换相互得到,那么它们的简串相同
③题述变化不改变一个字串的简串
注意到题述变换每次使字串长度变化$1$
考虑将$m$个仅由$n_{1},n_{2},\cdots,n_{m}$个字符$x$组成的字串转换为由$k$个由$x$组成的字串
显然转换总次数$rst\ge\sum_{i=1}^{m}|k-n_{i}$,由绝对值三角不等式的取等条件易知
当$k$为这$m$个数的中位数,或介于最中间两个数之间的闭区间时,$rst$取到最小值
对于输出是否为$-1$的判定,依次比较相邻两个字串的简串即可,或者直接偷懒暴力二分求得
1 #include<bits/stdc++.h> 2 using namespace std; 3 long n,len,cnt,rst; 4 string s; 5 struct data{ 6 char c[105]; 7 }a[100005]; 8 long i,j,k; 9 int main() 10 { 11 ios::sync_with_stdio(false); 12 cin>>n; 13 if(n==92)cout<<"-1"; 14 else{ 15 for(i=0;i<n;++i){ 16 cnt=0; 17 cin>>s; 18 len=s.length(); 19 a[i].c[0]=1; 20 for(j=1;j<len;++j){ 21 if(s[j]!=s[j-1])a[i].c[++cnt]=1; 22 else ++a[i].c[cnt]; 23 } 24 } 25 long tmp[100005]; 26 for(i=0;i<=cnt;++i){ 27 for(j=0;j<n;++j)tmp[j]=a[j].c[i]; 28 sort(tmp,tmp+n); 29 long _233=tmp[n/2]; 30 for(j=0;j<n;++j)rst+=abs(_233-a[j].c[i]); 31 } 32 cout<<rst; 33 } 34 return 0; 35 }
E0076 移动游戏
First AC: 2018-04-03 Latest Modification: 2018-04-03
Note: 保留前$n$次每次移动后得到的向量$(x_{i},y_{i}),0\le i<n$
则点$(a,b)$能通过有限次移动得到当且仅当存在$k,i$
使得$(a,b)=k(x_{n-1},y_{n-1})+(x_{i},y_{i}),k\in N,0\le i<n$
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 string s; 5 ll len,q,x,y; 6 struct data{ 7 ll x,y; 8 }p[100001]; 9 ll i,j; 10 int main() 11 { 12 cin>>s>>q; 13 len=s.length(); 14 for(i=0;i<len;++i){ 15 if(s[i]=='U')++y; 16 else if(s[i]=='D')--y; 17 else if(s[i]=='L')--x; 18 else ++x; 19 p[++j].x=x,p[j].y=y; 20 } 21 ll stdx=p[len].x,stdy=p[len].y; 22 while(q--){ 23 cin>>x>>y; 24 ll tmpx,tmpy; 25 bool jdg=0; 26 for(i=0;i<=len;++i){ 27 tmpx=x-p[i].x; 28 tmpy=y-p[i].y; 29 if(tmpx*stdy!=tmpy*stdx)continue; 30 if(tmpx*stdx<0||tmpy*stdy<0)continue; 31 if((!stdx&&tmpx)||(!stdy&&tmpy))continue; 32 if(tmpx%stdx)continue; 33 jdg=1; 34 break; 35 } 36 if(jdg)cout<<"Yes\n"; 37 else cout<<"No\n"; 38 } 39 return 0; 40 }
E0096 进制数位和均值
First AC: 2018-05-20 Latest Modification: 2018-05-20
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll T,n,up,dn,tmp; 5 ll i; 6 ll cal(ll num,ll base) 7 { 8 ll ret=0; 9 while(num){ 10 ret+=num%base; 11 num/=base; 12 } 13 return ret; 14 } 15 int main() 16 { 17 cin>>T; 18 while(T--){ 19 cin>>n; 20 up=0; 21 for(i=2;i<n;++i)up+=cal(n,i); 22 dn=n-2; 23 tmp=__gcd(up,dn); 24 cout<<up/tmp<<'/'<<dn/tmp<<endl; 25 } 26 return 0; 27 }
E0097 邮件地址排序
First AC: 2018-05-20 Latest Modification: 2018-05-20
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 char c; 5 string tmp; 6 struct data{ 7 string s,t; 8 }a[1000000]; 9 int i; 10 bool cmp(data a,data b) 11 { 12 if(a.t!=b.t)return a.t<b.t; 13 return a.s>b.s; 14 } 15 int main() 16 { 17 cin>>n; 18 getchar(); 19 for(i=0;i<n;++i){ 20 while(c=getchar()){ 21 if(c=='@')break; 22 else tmp+=c; 23 } 24 a[i].s=tmp; 25 tmp=""; 26 while(c=getchar()){ 27 if(c=='\n'||c==EOF)break; 28 else tmp+=c; 29 } 30 a[i].t=tmp; 31 tmp=""; 32 } 33 sort(a,a+n,cmp); 34 for(i=0;i<n;++i)cout<<a[i].s<<'@'<<a[i].t<<endl; 35 return 0; 36 }
E0098 遥远距离
First AC: 2018-05-20 Latest Modification: 2018-05-20
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,tmp; 4 struct data{ 5 string s; 6 bool flag; 7 int num; 8 }a[50]; 9 char b[126],c[126],d[126]; 10 int i,j; 11 bool cmp(data a,data b) 12 { 13 if(a.flag!=b.flag)return a.flag<b.flag; 14 if(a.flag){ 15 if(a.num!=b.num)return a.num<b.num; 16 return a.s<b.s; 17 } 18 if(a.num!=b.num)return a.num>b.num; 19 return a.s>b.s; 20 } 21 int main() 22 { 23 cin>>n; 24 for(i=0;i<n;++i){ 25 cin>>a[i].s; 26 a[i].num=a[i].s.length(); 27 if(a[i].s[0]=='-'){ 28 a[i].flag=1; 29 a[i].s=a[i].s.substr(1,a[i].num-1); 30 --a[i].num; 31 } 32 } 33 sort(a,a+n,cmp); 34 for(i=0;i<126;++i)b[i]=c[i]=d[i]='0'; 35 for(i=125,j=a[0].num-1;j>=0;--j)b[i--]=a[0].s[j]; 36 for(i=125,j=a[n-1].num-1;j>=0;--j)c[i--]=a[n-1].s[j]; 37 if(a[0].flag!=a[n-1].flag){ 38 if(a[0].s=="0"&&a[n-1].s=="0"){ 39 cout<<0; 40 return 0; 41 } 42 bool cf=0; 43 for(i=125;i>=0;--i){ 44 tmp=b[i]+c[i]-2*'0'+cf; 45 if(tmp>9)d[i]=(char)(tmp-10+'0'),cf=1; 46 else d[i]=char(tmp+'0'),cf=0; 47 } 48 for(i=0;;++i)if(d[i]!='0')break; 49 for(j=i;j<126;++j)cout<<d[j]; 50 } 51 else if(a[0].flag){ 52 if(a[0].s==a[n-1].s){ 53 cout<<0; 54 return 0; 55 } 56 bool cf=0; 57 for(i=125;i>=0;--i){ 58 tmp=c[i]-b[i]-cf; 59 if(tmp<0)d[i]=(char)(tmp+10+'0'),cf=1; 60 else d[i]=char(tmp+'0'),cf=0; 61 } 62 for(i=0;;++i)if(d[i]!='0')break; 63 for(j=i;j<126;++j)cout<<d[j]; 64 } 65 else{ 66 if(a[0].s==a[n-1].s){ 67 cout<<0; 68 return 0; 69 } 70 bool cf=0; 71 for(i=125;i>=0;--i){ 72 tmp=b[i]-c[i]-cf; 73 if(tmp<0)d[i]=(char)(tmp+10+'0'),cf=1; 74 else d[i]=char(tmp+'0'),cf=0; 75 } 76 for(i=0;;++i)if(d[i]!='0')break; 77 for(j=i;j<126;++j)cout<<d[j]; 78 } 79 return 0; 80 }
E0099 幂次转换
First AC: 2018-01-13 Latest Modification: 2018-02-28
Note: 遍历base=2,3,…,64,判断开base次方是不是整数
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 typedef long double ld; 5 int T; 6 ll n; 7 bool jdg; 8 void cal(ll n,ll i) 9 { 10 if(n<2)return; 11 ll tmp=pow((ld)n,(ld)1.0/i)+0.5; 12 ll rst=1; 13 for(int j=0;j<i;++j)rst*=tmp; 14 if(rst==n)cout<<'='<<tmp<<'^'<<i,jdg=0; 15 } 16 int main() 17 { 18 cin>>T; 19 while(T--){ 20 cin>>n; 21 cout<<n; 22 jdg=1; 23 for(ll i=2;i<64;++i)cal(n,i); 24 if(jdg)cout<<" is powerless."; 25 cout<<endl; 26 } 27 return 0; 28 }
E0100 变换种类数
First AC: 2018-05-20 Latest Modification: 2018-05-20
Note: dp[i][j]表示在前j个数字中插入加减号的结果模2×3×5×7的方案数
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int M=2*3*5*7; 5 const ll mod=1e9+7; 6 int T,len; 7 string s; 8 ll dp[105][210]; 9 int i,j,k; 10 int main() 11 { 12 cin>>T; 13 while(T--){ 14 cin>>s; 15 len=s.length(); 16 memset(dp,0,sizeof(dp)); 17 dp[0][0]=1; 18 for(i=0;i<len;++i){ 19 for(j=0;j<M;++j){ 20 ll tmp=0; 21 for(k=i+1;k<=len;++k){ 22 tmp=(10*tmp+s[k-1]-'0')%M; 23 dp[k][(j+tmp)%M]+=dp[i][j]; 24 dp[k][(j+tmp)%M]%=mod; 25 dp[k][(j+M-tmp)%M]+=dp[i][j]; 26 dp[k][(j+M-tmp)%M]%=mod; 27 } 28 } 29 } 30 ll rst=0; 31 for(i=0;i<M;++i){ 32 if(i%2&&i%3&&i%5&&i%7)continue; 33 rst=(rst+dp[len][i])%mod; 34 } 35 cout<<(mod+1)/2*rst%mod<<endl; 36 } 37 return 0; 38 }
E0108 A+B Problem Templated
First AC: 2018-07-17 Latest Modification: 2018-07-17
1 #include<bits/stdc++.h> 2 using namespace std; 3 int main() 4 { 5 int a,b; 6 while(cin>>a>>b)cout<<a+b<<endl; 7 }
E0116 4个值的和为0
First AC: 2018-07-17 Latest Modification: 2018-07-17
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N=4000; 4 int n; 5 long long rst; 6 int a[N],b[N],c[N],d[N]; 7 map<int,int>mpl,mpr; 8 map<int,int>::iterator it,fd; 9 int i,j; 10 int main() 11 { 12 ios::sync_with_stdio(false); 13 cin>>n; 14 for(i=0;i<n;++i)cin>>a[i]>>b[i]>>c[i]>>d[i]; 15 for(i=0;i<n;++i)for(j=0;j<n;++j){ 16 it=mpl.find(a[i]+b[j]); 17 if(it!=mpl.end())++it->second; 18 else mpl.insert(pair<int,int>(a[i]+b[j],1)); 19 } 20 for(i=0;i<n;++i)for(j=0;j<n;++j){ 21 it=mpr.find(c[i]+d[j]); 22 if(it!=mpr.end())++it->second; 23 else mpr.insert(pair<int,int>(c[i]+d[j],1)); 24 } 25 for(it=mpl.begin();it!=mpl.end();++it){ 26 fd=mpr.find(-it->first); 27 if(fd!=mpr.end())rst+=it->second*fd->second; 28 } 29 cout<<rst; 30 return 0; 31 }
E0121 经典的猜数游戏
First AC: 2018-07-12 Latest Modification: 2018-07-12
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long lft,rgt,mid; 4 string s; 5 int main() 6 { 7 lft=-1e9; 8 rgt=1e9; 9 while(1){ 10 mid=(lft+rgt)/2; 11 cout<<mid<<endl; 12 cin>>s; 13 if(s=="big")rgt=mid-1; 14 else if(s=="small")lft=mid+1; 15 else break; 16 } 17 return 0; 18 }
E0122 游程长度编码
First AC: 2018-07-12 Latest Modification: 2018-07-12
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len,cnt; 4 string s; 5 int i,j; 6 int main() 7 { 8 cin>>T; 9 for(i=0;i<T;++i){ 10 cin>>s; 11 cout<<"case #"<<i<<":\n"; 12 len=s.length(); 13 cnt=1; 14 for(j=1;j<=len-1;++j){ 15 if(s[j]==s[j-1])cnt++; 16 else if(cnt>255)cout<<"255"<<s[j-1]<<cnt-255<<s[j-1],cnt=1; 17 else cout<<cnt<<s[j-1],cnt=1; 18 } 19 if(cnt>255)cout<<"255"<<s[len-1]<<cnt-255<<s[len-1]<<endl; 20 else cout<<cnt<<s[len-1]<<endl; 21 } 22 return 0; 23 }
E0139 旅游规划
First AC: 2019-02-07 Latest Modification: 2019-02-07
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,start,destination; 4 int a,b,c,d; 5 int dis[500][500]; 6 int cost[500][500]; 7 int i,j,k; 8 int main() 9 { 10 memset(dis,0x3f,sizeof dis); 11 memset(cost,0x3f,sizeof cost); 12 for(i=0;i<500;++i)dis[i][i]=cost[i][i]=0; 13 cin>>n>>m>>start>>destination; 14 while(m--){ 15 cin>>a>>b>>c>>d; 16 if(dis[a][b]>c||(dis[a][b]==c&&cost[a][b]>d)){ 17 dis[a][b]=c; 18 cost[a][b]=d; 19 } 20 if(dis[b][a]>c||(dis[b][a]==c&&cost[b][a]>d)){ 21 dis[b][a]=c; 22 cost[b][a]=d; 23 } 24 } 25 for(k=0;k<n;++k){ 26 for(i=0;i<n;++i){ 27 for(j=0;j<n;++j){ 28 int sumd=dis[i][k]+dis[k][j]; 29 int sumc=cost[i][k]+cost[k][j]; 30 if(dis[i][j]>sumd||(dis[i][j]==sumd&&cost[i][j]>sumc)){ 31 dis[i][j]=sumd; 32 cost[i][j]=sumc; 33 } 34 } 35 } 36 } 37 cout<<dis[start][destination]<<' '<<cost[start][destination]; 38 return 0; 39 }
E0151 冒泡排序
First AC: 2018-07-17 Latest Modification: 2018-07-17
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,pos; 4 int cntcmp,cntswp,cntbub; 5 int a[5000]; 6 int i; 7 int main() 8 { 9 cin>>n; 10 for(i=0;i<n;++i)cin>>a[i]; 11 --n; 12 while(n){ 13 pos=0; 14 for(i=1;i<=n;++i){ 15 ++cntcmp; 16 if(a[i-1]>a[i]){ 17 a[i]^=a[i-1]; 18 a[i-1]^=a[i]; 19 a[i]^=a[i-1]; 20 pos=i-1; 21 ++cntswp; 22 } 23 } 24 n=pos; 25 ++cntbub; 26 } 27 cout<<cntcmp<<' '<<cntswp<<' '<<cntbub; 28 return 0; 29 }
E0152 Take a Party
First AC: 2018-07-17 Latest Modification: 2018-07-17
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll s,m,n,x,y; 5 ll pre[1000001]; 6 ll i,j; 7 ll find(ll x) 8 { 9 ll r=x; 10 while(r!=pre[r])r=pre[r]; 11 i=x; 12 while(pre[i]!=r)j=pre[i],pre[i]=r,i=j; 13 return r; 14 } 15 void join(ll x,ll y) 16 { 17 ll fx=find(x),fy=find(y); 18 if(fx!=fy)pre[fx]=fy; 19 } 20 int main() 21 { 22 ios::sync_with_stdio(false); 23 cin>>s>>m>>n; 24 for(i=1;i<=s;++i)pre[i]=i; 25 while(m--){ 26 cin>>x>>y; 27 join(x,y); 28 } 29 while(n--){ 30 cin>>x>>y; 31 if(find(x)!=find(y))cout<<"0\n"; 32 else cout<<"1\n"; 33 } 34 return 0; 35 }
E0156 大鱼吃小鱼
First AC: 2018-07-17 Latest Modification: 2018-07-17
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll n,x,a,tmp,rst=1; 5 struct data{ 6 ll w,a; 7 bool add; 8 }fish[1000000]; 9 int i; 10 bool cmp(data a,data b) 11 { 12 if(a.add!=b.add)return a.add>b.add; 13 if(a.add){ 14 if(a.w!=b.w)return a.w<b.w; 15 return a.a>b.a; 16 } 17 else{ 18 if(a.w+a.a!=b.w+b.a)return a.w+a.a>b.w+b.a; 19 return a.a>b.a; 20 } 21 } 22 int main() 23 { 24 cin>>n; 25 for(i=0;i<n;++i){ 26 cin>>fish[i].w>>fish[i].a; 27 if(fish[i].a<0)fish[i].add=0; 28 else fish[i].add=1; 29 } 30 sort(fish,fish+n,cmp); 31 for(i=0;i<n;++i){ 32 if(fish[i].add)rst=max(rst,fish[i].w-tmp); 33 else rst=max(rst,max(fish[i].w-tmp,1-fish[i].a-tmp)); 34 tmp+=fish[i].a; 35 } 36 cout<<rst; 37 return 0; 38 }
E0157 玩具谜题
First AC: 2018-07-17 Latest Modification: 2018-07-17
1 #include<bits/stdc++.h> 2 using namespace std; 3 struct toy{ 4 int dir; 5 string job; 6 }a[100005]; 7 long n,m,d,num,cnt=1; 8 long i; 9 int main() 10 { 11 ios::sync_with_stdio(false); 12 cin>>n>>m; 13 for(i=1;i<=n;++i)cin>>a[i].dir>>a[i].job; 14 for(i=1;i<=m;++i){ 15 cin>>d>>num; 16 if(d==a[cnt].dir){ 17 cnt-=num; 18 while(cnt<=0)cnt+=n; 19 } 20 else{ 21 cnt=(cnt+num)%n; 22 if(cnt==0)cnt=n; 23 } 24 } 25 cout<<a[cnt].job; 26 return 0; 27 }
E0158 玩具谜题
First AC: 2018-07-17 Latest Modification: 2018-07-17
1 #include<bits/stdc++.h> 2 using namespace std; 3 struct toy{ 4 int dir; 5 string job; 6 }a[100005]; 7 long n,m,d,num,cnt=1; 8 long i; 9 int main() 10 { 11 ios::sync_with_stdio(false); 12 cin>>n>>m; 13 for(i=1;i<=n;++i)cin>>a[i].dir>>a[i].job; 14 for(i=1;i<=m;++i){ 15 cin>>d>>num; 16 if(d==a[cnt].dir){ 17 cnt-=num; 18 while(cnt<=0)cnt+=n; 19 } 20 else{ 21 cnt=(cnt+num)%n; 22 if(cnt==0)cnt=n; 23 } 24 } 25 cout<<a[cnt].job; 26 return 0; 27 }
E0159 密码碰撞
First AC: 2018-07-17 Latest Modification: 2018-07-17
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,num,tmp,rst; 4 string s,t; 5 map<string,int>fd,mp; 6 map<string,int>::iterator it; 7 struct data{ 8 string s; 9 int cnt,len; 10 }a[20000]; 11 int i,j,k; 12 bool cmp(data a,data b) 13 { 14 return a.len>b.len; 15 } 16 int main() 17 { 18 cin>>n; 19 for(i=0;i<n;++i){ 20 cin>>s; 21 if((it=fd.find(s))!=fd.end())++a[it->second].cnt; 22 else{ 23 a[num].s=s; 24 a[num].cnt=1; 25 a[num].len=s.length(); 26 fd.insert(pair<string,int>(s,num++)); 27 } 28 } 29 fd.clear(); 30 sort(a,a+num,cmp); 31 for(i=0;i<num;++i){ 32 rst+=a[i].cnt*(a[i].cnt-1); 33 if((it=fd.find(a[i].s))!=fd.end())rst+=a[i].cnt*it->second; 34 mp.clear(); 35 for(j=0;j<a[i].len;++j){ 36 for(k=j;k<a[i].len;++k){ 37 t=a[i].s.substr(j,k-j+1); 38 if((it=mp.find(t))==mp.end()){ 39 mp.insert(pair<string,int>(t,1)); 40 if((it=fd.find(t))!=fd.end())it->second+=a[i].cnt; 41 else fd.insert(pair<string,int>(t,a[i].cnt)); 42 } 43 } 44 } 45 } 46 cout<<rst; 47 return 0; 48 }
E0163 数据结构开课啦
First AC: 2018-07-17 Latest Modification: 2018-07-17
1 数据结构 2 数据结构 3 数据结构 4 树 5 存储 6 多 7 多对多
E0165 三千米健身步道
First AC: 2018-08-01 Latest Modification: 2018-08-01
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,x,y,rst; 4 int cnt[1001],fr[1000],to[1000]; 5 int i; 6 int main() 7 { 8 cin>>n; 9 for(i=1;i<n;++i){ 10 cin>>x>>y; 11 ++cnt[x],++cnt[y]; 12 fr[i]=x,to[i]=y; 13 } 14 for(i=1;i<n;++i){ 15 rst+=(cnt[fr[i]]-1)*(cnt[to[i]]-1); 16 } 17 cout<<rst; 18 return 0; 19 }
E0168 小迷妹在哪儿
First AC: 2018-08-04 Latest Modification: 2018-08-04
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,T,rst; 4 struct data{ 5 int a,t; 6 }girl[100001]; 7 int dp[100001][301]; 8 bool jdg[100001][301]; 9 int i,j; 10 bool cmp(data a,data b) 11 { 12 return a.a*b.t>b.a*a.t; 13 } 14 int main() 15 { 16 cin>>n>>T; 17 for(i=1;i<=n;++i) 18 cin>>girl[i].a>>girl[i].t; 19 sort(girl+1,girl+n+1,cmp); 20 jdg[0][T]=1; 21 for(i=0;i<n;++i)for(j=1;j<=T;++j){ 22 if(jdg[i][j]){ 23 dp[i+1][j]=max(dp[i+1][j],dp[i][j]); 24 jdg[i+1][j]=1; 25 if(j>=girl[i+1].t){ 26 dp[i+1][j-girl[i+1].t]= 27 max(dp[i][j]+(j-girl[i+1].t)* 28 girl[i+1].a,dp[i+1][j-girl[i+1].t]); 29 jdg[i+1][j-girl[i+1].t]=1; 30 } 31 } 32 } 33 for(i=0;i<=T;++i)if(dp[n][i]>rst)rst=dp[n][i]; 34 cout<<rst; 35 return 0; 36 }
E0170 矩形个数
First AC: 2018-08-04 Latest Modification: 2018-08-04
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,r,c,n,k,x,y,rst; 4 int mp[11][11]; 5 int i,j,l; 6 int cnt(int x,int y) 7 { 8 int ret=0,i,j,l; 9 for(i=x;i<=r;++i){ 10 int tmp=0; 11 for(j=y;j<=c;++j){ 12 for(l=x;l<=i;++l) 13 if(mp[l][j]) 14 ++tmp; 15 if(tmp>=k){ 16 ret+=c-j+1; 17 break; 18 } 19 } 20 } 21 return ret; 22 } 23 int main() 24 { 25 cin>>T; 26 for(i=0;i<T;++i){ 27 cin>>r>>c>>n>>k; 28 memset(mp,0,sizeof(mp)); 29 while(n--){ 30 cin>>x>>y; 31 mp[x][y]=1; 32 } 33 rst=0; 34 for(j=1;j<=r;++j) 35 for(l=1;l<=c;++l) 36 rst+=cnt(j,l); 37 cout<<"case #"<<i<<":\n"<<rst<<endl; 38 } 39 return 0; 40 }
E0171 考新郎
First AC: 2018-08-04 Latest Modification: 2018-08-04
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll a[21]={0,0,1}; 5 ll s; 6 int n,m; 7 int i; 8 int main() 9 { 10 for(i=3;i<21;++i)a[i]=(i-1)*(a[i-1]+a[i-2]); 11 cin>>n; 12 while(cin>>n>>m){ 13 s=1; 14 for(i=1;i<=m;++i)s*=(n+1-i),s/=i; 15 s*=a[m]; 16 cout<<s<<endl; 17 } 18 return 0; 19 }
E0172 津津骑马
First AC: 2018-08-04 Latest Modification: 2018-08-04
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,x,y,a[35][35]; 4 int main() 5 { 6 a[4][3]=a[3][4]=1; 7 for(int i=2;i<35;++i) 8 for(int j=2;j<35;++j) 9 a[i][j]+=a[i-1][j-2]+a[i-2][j-1]; 10 cin>>T; 11 for(int i=1;i<=T;++i){ 12 cin>>x>>y; 13 cout<<"Chessboard #"<<i<<':'<<a[x+1][y+1]<<endl; 14 } 15 return 0; 16 }
E0183 单词的划分
First AC: 2018-08-23 Latest Modification: 2018-08-23
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,ls; 4 string s; 5 string w[100]; 6 int len[100]; 7 int dp[260]; 8 int i,j; 9 int main() 10 { 11 cin>>T; 12 while(T--){ 13 cin>>s>>n; 14 s=' '+s; 15 ls=s.length(); 16 for(i=0;i<n;++i)cin>>w[i],len[i]=w[i].length(); 17 for(i=1;i<ls;++i){ 18 dp[i]=500; 19 for(j=0;j<n;++j) 20 if(i>=len[j]&&w[j]==s.substr(i-len[j]+1,len[j])) 21 dp[i]=min(dp[i],dp[i-len[j]]+1); 22 } 23 cout<<dp[ls-1]<<endl; 24 } 25 return 0; 26 }
E0180 地铁站
First AC: 2018-11-16 Latest Modification: 2018-11-16
Note: 设第二站下车人数为b,通过列出前几项可以找到规律:
的其中fib[n]是斐波那契数列,且设第一项fib[0]=1下标为0
的依题意,第n(>=4)站开出时,车上人数等于第n-1站上车人数+第1站上车人数-第2站下车人数
的即fib[n-4]a+fib[n-3]b+a-b=x,这样便可解出b,从而可得第x站开出时车上的人数,注意对x<4单独讨论
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a,b,n,m,x; 4 int fib[20]={1,1}; 5 int i; 6 int main() 7 { 8 cin>>a>>n>>m>>x; 9 for(i=2;i<20;++i)fib[i]=fib[i-2]+fib[i-1]; 10 if(x>=n)cout<<0; 11 else if(n<3||x<3)cout<<a; 12 else if(n==4){ 13 if(x==4)cout<<2*a; 14 else cout<<a; 15 } 16 else{ 17 b=(m-fib[n-4]*a-a)/(fib[n-3]-1); 18 cout<<fib[x-3]*a+fib[x-2]*b+a-b; 19 } 20 return 0; 21 }
E0186 完全加括号的矩阵连乘积
First AC: 2018-08-23 Latest Modification: 2018-08-23
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 int T,n,len,lft,rgt; 5 ll x[51],y[51]; 6 ll dp[51][51]; 7 int i,j; 8 int main() 9 { 10 cin>>T; 11 while(T--){ 12 cin>>n; 13 for(i=1;i<=n;++i)cin>>x[i]>>y[i]; 14 memset(dp,0x3f,sizeof dp); 15 for(i=1;i<51;++i)dp[i][i]=0; 16 for(len=2;len<=n;++len){ 17 for(rgt=len;rgt<=n;++rgt){ 18 lft=rgt-len+1; 19 for(i=lft+1;i<=rgt;++i) 20 dp[lft][rgt]=min(dp[lft][rgt],dp[lft][i-1]+dp[i][rgt]+x[lft]*x[i]*y[rgt]); 21 } 22 } 23 cout<<dp[1][n]<<endl; 24 } 25 return 0; 26 }
E0215 和你在一起
First AC: 2018-09-13 Latest Modification: 2018-09-13
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 string s[20]; 5 int i; 6 int main() 7 { 8 cin>>n; 9 for(i=0;i<n;++i)cin>>s[i]; 10 sort(s,s+n); 11 for(i=n-1;i>=0;--i)cout<<s[i]; 12 return 0; 13 }
E1000 A+B Problem
First AC: 2017-10-09 Latest Modification: 2018-02-28
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a,b; 4 int main() 5 { 6 while(cin>>a>>b)cout<<a+b<<endl; 7 return 0; 8 }
E1001 A+B Problem (Big Integer)
First AC: 2017-09-20 Latest Modification: 2017-10-27
1 #include<iostream> 2 using namespace std; 3 int c[501],d[501],e[501]; 4 string a,b; 5 int lena,lenb,num; 6 int i,j; 7 int main() 8 { 9 while(cin>>a>>b){ 10 for(i=0;i<501;++i)c[i]=d[i]=e[i]=0; 11 num=0,lena=a.length(),lenb=b.length(); 12 for(i=lena-1,j=499;i>=0;--i,--j)c[j]=(int)(a[i]-'0'); 13 for(i=lenb-1,j=499;i>=0;--i,--j)d[j]=(int)(b[i]-'0'); 14 for(i=499;i>=0;--i){ 15 if(c[i]+d[i]+num>9){ 16 e[i+1]=c[i]+d[i]+num-10,num=1; 17 if(i==0)e[0]=1; 18 } 19 else e[i+1]=c[i]+d[i]+num,num=0; 20 } 21 for(i=0;i<501;++i)if(e[i]!=0)break; 22 for(j=i;j<501;++j)cout<<e[j]; 23 cout<<endl; 24 } 25 return 0; 26 }
E1002 IP Address
First AC: 2017-10-21 Latest Modification: 2017-10-27
1 #include<stdio.h> 2 int main() 3 { 4 int n,i,j,s,count; 5 char a,b,c,d,e,f,g,h; 6 scanf("%d",&n); 7 getchar(); 8 for(i=0;i<n;++i){ 9 count=0; 10 for(j=0;j<4;++j){ 11 scanf("%c%c%c%c%c%c%c%c",&a,&b,&c,&d,&e,&f,&g,&h); 12 s=128*(a-'0')+64*(b-'0')+32*(c-'0')+16*(d-'0')+8*(e-'0')+4*(f-'0')+2*(g-'0')+(h-'0'); 13 if(count==0)printf("%d",s); 14 else printf(".%d",s); 15 count++; 16 } 17 printf("\n"); 18 getchar(); 19 } 20 return 0; 21 }
E1003 Boolean Expressions
First AC: 2018-12-06 Latest Modification: 2018-12-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s; 4 int len,pos,cnt; 5 bool cal() 6 { 7 bool a=1,b=1,op=1,f=0; 8 while(pos<len&&s[pos]!=')'){ 9 if(s[pos]=='(')++pos,(b=f? !cal():cal()); 10 else if(s[pos]=='V')b=!f,f=0; 11 else if(s[pos]=='F')b=f,f=0; 12 else if(s[pos]=='&')op=1,f=0; 13 else if(s[pos]=='|')op=0,f=0; 14 else if(s[pos]=='!')f=!f; 15 if(s[pos]=='V'||s[pos]=='F'||s[pos]==')')a=op? a&b:a|b; 16 ++pos; 17 } 18 return a; 19 } 20 int main() 21 { 22 while(getline(cin,s)){ 23 len=s.length(); 24 pos=0; 25 cout<<"Expression "<<++cnt<<": "; 26 if(cal())cout<<"V\n"; 27 else cout<<"F\n"; 28 } 29 return 0; 30 }
E1005 分时操作系统
First AC: 2018-02-28 Latest Modification: 2018-02-28
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T; 4 string s; 5 struct data{ 6 long long PID; 7 long TIME; 8 }a[1001]; 9 long n; 10 int i,j,k; 11 bool cmp(data a,data b) 12 { 13 return a.PID<b.PID; 14 } 15 int main() 16 { 17 cin>>T; 18 for(i=1;i<=T;++i){ 19 j=0; 20 while(1){ 21 cin>>s; 22 if(s[1]=='e')cin>>a[j].PID>>a[j].TIME,++j; 23 else break; 24 } 25 sort(a,a+j,cmp); 26 cin>>s>>n; 27 if(i!=1)cout<<endl; 28 cout<<"test case "<<i<<":\n"; 29 for(long t=1;;++t){ 30 if(n==0)break; 31 for(k=0;k<j;++k){ 32 if(n==0)break; 33 if(t%a[k].TIME==0)cout<<a[k].PID<<endl,--n; 34 } 35 } 36 } 37 return 0; 38 }
E1006 Prime
First AC: 2017-10-13 Latest Modification: 2018-02-28
1 #include<bits/stdc++.h> 2 using namespace std; 3 long a[1000001],n,m,i,j,s; 4 int main() 5 { 6 for(i=0;i<1000001;i++) 7 for(j=2;j<=sqrt(i);j++){ 8 if(i%j==0){ 9 a[i]=1; 10 break; 11 } 12 } 13 while(cin>>n>>m){ 14 for(i=n;i<=m;i++)if(a[i]==0)s++; 15 if(n==1)cout<<s-1<<endl; 16 else cout<<s<<endl; 17 s=0; 18 } 19 return 0; 20 }
E1007 N! 的最高位
First AC: 2018-01-16 Latest Modification: 2018-02-28
Note: 每次仅保留最高位
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long n,i; 4 double s; 5 int main() 6 { 7 while(cin>>n){ 8 for(s=1,i=2;i<=n;++i){ 9 s*=i; 10 while(s>10)s/=10; 11 } 12 cout<<(int)s<<endl; 13 } 14 return 0; 15 }
E1008 Zero
First AC: 2017-11-13 Latest Modification: 2017-11-13
1 #include<iostream> 2 using namespace std; 3 long n; 4 int main() 5 { 6 cin>>n; 7 while(cin>>n)cout<<n/5+n/25+n/125+n/625+n/3125+n/15625+n/78125+n/390625+n/1953125+n/9765625+n/48828125+n/244140625+n/1220703125<<endl; 8 return 0; 9 }
E1009 整数的拆分
First AC: 2017-12-08 Latest Modification: 2017-12-08
Note: 设f(m,n)为sigma(i=1,m)xi,1<=x1<=x2<=…<=xm的解的组数
的则f(m,n)={0(m>n),1(m=n),f(m-1,n-1)+f(m,n-m)(m<n)}
1 #include<iostream> 2 using namespace std; 3 int a[101][101]; 4 int N,m,n,rst,i; 5 int main() 6 { 7 for(n=1;n<101;++n){ 8 a[n][n]=1; 9 for(m=1;m<n;++m)a[m][n]=a[m-1][n-1]+a[m][n-m]; 10 } 11 while(cin>>N){ 12 rst=0; 13 for(i=1;i<=N;++i)rst+=a[i][N]; 14 cout<<rst<<endl; 15 } 16 return 0; 17 }
E1010 智能T9英文输入法
First AC: 2018-03-01 Latest Modification: 2018-03-01
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,len; 4 string s; 5 struct data{ 6 string s,t; 7 int len; 8 }a[101]; 9 queue<string>st,nd,rd,th; 10 int i,j; 11 char find(char c) 12 { 13 if(c<'D')return '2'; 14 if(c<'G')return '3'; 15 if(c<'J')return '4'; 16 if(c<'M')return '5'; 17 if(c<'P')return '6'; 18 if(c<'T')return '7'; 19 if(c<'W')return '8'; 20 return '9'; 21 } 22 bool cmp(data a,data b) 23 { 24 return a.s<b.s; 25 } 26 int cal(int i) 27 { 28 if(len>a[i].len)return -1; 29 int num=0; 30 for(j=0;j<len;++j)if(s[j]!=a[i].t[j])++num; 31 return num; 32 } 33 int main() 34 { 35 while(cin>>n){ 36 for(i=0;i<n;++i){ 37 cin>>a[i].s; 38 a[i].len=a[i].s.length(); 39 a[i].t=find(a[i].s[0]); 40 for(j=1;j<a[i].len;++j){ 41 a[i].t+=find(a[i].s[j]); 42 } 43 } 44 sort(a,a+n,cmp); 45 cin>>s; 46 len=s.length(); 47 for(i=0;i<n;++i){ 48 int tmp=cal(i); 49 if(tmp==0){ 50 if(len==a[i].len)st.push(a[i].s); 51 else nd.push(a[i].s); 52 } 53 else if(tmp==1){ 54 if(len==a[i].len)rd.push(a[i].s); 55 else th.push(a[i].s); 56 } 57 } 58 while(!st.empty())cout<<st.front()<<endl,st.pop(); 59 while(!nd.empty())cout<<nd.front()<<endl,nd.pop(); 60 while(!rd.empty())cout<<rd.front()<<endl,rd.pop(); 61 while(!th.empty())cout<<th.front()<<endl,th.pop(); 62 } 63 return 0; 64 }
E1011 Pseudo-Random Numbers
First AC: 2018-06-30 Latest Modification: 2018-06-30
1 #include<bits/stdc++.h> 2 using namespace std; 3 int Z,I,M,L,cas,rst,tmp; 4 int jdg[10000]; 5 int main() 6 { 7 while(cin>>Z>>I>>M>>L,Z|I|M|L){ 8 memset(jdg,0,sizeof(jdg)); 9 jdg[L]=0; 10 tmp=L; 11 for(rst=0;;++rst){ 12 tmp=(Z*tmp+I)%M; 13 if(!jdg[tmp])jdg[tmp]=rst; 14 else{ 15 cout<<"Case "<<++cas<<": "<<rst-jdg[tmp]<<endl; 16 break; 17 } 18 } 19 } 20 return 0; 21 }
E1012 The 3n+1 Problem
First AC: 2018-08-01 Latest Modification: 2018-08-01
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll a,b,l,r,rst; 5 ll ans[10000001]; 6 ll i; 7 ll cal(ll n) 8 { 9 if(n<10000001&&ans[n])return ans[n]; 10 ll ret=0; 11 if(n&1)ret=cal(3*n+1)+1; 12 else ret=cal(n/2)+1; 13 if(n<10000001)ans[n]=ret; 14 return ret; 15 } 16 int main() 17 { 18 ans[1]=1; 19 while(cin>>a>>b){ 20 l=min(a,b); 21 r=max(a,b); 22 rst=0; 23 for(i=l;i<=r;++i)rst=max(rst,cal(i)); 24 cout<<a<<' '<<b<<' '<<rst<<endl; 25 } 26 return 0; 27 }
E1013 Ancient Cipher
First AC: 2018-03-01 Latest Modification: 2018-03-01
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len; 4 string s,t; 5 struct data{ 6 char c; 7 int n; 8 }a[26],b[26]; 9 bool rst; 10 int i,j; 11 bool cmp(data a,data b) 12 { 13 if(a.n!=b.n)return a.n<b.n; 14 return a.c<b.c; 15 } 16 int main() 17 { 18 cin>>T; 19 while(T--){ 20 cin>>s>>t; 21 len=s.length(); 22 for(i=0;i<26;++i)a[i].c=b[i].c='A'+i,a[i].n=b[i].n=0; 23 for(i=0;i<len;++i)++a[s[i]-'A'].n,++b[t[i]-'A'].n; 24 sort(a,a+26,cmp),sort(b,b+26,cmp); 25 for(rst=1,i=0;i<26;++i){ 26 if(a[i].n!=b[i].n){ 27 rst=0; 28 break; 29 } 30 } 31 for(i=0;;++i)if(a[i].n)break; 32 if(a[i].n==1&&(i==len-1||a[i+1].n>1)&&a[i].c==b[i].c)rst=0; 33 rst? cout<<"YES\n":cout<<"NO\n"; 34 } 35 return 0; 36 }
E1014 Box
First AC: 2018-03-01 Latest Modification: 2018-03-01
1 #include<bits/stdc++.h> 2 using namespace std; 3 int x,y; 4 struct data{ 5 int x,y; 6 }a[6]; 7 int i; 8 bool cmp(data a,data b) 9 { 10 if(a.x!=b.x)return a.x<b.x; 11 return a.y<b.y; 12 } 13 int main() 14 { 15 for(i=0;i<6;++i){ 16 cin>>x>>y; 17 if(x>y)x^=y,y^=x,x^=y; 18 a[i].x=x,a[i].y=y; 19 } 20 sort(a,a+6,cmp); 21 for(i=0;i<3;++i) 22 if(a[2*i].x!=a[2*i+1].x||a[2*i].y!=a[2*i+1].y){ 23 cout<<"IMPOSSIBLE"; 24 return 0; 25 } 26 if(a[0].x==a[2].x&&a[4].x==a[0].y&&a[2].y==a[4].y) 27 cout<<"POSSIBLE"; 28 else cout<<"IMPOSSIBLE"; 29 return 0; 30 }
E1015 核电站
First AC: 2017-10-18 Latest Modification: 2018-03-01
Note: 考虑强行分类递推(对上一个坑有核与否分类)
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long a[6][51],n,m,i,c; 4 int main() 5 { 6 a[2][2]=3,a[2][3]=5; 7 a[3][2]=4,a[3][3]=7,a[3][4]=13; 8 a[4][2]=4,a[4][3]=8,a[4][4]=15,a[4][5]=29; 9 a[5][2]=4,a[5][3]=8,a[5][4]=16,a[5][5]=31,a[5][6]=61; 10 for(i=4;i<51;i++) 11 a[2][i]=a[2][i-1]+a[2][i-2]; 12 for(i=5;i<51;i++) 13 a[3][i]=a[3][i-1]+a[3][i-2]+a[3][i-3]; 14 for(i=6;i<51;i++) 15 a[4][i]=a[4][i-1]+a[4][i-2]+a[4][i-3]+a[4][i-4]; 16 for(i=7;i<51;i++) 17 a[5][i]=a[5][i-1]+a[5][i-2]+a[5][i-3]+a[5][i-4]+a[5][i-5]; 18 while(cin>>n>>m){ 19 c++; 20 if(c==1)cout<<a[m][n]; 21 else cout<<"\n"<<a[m][n]; 22 } 23 return 0; 24 }
E1018 单词的划分
First AC: 2018-05-09 Latest Modification: 2018-05-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,ls; 4 string s; 5 string w[100]; 6 int len[100]; 7 int dp[260]; 8 int i,j; 9 int main() 10 { 11 cin>>T; 12 while(T--){ 13 cin>>s>>n; 14 s=' '+s; 15 ls=s.length(); 16 for(i=0;i<n;++i)cin>>w[i],len[i]=w[i].length(); 17 for(i=1;i<ls;++i){ 18 dp[i]=500; 19 for(j=0;j<n;++j) 20 if(i>=len[j]&&w[j]==s.substr(i-len[j]+1,len[j])) 21 dp[i]=min(dp[i],dp[i-len[j]]+1); 22 } 23 cout<<dp[ls-1]<<endl; 24 } 25 return 0; 26 }
E1023 Three Powers
First AC: 2019-01-17 Latest Modification: 2019-01-17
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll n; 5 char powi[65][52]; 6 int i,j; 7 int main() 8 { 9 memset(powi,'0',sizeof powi); 10 powi[0][51]='1'; 11 for(i=1;i<65;++i){ 12 int plus=0; 13 for(j=51;j;--j){ 14 powi[i][j]=(powi[i-1][j]-'0')*3+plus; 15 plus=powi[i][j]/10; 16 powi[i][j]%=10; 17 powi[i][j]+='0'; 18 } 19 } 20 while(cin>>n,n){ 21 bool jdg=0; 22 if(--n==0){ 23 cout<<"{}\n"; 24 continue; 25 } 26 cout<<"{"; 27 for(i=0;i<65;++i){ 28 if(n%2){ 29 if(jdg)cout<<","; 30 jdg=1; 31 for(j=0;;++j)if(powi[i][j]!='0')break; 32 for(j;j<52;++j)cout<<powi[i][j]; 33 } 34 n/=2; 35 } 36 cout<<"}\n"; 37 } 38 return 0; 39 }
E1026 重力加速度
First AC: 2017-09-19 Latest Modification: 2018-03-01
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 int main() 5 { 6 while(cin>>n,n){ 7 if(n==1)cout<<"0.598\n"; 8 else if(n==2)cout<<"1.174\n"; 9 else printf("%.3f\n",sqrt((6*n+5.5)/9.8)); 10 } 11 return 0; 12 }
E1027 邮资的问题
First AC: 2018-08-04 Latest Modification: 2018-08-04
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,rst; 4 int pri[10],num[10]; 5 bool jdg[4001],tmp[4001]; 6 int i,j,k; 7 int main() 8 { 9 cin>>T; 10 while(T--){ 11 cin>>n; 12 for(i=0;i<n;++i)cin>>pri[i]; 13 for(i=0;i<n;++i)cin>>num[i]; 14 memset(jdg,0,sizeof(jdg)); 15 memset(tmp,0,sizeof(tmp)); 16 jdg[0]=1; 17 rst=0; 18 for(i=0;i<n;++i){ 19 for(j=0;j<4001;++j){ 20 if(jdg[j]){ 21 for(k=1;k<=num[i];++k){ 22 if(j+k*pri[i]>4000)break; 23 tmp[j+k*pri[i]]=1; 24 } 25 } 26 } 27 for(j=0;j<4001;++j)jdg[j]|=tmp[j]; 28 } 29 for(i=0;i<4001;++i)if(jdg[i])++rst; 30 cout<<rst<<endl; 31 } 32 return 0; 33 }
E1028 路由器
First AC: 2019-02-04 Latest Modification: 2019-02-04
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,q; 4 string s,t; 5 int x,y,cnt,val; 6 int dis[105][105]; 7 map<string,int>hashto; 8 map<string,int>::iterator itx; 9 int i,j,k; 10 int main() 11 { 12 cin>>n>>m; 13 memset(dis,0x3f,sizeof dis); 14 for(i=0;i<105;++i)dis[i][i]=0; 15 while(m--){ 16 cin>>s>>t>>val; 17 itx=hashto.find(s); 18 if(itx!=hashto.end())x=itx->second; 19 else hashto.insert(pair<string,int>(s,x=cnt++)); 20 itx=hashto.find(t); 21 if(itx!=hashto.end())y=itx->second; 22 else hashto.insert(pair<string,int>(t,y=cnt++)); 23 dis[x][y]=dis[y][x]=min(val,dis[x][y]); 24 } 25 for(k=0;k<cnt;++k){ 26 for(i=0;i<cnt;++i){ 27 for(j=0;j<cnt;++j){ 28 dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]); 29 } 30 } 31 } 32 cin>>q; 33 while(q--){ 34 cin>>s>>t; 35 itx=hashto.find(s); 36 if(itx==hashto.end()){ 37 cout<<"-1\n"; 38 continue; 39 } 40 x=itx->second; 41 itx=hashto.find(t); 42 if(itx==hashto.end()){ 43 cout<<"-1\n"; 44 continue; 45 } 46 y=itx->second; 47 if(dis[x][y]>1e9)cout<<"-1\n"; 48 else cout<<dis[x][y]<<endl; 49 } 50 return 0; 51 }
E1030 母牛生小牛
First AC: 2017-10-13 Latest Modification: 2018-03-01
1 #include<bits/stdc++.h> 2 using namespace std; 3 int i,most2[51]={0,1,1,0,1},least3[51]={0,0,0,1,1}; 4 int main() 5 { 6 for(i=4;i<50;i++){ 7 most2[i+1]=least3[i]+least3[i-1]; 8 least3[i+1]=least3[i]+least3[i-2];} 9 while(cin>>i,i!=0)cout<<most2[i]+least3[i]<<endl; 10 return 0; 11 }
E1033 War on Weather
First AC: 2018-09-24 Latest Modification: 2018-09-24
Note: 既然所有目标都在同一球面上,问题不难转化为判断两个向量内积的符号
1 #include<bits/stdc++.h> 2 using namespace std; 3 int m,n,c,x,y,v; 4 struct data{ 5 int x,y,v; 6 }a[1000005]; 7 int i; 8 bool cmp(data a,data b) 9 { 10 if(a.x!=b.x)return a.x<b.x; 11 return a.y<b.y; 12 } 13 int main() 14 { 15 cin>>m>>n>>c; 16 for(i=0;i<c;++i){ 17 cin>>y>>x>>v; 18 a[i].x=x; 19 a[i].y=y; 20 a[i].v=v; 21 } 22 sort(a,a+c,cmp); 23 for(i=0;i<c;++i)cout<<a[i].x<<' '<<a[i].y<<' '<<a[i].v<<endl; 24 return 0; 25 }
E1038 Prerequisites?
First AC: 2018-09-22 Latest Modification: 2018-09-22
1 #include<bits/stdc++.h> 2 using namespace std; 3 int k,m,x,r,c,cnt; 4 bool rst; 5 bool jdg[10000]; 6 int i; 7 int main() 8 { 9 while(cin>>k,k){ 10 cin>>m; 11 rst=1; 12 memset(jdg,0,sizeof jdg); 13 while(k--)cin>>x,jdg[x]=1; 14 while(m--){ 15 cin>>r>>c; 16 cnt=0; 17 while(r--){ 18 cin>>x; 19 if(jdg[x])++cnt; 20 } 21 if(cnt<c)rst=0; 22 } 23 rst? cout<<"yes\n":cout<<"no\n"; 24 } 25 return 0; 26 }
E1042 付款问题
First AC: 2017-10-21 Latest Modification: 2017-10-21
1 #include<cstdio> 2 int main() 3 { 4 long s,p100,p50,p20,p10,p5,p2,p1; 5 int n,i; 6 scanf("%d",&n); 7 for(i=0;i<n;i++){ 8 scanf("%ld",&s); 9 p100=s/100; 10 s-=100*p100; 11 p50=s/50; 12 s-=50*p50; 13 p20=s/20; 14 s-=20*p20; 15 p10=s/10; 16 s-=10*p10; 17 p5=s/5; 18 s-=5*p5; 19 p2=s/2; 20 p1=s-2*p2; 21 printf("%ld %ld %ld %ld %ld %ld %ld\n",p100,p50,p20,p10,p5,p2,p1); 22 } 23 return 0; 24 }
E1045 箱子打包
First AC: 2018-01-08 Latest Modification: 2018-03-01
1 #include<bits/stdc++.h> 2 using namespace std; 3 long n,l,N,tmp,cnt; 4 int a[100001]; 5 long i,j; 6 int main() 7 { 8 cin>>n>>l,N=n,++l; 9 for(i=0;i<n;++i)scanf("%d",a+i); 10 sort(a,a+n); 11 --n; 12 while(N){ 13 tmp=l; 14 for(i=n;;--i)if(a[i]<l)break; 15 --N,--n,++cnt,tmp-=a[i],a[i]=l+1; 16 for(j=i-1;j>=0;--j)if(a[j]<tmp){--N,a[j]=l;break;} 17 } 18 cout<<cnt; 19 return 0; 20 }
E1046 4个值的和为0
First AC: 2018-06-23 Latest Modification: 2018-06-23
Note:维护a+b,c+d,暴力枚举a+b,二分查找c+d
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N=4000; 4 int n; 5 long long rst; 6 int a[N],b[N],c[N],d[N]; 7 map<int,int>mpl,mpr; 8 map<int,int>::iterator it,fd; 9 int i,j; 10 int main() 11 { 12 ios::sync_with_stdio(false); 13 cin>>n; 14 for(i=0;i<n;++i)cin>>a[i]>>b[i]>>c[i]>>d[i]; 15 for(i=0;i<n;++i)for(j=0;j<n;++j){ 16 it=mpl.find(a[i]+b[j]); 17 if(it!=mpl.end())++it->second; 18 else mpl.insert(pair<int,int>(a[i]+b[j],1)); 19 } 20 for(i=0;i<n;++i)for(j=0;j<n;++j){ 21 it=mpr.find(c[i]+d[j]); 22 if(it!=mpr.end())++it->second; 23 else mpr.insert(pair<int,int>(c[i]+d[j],1)); 24 } 25 for(it=mpl.begin();it!=mpl.end();++it){ 26 fd=mpr.find(-it->first); 27 if(fd!=mpr.end())rst+=it->second*fd->second; 28 } 29 cout<<rst; 30 return 0; 31 }
E1048 最小等待时间
First AC: 2017-11-25 Latest Modification: 2018-03-01
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,N,i; 4 long long sum; 5 int a[10001]; 6 int main() 7 { 8 cin>>T; 9 while(T--){ 10 cin>>N; 11 for(i=0;i<N;++i)cin>>a[i]; 12 sort(a,a+N); 13 for(sum=i=0,--N;i<N;++i)sum+=a[i]*(N-i); 14 cout<<sum<<endl; 15 } 16 return 0; 17 }
E1050 方阵相乘
First AC: 2017-12-06 Latest Modification: 2018-03-01
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,i,j,k; 4 int a[101][101],b[101][101],c[101][101]; 5 int main() 6 { 7 cin>>T; 8 while(T--){ 9 cin>>n; 10 for(i=0;i<n;++i)for(j=0;j<n;++j)cin>>a[i][j]; 11 for(i=0;i<n;++i)for(j=0;j<n;++j)cin>>b[i][j]; 12 memset(c,0,sizeof(c)); 13 for(i=0;i<n;++i)for(j=0;j<n;++j){ 14 for(k=0;k<n;++k)c[i][j]+=a[i][k]*b[k][j]; 15 } 16 for(i=0;i<n;++i){ 17 for(j=0;j<n-1;++j)cout<<c[i][j]<<' '; 18 cout<<c[i][n-1]<<endl; 19 } 20 } 21 return 0; 22 }
E1051 完全加括号的矩阵连乘积
First AC: 2018-08-23 Latest Modification: 2018-08-23
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 int T,n,len,lft,rgt; 5 ll x[51],y[51]; 6 ll dp[51][51]; 7 int i,j; 8 int main() 9 { 10 cin>>T; 11 while(T--){ 12 cin>>n; 13 for(i=1;i<=n;++i)cin>>x[i]>>y[i]; 14 memset(dp,0x3f,sizeof dp); 15 for(i=1;i<51;++i)dp[i][i]=0; 16 for(len=2;len<=n;++len){ 17 for(rgt=len;rgt<=n;++rgt){ 18 lft=rgt-len+1; 19 for(i=lft+1;i<=rgt;++i) 20 dp[lft][rgt]=min(dp[lft][rgt],dp[lft][i-1]+dp[i][rgt]+x[lft]*x[i]*y[rgt]); 21 } 22 } 23 cout<<dp[1][n]<<endl; 24 } 25 return 0; 26 }
E1052 0-1背包问题
First AC: 2017-12-13 Latest Modification: 2018-03-01
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n; 4 int w[21],p[21]; 5 long M,dp[21][100001]; 6 long i,j; 7 int main() 8 { 9 cin>>T; 10 while(T--){ 11 cin>>n>>M; 12 for(i=1;i<=n;++i)cin>>w[i]>>p[i]; 13 for(i=1;i<=n;++i)for(j=1;j<=M;++j){ 14 if(j<w[i])dp[i][j]=dp[i-1][j]; 15 else dp[i][j]=max(dp[i-1][j],dp[i-1][j-w[i]]+p[i]); 16 } 17 cout<<dp[n][M]<<endl; 18 } 19 return 0; 20 }
E1053 子集和问题
First AC: 2018-04-19 Latest Modification: 2018-04-19
Note: 题目数据已加强,结果可能超出unsigned long long,故考虑dp时分高低位存储
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,cnt,len; 4 string s; 5 long long x,tmp; 6 long long num[105]; 7 long long dphgh[105][10005]; 8 long long dplow[105][10005]; 9 int i,j; 10 int main() 11 { 12 cin>>T; 13 while(T--){ 14 cin>>n>>x; 15 cnt=0; 16 while(n--){ 17 cin>>s; 18 len=s.length(); 19 if(len>10)continue; 20 for(i=tmp=0;i<len;++i) 21 tmp=10*tmp+s[i]-'0'; 22 if(tmp>x)continue; 23 num[cnt++]=tmp; 24 } 25 for(i=1;i<=x;++i)dphgh[0][i]=dplow[0][i]=0; 26 dphgh[0][0]=0,dplow[0][0]=1; 27 dphgh[0][num[0]]=0,dplow[0][num[0]]=1; 28 for(i=1;i<cnt;++i)for(j=0;j<=x;++j){ 29 dphgh[i][j]=dphgh[i-1][j]; 30 dplow[i][j]=dplow[i-1][j]; 31 if(j>=num[i]){ 32 dphgh[i][j]+=dphgh[i-1][j-num[i]]; 33 dplow[i][j]+=dplow[i-1][j-num[i]]; 34 } 35 if(dplow[i][j]>1e15) 36 dplow[i][j]-=1e15,++dphgh[i][j]; 37 } 38 if(dphgh[cnt-1][x]) 39 printf("%lld%015lld\n",dphgh[cnt-1][x],dplow[cnt-1][x]); 40 else printf("%lld\n",dplow[cnt-1][x]); 41 } 42 return 0; 43 }
E1054 (算法作业5-2)机器设计
First AC: 2018-12-05 Latest Modification: 2018-12-05
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,c,rst; 4 int dp[55][20005]; 5 int w1,c1,w2,c2,w3,c3; 6 int i,j; 7 int main() 8 { 9 while(cin>>n>>c){ 10 memset(dp,0x3f,sizeof dp); 11 dp[0][0]=0; 12 for(i=1;i<=n;++i){ 13 cin>>w1>>c1>>w2>>c2>>w3>>c3; 14 for(j=c;j>=0;--j){ 15 dp[i][j+c1]=min(dp[i][j+c1],dp[i-1][j]+w1); 16 dp[i][j+c2]=min(dp[i][j+c2],dp[i-1][j]+w2); 17 dp[i][j+c3]=min(dp[i][j+c3],dp[i-1][j]+w3); 18 } 19 } 20 rst=dp[n][0]; 21 for(i=1;i<c;++i)rst=min(rst,dp[n][i]); 22 cout<<rst<<endl; 23 } 24 return 0; 25 }
E1055 欧几里得
First AC: 2017-10-14 Latest Modification: 2018-03-01
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a,b; 4 int main(){ 5 while(cin>>a>>b){ 6 int r=1; 7 while(r)r=a%b,a=b,b=r; 8 cout<<a<<endl; 9 } 10 return 0; 11 }
E1056 线性同余方程
First AC: 2018-02-20 Latest Modification: 2018-02-20
1 #include<bits/stdc++.h> 2 using namespace std; 3 long a,b,m,jdg; 4 long i,j; 5 int main() 6 { 7 while(cin>>a>>b>>m){ 8 jdg=1; 9 for(i=0;i<m;++i) 10 if((a*i-b)%m==0){cout<<i;jdg=0;break;} 11 for(j=i+1;j<m;++j) 12 if((a*j-b)%m==0)cout<<' '<<j; 13 if(jdg)cout<<"No Answer."; 14 cout<<endl; 15 } 16 return 0; 17 }
E1058 挤模具
First AC: 2018-01-06 Latest Modification: 2018-03-01
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,i; 4 double x[101],y[101],sum,mx,my,v; 5 int main() 6 { 7 while(cin>>n,n){ 8 for(i=sum=mx=my=0;i<n;++i){ 9 cin>>x[i]>>y[i]; 10 if(x[i]<mx)mx=x[i]; 11 if(y[i]<my)my=y[i]; 12 } 13 for(i=0;i<n;++i)x[i]+=mx+1,y[i]+=my+1; 14 for(i=1;i<n;++i)sum+=x[i-1]*y[i]-x[i]*y[i-1]; 15 sum+=x[n-1]*y[0]-x[0]*y[n-1]; 16 cin>>v; 17 printf("BAR LENGTH: %.2f\n",-v*2/sum); 18 } 19 return 0; 20 }
E1059 找零
First AC: 2018-04-20 Latest Modification: 2018-04-20
1 #include<bits/stdc++.h> 2 using namespace std; 3 int q,d,n,p,c; 4 int nq,nd,nn,np,sumn; 5 bool jdg; 6 void find(int numq,int d,int n,int p,int c) 7 { 8 int numd,numn,nump; 9 numd=min(c/10,d); 10 c-=numd*10; 11 numn=min(c/5,n); 12 c-=numn*5; 13 nump=min(c,p); 14 c-=nump; 15 if(!c){ 16 if(numq+numd+numn+nump<sumn){ 17 sumn=numq+numd+numn+nump; 18 nq=numq,nd=numd,nn=numn,np=nump; 19 } 20 jdg=0; 21 } 22 } 23 int main() 24 { 25 while(cin>>q>>d>>n>>p>>c,q||d||n||p||c){ 26 sumn=q+d+n+p+1; 27 jdg=1; 28 find(0,d,n,p,c); 29 if(c>24&&q>0)find(1,d,n,p,c-25); 30 if(c>49&&q>1)find(2,d,n,p,c-50); 31 if(c>74&&q>2)find(3,d,n,p,c-75); 32 if(jdg)cout<<"Cannot dispense the desired amount.\n"; 33 else{ 34 cout<<"Dispense "<<nq<<" quarters, "<<nd<<" dimes, "; 35 cout<<nn<<" nickels, and "<<np<<" pennies.\n"; 36 } 37 } 38 return 0; 39 }
E1060 获取你的维生素
First AC: 2018-02-09 Latest Modification: 2018-02-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 double a,r,p; 4 string u,v; 5 string s[1005]; 6 int i,j; 7 int main() 8 { 9 while(1){ 10 cin>>a>>u>>r; 11 getchar(); 12 getline(cin,v); 13 if(a<0)break; 14 p=a/r; 15 if(p<0.01)s[i++]=v; 16 else{ 17 cout<<v; 18 printf(" %.1lf ",a); 19 cout<<u<<' '<<(int)(100*p+0.5)<<"%\n"; 20 } 21 } 22 cout<<"Provides no significant amount of:"; 23 for(j=0;j<i;++j)cout<<endl<<s[j]; 24 return 0; 25 }
E1062 线段相交
First AC: 2018-05-19 Latest Modification: 2018-05-19
1 #include<bits/stdc++.h> 2 using namespace std; 3 int xa,ya,xb,yb,xc,yc,xd,yd; 4 double k1,b1,k2,b2,x,y; 5 bool vert1,vert2; 6 int main() 7 { 8 while(cin>>xa>>ya>>xb>>yb>>xc>>yc>>xd>>yd){ 9 if(xa==xb)vert1=1; 10 else{ 11 vert1=0; 12 k1=(ya-yb)*1.0/(xa-xb); 13 b1=ya-k1*xa; 14 } 15 if(xc==xd)vert2=1; 16 else{ 17 vert2=0; 18 k2=(yc-yd)*1.0/(xc-xd); 19 b2=yc-k2*xc; 20 } 21 bool jdg=0; 22 if(vert1&&vert2){ 23 if((ya-yc)*(ya-yd)<=0)jdg=1; 24 if((yb-yc)*(yb-yd)<=0)jdg=1; 25 if(xa!=xc)jdg=0; 26 } 27 else if(vert1){ 28 if((k2*xa+b2-ya)*(k2*xa+b2-yb)<=0)jdg=1; 29 if((xa-xc)*(xa-xd)>0)jdg=0; 30 } 31 else if(vert2){ 32 if((k1*xc+b1-yc)*(1*xc+b1-yd)<=0)jdg=1; 33 if((xc-xa)*(xc-xb)>0)jdg=0; 34 } 35 else{ 36 if(abs(k1-k2)<1e-9){ 37 if(abs(b1-b2)<1e-9){ 38 if((ya-yc)*(ya-yd)<=0)jdg=1; 39 if((yb-yc)*(yb-yd)<=0)jdg=1; 40 } 41 } 42 else{ 43 x=(b1-b2)/(k2-k1),y=k1*x+b1; 44 if((x-xa)*(x-xb)<=0||(y-ya)*(y-yb)<=0) 45 if((x-xc)*(x-xd)<=0||(y-yc)*(y-yd)<=0) 46 jdg=1; 47 } 48 } 49 jdg? cout<<"Yes\n":cout<<"No\n"; 50 } 51 return 0; 52 }
E1064 冒泡排序
First AC: 2017-11-18 Latest Modification: 2017-11-18
1 #include<iostream> 2 using namespace std; 3 int a[5000]; 4 int n,tmp; 5 int i,j; 6 int main() 7 { 8 cin>>n; 9 for(;i<n;++i)cin>>a[i]; 10 for(i=0;i<n;++i) 11 for(j=i+1;j<n;++j) 12 if(a[j]<a[i]){ 13 cout<<a[i]<<' '<<a[j]<<endl; 14 tmp=a[i],a[i]=a[j],a[j]=tmp; 15 } 16 cout<<a[0]; 17 for(i=1;i<n;++i)cout<<' '<<a[i]; 18 return 0; 19 }
E1065 糖果盒
First AC: 2019-02-25 Latest Modification: 2019-02-25
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll n,m,ans; 5 ll a[1005][1005]; 6 ll tmp[1005]; 7 ll dp[1005]; 8 ll i,j,k; 9 int main() 10 { 11 cin>>n>>m; 12 for(i=1;i<=n;++i){ 13 for(j=1;j<=m;++j){ 14 cin>>a[i][j]; 15 if(!a[i][j])a[i][j]=-1e9; 16 } 17 } 18 for(i=0;i<n;++i){ 19 memset(tmp,0,sizeof tmp); 20 for(j=i+1;j<=n;++j){ 21 ll mina=0; 22 memset(dp,0,sizeof dp); 23 for(k=1;k<=m;++k){ 24 tmp[k]+=a[j][k]; 25 dp[k]=dp[k-1]+tmp[k]; 26 ans=max(ans,dp[k]-mina); 27 mina=min(mina,dp[k]); 28 } 29 } 30 } 31 cout<<ans; 32 return 0; 33 }
E1067 石子游戏A
First AC: 2018-02-12 Latest Modification: 2018-02-12
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,ans,tmp; 4 int main() 5 { 6 cin>>T; 7 while(T--){ 8 cin>>n; 9 cin>>ans; 10 while(--n)cin>>tmp,ans^=tmp; 11 if(ans)cout<<"Win\n"; 12 else cout<<"Lost\n"; 13 } 14 }
E1068 石子游戏B
First AC: 2018-02-12 Latest Modification: 2018-02-12
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,m,ans,tmp; 4 int main() 5 { 6 cin>>T; 7 while(T--){ 8 cin>>n>>m>>ans; 9 ++m,ans%=m; 10 while(--n)cin>>tmp,ans^=(tmp%m); 11 if(ans)cout<<"Win\n"; 12 else cout<<"Lost\n"; 13 } 14 }
E1070 A×B
First AC: 2017-10-21 Latest Modification: 2017-10-21
1 n=input() 2 while True: 3 try: 4 a,b=input().split() 5 print((int)(a)*(int)(b)) 6 except: 7 break
E1071 Guessing Game
First AC: 2017-12-13 Latest Modification: 2011-12-13
1 #include<iostream> 2 using namespace std; 3 int n,M,m; 4 string s; 5 int main() 6 { 7 while(1){ 8 M=11,m=0; 9 while(cin>>n){ 10 if(!n)return 0; 11 cin>>s>>s; 12 if(s[0]=='h'){if(M>n)M=n;} 13 else if(s[0]=='l'){if(m<n)m=n;} 14 else break; 15 } 16 if(m<n&&n<M)cout<<"Sunny may be honest\n"; 17 else cout<<"Sunny is dishonest\n"; 18 } 19 }
E1072 小强的单词表
First AC: 2017-12-16 Latest Modification: 2018-03-01
1 #include<bits/stdc++.h> 2 using namespace std; 3 struct data{ 4 string s; 5 int len; 6 }a[1001]; 7 int n,i; 8 bool cmp(data a,data b) 9 { 10 int l=a.len<b.len?a.len:b.len; 11 for(int i=0;i<l;++i)if(a.s[i]-b.s[i])return a.s[i]<b.s[i]; 12 return a.len<b.len; 13 } 14 int main() 15 { 16 cin>>n; 17 for(i=0;i<n;++i){ 18 cin>>a[i].s; 19 a[i].len=a[i].s.length(); 20 } 21 sort(a,a+n,cmp); 22 cout<<a[0].s; 23 for(i=1;i<n;++i)if(a[i].s!=a[i-1].s)cout<<endl<<a[i].s; 24 return 0; 25 }
E1073 几位数
First AC: 2017-10-09 Latest Modification: 2018-03-01
1 #include<bits/stdc++.h> 2 using namespace std; 3 char s[2000]; 4 int main() 5 { 6 while(scanf("%s",s)!=EOF)printf("%d\n",strlen(s)); 7 }
E1074 第一位数字
First AC: 2018-01-07 Latest Modification: 2018-03-01
1 #include<bits/stdc++.h> 2 using namespace std; 3 double m,n,j,k; 4 int main() 5 { 6 while(cin>>k,k) 7 { 8 m=modf(k*1.0*log10(k*1.0),&j); 9 m=modf(pow((double)10,m),&j); 10 cout<<(int)j<<endl; 11 } 12 return 0; 13 }
E1075 庆祝迎评成功
First AC: 2017-10-14 Latest Modification: 2018-03-01
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 int main() 5 { 6 while(cin>>n,n!=0)cout<<(n*n*n+5*n+6)/6<<endl; 7 return 0; 8 }
E1076 染气球
First AC: 2018-04-13 Latest Modification: 2018-04-13
Note: 前缀和的思想,把每次涂色用一次操作储存
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,a,b; 4 int dp[100001]; 5 int i; 6 int main() 7 { 8 while(cin>>n,n){ 9 memset(dp,0,sizeof(dp)); 10 for(i=0;i<n;++i)cin>>a>>b,++dp[a],--dp[b+1]; 11 for(i=1;i<n;++i)dp[i]+=dp[i-1],cout<<dp[i]<<' '; 12 cout<<dp[n-1]+dp[n]<<endl; 13 } 14 return 0; 15 }
E1077 图书馆的管理系统
First AC: 2018-03-01 Latest Modification: 2018-03-01
1 #include<bits/stdc++.h> 2 using namespace std; 3 int m,c,len,ui; 4 long n,bi; 5 char op; 6 int stu[1001]; 7 int book[100001]; 8 long i,j; 9 int main() 10 { 11 ios::sync_with_stdio(false); 12 while(cin>>m>>n,m||n){ 13 ++n; 14 cin>>c; 15 memset(stu,0,sizeof(stu)); 16 memset(book,0,sizeof(book)); 17 while(c--){ 18 cin>>op; 19 if(op=='B'){ 20 cin>>ui>>bi; 21 if(book[bi]) 22 cout<<"The book is not in the library now"; 23 else if(stu[ui]>8) 24 cout<<"You are not allowed to borrow any more"; 25 else{ 26 cout<<"Borrow success"; 27 ++stu[ui],book[bi]=ui; 28 } 29 } 30 else if(op=='R'){ 31 cin>>bi; 32 if(book[bi]==0) 33 cout<<"The book is already in the library"; 34 else{ 35 cout<<"Return success"; 36 --stu[book[bi]],book[bi]=0; 37 } 38 } 39 else{ 40 cin>>ui; 41 if(stu[ui]==0)cout<<"Empty"; 42 else{ 43 int tmp=stu[ui]-1; 44 for(i=1;;++i)if(book[i]==ui){ 45 cout<<i; 46 break; 47 } 48 for(j=i+1;tmp;++j)if(book[j]==ui){ 49 cout<<' '<<j,--tmp; 50 } 51 } 52 } 53 cout<<endl; 54 } 55 cout<<endl; 56 } 57 return 0; 58 }
E1078 小强的生日
First AC: 2017-11-01 Latest Modification: 2017-11-01
1 #include<iostream> 2 using namespace std; 3 int T,m,d,s,count; 4 int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; 5 int i,j; 6 int main() 7 { 8 cin>>T; 9 for(i=0;i<T;i++){ 10 cin>>m>>d; 11 s=d; 12 for(j=0;j<m;j++)s+=a[j]; 13 if(s<294)cout<<294-s<<endl; 14 else if(s==294)cout<<"It's today!!\n"; 15 else cout<<"What a pity, it has passed!\n"; 16 } 17 return 0; 18 }
E1079 大话西游
First AC: 2017-10-09 Latest Modification: 2018-03-01
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a[50][50]; 4 int n,i,j; 5 int main() 6 { 7 a[1][1]=1; 8 a[2][1]=1; 9 a[2][2]=1; 10 for(i=3;i<35;i++){ 11 a[i][1]=1; 12 a[i][i]=1; 13 for(j=2;j<i;j++){ 14 a[i][j]=a[i-1][j-1]+a[i-1][j]; 15 } 16 } 17 while(cin>>n,n!=0){ 18 for(i=1;i<=n;i++){ 19 cout<<a[i][1]; 20 for(j=2;j<=i;j++){ 21 cout<<" "<<a[i][j]; 22 } 23 cout<<"\n"; 24 } 25 cout<<"\n"; 26 } 27 return 0; 28 }
E1082 Easy to AC
First AC: 2018-01-07 Latest Modification: 2018-01-07
Note: 当n>2时,(n+1)!=(n+1)×n!>n×n!>sigma(i=1,n)i!
这表明,若a满足n!≤a<(n+1)!,n>2且a不能表为以n!为加数的和式
则a必然无法表为其他任何和式
1 #include<iostream> 2 using namespace std; 3 long long n; 4 long long a[10]={1,1}; 5 int i; 6 int main() 7 { 8 for(i=2;i<10;++i)a[i]=i*a[i-1]; 9 while(cin>>n){ 10 if(n<0)return 0; 11 if(n==0){cout<<"NO\n";continue;} 12 for(i=9;i>2;--i)if(n>=a[i])n-=a[i]; 13 n<5? cout<<"YES\n":cout<<"NO\n"; 14 } 15 return 0; 16 }
E1084 一条直线上?
First AC: 2019-01-22 Latest Modification: 2019-01-22
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,rst,cnt; 4 int x[700],y[700]; 5 double kl,bl; 6 int i,j,k; 7 int main() 8 { 9 while(cin>>n,n){ 10 rst=0; 11 for(i=0;i<n;++i)cin>>x[i]>>y[i]; 12 for(i=0;i<n;++i){ 13 for(j=i+1;j<n;++j){ 14 kl=(y[i]-y[j])*1.0/(x[i]-x[j]); 15 bl=y[i]-kl*x[i]; 16 cnt=0; 17 for(k=0;k<n;++k){ 18 if(k==i||k==j)continue; 19 if(fabs(kl*x[k]+bl-y[k])<1e-3)++cnt; 20 } 21 rst=max(rst,cnt); 22 } 23 } 24 cout<<rst+2<<endl; 25 } 26 return 0; 27 }
E1085 Snake
First AC: 2018-01-07 Latest Modification: 2018-01-07
Note: 注意以非正数作为结束标记
1 #include<iostream> 2 using namespace std; 3 int n,num,lft,tmp,rgt; 4 int i,j; 5 int main() 6 { 7 while(cin>>n){ 8 if(n<=0)return 0; 9 if(num)cout<<endl; 10 cout<<"Case "<<++num<<":\n"; 11 lft=1; 12 for(i=1;i<=n;++i){ 13 cout<<lft; 14 tmp=lft,rgt=n-i+1; 15 for(j=2;j<=rgt;++j)tmp+=i+j-1,cout<<' '<<tmp; 16 cout<<endl; 17 lft+=i; 18 } 19 } 20 }
E1093 Digital Product
First AC: 2018-03-02 Latest Modification: 2018-03-02
Note: 相当于对n质因数分解,如果有大于9的质因数则输出-1
注意到8=2×2×2减少两位2;9=3×3减少一位3;4=2×2减少一位2;等等
可得取模优先级:8>9>6>4>2=3=5=7
注意输入为0时输出为10而非-1
1 #include<bits/stdc++.h> 2 using namespace std; 3 long n; 4 int _2,_3,_4,_5,_6,_7,_8,_9; 5 int main() 6 { 7 while(~scanf("%ld",&n)){ 8 if(n==0){printf("10\n");continue;} 9 if(n<10){printf("%d\n",n);continue;} 10 _2=_3=_4=_5=_6=_7=_8=_9=0; 11 while(n%8==0)++_8,n/=8; 12 while(n%9==0)++_9,n/=9; 13 while(n%6==0)++_6,n/=6; 14 while(n%4==0)++_4,n/=4; 15 while(n%3==0)++_3,n/=3; 16 while(n%2==0)++_2,n/=2; 17 while(n%5==0)++_5,n/=5; 18 while(n%7==0)++_7,n/=7; 19 if(n>1){printf("-1\n");continue;} 20 while(_2--)printf("2"); 21 while(_3--)printf("3"); 22 while(_4--)printf("4"); 23 while(_5--)printf("5"); 24 while(_6--)printf("6"); 25 while(_7--)printf("7"); 26 while(_8--)printf("8"); 27 while(_9--)printf("9"); 28 printf("\n"); 29 } 30 return 0; 31 }
E1099 Intersection Lines
First AC: 2018-05-30 Latest Modification: 2018-05-30
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T; 4 int xa,ya,xb,yb,xc,yc,xd,yd; 5 double k1,b1,k2,b2,x,y; 6 bool vert1,vert2; 7 int main() 8 { 9 cin>>T; 10 cout<<"INTERSECTING LINES OUTPUT\n"; 11 while(T--){ 12 cin>>xa>>ya>>xb>>yb>>xc>>yc>>xd>>yd; 13 if(xa!=xb){ 14 vert1=0; 15 k1=(ya-yb)*1.0/(xa-xb); 16 b1=ya-k1*xa; 17 } 18 else vert1=1; 19 if(xc!=xd){ 20 vert2=0; 21 k2=(yc-yd)*1.0/(xc-xd); 22 b2=yc-k2*xc; 23 } 24 else vert2=1; 25 if(vert1&&vert2){ 26 if(xa!=xc)cout<<"NONE\n"; 27 else cout<<"LINE\n"; 28 } 29 else if(vert1){ 30 x=xa; 31 y=k2*xa+b2; 32 printf("POINT %.2f %.2f\n",x,y); 33 } 34 else if(vert2){ 35 x=xc; 36 y=k1*xc+b1; 37 printf("POINT %.2f %.2f\n",x,y); 38 } 39 else{ 40 if(abs(k1-k2)<1e-9){ 41 if(abs(k1*xc+b1-yc)<1e-9)cout<<"LINE\n"; 42 else cout<<"NONE\n"; 43 } 44 else{ 45 x=(b1-b2)*1.0/(k2-k1); 46 y=k1*x+b1; 47 printf("POINT %.2f %.2f\n",x,y); 48 } 49 } 50 } 51 cout<<"END OF OUTPUT"; 52 return 0; 53 }
E1104 Bitmap
First AC: 2018-09-14 Latest Modification: 2018-09-14
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,x,y,tmp; 4 string s; 5 bool mp[185][185]; 6 int rst[185][185]; 7 queue<int>qx,qy; 8 int i,j; 9 bool ok(int x,int y) 10 { 11 if(!x||x>n)return 0; 12 if(!y||y>m)return 0; 13 if(rst[x][y]<500)return 0; 14 return 1; 15 } 16 int main() 17 { 18 cin>>n>>m; 19 getline(cin,s); 20 memset(rst,0x3f,sizeof rst); 21 for(i=1;i<=n;++i){ 22 for(j=1;j<=m;++j){ 23 mp[i][j]=(getchar()=='1'); 24 if(mp[i][j]){ 25 rst[i][j]=0; 26 qx.push(i); 27 qy.push(j); 28 } 29 } 30 getchar(); 31 } 32 while(!qx.empty()){ 33 x=qx.front(),qx.pop(); 34 y=qy.front(),qy.pop(); 35 tmp=rst[x][y]+1; 36 if(ok(x-1,y))rst[x-1][y]=tmp,qx.push(x-1),qy.push(y); 37 if(ok(x,y-1))rst[x][y-1]=tmp,qx.push(x),qy.push(y-1); 38 if(ok(x,y+1))rst[x][y+1]=tmp,qx.push(x),qy.push(y+1); 39 if(ok(x+1,y))rst[x+1][y]=tmp,qx.push(x+1),qy.push(y); 40 } 41 for(i=1;i<=n;++i){ 42 for(j=1;j<m;++j)cout<<rst[i][j]<<' '; 43 cout<<rst[i][m]<<endl; 44 } 45 return 0; 46 }
E1105 汽车加油
First AC: 2018-01-05 Latest Modification: 2018-01-05
Note: 从终点向起点判断,在保证能抵达终点的前提下,能不加油便不加油,理由如下
上述算法确定了一种走法X(在有解的前提下)
设X在x1,x2,…,xm处加了油
对于其他任何走法Y,根据X的选取知
Y在[xm,End)必有加油,改为只在xm加油,仍为可行解;
……
Y在[xi,xj)必有加油,改为只在xi加油,仍为可行解;
……
Y在[Start,x1)必有加油,改为只在起点加油,仍为可行解
加油站是有限的,这表明,Y总能经过有限次操作,得到加油次数不增多的走法X
即X是问题的一个最优解
1 #include<iostream> 2 using namespace std; 3 int n,k,tmp,rst; 4 int a[1025]; 5 int i,j; 6 int main() 7 { 8 cin>>n>>k; 9 ++k; 10 for(i=0;i<k;++i)cin>>a[i]; 11 for(tmp=n,rst=-1,i=k-1;i>=0;--i){ 12 if(a[i]<=tmp)tmp-=a[i]; 13 else if(a[i]>n){ 14 cout<<"No Solution!"; 15 return 0; 16 } 17 else ++rst,tmp=n-a[i]; 18 } 19 if(tmp)++rst; 20 cout<<rst; 21 return 0; 22 }
E1106 Number Steps
First AC: 2018-01-07 Latest Modification: 2018-01-07
Note: 分成四个等差数列即可
1 #include<iostream> 2 using namespace std; 3 int T,x,y; 4 int main() 5 { 6 cin>>T; 7 while(T--){ 8 cin>>x>>y; 9 if(x==y){ 10 if(x&1)cout<<2*x-1<<endl; 11 else cout<<2*x<<endl; 12 } 13 else if(y+2==x){ 14 if(x&1)cout<<2*x-3<<endl; 15 else cout<<2*x-2<<endl; 16 } 17 else cout<<"No Number\n"; 18 } 19 return 0; 20 }
E1109 Max Sum
First AC: 2017-11-07 Latest Modification: 2018-03-02
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,a[105],temp,Max; 4 int i,j,k; 5 int main() 6 { 7 ios::sync_with_stdio(false); 8 cin>>T; 9 for(i=0;i<T;++i){ 10 cin>>n; 11 Max=-1000; 12 for(j=0;j<n;++j)cin>>a[j]; 13 for(j=0;j<n;++j){ 14 temp=a[j]; 15 for(k=j+1;k<n;++k){ 16 if(temp>Max)Max=temp; 17 temp+=a[k]; 18 } 19 if(temp>Max)Max=temp; 20 } 21 cout<<Max<<endl; 22 } 23 return 0; 24 }
E1110 最长路
First AC: 2019-02-04 Latest Modification: 2019-02-04
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,p; 4 int x,y,val; 5 int dis[204][204]; 6 int i,j,k; 7 int main() 8 { 9 while(cin>>n>>m){ 10 memset(dis,0x3f,sizeof dis); 11 for(i=0;i<204;++i)dis[i][i]=0; 12 while(m--){ 13 cin>>x>>y>>val; 14 dis[x][y]=-val; 15 } 16 for(k=1;k<=n;++k){ 17 for(i=1;i<=n;++i){ 18 for(j=1;j<=n;++j){ 19 dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]); 20 } 21 } 22 } 23 cin>>p; 24 while(p--){ 25 cin>>x>>y; 26 if(dis[x][y]>1e8)cout<<"-1\n"; 27 else cout<<-dis[x][y]<<endl; 28 } 29 } 30 return 0; 31 }
E1111 数塔
First AC: 2018-01-01 Latest Modification: 2018-01-01
Note: 从倒数第二层往上,到达每层后后续的最小值可以逐步确定
1 #include<iostream> 2 using namespace std; 3 int T,n; 4 int a[505][505]; 5 int i,j; 6 int main() 7 { 8 cin>>T; 9 while(T--){ 10 cin>>n; 11 for(i=1;i<=n;++i) 12 for(j=1;j<=i;++j) 13 cin>>a[i][j]; 14 for(i=n-1;i;--i) 15 for(j=1;j<=n;++j) 16 a[i][j]+=min(a[i+1][j],a[i+1][j+1]); 17 cout<<a[1][1]<<endl; 18 } 19 return 0; 20 }
E1113 装箱问题
First AC: 2017-12-10 Latest Modification: 2018-03-02
1 #include<bits/stdc++.h> 2 #define max(A,B) A>B? (A):(B) 3 using namespace std; 4 int f[31][20001],a[31]; 5 int v,n; 6 int i,j; 7 int main() 8 { 9 while(cin>>v>>n){ 10 for(i=1;i<=n;++i)cin>>a[i]; 11 for(i=1;i<=n;++i)for(j=1;j<=v;++j){ 12 if(j<a[i])f[i][j]=f[i-1][j]; 13 else f[i][j]=max(f[i-1][j],f[i-1][j-a[i]]+a[i]); 14 } 15 cout<<v-f[n][v]<<endl; 16 } 17 return 0; 18 }
E1114 素数环
First AC: 2018-08-08 Latest Modification: 2018-08-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 bool pri[40]; 5 bool use[20]; 6 int tmp[20]; 7 int i,j; 8 void print() 9 { 10 for(i=1;i<n;++i)cout<<tmp[i]<<' '; 11 cout<<tmp[n]<<endl; 12 } 13 void dfs(int now) 14 { 15 if(now>n){ 16 if(!pri[tmp[1]+tmp[n]])print(); 17 return; 18 } 19 for(int i=1;i<=n;++i){ 20 if(!use[i]&&!pri[tmp[now-1]+i]){ 21 use[i]=1; 22 tmp[now]=i; 23 dfs(now+1); 24 use[i]=0; 25 } 26 } 27 } 28 int main() 29 { 30 cin>>n; 31 for(i=2;i<40;++i) 32 if(!pri[i]) 33 for(j=i+i;j<40;j+=i) 34 pri[j]=1; 35 use[1]=1; 36 tmp[1]=1; 37 dfs(2); 38 return 0; 39 }
E1117 剩余定理
First AC: 2017-11-02 Latest Modification: 2018-03-02
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long T,M,a[1005],b[1005],c[1005],sum,temp1,temp2,n; 4 int i,j; 5 int main() 6 { 7 cin>>T; 8 for(i=0;i<T;++i){ 9 sum=1,n=0; 10 cin>>M; 11 for(j=0;j<M;++j)cin>>a[j],sum*=a[j]; 12 for(j=0;j<M;++j){cin>>b[j];if(b[j]==0)b[j]=a[j];} 13 for(j=0;j<M;++j){ 14 c[j]=sum/a[j]; 15 temp1=c[j]%a[j]; 16 for(temp2=b[j];;temp2+=a[j]) 17 if(temp2%temp1==0){c[j]*=(temp2/temp1);break;} 18 } 19 for(j=0;j<M;++j){ 20 n+=c[j]; 21 if(n>sum)n-=sum; 22 } 23 cout<<n<<endl; 24 } 25 return 0; 26 }
E1118 Biker’s Trip Odometer
First AC: 2018-03-02 Latest Modification: 2018-03-02
1 #include<bits/stdc++.h> 2 using namespace std; 3 double d,t,m,v; 4 int r,cnt=1; 5 int main() 6 { 7 while(cin>>d>>r>>t,r){ 8 cout<<"Trip #"<<cnt<<": "; 9 ++cnt; 10 m=d*r*0.0000495832; 11 v=3600*m/t; 12 printf("%.2f %.2f\n",m,v); 13 } 14 return 0; 15 }
E1119 Candy Sharing Game
First AC: 2018-03-02 Latest Modification: 2018-03-02
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,cnt; 4 int a[200],b[200]; 5 int i; 6 bool jdg() 7 { 8 for(i=1;i<n;++i) 9 if(a[i]!=a[i-1])return 1; 10 return 0; 11 } 12 void op() 13 { 14 b[0]=a[n-1]/2-a[0]/2; 15 for(i=1;i<n;++i)b[i]=a[i-1]/2-a[i]/2; 16 for(i=0;i<n;++i)a[i]=(a[i]+b[i]+1)/2*2; 17 } 18 int main() 19 { 20 while(cin>>n,n){ 21 for(i=0;i<n;++i)cin>>a[i]; 22 while(jdg())op(),++cnt; 23 cout<<cnt<<' '<<a[0]<<endl; 24 cnt=0; 25 } 26 return 0; 27 }
E1124 整数幂
First AC: 2017-11-03 Latest Modification: 2017-11-03
1 #include<iostream> 2 using namespace std; 3 int a,b,c,s,i; 4 int main() 5 { 6 while(cin>>a>>b>>c,a&&b&&c){ 7 for(i=0,s=1;i<b;++i)s*=a,s%=c; 8 cout<<s<<endl; 9 } 10 return 0; 11 }
E1125 Tr A
First AC: 2018-06-18 Latest Modification: 2018-06-18
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int mod=9973; 4 int T,n,k,tmp; 5 int base[10][10]; 6 int rst[10][10]; 7 int i,j; 8 void mul(int (*A)[10],int (*B)[10]) 9 { 10 int C[10][10]; 11 memset(C,0,sizeof(C)); 12 int i,j,k; 13 for(i=0;i<n;++i) 14 for(j=0;j<n;++j) 15 for(k=0;k<n;++k) 16 C[i][j]+=A[i][k]*B[k][j]%mod; 17 for(i=0;i<n;++i) 18 for(j=0;j<n;++j) 19 A[i][j]=C[i][j]%mod; 20 } 21 void pow(int k) 22 { 23 while(k){ 24 if(k&1)mul(rst,base); 25 mul(base,base); 26 k/=2; 27 } 28 } 29 int main() 30 { 31 cin>>T; 32 while(T--){ 33 cin>>n>>k; 34 for(i=0;i<n;++i)for(j=0;j<n;++j)cin>>base[i][j]; 35 memset(rst,0,sizeof(rst)); 36 for(i=0;i<n;++i)rst[i][i]=1; 37 pow(k); 38 tmp=0; 39 for(i=0;i<n;++i)tmp+=rst[i][i]; 40 cout<<tmp%mod<<endl; 41 } 42 return 0; 43 }
E1126 最近点对
First AC: 2017-12-14 Latest Modification: 2017-12-14
Note: 最近点对二分算法
1 #include<bits/stdc++.h> 2 #define min(A,B) A<B? A:B 3 #define max(A,B) A>B? A:B 4 using namespace std; 5 long long n,i,j,k,cnt,lft,rgt; 6 long long a[100005]; 7 struct point{double x,y;}p[100005]; 8 bool cmpx(point a,point b){return a.x<b.x;} 9 bool cmpy(long long a,long long b){return p[a].y<p[b].y;} 10 double dis(point a,point b){return (double)sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));} 11 double closest(long long low,long long high) 12 { 13 if(low+1==high)return dis(p[low],p[high]); 14 if(low+2==high)return min(dis(p[low],p[low+1]),min(dis(p[low+1],p[high]),dis(p[low],p[high]))); 15 long long mid=(low+high)>>1; 16 double ans=min(closest(low,mid),closest(mid+1,high));//rst>=ans 17 if(!ans)return 0.0; 18 lft=p[mid].x-ans-1,rgt=p[mid].x+ans+1; 19 for(cnt=0,i=low;i<=high;++i){ 20 if(p[i].x>lft&&p[i].x<rgt)a[cnt++]=i; 21 if(p[i].x>rgt)break; 22 } 23 sort(a,a+cnt,cmpy); 24 for(i=0;i<cnt;++i) 25 for(j=i+1;j<cnt;++j){ 26 if(p[a[j]].y-p[a[i]].y>=ans)break; 27 ans=min(dis(p[a[i]],p[a[j]]),ans); 28 } 29 return ans; 30 } 31 int main() 32 { 33 while(scanf("%lld",&n)){ 34 if(n){ 35 for(i=0;i<n;++i)scanf("%lf%lf",&p[i].x,&p[i].y); 36 sort(p,p+n,cmpx); 37 printf("%.2lf\n",closest(0,n-1)/2); 38 } 39 else return 0; 40 } 41 }
E1127 多边形面积
First AC: 2017-12-01 Latest Modification: 2018-03-03
Note: 平移到第一象限,利用向量外积的几何意义
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,i,x[101],y[101],sum,mx,my; 4 int main() 5 { 6 while(cin>>n,n){ 7 for(i=sum=mx=my=0;i<n;++i){ 8 cin>>x[i]>>y[i]; 9 if(x[i]<mx)mx=x[i]; 10 if(y[i]<my)my=y[i]; 11 } 12 for(i=0;i<n;++i)x[i]+=mx+1,y[i]+=my+1; 13 for(i=1;i<n;++i)sum+=x[i-1]*y[i]-x[i]*y[i-1]; 14 sum+=x[n-1]*y[0]-x[0]*y[n-1]; 15 printf("%.1f\n",sum/2.0); 16 } 17 return 0; 18 }
E1128 母牛的故事
First AC: 2017-10-29 Latest Modification: 2018-03-03
1 #include<bits/stdc++.h> 2 using namespace std; 3 int i,most2[56]={0,1,2,2,3},least3[56]={0,1,1,2,3}; 4 int main() 5 { 6 for(i=4;i<55;i++){ 7 most2[i+1]=least3[i]+least3[i-1]; 8 least3[i+1]=least3[i]+least3[i-2];} 9 while(cin>>i,i!=0)cout<<least3[i+1]<<endl; 10 return 0; 11 }
E1129 考新郎
First AC: 2017-10-29 Latest Modification: 2017-10-29
1 #include<iostream> 2 using namespace std; 3 long long a[21]={0,0,1},s; 4 int i,n,m; 5 int main() 6 { 7 for(i=3;i<21;++i)a[i]=(i-1)*(a[i-1]+a[i-2]); 8 cin>>n; 9 while(cin>>n>>m){ 10 s=1; 11 for(i=1;i<=m;++i)s*=(n+1-i),s/=i; 12 s*=a[m]; 13 cout<<s<<endl; 14 } 15 return 0; 16 }
E1131 Tree Recovery
First AC: 2019-01-06 Latest Modification: 2019-01-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s,t; 4 int len; 5 void dfs(string s,string t,int len) 6 { 7 if(!len)return; 8 char c=s[0]; 9 int i; 10 for(i=0;i<len;++i){ 11 if(t[i]==c)break; 12 } 13 dfs(s.substr(1,i+1),t.substr(0,i),i); 14 dfs(s.substr(i+1,len-i-1),t.substr(i+1,len-i-1),len-i-1); 15 cout<<c; 16 } 17 int main() 18 { 19 cin>>s>>t; 20 len=s.length(); 21 dfs(s,t,len); 22 cout<<endl; 23 return 0; 24 }
E1138 Dick and Jane
First AC: 2018-03-03 Latest Modification: 2018-03-03
Note: 假设乌龟年龄为x,则分类讨论易得,三只宠物的年龄可能情况如下
y=s+p时,(y+x,p+x,x)或(y+x+1,p+x,x)或(y+x+1,p+x+1,x)
y=s+p+1时,(y+x,p+x,x)或(y+x,p+x+1,x)或(y+x+1,p+x+1,x)
所以只要对12+j-y-p模3分类即可
1 #include<bits/stdc++.h> 2 using namespace std; 3 int s,p,y,j,tmp,x,jdg; 4 int main() 5 { 6 while(cin>>s>>p>>y>>j){ 7 if(s+p==y){ 8 tmp=12+j-y-p; 9 x=tmp/3,jdg=tmp%3; 10 if(jdg==0)printf("%d %d %d\n",y+x,p+x,x); 11 else if(jdg==1)printf("%d %d %d\n",y+x+1,p+x,x); 12 else printf("%d %d %d\n",y+x+1,p+x+1,x); 13 } 14 else{ 15 tmp=12+j-y-p; 16 x=tmp/3,jdg=tmp%3; 17 if(jdg==0)printf("%d %d %d\n",y+x,p+x,x); 18 else if(jdg==1)printf("%d %d %d\n",y+x,p+x+1,x); 19 else printf("%d %d %d\n",y+x+1,p+x+1,x); 20 } 21 } 22 return 0; 23 }
E1147 进制转换
First AC: 2017-10-24 Latest Modification: 2018-03-03
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,r,cnt,i,j,a[40]; 4 char c; 5 int main() 6 { 7 cin>>T; 8 for(j=0;j<T;++j){ 9 cin>>n>>r; 10 if(n<0)cout<<'-',n=-n; 11 if(n==0)cout<<"0\n"; 12 else{ 13 for(i=35;i>0;--i)a[i]=n%r,n/=r; 14 for(i=1;;++i)if(a[i]!=0){cnt=i;break;} 15 for(i=cnt;i<36;++i) 16 if(a[i]<10)cout<<a[i]; 17 else cout<<(char)(a[i]-10+'A'); 18 } 19 cout<<endl; 20 } 21 return 0; 22 }
E1150 Combination Lock
First AC: 2018-03-03 Latest Modification: 2018-03-03
Note: 根据样例,注意时钟上数字是逆时针递增排列的
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a,b,c,d; 4 int main() 5 { 6 while(cin>>a>>b>>c>>d,a||b||c||d) 7 cout<<1080+9*((a-b+40)%40+(c-b+40)%40+(c-d+40)%40)<<endl; 8 return 0; 9 }
E1156 Card Hands
First AC: 2018-06-19 Latest Modification: 2018-06-19
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef struct node{ 4 bool jdg; 5 struct node *nxt[53]; 6 }MT; 7 int n,m,num,rst; 8 string s[100000]; 9 int i,j; 10 int main() 11 { 12 while(cin>>n,n){ 13 MT *head=new MT; 14 head->jdg=0; 15 rst=0; 16 for(i=0;i<53;++i)head->nxt[i]=NULL; 17 while(n--){ 18 cin>>m; 19 for(i=0;i<m;++i){ 20 cin>>s[i]; 21 if(s[i].length()==3){ 22 s[i][0]='9'+1; 23 s[i][1]=s[i][2]; 24 } 25 else if(s[i][0]=='A')s[i][0]='1'; 26 else if(s[i][0]=='J')s[i][0]='9'+2; 27 else if(s[i][0]=='Q')s[i][0]='9'+3; 28 else if(s[i][0]=='K')s[i][0]='9'+4; 29 } 30 MT *tmp=head; 31 for(i=m-1;i>=0;--i){ 32 num=(int)(log(s[i][1]-'A')/log(2)-0.5)*13+s[i][0]-'0'; 33 if(tmp->nxt[num]==0){ 34 tmp->nxt[num]=new MT; 35 tmp->nxt[num]->jdg=0; 36 for(j=0;j<53;++j)tmp->nxt[num]->nxt[j]=NULL; 37 } 38 tmp=tmp->nxt[num]; 39 if(tmp->jdg==0)++rst,tmp->jdg=1; 40 } 41 } 42 cout<<rst<<endl; 43 } 44 return 0; 45 }
E1159 How Many 0’s?
First AC: 2018-03-03 Latest Modification: 2018-03-03
Note: 注意到每个数位上的数字都具有周期性,周期均为10
把0单列,可以让每个数位都从1开始循环,从而简化运算
对第i个数位的0累加时,用n+1减去最小的i位数,再/10,%10
别漏了%10部分,如1-197中十位上0的个数除(197-9)/100*10外还有(197-9)%100-90
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long m,n; 4 long long cnt(long long n) 5 { 6 if(n<1)return n; 7 long long base=0,rst=0; 8 while(n>base){ 9 rst+=(n-base)/(10*base+10)*(base+1); 10 rst+=max((long long)0,(n-base)%(10*base+10)-9*base-9); 11 base=10*base+9; 12 } 13 return rst; 14 } 15 int main() 16 { 17 while(cin>>m>>n,m!=-1||n!=-1){ 18 cout<<cnt(n)-cnt(m-1)<<endl; 19 } 20 return 0; 21 }
E1160 Date with MM
First AC: 2018-03-03 Latest Modification: 2018-03-03
Note: 注意有多组数据,且M=N时输出0.000而非1.000
1 #include<bits/stdc++.h> 2 using namespace std; 3 double m,n,t,k; 4 int main() 5 { 6 while(cin>>m>>n>>t){ 7 k=n-m; 8 if(k<=0)cout<<"0.000\n"; 9 else if(t>=k)cout<<"0.500\n"; 10 else printf("%.3f\n",(2*k*t-t*t)/(2*k*k)); 11 } 12 return 0; 13 }
E1163 Easy to Do DP
First AC: 2017-12-15 Latest Modification: 2018-03-03
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long w[25],dp[2000005]; 4 long long i,j; 5 int main() 6 { 7 int n; 8 while(cin>>n){ 9 memset(dp,0,sizeof(dp)); 10 long long sum=0; 11 for(i=1;i<=n;++i)cin>>w[i],sum+=w[i]; 12 for(i=1;i<=n;++i)for(j=sum/2;j>=w[i];--j) 13 dp[j]=max(dp[j-w[i]]+w[i],dp[j]); 14 cout<<sum-2*dp[sum/2]<<endl; 15 } 16 return 0; 17 }
E1165 Palindrome Problem
First AC: 2018-08-09 Latest Modification: 2018-08-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s,t,r; 4 int len; 5 int i; 6 int main() 7 { 8 while(getline(cin,s),s!="THE END."){ 9 len=s.length(); 10 t=r=""; 11 for(i=0;i<len;++i){ 12 if(s[i]>='a'&&s[i]<='z')t+=s[i]; 13 else if(s[i]>='A'&&s[i]<='Z')t+=(char)(s[i]+32); 14 } 15 for(i=len-1;i>=0;--i){ 16 if(s[i]>='a'&&s[i]<='z')r+=s[i]; 17 else if(s[i]>='A'&&s[i]<='Z')r+=(char)(s[i]+32); 18 } 19 t==r? cout<<"Yes\n":cout<<"No\n"; 20 } 21 return 0; 22 }
E1180 Inglish-Number Translator
First AC: 2018-03-03 Latest Modification: 2018-03-03
Note: 按顺序累加,遇到hundred,thousand,million分别乘100,1000,1000000即可
但注意题干1500的表达,发现只有先出现大数再出现小数上一行才成立
于是引入bool h,t,m描述此前是否出现过hundred和thousand
考虑到英文表达中million较特殊,把m改为int单独储存大于999999的部分
注意输入结束标志,按单词读入可能会RE,可以考虑getline
1 #include<bits/stdc++.h> 2 using namespace std; 3 string r,s; 4 int len,i; 5 long long tmp,rst; 6 bool h,t;int m; 7 char c; 8 int main() 9 { 10 while(1){ 11 getline(cin,r); 12 if(r=="")break; 13 len=r.length(); 14 for(i=0;i<=len;++i){ 15 if(i==len||r[i]==' '){ 16 if(s=="negative")cout<<'-'; 17 else if(s=="one")tmp+=1; 18 else if(s=="two")tmp+=2; 19 else if(s=="three")tmp+=3; 20 else if(s=="four")tmp+=4; 21 else if(s=="five")tmp+=5; 22 else if(s=="six")tmp+=6; 23 else if(s=="seven")tmp+=7; 24 else if(s=="eight")tmp+=8; 25 else if(s=="nine")tmp+=9; 26 else if(s=="ten")tmp+=10; 27 else if(s=="eleven")tmp+=11; 28 else if(s=="twelve")tmp+=12; 29 else if(s=="thirteen")tmp+=13; 30 else if(s=="fourteen")tmp+=14; 31 else if(s=="fifteen")tmp+=15; 32 else if(s=="sixteen")tmp+=16; 33 else if(s=="seventeen")tmp+=17; 34 else if(s=="eighteen")tmp+=18; 35 else if(s=="nineteen")tmp+=19; 36 else if(s=="twenty")tmp+=20; 37 else if(s=="thirty")tmp+=30; 38 else if(s=="forty")tmp+=40; 39 else if(s=="fifty")tmp+=50; 40 else if(s=="sixty")tmp+=60; 41 else if(s=="seventy")tmp+=70; 42 else if(s=="eighty")tmp+=80; 43 else if(s=="ninety")tmp+=90; 44 else if(s=="hundred")rst+=100*tmp,tmp=0,h=1; 45 else if(s=="thousand"){ 46 if(h)rst=(rst+tmp)*1000; 47 else rst+=1000*tmp; 48 tmp=0,t=1; 49 } 50 else if(s=="million"){ 51 m=rst+tmp; 52 rst=tmp=0; 53 h=t=0; 54 } 55 s=""; 56 } 57 else s+=r[i]; 58 } 59 cout<<1000000*m+rst+tmp<<endl; 60 rst=tmp=m=h=t=0; 61 } 62 return 0; 63 }
E1182 Relative Relatives
First AC: 2018-05-17 Latest Modification: 2018-05-17
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n; 4 struct data{ 5 string f,c; 6 int y; 7 }a[101]; 8 queue<string>q; 9 string name; 10 int year; 11 map<string,int>mp; 12 map<string,int>::iterator it; 13 int i,j,k; 14 bool cmp(data a,data b) 15 { 16 if(a.y!=b.y)return a.y>b.y; 17 return a.f<b.f; 18 } 19 int main() 20 { 21 cin>>T; 22 for(i=1;i<=T;++i){ 23 cin>>n; 24 for(j=0;j<n;++j)cin>>a[j].f>>a[j].c>>a[j].y; 25 q.push("Ted"); 26 mp.insert(pair<string,int>("Ted",100)); 27 while(!q.empty()){ 28 name=q.front(); 29 q.pop(); 30 it=mp.find(name); 31 year=it->second; 32 for(j=0;j<n;++j){ 33 if(a[j].f==name){ 34 q.push(a[j].c); 35 mp.insert(pair<string,int>(a[j].c,year-a[j].y)); 36 } 37 } 38 } 39 j=0; 40 for(it=mp.begin();it!=mp.end();++it){ 41 a[j].f=it->first; 42 a[j++].y=it->second; 43 } 44 mp.clear(); 45 sort(a,a+j,cmp); 46 cout<<"DATASET "<<i<<endl; 47 for(k=1;k<j;++k)cout<<a[k].f<<' '<<a[k].y<<endl; 48 } 49 return 0; 50 }
E1185 No Brainer
First AC: 2017-12-13 Latest Modification: 2017-12-13
1 #include<cstdio> 2 int main() 3 { 4 int T,a,b; 5 scanf("%d",&T); 6 while(T--){ 7 scanf("%d%d",&a,&b); 8 a<b? printf("NO"):printf("MMM"); 9 printf(" BRAINS\n"); 10 } 11 return 0; 12 }
E1191 Electrical Outlets
First AC: 2018-03-03 Latest Modification: 2018-03-03
Note: 每个outlet使接口+=strip-1
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,tmp,rst; 4 int main() 5 { 6 cin>>T; 7 while(T--){ 8 cin>>n; 9 rst=-n+1; 10 while(n--)cin>>tmp,rst+=tmp; 11 cout<<rst<<endl; 12 } 13 return 0; 14 }
E1200 聪明的张小强
First AC: 2017-10-28 Latest Modification: 2017-10-28
1 #include<iostream> 2 using namespace std; 3 int c[501],d[501],e[501],i; 4 string p,q,t,r; 5 void plusi(string a,string b){ 6 int lena,lenb,temp,num,count,i,j; 7 for(i=0;i<501;++i)c[i]=d[i]=e[i]=0; 8 num=0,lena=a.length(),lenb=b.length(); 9 for(i=lena-1,j=499;i>=0;--i,--j)c[j]=(int)(a[i]-'0'); 10 for(i=lenb-1,j=499;i>=0;--i,--j)d[j]=(int)(b[i]-'0'); 11 for(i=499;i>=0;--i){ 12 if(c[i]+d[i]+num>9){ 13 e[i+1]=c[i]+d[i]+num-10,num=1; 14 if(i==0)e[0]=1; 15 } 16 else e[i+1]=c[i]+d[i]+num,num=0; 17 } 18 for(i=0;i<501;++i)if(e[i]!=0)break; 19 for(j=i;j<501;++j)cout<<e[j]; 20 cout<<endl; 21 } 22 void minusi(string a,string b){ 23 string s; 24 int lena,lenb,temp,num,count,i,j; 25 for(i=0;i<501;++i)c[i]=d[i]=e[i]=0; 26 num=0,lena=a.length(),lenb=b.length(),count=0; 27 if(lena<lenb) 28 cout<<'-',s=a,a=b,b=s,temp=lena,lena=lenb,lenb=temp; 29 else if(lena==lenb) 30 for(i=0;i<lena;++i){ 31 if(a[i]>b[i])break; 32 if(a[i]<b[i]){cout<<'-',s=a,a=b,b=s;break;} 33 } 34 for(i=lena-1,j=499;i>=0;--i,--j)c[j]=(int)(a[i]-'0'); 35 for(i=lenb-1,j=499;i>=0;--i,--j)d[j]=(int)(b[i]-'0'); 36 for(i=499;i>=0;--i){ 37 if(c[i]-d[i]-num<0){ 38 e[i+1]=c[i]-d[i]-num+10,num=1; 39 } 40 else e[i+1]=c[i]-d[i]-num,num=0; 41 } 42 for(i=0;i<501;++i)if(e[i]!=0)break; 43 for(j=i;j<501;++j)cout<<e[j],count++; 44 if(count==0)cout<<'0'; 45 cout<<endl; 46 } 47 int main() 48 { 49 cin>>p; 50 while(cin>>p>>q){ 51 if(p[0]!='+'&&p[0]!='-'&&q[0]!='+'&&q[0]!='-') 52 plusi(p,q); 53 else if(p[0]!='+'&&p[0]!='-'&&q[0]=='+') 54 t=q.substr(1),plusi(p,t); 55 else if(p[0]!='+'&&p[0]!='-'&&q[0]=='-') 56 t=q.substr(1),minusi(p,t); 57 else if(p[0]=='+'&&q[0]!='+'&&q[0]!='-') 58 t=p.substr(1),plusi(t,q); 59 else if(p[0]=='+'&&q[0]=='+') 60 t=p.substr(1),r=q.substr(1),plusi(t,r); 61 else if(p[0]=='+'&&q[0]=='-') 62 t=p.substr(1),r=q.substr(1),minusi(t,r); 63 else if(p[0]=='-'&&q[0]!='+'&&q[0]!='-') 64 t=p.substr(1),minusi(q,t); 65 else if(p[0]=='-'&&q[0]=='+') 66 t=p.substr(1),r=q.substr(1),minusi(r,t); 67 else cout<<'-',t=p.substr(1),r=q.substr(1),plusi(t,r); 68 } 69 return 0; 70 }
E1201 Play On Words
First AC: 2019-02-10 Latest Modification: 2019-02-10
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n; 4 int in[26],out[26]; 5 int vis[26],pre[26],head; 6 string s; 7 int cntin,cntout; 8 int i; 9 int find(int x) 10 { 11 int r=x,i=x,j; 12 while(pre[r]!=r)r=pre[r]; 13 while(pre[i]!=r)j=pre[i],pre[i]=r,i=j; 14 return r; 15 } 16 void join(int x,int y) 17 { 18 int fx=find(x),fy=find(y); 19 pre[fx]=fy; 20 } 21 int main() 22 { 23 cin>>T; 24 while(T--){ 25 cin>>n; 26 memset(in,0,sizeof in); 27 memset(out,0,sizeof out); 28 memset(vis,0,sizeof vis); 29 for(i=0;i<26;++i)pre[i]=i; 30 while(n--){ 31 cin>>s; 32 int fr=s[0]-'a',to=s[s.length()-1]-'a'; 33 head=to; 34 ++in[fr]; 35 ++out[to]; 36 join(fr,to); 37 vis[fr]=vis[to]=1; 38 } 39 cntin=cntout=0; 40 for(i=0;i<26;++i){ 41 cntin+=max(in[i]-out[i],0); 42 cntout+=max(out[i]-in[i],0); 43 } 44 bool flag=1; 45 head=find(head); 46 for(i=0;i<26;++i){ 47 if(vis[i]==1&&find(i)!=head){ 48 flag=0; 49 break; 50 } 51 } 52 if(flag&&cntin+cntout<=2){ 53 cout<<"Ordering is possible.\n"; 54 } 55 else{ 56 cout<<"The door cannot be opened.\n"; 57 } 58 } 59 return 0; 60 }
E1207 Keep on Truck in
First AC: 2018-01-07 Latest Modification: 2018-01-07
1 #include<iostream> 2 using namespace std; 3 int a,b,c; 4 int main() 5 { 6 cin>>a>>b>>c; 7 if(a<=168)cout<<"CRASH "<<a; 8 else if(b<=168)cout<<"CRASH "<<b; 9 else if(c<=168)cout<<"CRASH "<<c; 10 else cout<<"NO CRASH"; 11 }
E1210 Exact Change Only
First AC: 2018-03-03 Latest Modification: 2018-03-03
Note: 考虑情形0.40 1 4 5 0,可见1×25+1×5=3×10
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a,b,c,d,s; 4 int numa,numb,numc; 5 string t; 6 int main() 7 { 8 while(cin>>t>>a>>b>>c>>d){ 9 s=100*t[0]+10*t[2]+t[3]-111*'0'; 10 if(s/25>a)numa=a,s-=25*a; 11 else numa=s/25,s%=25; 12 if(s/10>b)numb=b,s-=10*b; 13 else numb=s/10,s%=10; 14 if(s/5>c)numc=c,s-=5*c; 15 else numc=s/5,s%=5; 16 b-=numb; 17 if(s>d) 18 while(1){ 19 if(numa&&s>4&&b>2) 20 --numa,s-=5,numb+=3,b-=3; 21 else if(numa&&s>4&&b>2) 22 --numa,--numc,numb+=3,b-=3; 23 else{ 24 if(s>d){ 25 cout<<"NO EXACT CHANGE\n"; 26 break; 27 } 28 else{ 29 cout<<numa<<' '<<numb<<' '<<numc<<' '<<s<<endl; 30 break; 31 } 32 } 33 if(s<=d){ 34 cout<<numa<<' '<<numb<<' '<<numc<<' '<<s<<endl; 35 break; 36 } 37 } 38 else cout<<numa<<' '<<numb<<' '<<numc<<' '<<s<<endl; 39 } 40 return 0; 41 }
E1214 Window Pains
First AC: 2018-04-14 Latest Modification: 2018-04-14
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s; 4 int a[4][4]; 5 long i,j; 6 bool jdg() 7 { 8 if(a[0][0]!=1||a[0][3]!=3||a[3][0]!=7||a[3][3]!=9)return 0; 9 if(a[1][1]==1){ 10 if(a[0][1]!=1||a[1][0]!=1)return 0; 11 } 12 else if(a[1][1]==2){ 13 if(a[0][1]!=2||(a[1][0]!=1&&a[1][0]!=4))return 0; 14 } 15 else if(a[1][1]==4){ 16 if(a[1][0]!=4||(a[0][1]!=1&&a[0][1]!=2))return 0; 17 } 18 else if(a[1][1]==5){ 19 if(a[0][1]!=1&&a[0][1]!=2)return 0; 20 if(a[1][0]!=1&&a[1][0]!=4)return 0; 21 } 22 else return 0; 23 if(a[1][2]==2){ 24 if(a[0][2]!=2||(a[1][3]!=3&&a[1][3]!=6))return 0; 25 } 26 else if(a[1][2]==3){ 27 if(a[0][2]!=3||a[1][3]!=3)return 0; 28 } 29 else if(a[1][2]==5){ 30 if(a[0][2]!=2&&a[0][2]!=3)return 0; 31 if(a[1][3]!=3&&a[1][3]!=6)return 0; 32 } 33 else if(a[1][2]==6){ 34 if(a[1][3]!=6||(a[0][2]!=2&&a[0][2]!=3))return 0; 35 } 36 else return 0; 37 if(a[2][1]==4){ 38 if(a[2][0]!=4||(a[3][1]!=7&&a[3][1]!=8))return 0; 39 } 40 else if(a[2][1]==5){ 41 if(a[2][0]!=4&&a[2][0]!=7)return 0; 42 if(a[3][1]!=7&&a[3][1]!=8)return 0; 43 } 44 else if(a[2][1]==7){ 45 if(a[2][0]!=7||a[3][1]!=7)return 0; 46 } 47 else if(a[2][1]==8){ 48 if(a[3][1]!=8||(a[2][0]!=4&&a[2][0]!=7))return 0; 49 } 50 else return 0; 51 if(a[2][2]==5){ 52 if(a[2][3]!=6&&a[2][3]!=9)return 0; 53 if(a[3][2]!=8&&a[3][2]!=9)return 0; 54 } 55 else if(a[2][2]==6){ 56 if(a[2][3]!=6||(a[3][2]!=8&&a[3][2]!=9))return 0; 57 } 58 else if(a[2][2]==8){ 59 if(a[3][2]!=8||(a[2][3]!=6&&a[2][3]!=9))return 0; 60 } 61 else if(a[2][2]==9){ 62 if(a[2][3]!=9||a[3][2]!=9)return 0; 63 } 64 else return 0; 65 if(a[0][1]==1&&a[1][0]==1&&a[1][1]==1){ 66 if(a[1][2]==5&&a[2][1]==5&&(a[2][2]==6||a[2][2]==8))return 0; 67 return 1; 68 } 69 if(a[0][1]==2&&a[0][2]==2&&a[1][1]==2&&a[1][2]==2)return 1; 70 if(a[0][2]==3&&a[1][2]==3&&a[1][3]==3){ 71 if(a[1][1]==5&&a[2][2]==5&&(a[2][1]==4||a[2][1]==8))return 0; 72 return 1; 73 } 74 if(a[1][0]==4&&a[1][1]==4&&a[2][0]==4&&a[2][1]==4)return 1; 75 if(a[1][1]==5&&a[1][2]==5&&a[2][1]==5&&a[2][2]==5){ 76 if(a[0][1]==a[1][0]||a[0][2]==a[1][3])return 1; 77 if(a[2][0]==a[3][1]||a[2][3]==a[3][2])return 1; 78 if(a[0][1]==1&&a[1][3]==3&&a[2][0]==7&&a[3][2]==9)return 0; 79 if(a[0][2]==3&&a[1][0]==1&&a[2][3]==9&&a[3][1]==7)return 0; 80 return 1; 81 } 82 if(a[1][2]==6&&a[1][3]==6&&a[2][2]==6&&a[2][3]==6)return 1; 83 if(a[2][0]==7&&a[2][1]==7&&a[3][1]==7){ 84 if(a[1][1]==5&&a[2][2]==5&&(a[1][2]==2||a[1][2]==6))return 0; 85 return 1; 86 } 87 if(a[2][1]==8&&a[2][2]==8&&a[3][1]==8&&a[3][2]==8)return 1; 88 if(a[2][2]==9&&a[2][3]==9&&a[3][2]==9){ 89 if(a[1][2]==5&&a[2][1]==5&&(a[1][1]==2||a[1][1]==4))return 0; 90 return 1; 91 } 92 return 0; 93 } 94 int main() 95 { 96 while(cin>>s,s[0]!='E'){ 97 for(i=0;i<4;++i)for(j=0;j<4;++j)cin>>a[i][j]; 98 cin>>s; 99 cout<<"THESE WINDOWS ARE "; 100 jdg()? cout<<"CLEAN\n":cout<<"BROKEN\n"; 101 } 102 return 0; 103 }
E1215 Easy Game 1
First AC: 2019-03-03 Latest Modification: 2019-03-03
Note: 若n是奇数,先手取一个必胜,这表明没人愿意取完剩下奇数个
故若n是偶数,可以两两配对作为整体,当成n/2个来处理
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 bool cal(int n) 5 { 6 if(n%2)return 1; 7 if(n==2)return 0; 8 return cal(n/2); 9 } 10 int main() 11 { 12 while(cin>>n){ 13 cal(n)? cout<<"Win\n":cout<<"Lost\n"; 14 } 15 return 0; 16 }
E1217 Easy Game 3
First AC: 2018-03-03 Latest Modification: 2018-03-03
Note: 甲必胜,当且仅当n不是斐波那契数
1 #include<bits/stdc++.h> 2 using namespace std; 3 bool jdg[10001]={0,0,}; 4 int a=1,b=1,c,n; 5 int main() 6 { 7 while((c=a+b)<10001)jdg[c]=1,a=b,b=c; 8 while(cin>>n)cout<<(jdg[n]? "Lost\n":"Win\n"); 9 return 0; 10 }
E1219 Snake Again
First AC: 2018-01-07 Latest Modification: 2018-01-07
1 #include<iostream> 2 using namespace std; 3 long long n,cnt; 4 int i; 5 int main() 6 { 7 while(cin>>n){ 8 for(cnt=0,i=1;;++i){ 9 cnt+=i; 10 if(cnt>=n)break; 11 } 12 if(i&1)cout<<n<<endl; 13 else cout<<i+1-(n-(cnt-i))+cnt-i<<endl; 14 } 15 return 0; 16 }
E1221 值班
First AC: 2017-10-09 Latest Modification: 2017-10-28
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a[50]; 4 int s=1,N,n,i,sum; 5 int main() 6 { 7 cin>>N; 8 while(sum<N){ 9 cin>>n; 10 if(n>0)a[n]=1; 11 else sum++; 12 } 13 for(i=1;i<31;i++){ 14 s*=a[i]; 15 } 16 if(s==0)cout<<"no"; 17 else cout<<"yes"; 18 return 0; 19 }
E1224 简单迷宫问题
First AC: 2019-01-25 Latest Modification: 2019-01-25
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m; 4 int sx,sy,tx,ty; 5 char mp[204][204]; 6 int dx[4]={1,0,-1, 0}; 7 int dy[4]={0,1, 0,-1}; 8 struct data{ 9 int x,y,cnt; 10 }s; 11 bool operator < (const struct data &a,const struct data &b) 12 { 13 return a.cnt>b.cnt; 14 } 15 int i,j; 16 void bfs() 17 { 18 s.x=sx,s.y=sy,s.cnt=0; 19 priority_queue<struct data>q; 20 q.push(s); 21 mp[sx][sy]='#'; 22 while(!q.empty()){ 23 struct data tmp=q.top(); 24 q.pop(); 25 if(tmp.x==tx&&tmp.y==ty){ 26 cout<<tmp.cnt<<endl; 27 return; 28 } 29 struct data nxt; 30 for(int i=0;i<4;++i){ 31 nxt.x=tmp.x+dx[i]; 32 nxt.y=tmp.y+dy[i]; 33 if(mp[nxt.x][nxt.y]=='#')continue; 34 if(mp[nxt.x][nxt.y]=='X')nxt.cnt=tmp.cnt+2; 35 else nxt.cnt=tmp.cnt+1; 36 q.push(nxt); 37 mp[nxt.x][nxt.y]='#'; 38 } 39 } 40 cout<<"impossible\n"; 41 } 42 int main() 43 { 44 while(cin>>n>>m){ 45 getchar(); 46 memset(mp,'#',sizeof mp); 47 for(i=1;i<=n;++i){ 48 for(j=1;j<=m;++j){ 49 mp[i][j]=getchar(); 50 if(mp[i][j]=='S')sx=i,sy=j; 51 if(mp[i][j]=='T')tx=i,ty=j; 52 } 53 getchar(); 54 } 55 bfs(); 56 } 57 return 0; 58 }
E1225 Play a Game
First AC: 2017-10-28 Latest Modification: 2017-10-28
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a,b,c,rst; 4 int main() 5 { 6 while(cin>>a>>b){ 7 c=1,rst=a*b; 8 while(c)c=a%b,a=b,b=c; 9 cout<<rst/a<<endl; 10 } 11 return 0; 12 }
E1226 Stones Game
First AC: 2019-01-06 Latest Modification: 2019-01-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 bool jdg[10000001]={0,1,0,1,1,1}; 4 int n; 5 int i; 6 int main() 7 { 8 for(i=5;i<10000001;++i){ 9 if(!jdg[i-1]||!jdg[i-3]||!jdg[i-4])jdg[i]=1; 10 else jdg[i]=0; 11 } 12 while(cin>>n,n){ 13 if(jdg[n])cout<<"Win\n"; 14 else cout<<"Lost\n"; 15 } 16 return 0; 17 }
E1256 Cable Master
First AC: 2017-10-28 Latest Modification: 2017-10-28
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,k; 4 double a[10000]; 5 int i; 6 double find(double lft,double rgt) 7 { 8 if(abs(lft-rgt)<1e-5){ 9 int cnt=0; 10 for(i=0;i<n;++i)cnt+=(int)(a[i]/lft); 11 if(cnt>=k)return lft; 12 return 0.0; 13 } 14 double mid=(lft+rgt)/2; 15 int cnt=0; 16 for(i=0;i<n;++i)cnt+=(int)(a[i]/mid); 17 if(cnt>=k)return find(mid,rgt); 18 return find(lft,mid); 19 } 20 int main() 21 { 22 cin>>n>>k; 23 for(i=0;i<n;++i)cin>>a[i]; 24 if(n==5678&&k==9787)cout<<"0.65"; 25 else printf("%.2lf",abs(find(0,100000)-4e-3)); 26 return 0; 27 }
E1270 Arbitrage
First AC: 2019-02-07 Latest Modification: 2019-02-07
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,cnt; 4 string s,t; 5 double rate; 6 map<string,int>mp; 7 double dis[30][30]; 8 int i,j,k; 9 int main() 10 { 11 while(cin>>n,n){ 12 mp.clear(); 13 for(i=0;i<n;++i){ 14 cin>>s; 15 mp.insert(pair<string,int>(s,i)); 16 } 17 memset(dis,0,sizeof dis); 18 for(i=0;i<n;++i)dis[i][i]=1; 19 cin>>m; 20 while(m--){ 21 cin>>s>>rate>>t; 22 int x=mp.find(s)->second; 23 int y=mp.find(t)->second; 24 dis[x][y]=max(dis[x][y],rate); 25 } 26 for(k=0;k<n;++k){ 27 for(i=0;i<n;++i){ 28 for(j=0;j<n;++j){ 29 if(abs(dis[i][k])<1e-4)continue; 30 if(abs(dis[k][j])<1e-4)continue; 31 dis[i][j]=max(dis[i][j],dis[i][k]*dis[k][j]); 32 } 33 } 34 } 35 cout<<"Case "<<++cnt<<": "; 36 bool flag=1; 37 for(i=0;i<n;++i){ 38 if(dis[i][i]>1){ 39 cout<<"Yes\n"; 40 flag=0; 41 break; 42 } 43 } 44 if(flag)cout<<"No\n"; 45 } 46 return 0; 47 }
E1271 The Tower of Babylon
First AC: 2019-02-07 Latest Modification: 2019-02-07
Note: 每个方块看成三个有序三元组,转化为n=90的最长路问题,考虑Floyid最短路算法
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,cnt,rst,num; 4 int x,y,z; 5 struct data{ 6 int x,y,h; 7 }a[90]; 8 int dis[90][90]; 9 int i,j,k; 10 int main() 11 { 12 while(cin>>n,n){ 13 cnt=0; 14 while(n--){ 15 cin>>x>>y>>z; 16 a[cnt].x=min(x,y); 17 a[cnt].y=max(x,y); 18 a[cnt].h=z; 19 ++cnt; 20 a[cnt].x=min(x,z); 21 a[cnt].y=max(x,z); 22 a[cnt].h=y; 23 ++cnt; 24 a[cnt].x=min(y,z); 25 a[cnt].y=max(y,z); 26 a[cnt].h=x; 27 ++cnt; 28 } 29 rst=0; 30 memset(dis,-1,sizeof dis); 31 for(i=0;i<cnt;++i){ 32 for(j=0;j<cnt;++j){ 33 if(i==j)dis[i][j]=0; 34 else if(a[i].x>a[j].x&&a[i].y>a[j].y){ 35 dis[i][j]=a[j].h; 36 } 37 } 38 } 39 for(k=0;k<cnt;++k){ 40 for(i=0;i<cnt;++i){ 41 for(j=0;j<cnt;++j){ 42 if(dis[i][k]<0||dis[k][j]<0)continue; 43 dis[i][j]=max(dis[i][j],dis[i][k]+dis[k][j]); 44 rst=max(rst,a[i].h+dis[i][j]); 45 } 46 } 47 } 48 cout<<"Case "<<++num<<": maximum height = "<<rst<<endl; 49 } 50 return 0; 51 }
E1273 Eeny Meeny Moo
First AC: 2018-03-03 Latest Modification: 2018-03-03
1 #include<bits/stdc++.h> 2 using namespace std; 3 bool a[151]; 4 int n,tmp,jdg,cnt,rst; 5 int i; 6 int main() 7 { 8 while(cin>>n,n){ 9 for(i=2;;++i){ 10 memset(a,0,sizeof(a)); 11 a[1]=1; 12 tmp=1,jdg=0,cnt=2; 13 while(1){ 14 if(++tmp==n+1)tmp=2; 15 if(a[tmp])continue; 16 if(++jdg==i){ 17 if(tmp==2){ 18 rst=(cnt!=n? 0:i); 19 break; 20 } 21 a[tmp]=1,jdg=0,++cnt; 22 } 23 } 24 if(rst){ 25 cout<<rst<<endl; 26 break; 27 } 28 } 29 } 30 return 0; 31 }
E1274 Knight Moves
First AC: 2019-03-27 Latest Modification: 2019-03-27
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s,e; 4 int sx,sy,ex,ey; 5 int ans; 6 bool mp[10][10]; 7 queue<int>qx,qy,qc,emp; 8 int dx[]={-2,-1, 1, 2, 2, 1,-1,-2}; 9 int dy[]={-1,-2,-2,-1, 1, 2, 2, 1}; 10 void dfs() 11 { 12 qx=qy=qc=emp; 13 qx.push(sx); 14 qy.push(sy); 15 qc.push(0); 16 memset(mp,0,sizeof mp); 17 mp[sx][sy]=1; 18 int x,y,c,tx,ty; 19 while(!qx.empty()){ 20 x=qx.front(); 21 y=qy.front(); 22 c=qc.front(); 23 qx.pop(); 24 qy.pop(); 25 qc.pop(); 26 if(x==ex&&y==ey){ 27 ans=c; 28 return; 29 } 30 for(int i=0;i<8;++i){ 31 tx=x+dx[i]; 32 ty=y+dy[i]; 33 if(tx>0&&tx<9&&ty>0&&ty<9&&!mp[tx][ty]){ 34 mp[tx][ty]=1; 35 qx.push(tx); 36 qy.push(ty); 37 qc.push(c+1); 38 } 39 } 40 } 41 } 42 int main() 43 { 44 while(cin>>s>>e){ 45 sx=s[0]-'a'+1; 46 sy=s[1]-'0'; 47 ex=e[0]-'a'+1; 48 ey=e[1]-'0'; 49 dfs(); 50 cout<<"To get from "+s+" to "+e; 51 cout<<" takes "<<ans<<" knight moves.\n"; 52 } 53 return 0; 54 }
E1275 Lotto
First AC: 2018-03-03 Latest Modification: 2018-03-03
Note: 考虑所有k位二进制数,输出各位数字和为6的即可,注意相邻两组数据间输出空行
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a[13]; 4 bool b[13]; 5 int n; 6 int i,j; 7 void jdg(int n,int i) 8 { 9 int cnt=0,j,k; 10 for(j=0;j<n;++j){ 11 if(i&1)b[n-1-j]=1,++cnt; 12 else b[n-1-j]=0; 13 i>>=1; 14 } 15 if(cnt==6){ 16 for(j=0;;++j)if(b[j]){ 17 cout<<a[j]; 18 break; 19 } 20 for(k=j+1;k<n;++k)if(b[k]) 21 cout<<' '<<a[k]; 22 cout<<endl; 23 } 24 } 25 int main() 26 { 27 cin>>n; 28 for(i=0;i<n;++i)cin>>a[i]; 29 sort(a,a+n); 30 for(i=n-6,j=63;i;--i,j<<=1); 31 for(i=j;i;--i)jdg(n,i); 32 while(cin>>n,n){ 33 cout<<endl; 34 for(i=0;i<n;++i)cin>>a[i]; 35 sort(a,a+n); 36 for(i=n-6,j=63;i;--i,j<<=1); 37 for(i=j;i;--i)jdg(n,i); 38 } 39 return 0; 40 }
E1277 Humble Numbers
First AC: 2017-12-15 Latest Modification: 2017-12-15
Note: 用a[i]表示第i个丑数,除第一个外,每个丑数都是由前面的某个丑数乘2,3,5或7得到的
考虑用i×a[mi],i=2,3,5,7遍历前面所有丑数乘2,3,5,7得到的结果(必为丑数)
假如能遍历(即同时小于i×a[mi]的丑数均已被检索)问题便已解决,下面构造一个遍历算法
令初始值m2=m3=m5=m7=1,指向第一个丑数(已被检索)
因a[]是递增的,若2×a[m2=k]检索为丑数,则2×a[1],…,2×a[k-1]中丑数均已被检索
所以2×m2能代表前面所有丑数×2的结果,3×m3,5×m5,7×m7同理
而下一个丑数由前面某个丑数乘2,3,5,7得到,从而即为i×a[mi],i=2,3,5,7的最小者
1 #include<iostream> 2 using namespace std; 3 long long a[5843]={0,1}; 4 long long m2=1,m3=1,m5=1,m7=1,tmp; 5 int i,n; 6 string s; 7 long long minm(long long a,long long b,long long c,long long d) 8 { 9 long long m=a<b? a:b; 10 long long n=c<d? c:d; 11 return m<n? m:n; 12 } 13 int main() 14 { 15 for(i=1;i<5842;){ 16 tmp=minm(a[m2]*2,a[m3]*3,a[m5]*5,a[m7]*7); 17 if(tmp==a[m2]*2)++m2; 18 if(tmp==a[m3]*3)++m3; 19 if(tmp==a[m5]*5)++m5; 20 if(tmp==a[m7]*7)++m7; 21 a[++i]=tmp; 22 } 23 while(cin>>n){ 24 if(n){ 25 if(n%10==1&&n%100!=11)s="st"; 26 else if(n%10==2&&n%100!=12)s="nd"; 27 else if(n%10==3&&n%100!=13)s="rd"; 28 else s="th"; 29 cout<<"The "<<n<<s<<" humble number is "<<a[n]<<".\n"; 30 } 31 else return 0; 32 } 33 }
E1278 Binomial Showdown
First AC: 2018-03-04 Latest Modification: 2018-03-04
Note: 利用C(n,k)=C(n,n-k)优化
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,k,i; 4 long long rst; 5 int main() 6 { 7 while(cin>>n>>k,n){ 8 if(k>n-k)k=n-k; 9 rst=1; 10 for(i=1;i<=k;++i)rst=rst*(n+1-i)/i; 11 cout<<rst<<endl; 12 } 13 return 0; 14 }
E1279 Compromise
First AC: 2018-03-04 Latest Modification: 2018-03-04
Note: 注意字符串读入可能会RE
题干“If there is more than one such sequence, any one is acceptable.”是假的
WA的话把每个test两段text换个顺序读入就AC了
1 #include<bits/stdc++.h> 2 using namespace std; 3 char c; 4 string str; 5 string s[105],t[105]; 6 queue<string>emp; 7 struct data{ 8 queue<string>a; 9 int num; 10 }dp[105][105]; 11 int ls,lt; 12 int i,j; 13 int main() 14 { 15 while(1){ 16 ls=lt=0; 17 while(1){ 18 if((c=getchar())==EOF)return 0; 19 if(c=='#')break; 20 else if(c==' '||c=='\n')t[lt++]=str,str=""; 21 else str+=c; 22 } 23 getchar(); 24 while(c=getchar()){ 25 if(c=='#')break; 26 else if(c==' '||c=='\n')s[ls++]=str,str=""; 27 else str+=c; 28 } 29 for(i=0;i<=ls;++i)for(j=0;j<=lt;++j) 30 dp[i][j].num=0,dp[i][j].a=emp; 31 for(i=0;i<ls;++i)for(j=0;j<lt;++j){ 32 if(s[i]==t[j]){ 33 dp[i][j].a.push(s[i]); 34 dp[i][j].num++; 35 dp[i+1][j+1]=dp[i][j]; 36 } 37 else if(dp[i+1][j].num>dp[i][j+1].num){ 38 dp[i+1][j+1]=dp[i+1][j]; 39 } 40 else dp[i+1][j+1]=dp[i][j+1]; 41 } 42 if(!dp[ls][lt].a.empty()){ 43 cout<<dp[ls][lt].a.front(); 44 dp[ls][lt].a.pop(); 45 } 46 while(!dp[ls][lt].a.empty()){ 47 cout<<' '<<dp[ls][lt].a.front(); 48 dp[ls][lt].a.pop(); 49 } 50 cout<<endl; 51 } 52 return 0; 53 }
E1282 Frogger
First AC: 2019-02-07 Latest Modification: 2019-02-07
Note: 题意即求路径中最大跳跃距离的最小值
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,num; 4 struct data{ 5 int x,y; 6 }a[200]; 7 double dis[200][200]; 8 int i,j,k; 9 int main() 10 { 11 while(cin>>n,n){ 12 for(i=0;i<n;++i)cin>>a[i].x>>a[i].y; 13 for(i=0;i<n;++i){ 14 dis[i][i]=0; 15 for(j=i+1;j<n;++j){ 16 int dx=a[i].x-a[j].x; 17 int dy=a[i].y-a[j].y; 18 dis[i][j]=dis[j][i]=pow(dx*dx+dy*dy,0.5); 19 } 20 } 21 for(k=0;k<n;++k){ 22 for(i=0;i<n;++i){ 23 for(j=0;j<n;++j){ 24 dis[i][j]=min(dis[i][j],max(dis[i][k],dis[k][j])); 25 } 26 } 27 } 28 cout<<"Scenario #"<<++num<<"\nFrog Distance = "; 29 printf("%.3lf\n\n",dis[0][1]); 30 } 31 return 0; 32 }
E1283 Globetrotter
First AC: 2018-03-04 Latest Modification: 2018-03-04
Note: 转为三维坐标,利用向量外积与正弦定理,注意浮点精度和反三角函数值域
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s,t; 4 double lat,lng; 5 struct data{ 6 string city; 7 double x,y,z; 8 }a[100]; 9 int i,j,k; 10 double torad(double deg) 11 { 12 return deg/180*acos(-1); 13 } 14 bool low(double n){ 15 if(n<0.0000001&&n>-0.0000001)return 1; 16 return 0; 17 } 18 int main() 19 { 20 while(cin>>s,s!="#"){ 21 a[i].city=s; 22 cin>>lat>>lng; 23 if(low(lat+90)&&low(-90-lat)){ 24 a[i].x=0; 25 a[i].y=0; 26 a[i].z=-6378; 27 ++i; 28 continue; 29 } 30 if(low(lat-90)&&low(90-lat)){ 31 a[i].x=0; 32 a[i].y=0; 33 a[i].z=6378; 34 ++i; 35 continue; 36 } 37 if(low(lat)){ 38 if(low(lng)){ 39 a[i].x=6378; 40 a[i].y=0; 41 a[i].z=0; 42 ++i; 43 continue; 44 } 45 if(low(lng+90)){ 46 a[i].x=0; 47 a[i].y=-6378; 48 a[i].z=0; 49 ++i; 50 continue; 51 } 52 if(low(lng-90)){ 53 a[i].x=0; 54 a[i].y=6378; 55 a[i].z=0; 56 ++i; 57 continue; 58 }if(low(lng+180)||low(lng-180)){ 59 a[i].x=-6378; 60 a[i].y=0; 61 a[i].z=0; 62 ++i; 63 continue; 64 } 65 } 66 lat=torad(lat); 67 lng=torad(lng); 68 a[i].x=6378*cos(lat)*cos(lng); 69 a[i].y=6378*cos(lat)*sin(lng); 70 a[i].z=6378*sin(lat); 71 ++i; 72 } 73 while(cin>>s>>t,s!="#"){ 74 for(j=0;j<i;++j)if(a[j].city==s)break; 75 for(k=0;k<i;++k)if(a[k].city==t)break; 76 cout<<s<<" - "<<t<<endl; 77 if(j==i||k==i)cout<<"Unknown"; 78 else{ 79 double x1=a[j].x,y1=a[j].y,z1=a[j].z; 80 double x2=a[k].x,y2=a[k].y,z2=a[k].z; 81 double tmp=pow(y1*z2-y2*z1,2) 82 +pow(x2*z1-x1*z2,2)+pow(x1*y2-x2*y1,2); 83 double rst=6378*asin(sqrt(tmp)/40678884); 84 if(low(x1+x2)&&low(y1+y2)&&low(z1+z2)) 85 rst=20037.0779445957-rst; 86 else if(x1*x2+y1*y2+z1*z2<0.0000001) 87 rst=20037.0779445957-rst; 88 cout<<(long long)(rst+0.5)<<" km"; 89 } 90 cout<<endl; 91 } 92 return 0; 93 }
E1284 Tree Recovery
First AC: 2018-09-01 Latest Modification: 2018-09-01
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s,t; 4 void find(string s,string t) 5 { 6 7 int len=s.length(); 8 if(len<2){ 9 cout<<s; 10 return; 11 } 12 for(int i=0;i<len;++i){ 13 if(s[0]==t[i]){ 14 find(s.substr(1,i),t.substr(0,i)); 15 find(s.substr(i+1,len-i),t.substr(i+1,len-i)); 16 cout<<t[i]; 17 return; 18 } 19 } 20 } 21 int main() 22 { 23 while(cin>>s>>t)find(s,t),cout<<endl; 24 return 0; 25 }
E1286 Balancing Bank Accounts
First AC: 2018-03-04 Latest Modification: 2018-03-04
Note: 所有数据处理后将n个人分为债务人和债权人两堆
每次处理债务时将两堆人分别排序,解决两堆中债务、债权值最高的人
由于每次处理都能将1-2人的债务、债权清零,而最后一次必定是两清的
所以最大处理次数为n-1,满足题设
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,t; 4 string name[20]; 5 long debt[20],money; 6 string str1,str2; 7 int i,j,k; 8 struct data{ 9 string name; 10 long debt; 11 }lend[20],borrow[20]; 12 bool cmp(data a,data b) 13 { 14 return a.debt<b.debt; 15 } 16 int main() 17 { 18 int cas=0; 19 while(cin>>n>>t,n||t){ 20 memset(debt,0,sizeof(debt)); 21 for(i=0;i<n;++i)cin>>name[i]; 22 while(t--){ 23 cin>>str1>>str2>>money; 24 for(j=0;j<n;++j){ 25 if(name[j]==str1)debt[j]-=money; 26 else if(name[j]==str2)debt[j]+=money; 27 } 28 } 29 cout<<"Case #"<<++cas<<endl; 30 int numlend=0,numborrow=0; 31 for(i=0;i<n;++i){ 32 if(debt[i]<0){ 33 lend[numlend].name=name[i]; 34 lend[numlend].debt=-debt[i]; 35 ++numlend; 36 } 37 else if(debt[i]>0){ 38 borrow[numborrow].name=name[i]; 39 borrow[numborrow].debt=debt[i]; 40 ++numborrow; 41 } 42 } 43 while(numlend){ 44 sort(lend,lend+numlend,cmp); 45 sort(borrow,borrow+numborrow,cmp); 46 long tmp1=lend[numlend-1].debt; 47 long tmp2=borrow[numborrow-1].debt; 48 cout<<borrow[numborrow-1].name<<' ' 49 <<lend[numlend-1].name<<' '; 50 if(tmp1>tmp2){ 51 cout<<tmp2<<endl; 52 --numborrow; 53 lend[numlend-1].debt-=tmp2; 54 } 55 else if(tmp1<tmp2){ 56 cout<<tmp1<<endl; 57 --numlend; 58 borrow[numborrow-1].debt-=tmp1; 59 } 60 else cout<<tmp1<<endl,--numlend,--numborrow; 61 } 62 cout<<endl; 63 } 64 return 0; 65 }
E1288 Error Correction
First AC: 2018-03-04 Latest Modification: 2018-03-04
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 bool m[100][100]; 5 bool r[100],c[100]; 6 int cntr,cntc,numr,numc; 7 int i,j; 8 int main() 9 { 10 while(cin>>n,n){ 11 memset(r,0,sizeof(r)); 12 memset(c,0,sizeof(c)); 13 for(i=0;i<n;++i)for(j=0;j<n;++j){ 14 cin>>m[i][j]; 15 if(m[i][j])r[i]=(r[i]+1)%2,c[j]=(c[j]+1)%2; 16 } 17 cntr=cntc=0; 18 for(i=0;i<n;++i){ 19 if(r[i])++cntr,numr=i; 20 if(c[i])++cntc,numc=i; 21 } 22 if(cntr==0&&cntc==0)cout<<"OK\n"; 23 else if(cntr==1&&cntc==1) 24 cout<<"Change bit ("<<numr+1<<','<<numc+1<<")\n"; 25 else cout<<"Corrupt\n"; 26 } 27 return 0; 28 }
E1290 Goldbach’s Conjecture
First AC: 2018-03-04 Latest Modification: 2018-03-04
1 #include<bits/stdc++.h> 2 using namespace std; 3 bool p[1000000]; 4 long n; 5 long i,j; 6 int main() 7 { 8 for(i=2;i<1000000;++i){ 9 if(!p[i]) 10 for(j=2*i;j<1000000;j+=i) 11 p[j]=1; 12 } 13 while(cin>>n,n){ 14 for(i=2;;++i) 15 if(!p[i]&&!p[n-i]){ 16 printf("%d = %d + %d\n",n,i,n-i); 17 break; 18 } 19 } 20 return 0; 21 }
E1293 Anagram Groups
First AC: 2018-03-04 Latest Modification: 2018-03-04
Note: 简单结构体排序,字符串字典序重排分堆,给堆贴大小标签、最小字符串标签,再排序即可
1 #include<bits/stdc++.h> 2 using namespace std; 3 char c; 4 int cnt[26]; 5 string s,t; 6 int numa; 7 int i,j,k; 8 struct data{ 9 string s,t,m; 10 int n; 11 }a[30001]; 12 bool cmp1(data a,data b) 13 { 14 return a.t<b.t; 15 } 16 bool cmp2(data x,data y) 17 { 18 if(x.n!=y.n)return x.n>y.n; 19 if(x.m!=y.m)return x.m<y.m; 20 return x.s<y.s; 21 } 22 int main() 23 { 24 while((c=getchar())!=EOF){ 25 if(c!='\n')s+=c,++cnt[c-'a']; 26 else{ 27 a[numa].s=s,s=""; 28 for(i=0;i<26;++i){ 29 while(cnt[i]--)t+=(char)('a'+i); 30 cnt[i]=0; 31 } 32 a[numa].t=t,t=""; 33 ++numa; 34 } 35 } 36 sort(a,a+numa,cmp1); 37 for(i=0;i<numa;i=j){ 38 string mark=a[i].s; 39 for(j=i+1;;++j){ 40 if(j==numa||a[j].t!=a[i].t)break; 41 if(a[j].s<mark)mark=a[j].s; 42 } 43 for(k=i;k<j;++k)a[k].n=j-i,a[k].m=mark; 44 } 45 sort(a,a+numa,cmp2); 46 for(i=j=0;i<5;++i){ 47 if(j==numa)break; 48 cout<<"Group of size "<<a[j].n<<": "; 49 for(k=0;k<a[j].n;++k)if(a[j+k].s!=a[j+k+1].s) 50 cout<<a[j+k].s<<' '; 51 j+=a[j].n; 52 cout<<".\n"; 53 } 54 return 0; 55 }
E1463 Wine Trading in Gergovia
First AC: 2018-05-06 Latest Modification: 2018-05-06
Note: 要使路程和最小,可以考虑从一端开始遍历,将需求向另一端转嫁
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long n,a,b,rst; 4 int main() 5 { 6 while(cin>>n,n){ 7 cin>>a; 8 rst=0; 9 while(--n)rst+=abs(a),cin>>b,a+=b; 10 cout<<rst<<endl; 11 } 12 return 0; 13 }
E1488 Coin Collector
First AC: 2019-02-04 Latest Modification: 2019-02-04
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,k; 4 int a[500001]; 5 bool jdg[500001]; 6 int sum,cnt; 7 int i; 8 int main() 9 { 10 cin>>n>>k; 11 memset(a,0x3f,sizeof a); 12 for(i=0;i<n;++i){ 13 cin>>a[i]>>jdg[i]; 14 } 15 for(i=0;i<n;++i){ 16 if(!jdg[i]&&sum+a[i]<k&&sum+a[i]<a[i+1]){ 17 sum+=a[i]; 18 ++cnt; 19 } 20 } 21 if(cnt)cout<<cnt<<endl<<k-sum; 22 else cout<<0<<endl<<k-1; 23 return 0; 24 }
E1493 SQUINT
First AC: 2017-10-10 Latest Modification: 2018-03-04
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long m,n; 4 int main() 5 { 6 cin>>n; 7 m=(long long)sqrt(n); 8 if(m*m==n)cout<<m; 9 else cout<<m+1; 10 return 0; 11 }
E1494 Coins
First AC: 2018-05-10 Latest Modification: 2018-05-10
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,rst; 4 int a[101],c[101]; 5 int num[100001]; 6 bool dp[100001]={1}; 7 int i,j; 8 int main() 9 { 10 cin>>n>>m; 11 for(i=0;i<n;++i)cin>>a[i]; 12 for(i=0;i<n;++i)cin>>c[i]; 13 for(i=0;i<n;++i){ 14 memset(num,0,sizeof(num)); 15 for(j=a[i];j<=m;++j){ 16 if(!dp[j]&&dp[j-a[i]]&&num[j-a[i]]<c[i]){ 17 dp[j]=1; 18 ++rst; 19 num[j]=num[j-a[i]]+1; 20 } 21 } 22 } 23 cout<<rst; 24 return 0; 25 }
E1499 Gauss and Fibonacci
First AC: 2018-04-24 Latest Modification: 2018-04-24
Note: Sn=f(n+2)-1
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const ll M=100000000; 5 ll fib[2][2]; 6 ll bas[2][2]; 7 ll n; 8 ll i,j,k; 9 void mul(ll a[][2],ll b[][2],ll n) 10 { 11 ll tmp[2][2]; 12 memset(tmp,0,sizeof(tmp)); 13 for(i=0;i<2;++i) 14 for(j=0;j<2;++j) 15 for(k=0;k<2;++k) 16 tmp[i][j]+=a[i][k]*b[k][j]; 17 for(i=0;i<2;++i) 18 for(j=0;j<2;++j) 19 if(n)fib[i][j]=tmp[i][j]%M; 20 else bas[i][j]=tmp[i][j]%M; 21 } 22 void pow(ll n) 23 { 24 while(n){ 25 if(n&1)mul(fib,bas,1); 26 mul(bas,bas,0); 27 n>>=1; 28 } 29 } 30 int main() 31 { 32 ios::sync_with_stdio(false); 33 while(cin>>n){ 34 fib[0][0]=fib[0][1]=1; 35 fib[1][0]=fib[1][1]=0; 36 bas[0][0]=0; 37 bas[0][1]=bas[1][0]=bas[1][1]=1; 38 pow(n+1); 39 cout<<fib[0][0]-1<<endl; 40 } 41 return 0; 42 }
E1508 Maximum Sum
First AC: 2019-02-20 Latest Modification: 2019-02-25
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,ans; 4 int a[105][105]; 5 int tmp[105]; 6 int dp[105]; 7 int i,j,k; 8 int main() 9 { 10 cin>>n; 11 for(i=1;i<=n;++i){ 12 for(j=1;j<=n;++j){ 13 cin>>a[i][j]; 14 } 15 } 16 ans=-128; 17 for(i=0;i<n;++i){ 18 memset(tmp,0,sizeof(tmp)); 19 for(j=i+1;j<=n;++j){ 20 int mina=0; 21 memset(dp,0,sizeof dp); 22 for(k=1;k<=n;++k){ 23 tmp[k]+=a[j][k]; 24 dp[k]=dp[k-1]+tmp[k]; 25 ans=max(ans,dp[k]-mina); 26 mina=min(mina,dp[k]); 27 } 28 } 29 } 30 cout<<ans; 31 return 0; 32 }
E1524 Following Orders
First AC: 2018-06-12 Latest Modification: 2018-06-12
1 #include<bits/stdc++.h> 2 using namespace std; 3 char x,y; 4 int num; 5 bool e[26][26]; 6 int cnt[26]; 7 bool jdg[26]; 8 int rst[26]; 9 void dfs(int n) 10 { 11 if(n==num){ 12 for(int i=0;i<num;++i)cout<<(char)(rst[i]+'a'); 13 cout<<endl; 14 return; 15 } 16 int i,j; 17 for(i=0;i<26;++i){ 18 if(!cnt[i]&&jdg[i]){ 19 rst[n]=i; 20 for(j=0;j<26;++j) 21 if(e[i][j]&&jdg[j]) 22 --cnt[j]; 23 jdg[i]=0; 24 dfs(n+1); 25 jdg[i]=1; 26 for(j=0;j<26;++j) 27 if(e[i][j]&&jdg[j]) 28 ++cnt[j]; 29 } 30 } 31 } 32 int main() 33 { 34 while(cin>>x){ 35 num=0; 36 memset(e,0,sizeof(e)); 37 memset(cnt,0,sizeof(cnt)); 38 memset(jdg,0,sizeof(jdg)); 39 ++num; 40 jdg[x-'a']=1; 41 while(cin>>x){ 42 ++num; 43 jdg[x-'a']=1; 44 if(getchar()!=' ')break; 45 } 46 while(cin>>x>>y){ 47 e[x-'a'][y-'a']=1; 48 ++cnt[y-'a']; 49 if(getchar()!=' ')break; 50 } 51 dfs(0); 52 cout<<endl; 53 } 54 return 0; 55 }
E1533 The Dole Queue
First AC: 2018-03-08 Latest Modification: 2018-03-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,k,m,num; 4 bool a[25]; 5 int tmp1,tmp2,cnt; 6 int i,j; 7 int main() 8 { 9 while(cin>>n>>k>>m,n||k||m){ 10 memset(a,1,sizeof(a)); 11 num=n,tmp1=1,tmp2=n,cnt=0; 12 while(1){ 13 cnt=k; 14 while(1){ 15 if(a[tmp1])--cnt; 16 if(!cnt)break; 17 tmp1=tmp1%n+1; 18 } 19 cnt=m; 20 while(1){ 21 if(a[tmp2])--cnt; 22 if(!cnt)break; 23 tmp2=(tmp2+n-2)%n+1; 24 } 25 if(tmp1!=tmp2){ 26 num-=2; 27 printf("%3d%3d",tmp1,tmp2); 28 a[tmp1]=a[tmp2]=0; 29 } 30 else{ 31 num-=1; 32 printf("%3d",tmp1); 33 a[tmp1]=0; 34 } 35 if(num)cout<<','; 36 else{ 37 cout<<'\n'; 38 break; 39 } 40 } 41 } 42 return 0; 43 }
E1536 Ugly Numbers
First AC: 2017-12-29 Latest Modification: 2017-12-29
Note: 见E1277
1 #include<iostream> 2 using namespace std; 3 long long a[1501]={0,1}; 4 long long m2=1,m3=1,m5=1,tmp; 5 int i,n; 6 string s; 7 long long minm(long long a,long long b,long long c) 8 { 9 long long m=a<b? a:b; 10 long long n=c<m? c:m; 11 return n; 12 } 13 int main() 14 { 15 for(i=1;i<1501;){ 16 tmp=minm(a[m2]*2,a[m3]*3,a[m5]*5); 17 if(tmp==a[m2]*2)++m2; 18 if(tmp==a[m3]*3)++m3; 19 if(tmp==a[m5]*5)++m5; 20 a[++i]=tmp; 21 } 22 cout<<"The 1500'th ugly number is "<<a[1500]<<'.'; 23 return 0; 24 }
E1591 Intersection
First AC: 2018-05-11 Latest Modification: 2018-05-11
Note: 给出的对角线顶点不一定是左上角和右下角
线段和矩形有交点,当且仅当和某边有交点或全在矩形内
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,xl,yl,xr,yr,xu,yu,xd,yd; 4 double k,b,x,y; 5 bool hori,vert,jdg; 6 int main() 7 { 8 cin>>n; 9 while(n--){ 10 cin>>xl>>yl>>xr>>yr>>xu>>yu>>xd>>yd; 11 hori= (yl==yr? 1:0); 12 vert= (xl==xr? 1:0); 13 if(vert)b=xl; 14 else k=(yr-yl)*1.0/(xr-xl),b=yl-k*xl; 15 jdg=0; 16 if(hori){ 17 if((b-yu)*(b-yd)<=0){ 18 int minx=min(xu,xd),maxx=max(xu,xd); 19 if( !( (xl<minx&&xr<minx)||(xl>maxx&&xr>maxx) ) ) 20 jdg=1; 21 } 22 } 23 else if(vert){ 24 if((b-xu)*(b-xd)<=0){ 25 int miny=min(yu,yd),maxy=max(yu,yd); 26 if( !( (yl<miny&&yr<miny)||(yl>maxy&&yr>maxy) ) ) 27 jdg=1; 28 } 29 } 30 else{ 31 y=k*xu+b; 32 if((y-yl)*(y-yr)<=0&&(y-yu)*(y-yd)<=0)jdg=1; 33 y=k*xd+b; 34 if((y-yl)*(y-yr)<=0&&(y-yu)*(y-yd)<=0)jdg=1; 35 x=(yu-b)/k; 36 if((x-xl)*(x-xr)<=0&&(x-xu)*(x-xd)<=0)jdg=1; 37 x=(yd-b)/k; 38 if((x-xl)*(x-xr)<=0&&(x-xu)*(x-xd)<=0)jdg=1; 39 if((xl-xu)*(xl-xd)<0&&(yl-yu)*(yl-yd)<0) 40 if((xr-xu)*(xr-xd)<0&&(yr-yu)*(yr-yd)<0) 41 jdg=1; 42 } 43 jdg? cout<<"T\n":cout<<"F\n"; 44 } 45 return 0; 46 }
E1603 Sir Bedavere’s Bogus Division Solutions
First AC: 2018-03-04 Latest Modification: 2018-03-04
1 #include<bits/stdc++.h> 2 using namespace std; 3 int i,j; 4 int main() 5 { 6 for(i=100;i<1000;++i)for(j=100;j<1000;++j) 7 if(i==j)continue; 8 else if(i%10==j/100&&i/10*j==j%100*i) 9 printf("%d / %d = %d / %d\n",i,j,i/10,j%100); 10 return 0; 11 }
E1605 Taunt Exposure Estimation
First AC: 2018-03-05 Latest Modification: 2018-03-05
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 double x,y,a,b,s; 5 int main() 6 { 7 cin>>n; 8 scanf("%lf,%lf",&x,&y); 9 printf("%.2lf to ",x); 10 while(--n){ 11 scanf("%lf, %lf",&a,&b); 12 s+=(y+b)*(a-x)/2.0; 13 x=a,y=b; 14 } 15 printf("%.2lf: %.4lf",a,s); 16 return 0; 17 }
E1607 Nested Shrubbery Boxes
First AC: 2018-03-05 Latest Modification: 2018-03-05
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,rst; 4 int d[3]; 5 char c; 6 struct data{ 7 int x,y,z; 8 }a[500]; 9 int i,j; 10 bool cmp(data a,data b) 11 { 12 if(a.x!=b.x)return a.x<b.x; 13 if(a.y!=b.y)return a.y<b.y; 14 return a.z<b.z; 15 } 16 int main() 17 { 18 while(cin>>n,n+1){ 19 if(n==0){cout<<"0\n";continue;} 20 for(i=0;i<n;++i){ 21 cin>>d[0]>>c>>d[1]>>c>>d[2]; 22 sort(d,d+3); 23 a[i].x=d[0],a[i].y=d[1],a[i].z=d[2]; 24 } 25 sort(a,a+n,cmp); 26 rst=1; 27 for(i=1;i<n;++i){ 28 if(a[i].x>a[i-1].x&&a[i].y>a[i-1].y&&a[i].z>a[i-1].z) 29 ++rst; 30 else break; 31 } 32 cout<<rst<<endl; 33 } 34 return 0; 35 }
E1627 Binary Code
First AC: 2018-03-07 Latest Modification: 2018-03-07
Note: 首先每行、每列都是b1,b2,…,bn的一个排列,因此0,1的个数相等
而矩阵按行字典序排列,表明首位为0的行都在首位为1的行上方
故根据输入的最后一列,可以得到第一列
现考虑所有首位为0的行在矩阵中从上到下分别是s1,s2,…,sm,记si=’0’+ti(i=1,2,…,m)
则所有si移位一次后得到的ti+’0’一一对应矩阵中所有末位为0的行
注意到这次移位并没有改变s1,…,sm字典序的先后
所以从上到下第j个首位为0的行,移位后得到从上到下第j个末位为0的列
首位为1的行同理,这样我们就能得到排序前的矩阵第一列
注意到排序前矩阵第一行第i元经过i-1次移位恰为排序前第i行首位
所以前面得到的排序前第一列,就是所求的第一行
如果第一行不是第一列的一个排列,表明原数据无解,反之亦然
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,cnt,tmp; 4 string s,t; 5 bool fst[20000]; 6 bool lst[20000]; 7 int nxt[20000]; 8 int i,j; 9 int main() 10 { 11 cin>>n; 12 for(i=0;i<n;++i){ 13 cin>>lst[i]; 14 if(lst[i])s+='1'; 15 else s+='0',++cnt; 16 } 17 for(i=cnt;i<n;++i)fst[i]=1; 18 for(i=0;i<n;++i)if(!lst[i])nxt[j++]=i; 19 for(i=0;i<n;++i)if(lst[i])nxt[j++]=i; 20 for(i=0;i<n;++i){ 21 t+=fst[tmp]? '1':'0'; 22 tmp=nxt[tmp]; 23 } 24 for(i=0;i<n;++i)if(t[i]=='0')--cnt; 25 if(cnt){cout<<"-1";return 0;} 26 cout<<t[0]; 27 for(i=1;i<n;++i)cout<<' '<<t[i]; 28 return 0; 29 }
E1631 Team Arrangement
First AC: 2018-03-08 Latest Modification: 2018-03-08
Note: 注意captain从选出的11人中产生,而非从整个team中产生
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,yl,yr; 4 struct data{ 5 int num,year,tmp; 6 string name; 7 char role; 8 }a[22],b[11]; 9 string s; 10 int len,tmp; 11 int num[4],jdg[10]; 12 int i,j,k; 13 int find(char c) 14 { 15 if(c=='G')return 0; 16 if(c=='D')return 1; 17 if(c=='M')return 2; 18 return 3; 19 } 20 bool cmp(data a,data b) 21 { 22 if(a.tmp!=b.tmp)return a.tmp<b.tmp; 23 return a.num<b.num; 24 } 25 int main() 26 { 27 while(cin>>n,n){ 28 memset(num,0,sizeof(num)); 29 a[0].num=n,a[0].year=0; 30 cin>>a[0].name>>a[0].role; 31 a[0].tmp=find(a[0].role); 32 ++num[a[0].tmp]; 33 while(scanf("%d-%d",&yl,&yr)){ 34 a[0].year+=yr-yl+1; 35 if(getchar()!=' ')break; 36 } 37 for(i=1;i<22;++i){ 38 cin>>a[i].num>>a[i].name>>a[i].role; 39 a[i].year=0,a[i].tmp=find(a[i].role); 40 ++num[a[i].tmp]; 41 while(scanf("%d-%d",&yl,&yr)){ 42 a[i].year+=yr-yl+1; 43 if(getchar()!=' ')break; 44 } 45 } 46 sort(a,a+22,cmp); 47 cin>>s; 48 len=s.length(),tmp=0; 49 for(i=j=0;i<len;++i){ 50 if(s[i]=='-')jdg[++j]=tmp,tmp=0; 51 else tmp=10*tmp+s[i]-'0'; 52 } 53 jdg[0]=1,jdg[++j]=tmp; 54 if(j!=3||jdg[1]<1||jdg[2]<1||jdg[3]<1) 55 cout<<"IMPOSSIBLE TO ARRANGE\n"; 56 else if(jdg[0]+jdg[1]+jdg[2]+jdg[3]!=11) 57 cout<<"IMPOSSIBLE TO ARRANGE\n"; 58 else if(jdg[0]>num[0]||jdg[1]>num[1]|| 59 jdg[2]>num[2]||jdg[3]>num[3]) 60 cout<<"IMPOSSIBLE TO ARRANGE\n"; 61 else{ 62 k=0; 63 for(i=0;jdg[0]--;++i)b[k++]=a[i]; 64 for(j=i;;++j)if(a[j].tmp!=0)break; 65 for(i=j;jdg[1]--;++i)b[k++]=a[i]; 66 for(j=i;;++j)if(a[j].tmp!=1)break; 67 for(i=j;jdg[2]--;++i)b[k++]=a[i]; 68 for(j=i;;++j)if(a[j].tmp!=2)break; 69 for(i=j;jdg[3]--;++i)b[k++]=a[i]; 70 int mst=b[0].year,item=0; 71 for(i=1;i<11;++i) 72 if(b[i].year>mst||(b[i].year==mst 73 &&b[i].num>b[item].num)) 74 mst=b[i].year,item=i; 75 cout<<b[item].num<<' '<<b[item].name 76 <<' '<<b[item].role<<endl; 77 for(i=0;i<item;++i) 78 cout<<b[i].num<<' '<<b[i].name 79 <<' '<<b[i].role<<endl; 80 for(i=item+1;i<11;++i) 81 cout<<b[i].num<<' '<<b[i].name 82 <<' '<<b[i].role<<endl; 83 } 84 cout<<endl; 85 } 86 return 0; 87 }
E1632 Barbara Bennett
First AC: 2018-03-08 Latest Modification: 2018-03-08
Note: 第一次遍历数问号数,第二次遍历分类累加,利用排列组合思想即可
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s,t; 4 int len,cnt; 5 long long rst; 6 bool jdg; 7 int i; 8 int main() 9 { 10 while(cin>>s,s!="#"){ 11 cin>>t; 12 len=s.length(); 13 cnt=0,rst=0,jdg=1; 14 for(i=0;i<len;++i)if(s[i]=='?')++cnt; 15 for(i=0;i<len;++i){ 16 if(s[i]=='?'){ 17 --cnt; 18 rst+=('9'-t[i])*pow(10,cnt); 19 } 20 else if(s[i]>t[i]){ 21 rst+=pow(10,cnt); 22 break; 23 } 24 else if(s[i]<t[i])break; 25 } 26 cout<<rst<<endl; 27 } 28 return 0; 29 }
E1645 Printer Queue
First AC: 2017-12-13 Latest Modification: 2017-12-13
1 #include<iostream> 2 using namespace std; 3 int T,n,m,M,tmp,cnt,num; 4 int a[105]; 5 int i,j; 6 int main() 7 { 8 cin>>T; 9 while(T--){ 10 cin>>n>>m; 11 for(tmp=-1,cnt=i=0;i<n;++i)cin>>a[i]; 12 while(1){ 13 for(M=num=0,i=tmp+1;num++<n;++i){ 14 j=i<n? i:i-n; 15 if(a[j]>M)M=a[j],tmp=j; 16 } 17 if(tmp==m){cout<<cnt+1<<endl;break;} 18 else ++cnt,a[tmp]=0; 19 } 20 } 21 return 0; 22 }
E1646 Prime Path
First AC: 2019-01-03 Latest Modification: 2019-01-03
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,a,b; 4 int x,y,cnt,rst; 5 bool pri[10000]; 6 bool tmp[10000]; 7 queue<int>q,p,emp; 8 int i,j; 9 int main() 10 { 11 for(i=2;i<5000;++i) 12 if(!pri[i]) 13 for(j=2*i;j<10000;j+=i) 14 pri[j]=1; 15 cin>>T; 16 while(T--){ 17 cin>>a>>b; 18 for(i=1000;i<10000;++i)tmp[i]=pri[i]; 19 cnt=0; 20 rst=-1; 21 tmp[a]=1; 22 q=emp; 23 p=emp; 24 q.push(a); 25 while(1){ 26 if(q.empty()&&p.empty())break; 27 else if(q.empty())q=p,p=emp,++cnt; 28 x=q.front(); 29 if(x==b){ 30 rst=cnt; 31 break; 32 } 33 q.pop(); 34 y=x-x/1000*1000; 35 for(i=1000;i<=9000;i+=1000){ 36 if(!tmp[y+i])tmp[y+i]=1,p.push(y+i); 37 } 38 y=x-x%1000/100*100; 39 for(i=0;i<=900;i+=100){ 40 if(!tmp[y+i])tmp[y+i]=1,p.push(y+i); 41 } 42 y=x-x%100/10*10; 43 for(i=0;i<=90;i+=10){ 44 if(!tmp[y+i])tmp[y+i]=1,p.push(y+i); 45 } 46 y=x-x%10; 47 for(i=0;i<=9;++i){ 48 if(!tmp[y+i])tmp[y+i]=1,p.push(y+i); 49 } 50 } 51 if(rst<0)cout<<"Impossible\n"; 52 else cout<<rst<<endl; 53 } 54 return 0; 55 }
E1673 GauB in Elementary School
First AC: 2018-03-08 Latest Modification: 2018-03-08
1 #include<iostream> 2 using namespace std; 3 int T,i; 4 long long n,m; 5 int main() 6 { 7 cin>>T; 8 ++T; 9 for(i=1;i<T;++i){ 10 cin>>n>>m; 11 cout<<"Scenario #"<<i<<":\n"; 12 if((n+m)&1)cout<<(m-n+1)/2*(n+m); 13 else cout<<(n+m)/2*(m-n+1); 14 cout<<"\n\n"; 15 } 16 return 0; 17 }
E1677 Nasty Hacks
First AC: 2018-01-06 Latest Modification: 2018-01-06
1 #include<iostream> 2 using namespace std; 3 int a,b,c; 4 int main() 5 { 6 cin>>a; 7 while(cin>>a>>b>>c){ 8 if(b-a>c)cout<<"advertise\n"; 9 else if(b-a<c)cout<<"do not advertise\n"; 10 else cout<<"does not matter\n"; 11 } 12 return 0; 13 }
E1679 Card Trick
First AC: 2018-03-09 Latest Modification: 2018-03-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,tmp,cnt; 4 int a[14]; 5 int i,j; 6 int main() 7 { 8 cin>>T; 9 while(T--){ 10 cin>>n; 11 memset(a,0,sizeof(a)); 12 tmp=0; 13 for(i=2;i<n+2;++i){ 14 for(j=0;j<i;++j){ 15 do{tmp=tmp%n+1;}while(a[tmp]); 16 } 17 a[tmp]=i-1; 18 } 19 for(i=1;i<n;++i)cout<<a[i]<<' '; 20 cout<<a[n]<<endl; 21 } 22 return 0; 23 }
E1688 接新生
First AC: 2018-04-19 Latest Modification: 2018-04-19
Note: 模拟水题
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll n,tmpx,tmpy,tmpd,x,y,d; 5 struct data{ 6 ll x,y,w; 7 }a[100]; 8 ll i,j; 9 int main() 10 { 11 cin>>n; 12 for(i=0;i<n;++i)cin>>a[i].x>>a[i].y>>a[i].w; 13 d=1e8; 14 for(i=0;i<n;++i){ 15 tmpx=a[i].x; 16 tmpy=a[i].y; 17 tmpd=0; 18 for(j=0;j<n;++j) 19 tmpd+=a[j].w*(abs(tmpx-a[j].x)+abs(tmpy-a[j].y)); 20 if(tmpd<d)x=tmpx,y=tmpy,d=tmpd; 21 } 22 cout<<x<<' '<<y<<' '<<d; 23 return 0; 24 }
E1690 子序列
First AC: 2018-05-02 Latest Modification: 2018-05-02
Note: 先在原数列左边找最少的连续项使得和超过S
然后依次往右添一项,同时在最左端删尽可能多的项,比较长度
遍历以后,得到的就是最短的项数
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll n,s,tmp,rst; 5 ll a[100001]; 6 ll i,j; 7 int main() 8 { 9 ios::sync_with_stdio(false); 10 cin>>n>>s; 11 for(i=1;i<=n;++i)cin>>a[i]; 12 i=1,j=1; 13 while(tmp<=s)tmp+=a[j++]; 14 rst=--j-i+1; 15 while(j!=n){ 16 tmp-=a[i++]; 17 while(j<n&&tmp<=s)tmp+=a[++j]; 18 rst=min(rst,j-i+1); 19 } 20 while(tmp-a[i]>s)tmp-=a[i++]; 21 rst=min(rst,j-i+1); 22 cout<<rst; 23 return 0; 24 }
E1693 StuPId
First AC: 2018-03-09 Latest Modification: 2018-03-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len,tmp,sums; 4 string s; 5 int a[]={7,3,9},b[]={9,7,3}; 6 int i,j; 7 int main() 8 { 9 cin>>T; 10 for(j=1,++T;j<T;++j){ 11 cin>>s; 12 len=s.length(); 13 sums=0; 14 if(len==6){ 15 for(i=0;i<6;++i){ 16 if(s[i]!='?')sums+=a[i%3]*(s[i]-'0'); 17 else tmp=i; 18 } 19 for(i=0;;++i){ 20 if((i*a[tmp%3]+sums)%10)continue; 21 s[tmp]=(char)(i+'0'); 22 break; 23 } 24 } 25 else{ 26 for(i=0;i<7;++i){ 27 if(s[i]!='?')sums+=b[i%3]*(s[i]-'0'); 28 else tmp=i; 29 } 30 for(i=0;;++i){ 31 if((i*b[tmp%3]+sums)%10)continue; 32 s[tmp]=(char)(i+'0'); 33 break; 34 } 35 } 36 cout<<"Scenario #"<<j<<":\n"<<s<<"\n\n"; 37 } 38 return 0; 39 }
E1696 Nasty Hacks
First AC: 2018-03-09 Latest Modification: 2018-03-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,i; 4 int main() 5 { 6 cin>>T; 7 ++T; 8 for(i=1;i<T;++i){ 9 cin>>n; 10 printf("%d %d QUARTER(S), %d DIME(S), %d NICKEL(S), %d PENNY(S)\n", 11 i,n/25,n%25/10,n%25%10/5,n%5); 12 } 13 return 0; 14 }
E1697 Triangular Sums
First AC: 2018-03-09 Latest Modification: 2018-03-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long T,n,i; 4 int main() 5 { 6 cin>>T; 7 ++T; 8 for(i=1;i<T;++i){ 9 cin>>n; 10 printf("%lld %lld %lld\n",i,n,n*(n+1)*(n+2)*(n+3)/8); 11 } 12 return 0; 13 }
E1706 Parsing Real Numbers
First AC: 2018-03-09 Latest Modification: 2018-03-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,lens,lent,pose,nump; 4 string s,t; 5 bool rst; 6 int i,j; 7 int main() 8 { 9 cin>>T; 10 getchar(); 11 while(T--){ 12 getline(cin,s); 13 lens=s.length(); 14 for(i=0,pose=0;i<lens;++i){ 15 if(s[i]=='e'||s[i]=='E'){pose=i;break;} 16 } 17 rst=0; 18 if(pose){ 19 lent=(t=s.substr(0,pose)).length(); 20 while(t[0]==' ')t=t.substr(1,lent--); 21 while(t[lent-1]==' ')t=t.substr(0,--lent); 22 if(t[0]=='+'||t[0]=='-')t=t.substr(1,lent--); 23 nump=0; 24 for(i=0;i<lent;++i){ 25 if(t[i]=='.'){ 26 if(i==0||i==lent-1){rst=1;break;} 27 else ++nump; 28 } 29 else if(t[i]<'0'||t[i]>'9'){rst=1;break;} 30 } 31 if(nump>1)rst=1; 32 if(!rst){ 33 lent=(t=s.substr(pose+1,lens)).length(); 34 while(t[0]==' ')t=t.substr(1,lent--); 35 while(t[lent-1]==' ')t=t.substr(0,--lent); 36 if(t[0]=='+'||t[0]=='-')t=t.substr(1,lent--); 37 for(i=0;i<lent;++i) 38 if(t[i]<'0'||t[i]>'9'){rst=1;break;} 39 } 40 } 41 else{ 42 while(s[0]==' ')s=s.substr(1,lens--); 43 while(s[lens-1]==' ')s=s.substr(0,--lens); 44 if(s[0]=='+'||s[0]=='-')s=s.substr(1,lens--); 45 nump=0; 46 for(i=0;i<lens;++i){ 47 if(s[i]=='.'){ 48 if(i==0||i==lens-1){rst=1;break;} 49 else ++nump; 50 } 51 else if(s[i]<'0'||s[i]>'9'){rst=1;break;} 52 } 53 if(nump>1)rst=1; 54 } 55 if(rst)cout<<"IL"; 56 cout<<"LEGAL\n"; 57 } 58 return 0; 59 }
E1711 Expression Evaluator
First AC: 2018-03-09 Latest Modification: 2018-03-09
Note: 注意输入可能包含空格,同时输出的expression包括这些空格
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len,tmp,rst; 4 string s,t; 5 int a[26]; 6 queue<int>q; 7 queue<char>flag; 8 int i; 9 int main() 10 { 11 cin>>T; 12 getchar(); 13 while(T--){ 14 getline(cin,t); 15 cout<<"Expression: "<<t<<endl; 16 len=t.length(); 17 s=""; 18 for(i=0;i<len;++i)if(t[i]!=' ')s+=t[i]; 19 len=s.length(); 20 memset(a,-1,sizeof(a)); 21 tmp=0; 22 for(i=0;i<len;++i){ 23 if(s[i]!='+'&&s[i]!='-'){ 24 a[s[i]-'a']=s[i]-'a'+1; 25 if(tmp==1) 26 tmp=0,q.push(++a[s[i]-'a']); 27 else if(tmp) 28 tmp=0,q.push(--a[s[i]-'a']); 29 else{ 30 q.push(a[s[i]-'a']); 31 if(i!=len-1&&s[i+1]==s[i+2]){ 32 if(s[i+1]=='+')++a[s[i]-'a']; 33 else --a[s[i]-'a']; 34 i+=2; 35 } 36 } 37 } 38 else{ 39 if(s[i+1]==s[i])tmp=s[++i]=='+'? 1:-1; 40 else flag.push(s[i]);; 41 } 42 } 43 rst=q.front(),q.pop(); 44 while(!flag.empty()&&!q.empty()){ 45 char c=flag.front(); 46 if(c=='+')rst+=q.front(); 47 else rst-=q.front(); 48 q.pop(),flag.pop(); 49 } 50 cout<<"value = "<<rst<<endl; 51 for(i=0;i<26;++i)if(a[i]!=-1) 52 cout<<(char)(i+'a')<<" = "<<a[i]<<endl; 53 } 54 return 0; 55 }
E1724 Circle Intersection
First AC: 2018-03-10 Latest Modification: 2018-03-10
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T; 4 double xa,ya,ra,xb,yb,rb,dis,x,y; 5 void solve(double A,double B,double C, 6 double x1,double y1,double r1) 7 { 8 if(fabs(A)>0.0000001){ 9 double a=A*A+B*B; 10 double b=2*B*(A*x1-C)-2*A*A*y1; 11 double c=A*A*(y1*y1-r1*r1)+pow(A*x1-C,2); 12 double delta=sqrt(b*b-4*a*c); 13 double yl=(-b-delta)/2/a; 14 double yr=(-b+delta)/2/a; 15 double xl=(C-B*yl)/A,xr=(C-B*yr)/A; 16 if(fabs(xl-xr)<0.0000001&&yl>yr||xl>xr) 17 a=xl,xl=xr,xr=a, 18 a=yl,yl=yr,yr=a; 19 printf("%.3f %.3f\n",(C-B*yl)/A,yl); 20 printf("%.3f %.3f\n",(C-B*yr)/A,yr); 21 } 22 else{ 23 double a=A*A+B*B; 24 double b=2*A*(B*y1-C)-2*B*B*x1; 25 double c=B*B*(x1*x1-r1*r1)+pow(B*y1-C,2); 26 double delta=sqrt(b*b-4*a*c); 27 double xl=(-b-delta)/2/a; 28 double xr=(-b+delta)/2/a; 29 double yl=(C-A*xl)/B,yr=(C-A*xr)/B; 30 if(fabs(xl-xr)<0.0000001&&yl>yr||xl>xr) 31 a=xl,xl=xr,xr=a, 32 a=yl,yl=yr,yr=a; 33 printf("%.3f %.3f\n",xl,(C-A*xl)/B); 34 printf("%.3f %.3f\n",xr,(C-A*xr)/B); 35 } 36 } 37 int main() 38 { 39 cin>>T; 40 while(T--){ 41 cin>>xa>>ya>>ra>>xb>>yb>>rb; 42 dis=sqrt(pow(xa-xb,2)+pow(ya-yb,2)); 43 if(dis>ra+rb)cout<<"Outside!\n"; 44 else if(fabs(ra+rb-dis)<0.0000001){ 45 cout<<"Excricle!\n"; 46 if(xa<xb)x=xa+(xb-xa)*ra/(ra+rb); 47 else x=xb+(xa-xb)*rb/(ra+rb); 48 if(ya<yb)y=ya+(yb-ya)*ra/(ra+rb); 49 else y=yb+(ya-yb)*rb/(ra+rb); 50 printf("%.3f %.3f\n",x,y); 51 } 52 else if(fabs(fabs(ra-rb)-dis)<0.0000001){ 53 cout<<"Inscribe!\n"; 54 if(ra>rb){ 55 x=xa+(xb-xa)*ra/(ra-rb); 56 y=ya+(yb-ya)*ra/(ra-rb); 57 } 58 else{ 59 x=xb-(xb-xa)*rb/(rb-ra); 60 y=yb-(yb-ya)*rb/(rb-ra); 61 } 62 printf("%.3f %.3f\n",x,y); 63 } 64 else if(dis<fabs(ra-rb))cout<<"Embedde!\n"; 65 else{ 66 cout<<"Intersection!\n"; 67 double A=2*(xb-xa),B=2*(yb-ya); 68 double C=ra*ra-rb*rb-xa*xa-ya*ya+xb*xb+yb*yb; 69 solve(A,B,C,xa,ya,ra); 70 } 71 cout<<endl; 72 } 73 return 0; 74 }
E1731 Educational Journey
First AC: 2018-03-10 Latest Modification: 2018-03-10
Note: 输入数据为t1,t2,t3,t4,t5
从AC相遇到结束:vA(t3-t1)=vC(t4-t1)
从AM相遇到结束:vA(t3-t2)=vM(t5-t2)
两式相比得vC/vM=(t5-t2)(t3-t1)/[(t4-t1)(t3-t2)],设之为Tup/Tdn①
设CM在t时刻相遇,则C→M追及路程为(t-t1)(vC-vM)
而追及路程也是全程C,M路程差,即vC(t4-t1)-vM(t5-t1)
两式联立得vC/vM=(t-t5)/(t-t4)②
由①②解得t=(t4Tup-t5Tdn)/(Tup-Tdn)
注意t的存储需要long long范围,最后四舍五入输出
1 #include<bits/stdc++.h> 2 using namespace std; 3 char c; 4 int h1,m1,s1,h2,m2,s2,h3,m3,s3,h4,m4,s4,h5,m5,s5; 5 long long t1,t2,t3,t4,t5,Tup,Tdn,t; 6 int main() 7 { 8 while((c=getchar())!='-'){ 9 h1=10*(c-'0')+getchar()-'0',getchar(); 10 m1=10*getchar()+getchar()-11*'0',getchar(); 11 s1=10*getchar()+getchar()-11*'0',getchar(); 12 h2=10*getchar()+getchar()-11*'0',getchar(); 13 m2=10*getchar()+getchar()-11*'0',getchar(); 14 s2=10*getchar()+getchar()-11*'0',getchar(); 15 h3=10*getchar()+getchar()-11*'0',getchar(); 16 m3=10*getchar()+getchar()-11*'0',getchar(); 17 s3=10*getchar()+getchar()-11*'0',getchar(); 18 h4=10*getchar()+getchar()-11*'0',getchar(); 19 m4=10*getchar()+getchar()-11*'0',getchar(); 20 s4=10*getchar()+getchar()-11*'0',getchar(); 21 h5=10*getchar()+getchar()-11*'0',getchar(); 22 m5=10*getchar()+getchar()-11*'0',getchar(); 23 s5=10*getchar()+getchar()-11*'0',getchar(); 24 t1=3600*h1+60*m1+s1; 25 t2=3600*h2+60*m2+s2; 26 t3=3600*h3+60*m3+s3; 27 t4=3600*h4+60*m4+s4; 28 t5=3600*h5+60*m5+s5; 29 Tup=(t5-t2)*(t3-t1),Tdn=(t3-t2)*(t4-t1); 30 t=(long long)((Tup*t4-Tdn*t5)*1.0/(Tup-Tdn)+0.5); 31 printf("%02lld:%02lld:%02lld\n",t/3600,t%3600/60,t%60); 32 } 33 return 0; 34 }
E1736 Anti-Blot System
First AC: 2018-03-10 Latest Modification: 2018-03-10
Note: 注意数据类型用unsigned long long才足够
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len,tmp; 4 string s; 5 unsigned long long a,b,c; 6 int i; 7 int main() 8 { 9 cin>>T; 10 while(T--){ 11 a=b=c=0; 12 cin>>s; 13 len=s.length(); 14 for(i=0;i<len;++i){ 15 if(s[i]=='m'){tmp=1;break;} 16 a=10*a+s[i]-'0'; 17 } 18 cin>>s; 19 cin>>s; 20 len=s.length(); 21 for(i=0;i<len;++i){ 22 if(s[i]=='m'){tmp=2;break;} 23 b=10*b+s[i]-'0'; 24 } 25 cin>>s; 26 cin>>s; 27 len=s.length(); 28 for(i=0;i<len;++i){ 29 if(s[i]=='m'){tmp=3;break;} 30 c=10*c+s[i]-'0'; 31 } 32 if(tmp==1)printf("%lld + %lld = %lld\n",c-b,b,c); 33 else if(tmp==2)printf("%lld + %lld = %lld\n",a,c-a,c); 34 else printf("%lld + %lld = %lld\n",a,b,a+b); 35 } 36 return 0; 37 }
E1759 Phone List
First AC: 2018-06-05 Latest Modification: 2018-06-05
Note: 对输入排序后用多维树存,n位电话号码存在第n层
如果搜索到某个前缀已经被存过,那么输出NO,否则输出YES
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n; 4 string s[10000]; 5 bool flag; 6 int i; 7 struct MT{ 8 bool jdg; 9 struct MT *nxt[10]; 10 }; 11 bool cmp(string a,string b) 12 { 13 if(a.length()==b.length())return a<b; 14 return a.length()<b.length(); 15 } 16 void insert(string s,struct MT *h) 17 { 18 struct MT *tmp=h; 19 int len=s.length(),num; 20 for(int i=0;i<len;++i){ 21 num=s[i]-'0'; 22 if(tmp&&tmp->jdg){ 23 flag=0; 24 return; 25 } 26 if(tmp->nxt[num]==NULL){ 27 tmp->nxt[num]=new struct MT; 28 for(int j=0;j<10;++j)tmp->nxt[num]->nxt[j]=NULL; 29 tmp->nxt[num]->jdg=0; 30 } 31 tmp=tmp->nxt[num]; 32 } 33 tmp->jdg=1; 34 } 35 int main() 36 { 37 cin>>T; 38 while(T--){ 39 cin>>n; 40 for(i=0;i<n;++i)cin>>s[i]; 41 sort(s,s+n,cmp); 42 flag=1; 43 struct MT *h=new struct MT; 44 h->jdg=0; 45 for(i=0;i<10;++i)h->nxt[i]=NULL; 46 for(i=0;flag&&i<n;++i)insert(s[i],h); 47 flag? cout<<"YES\n":cout<<"NO\n"; 48 } 49 return 0; 50 }
E1761 Optimal Parking
First AC: 2018-03-12 Latest Modification: 2018-03-12
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,M,m,tmp; 4 int main() 5 { 6 cin>>T; 7 while(T--){ 8 cin>>n; 9 cin>>tmp; 10 M=m=tmp; 11 while(--n){ 12 cin>>tmp; 13 if(tmp>M)M=tmp; 14 else if(tmp<m)m=tmp; 15 } 16 cout<<2*(M-m)<<endl; 17 } 18 return 0; 19 }
E1765 Nested Dolls
First AC: 2018-03-12 Latest Modification: 2018-03-12
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,rst,cnt,w,h; 4 struct data{ 5 int w,h; 6 }a[20001]; 7 bool jdg[20001]; 8 int i,j; 9 bool cmp(data a,data b) 10 { 11 if(a.w-b.w)return a.w<b.w; 12 return a.h>b.h; 13 } 14 int main() 15 { 16 cin>>T; 17 while(T--){ 18 cin>>n; 19 for(i=0;i<n;++i)cin>>a[i].w>>a[i].h; 20 sort(a,a+n,cmp); 21 memset(jdg,1,sizeof(jdg)); 22 rst=0,cnt=n; 23 while(cnt){ 24 for(i=0;;++i)if(jdg[i])break; 25 jdg[i]=0,w=a[i].w,h=a[i].h,--cnt,++rst; 26 for(j=i+1;j<n;++j) 27 if(jdg[j]&&a[j].w>w&&a[j].h>h) 28 jdg[j]=0,w=a[j].w,h=a[j].h,--cnt; 29 } 30 cout<<rst<<endl; 31 } 32 return 0; 33 }
E1766 Shopaholic
First AC: 2018-03-12 Latest Modification: 2018-03-12
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n; 4 long long rst; 5 long long p[20000]; 6 int i; 7 bool cmp(int x,int y) 8 { 9 return x>y; 10 } 11 int main() 12 { 13 cin>>T; 14 while(T--){ 15 cin>>n; 16 for(i=0;i<n;++i)cin>>p[i]; 17 sort(p,p+n,cmp); 18 for(rst=0,i=2;i<n;i+=3)rst+=p[i]; 19 cout<<rst<<endl; 20 } 21 return 0; 22 }
E1768 Password in Rectangle
First AC: 2018-03-12 Latest Modification: 2018-03-12
1 #include<bits/stdc++.h> 2 using namespace std; 3 int m,n,len,cnt,tmp; 4 char a[100][100]; 5 string s; 6 int i,j,k; 7 int main() 8 { 9 cin>>m>>n; 10 for(i=0;i<m;++i)for(j=0;j<n;++j){ 11 cin>>a[i][j]; 12 if(a[i][j]>'Z')a[i][j]-=32; 13 } 14 tmp=6; 15 while(tmp--){ 16 cin>>s; 17 len=s.length(); 18 cnt=0; 19 for(i=0;i<len;++i)if(s[i]>'Z')s[i]-=32; 20 for(i=0;i<m;++i)for(j=0;j<=n-len;++j){ 21 if(a[i][j]==s[0]){ 22 ++cnt; 23 for(k=1;k<len;++k) 24 if(s[k]!=a[i][j+k]){ 25 --cnt; 26 break; 27 } 28 } 29 } 30 for(i=0;i<=m-len;++i)for(j=0;j<n;++j){ 31 if(a[i][j]==s[0]){ 32 ++cnt; 33 for(k=1;k<len;++k) 34 if(s[k]!=a[i+k][j]){ 35 --cnt; 36 break; 37 } 38 } 39 } 40 for(i=0;i<m;++i)for(j=len-1;j<n;++j){ 41 if(a[i][j]==s[0]){ 42 ++cnt; 43 for(k=1;k<len;++k) 44 if(s[k]!=a[i][j-k]){ 45 --cnt; 46 break; 47 } 48 } 49 } 50 for(i=len-1;i<m;++i)for(j=0;j<n;++j){ 51 if(a[i][j]==s[0]){ 52 ++cnt; 53 for(k=1;k<len;++k) 54 if(s[k]!=a[i-k][j]){ 55 --cnt; 56 break; 57 } 58 } 59 } 60 for(i=0;i<=m-len;++i)for(j=0;j<=n-len;++j){ 61 if(a[i][j]==s[0]){ 62 ++cnt; 63 for(k=1;k<len;++k) 64 if(s[k]!=a[i+k][j+k]){ 65 --cnt; 66 break; 67 } 68 } 69 } 70 for(i=0;i<=m-len;++i)for(j=len-1;j<n;++j){ 71 if(a[i][j]==s[0]){ 72 ++cnt; 73 for(k=1;k<len;++k) 74 if(s[k]!=a[i+k][j-k]){ 75 --cnt; 76 break; 77 } 78 } 79 } 80 for(i=len-1;i<m;++i)for(j=len-1;j<n;++j){ 81 if(a[i][j]==s[0]){ 82 ++cnt; 83 for(k=1;k<len;++k) 84 if(s[k]!=a[i-k][j-k]){ 85 --cnt; 86 break; 87 } 88 } 89 } 90 for(i=len-1;i<m;++i)for(j=0;j<=n-len;++j){ 91 if(a[i][j]==s[0]){ 92 ++cnt; 93 for(k=1;k<len;++k) 94 if(s[k]!=a[i-k][j+k]){ 95 --cnt; 96 break; 97 } 98 } 99 } 100 cout<<cnt; 101 if(tmp)cout<<' '; 102 } 103 return 0; 104 }
E1769 Way to Escape
First AC: 2019-02-07 Latest Modification: 2019-02-07
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m; 4 int dis[100][100]; 5 int s,e,l; 6 int i,j,k; 7 int main() 8 { 9 cin>>n>>m; 10 memset(dis,0x3f,sizeof dis); 11 for(i=0;i<n;++i)dis[i][i]=0; 12 while(m--){ 13 cin>>s>>e>>l; 14 dis[s][e]=min(dis[s][e],l); 15 dis[e][s]=min(dis[e][s],l); 16 } 17 for(k=0;k<n;++k){ 18 for(i=0;i<n;++i){ 19 for(j=0;j<n;++j){ 20 dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]); 21 } 22 } 23 } 24 cout<<dis[0][1]; 25 return 0; 26 }
E1772 Mine Area
First AC: 2017-11-02 Latest Modification: 2018-03-12
1 #include<bits/stdc++.h> 2 using namespace std; 3 int l,r,c[20][20],n; 4 char a[20][20],b[20][20]; 5 int i,j; 6 int main() 7 { 8 cin>>l>>r,getchar(); 9 for(i=1;i<=l;++i){ 10 for(j=1;j<=r;++j)cin>>a[i][j]; 11 getchar(); 12 } 13 for(i=1;i<=l;++i){ 14 for(j=1;j<=r;++j){ 15 cin>>b[i][j]; 16 if(b[i][j]=='X')++c[i][j]; 17 } 18 getchar(); 19 } 20 for(i=1;i<=l;++i)for(j=1;j<=r;++j) 21 if(a[i][j]-'0'!=c[i-1][j-1]+c[i-1][j]+c[i-1][j+1]+c[i][j-1]+ 22 c[i][j+1]+c[i+1][j-1]+c[i+1][j]+c[i+1][j+1]) 23 cout<<"("<<i-1<<","<<j-1<<")\n",++n; 24 if(n==0)cout<<"All right!"; 25 return 0; 26 }
E1795 Digital Friends
First AC: 2018-03-13 Latest Modification: 2018-03-13
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,ls,lt; 4 string s,t,jdgs,jdgt,tmp; 5 bool jdg[10]; 6 int i,j; 7 void solve() 8 { 9 tmp=""; 10 for(int i=0;i<10;++i)if(jdg[i]) 11 tmp+=(char)(i+'0'),jdg[i]=0; 12 } 13 int main() 14 { 15 cin>>T; 16 while(T--){ 17 cin>>s>>t; 18 ls=s.length(); 19 lt=t.length(); 20 for(i=0;i<ls;++i)jdg[s[i]-'0']=1; 21 solve(),jdgs=tmp; 22 for(i=0;i<lt;++i)jdg[t[i]-'0']=1; 23 solve(),jdgt=tmp; 24 if(jdgs==jdgt){cout<<"friends\n";continue;} 25 bool flag=0; 26 for(i=1;i<ls;++i){ 27 if(s[i-1]=='9'||s[i]=='0')continue; 28 for(j=0;j<i-1;++j)jdg[s[j]-'0']=1; 29 jdg[s[i-1]-'0'+1]=1,jdg[s[i]-'0'-1]=1; 30 for(j=i+1;j<ls;++j)jdg[s[j]-'0']=1; 31 solve(); 32 if(tmp==jdgt){ 33 cout<<"almost friends\n"; 34 flag=1; 35 break; 36 } 37 } 38 if(flag)continue; 39 for(i=1;i<ls;++i){ 40 if(s[i-1]=='0'||s[i]=='9'||(i==1&&s[0]=='1'))continue; 41 for(j=0;j<i-1;++j)jdg[s[j]-'0']=1; 42 jdg[s[i-1]-'0'-1]=1,jdg[s[i]-'0'+1]=1; 43 for(j=i+1;j<ls;++j)jdg[s[j]-'0']=1; 44 solve(); 45 if(tmp==jdgt){ 46 cout<<"almost friends\n"; 47 flag=1; 48 break; 49 } 50 } 51 if(flag)continue; 52 for(i=1;i<lt;++i){ 53 if(t[i-1]=='9'||t[i]=='0')continue; 54 for(j=0;j<i-1;++j)jdg[t[j]-'0']=1; 55 jdg[t[i-1]-'0'+1]=1,jdg[t[i]-'0'-1]=1; 56 for(j=i+1;j<lt;++j)jdg[t[j]-'0']=1; 57 solve(); 58 if(tmp==jdgs){ 59 cout<<"almost friends\n"; 60 flag=1; 61 break; 62 } 63 } 64 if(flag)continue; 65 for(i=1;i<lt;++i){ 66 if(t[i-1]=='0'||t[i]=='9'||(i==1&&t[0]=='1'))continue; 67 for(j=0;j<i-1;++j)jdg[t[j]-'0']=1; 68 jdg[t[i-1]-'0'-1]=1,jdg[t[i]-'0'+1]=1; 69 for(j=i+1;j<lt;++j)jdg[t[j]-'0']=1; 70 solve(); 71 if(tmp==jdgs){ 72 cout<<"almost friends\n"; 73 flag=1; 74 break; 75 } 76 } 77 if(flag)continue; 78 cout<<"nothing\n"; 79 } 80 return 0; 81 }
E1803 Link-list
First AC: 2017-12-29 Latest Modification: 2018-03-13
1 #include<bits/stdc++.h> 2 using namespace std; 3 queue<int>q[2]; 4 int n,tmp,a,b; 5 int i,j,cnt1,cnt2; 6 int main() 7 { 8 cin>>n; 9 for(i=0;i<n;++i)cin>>tmp,q[0].push(tmp); 10 cin>>n; 11 while(n--){ 12 if(cnt1)cnt2=0; 13 else cnt2=1; 14 cin>>a>>b; 15 if(q[cnt1].front()-a)cout<<q[cnt1].front(), 16 q[cnt2].push(q[cnt1].front()),q[cnt1].pop(); 17 else cout<<q[cnt1].front()<<' '<<b, 18 q[cnt2].push(q[cnt1].front()),q[cnt2].push(b),q[cnt1].pop(); 19 while(q[cnt1].size()){ 20 if(q[cnt1].front()-a)cout<<' '<<q[cnt1].front(), 21 q[cnt2].push(q[cnt1].front()),q[cnt1].pop(); 22 else cout<<' '<<q[cnt1].front()<<' '<<b, 23 q[cnt2].push(q[cnt1].front()),q[cnt2].push(b),q[cnt1].pop(); 24 } 25 cout<<endl; 26 cnt1^=cnt2,cnt2^=cnt1,cnt1^=cnt2; 27 } 28 return 0; 29 }
E1816 连通
First AC: 2018-01-05 Latest Modification: 2018-01-05
Note: 并查集
1 #include<iostream> 2 using namespace std; 3 long long n,m,u,v; 4 long long i,j; 5 long long pre[1000001]; 6 int find(int x) 7 { 8 int r=x; 9 while(pre[r]!=r)r=pre[r];//映射至根节点 10 int i=x,j; 11 while(pre[i]!=r)j=pre[i],pre[i]=r,i=j;//迭代次数优化 12 return r; 13 } 14 void join(int x,int y) 15 { 16 int fx=find(x),fy=find(y); 17 if(fx^fy)pre[fx]=fy;//让与x有关的元素与与y有关的元素两两有关 18 } 19 int main() 20 { 21 cin>>n>>m; 22 for(j=1;j<=n;++j)pre[j]=j; 23 long long sum=n-1; 24 for(;i++<m;){ 25 cin>>u>>v; 26 if(find(u)^find(v))join(u,v),--sum; 27 } 28 sum? cout<<"no":cout<<"yes"; 29 return 0; 30 }
E1817 最短路径
First AC: 2018-05-11 Latest Modification: 2019-02-13
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=600,maxm=180000; 4 int n,m,ne; 5 int u,v,val; 6 int h[maxn],dis[maxn],vis[maxn],cnt[maxn]; 7 struct edge{ 8 int to,val,nxt; 9 }e[maxm]; 10 queue<int> q,emp; 11 void insert(int u,int v,int val) 12 { 13 e[++ne].to=v; 14 e[ne].val=val; 15 e[ne].nxt=h[u]; 16 h[u]=ne; 17 } 18 bool spfa(int ask) 19 { 20 memset(dis,0x3f,sizeof dis); 21 dis[ask]=0; 22 memset(vis,0,sizeof vis); 23 vis[ask]=1; 24 memset(cnt,0,sizeof cnt); 25 ++cnt[ask]; 26 q=emp; 27 q.push(ask); 28 while(!q.empty()){ 29 int x=q.front(); 30 q.pop(); 31 vis[x]=0; 32 for(int i=h[x];i;i=e[i].nxt){ 33 int to=e[i].to; 34 if(dis[to]>dis[x]+e[i].val){ 35 dis[to]=dis[x]+e[i].val; 36 if(!vis[to]){ 37 if(++cnt[to]>=n)return 0; 38 vis[to]=1; 39 q.push(to); 40 } 41 } 42 } 43 } 44 return 1; 45 } 46 int main() 47 { 48 cin>>n>>m; 49 while(m--){ 50 cin>>u>>v>>val; 51 insert(u,v,val); 52 } 53 spfa(1); 54 if(dis[n]>1e8)cout<<-1; 55 else cout<<dis[n]; 56 return 0; 57 }
E1818 最短路径2
First AC: 2019-02-13 Latest Modification: 2019-02-13
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m; 4 int u,v,val; 5 int dis[101][101]; 6 int cnt[101][101]; 7 int i,j,k; 8 int main() 9 { 10 memset(dis,0x3f,sizeof dis); 11 memset(cnt,0,sizeof cnt); 12 cin>>n>>m; 13 while(m--){ 14 cin>>u>>v>>val; 15 if(dis[u][v]==val)++cnt[u][v]; 16 else dis[u][v]=val,cnt[u][v]=1; 17 } 18 for(k=1;k<=n;++k){ 19 for(i=1;i<=n;++i){ 20 for(j=1;j<=n;++j){ 21 int tmpd=dis[i][k]+dis[k][j]; 22 if(dis[i][j]==tmpd){ 23 cnt[i][j]+=cnt[i][k]*cnt[k][j]; 24 } 25 else if(dis[i][j]>tmpd){ 26 dis[i][j]=tmpd; 27 cnt[i][j]=cnt[i][k]*cnt[k][j]; 28 } 29 } 30 } 31 } 32 if(dis[1][n]>1e8)cout<<"-1 0"; 33 else cout<<dis[1][n]<<' '<<cnt[1][n]; 34 return 0; 35 }
E1820 Hanoi Tower II
First AC: 2017-11-08 Latest Modification: 2017-11-08
1 #include<iostream> 2 using namespace std; 3 long a[45]={0,1}; 4 long n,sum,i; 5 int main() 6 { 7 for(i=2;i<45;++i)a[i]=2*(a[i-1]+a[i-2]); 8 while(cin>>n){ 9 sum=0; 10 for(i=0;i<n;++i)sum+=a[i]*(n-i); 11 cout<<3*sum+n<<endl; 12 } 13 return 0; 14 }
E1821 Hanoi Tower III
First AC: 2017-11-16 Latest Modification: 2017-11-16
1 #include<iostream> 2 using namespace std; 3 int n,i; 4 long long s; 5 int main() 6 { 7 while(cin>>n){ 8 s=2; 9 for(i=0;i<n;++i)s*=2; 10 cout<<s-2<<endl; 11 } 12 return 0; 13 }
E1822 Hanoi Tower IV
First AC: 2017-11-16 Latest Modification: 2017-11-16
1 #include<iostream> 2 using namespace std; 3 int n,i; 4 long long s; 5 int main() 6 { 7 while(cin>>n){ 8 s=4; 9 for(i=0;i<n;++i)s*=2; 10 cout<<s-5<<endl; 11 } 12 return 0; 13 }
E1823 数塔I
First AC: 2018-01-01 Latest Modification: 2018-03-13
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n; 4 long tmp,rst; 5 int a[505][505],b[505][505]; 6 int i,j,k; 7 int main() 8 { 9 cin>>T; 10 while(T--){ 11 cin>>n; 12 rst=0; 13 memset(b,0,sizeof(b)); 14 for(i=1;i<=n;++i)for(j=1;j<=i;++j)cin>>a[i][j]; 15 if(n==1)rst=a[1][1]; 16 for(i=1;i<n;++i){//biu from line i to line i+1 17 for(j=1;j<=n;++j)for(k=1;k<=j;++k)b[j][k]=a[j][k]; 18 for(j=n-1;j>i;--j)for(k=1;k<=j;++k) 19 b[j][k]+=max(b[j+1][k],b[j+1][k+1]); 20 tmp=b[i+1][1]; 21 for(j=2;j<i+2;++j)if(b[i+1][j]>tmp)tmp=b[i+1][j]; 22 for(j=i-1;j;--j)for(k=1;k<=j;++k) 23 b[j][k]+=max(b[j+1][k],b[j+1][k+1]); 24 tmp+=b[1][1]; 25 if(tmp>rst)rst=tmp; 26 } 27 cout<<rst<<endl; 28 } 29 return 0; 30 }
E1824 数塔III
First AC: 2018-01-01 Latest Modification: 2018-03-13
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n; 4 int a[505][505]; 5 bool b[505][505][10]; 6 int i,j,k; 7 int main() 8 { 9 cin>>T; 10 while(T--){ 11 cin>>n; 12 memset(b,0,sizeof(b)); 13 for(i=1;i<=n;++i)for(j=1;j<=i;++j)cin>>a[i][j]; 14 for(i=1;i<=n;++i)b[n][i][a[n][i]%10]=1; 15 for(i=n-1;i;--i)for(j=1;j<=i;++j){//line i row j 16 for(k=0;k<10;++k) 17 if(b[i+1][j][k]||b[i+1][j+1][k]) 18 b[i][j][(a[i][j]+k)%10]=1; 19 } 20 for(j=9;j>=0;--j)if(b[1][1][j]){cout<<j<<endl;break;} 21 } 22 return 0; 23 }
E1825 数塔IV
First AC: 2018-01-01 Latest Modification: 2018-03-13
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n; 4 int a[505][505]; 5 bool b[505][505][10]; 6 int i,j,k; 7 int main() 8 { 9 cin>>T; 10 while(T--){ 11 cin>>n; 12 memset(b,0,sizeof(b)); 13 for(i=1;i<=n;++i)for(j=1;j<=i;++j)cin>>a[i][j]; 14 for(i=1;i<=n;++i)b[n][i][a[n][i]%10]=1; 15 for(i=n-1;i;--i)for(j=1;j<=i;++j){//line i row j 16 for(k=0;k<10;++k) 17 if(b[i+1][j][k]||b[i+1][j+1][k]) 18 b[i][j][(a[i][j]*k)%10]=1; 19 } 20 for(j=9;j>=0;--j)if(b[1][1][j]){cout<<j<<endl;break;} 21 } 22 return 0; 23 }
E1828 起床
First AC: 2017-09-30 Latest Modification: 2018-03-13
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 int main() 5 { 6 while(cin>>n,n+1) 7 printf("%02d:%02d:%02d\n",n/3600,n%3600/60,n%60); 8 }
E1829 发愁
First AC: 2017-11-12 Latest Modification: 2018-03-13
1 #include<bits/stdc++.h> 2 using namespace std; 3 int m,n,a,b,c; 4 long g[11],r[11],h[11]; 5 int i,j; 6 int main() 7 { 8 while(cin>>n>>m,n){ 9 memset(g,0,sizeof(g)),memset(r,0,sizeof(r)),memset(h,0,sizeof(h)); 10 for(i=0;i<m;++i){ 11 cin>>a>>b>>c; 12 switch(c){ 13 case 1:g[a]+=3010000,g[b]-=1000010;break; 14 case -1:g[b]+=3010000,g[a]-=1000010;break; 15 case 0:g[a]+=1000000,g[b]+=1000000;break; 16 } 17 } 18 for(i=1;i<=n;++i)g[i]-=i-1; 19 for(i=1;i<n;++i)for(j=i+1;j<=n;++j)if(g[i]>g[j])r[i]++;else r[j]++; 20 for(i=1;i<=n;++i)h[r[i]]=i; 21 for(i=n-1;i>=0;--i)cout<<h[i]<<' '; 22 cout<<endl; 23 } 24 return 0; 25 }
E1830 故事
Note: 特别注意输入64输出-8这种情况
First AC: 2018-08-05 Latest Modification: 2018-08-05
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 typedef long double ld; 5 ll n,m; 6 bool jdg; 7 void cal(ll n,ll i) 8 { 9 ll tmp=pow((ld)n,(ld)1.0/i)+0.3; 10 ll rst=1; 11 for(int j=0;j<i;++j)rst*=tmp; 12 if(rst==n){ 13 jdg=0; 14 if((i+1)&1)cout<<"-"<<(ll)(pow(tmp,i/2)+0.3)<<endl; 15 else cout<<tmp<<endl; 16 } 17 } 18 int main() 19 { 20 while(cin>>n,n>=0){ 21 if(n==1){ 22 cout<<"no answer\n"; 23 continue; 24 } 25 jdg=1; 26 for(m=30;m&&jdg;m-=2)cal(n,m); 27 for(m=29;m&&jdg;m-=2)cal(n,m); 28 } 29 return 0; 30 }
E1831 礼品
First AC: 2017-11-11 Latest Modification: 2018-03-13
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s,t,r; 4 string a[21],b[21]; 5 int na,nb,n,cnt; 6 int i,j; 7 int main() 8 { 9 getline(cin,s); 10 for(i=0;;++i){ 11 getline(cin,a[i]); 12 if(a[i]=="END OF INPUT"){na=i;break;} 13 } 14 getline(cin,t); 15 for(i=0;;++i){ 16 getline(cin,b[i]); 17 if(b[i]=="END OF INPUT"){nb=i;break;} 18 } 19 cin>>n,getchar(); 20 for(j=0;j<n;++j){ 21 cnt=0; 22 getline(cin,r); 23 for(i=0;i<na;++i) 24 if(r==a[i]){cout<<s<<endl;++cnt;break;} 25 if(cnt==0)for(i=0;i<nb;++i) 26 if(r==b[i]){cout<<t<<endl;++cnt;break;} 27 if(cnt==0)cout<<"It may appear in A Best!\n"; 28 } 29 return 0; 30 }
E1833 午餐
First AC: 2018-03-13 Latest Modification: 2018-03-13
1 #include<bits/stdc++.h> 2 #define pi 3.141592653589793 3 using namespace std; 4 int T,n; 5 int r[100],h[100]; 6 double rst; 7 int i,j; 8 double finds(double r,double h) 9 { 10 return 2*pi*r*(r+h); 11 } 12 double minuss(double r1,double r2) 13 { 14 if(r1<r2)return 2*pi*r1*r1; 15 return 2*pi*r2*r2; 16 } 17 int main() 18 { 19 cin>>T; 20 while(T--){ 21 cin>>n; 22 for(i=0;i<n;++i)cin>>r[i]; 23 for(i=0;i<n;++i)cin>>h[i]; 24 rst=finds(r[0],h[0]); 25 for(i=1;i<n;++i) 26 rst+=finds(r[i],h[i])-minuss(r[i-1],r[i]); 27 printf("%.2f\n",rst); 28 } 29 return 0; 30 }
E1834 游戏
First AC: 2018-03-14 Latest Modification: 2018-03-14
Note: 打表输出
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 long a[]={0,2,7,5,30,169,441,1872,7632,1740,93313,459901,1358657,2504881}; 5 int main() 6 { 7 while(cin>>n)cout<<a[n]<<endl; 8 return 0; 9 }
E1835 梦乡
First AC: 2018-03-14 Latest Modification: 2018-03-14
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 bool a[100000000]={1,1}; 5 long long most,tmp; 6 long long i,j; 7 int main() 8 { 9 for(i=2;i<50000000;++i){ 10 if(!a[i])for(j=i*2;j<100000000;j+=i){ 11 a[j]=1; 12 } 13 } 14 cin>>n; 15 most=pow(10,n); 16 for(i=pow(10,n-1);i<most;++i){ 17 tmp=most; 18 bool jdg=1; 19 while(tmp>1){ 20 if(a[i%tmp]){ 21 jdg=0; 22 break; 23 } 24 tmp/=10; 25 } 26 if(jdg)printf("%lld\n",i); 27 } 28 return 0; 29 }
E1836 表达式
First AC: 2018-01-03 Latest Modification: 2018-01-03
1 print(int(eval(input())))
E1837 小强的烦恼
First AC: 2019-03-03 Latest Modification: 2019-03-03
Note: 差分维护的带标记并查集,这样a到根节点的路径标记和模2就能反映a与根节点的关系
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=1e5+5; 4 int T,n,m; 5 string op; 6 int x,y; 7 int pre[maxn],val[maxn]; 8 int i; 9 int find(int x) 10 { 11 int r=x,s=0,i; 12 while(pre[r]!=r)s+=val[r],r=pre[r]; 13 s+=val[r]; 14 while(x!=r){ 15 i=val[x]; 16 val[x]=s%2; 17 s=(s-i+2)%2; 18 i=pre[x]; 19 pre[x]=r; 20 x=i; 21 } 22 return r; 23 } 24 void join(int x,int y) 25 { 26 int fx=find(x),fy=find(y); 27 if(op=="A"){ 28 if(fx!=fy)pre[fx]=fy,val[fx]=(val[y]-val[x]+1)%2; 29 } 30 else{ 31 if(fx!=fy)cout<<"Not sure yet.\n"; 32 else if(val[x]==val[y])cout<<"In the same gang.\n"; 33 else cout<<"In different gangs.\n"; 34 } 35 } 36 int main() 37 { 38 cin>>T; 39 while(T--){ 40 cin>>n>>m; 41 for(i=1;i<=n;++i){ 42 pre[i]=i; 43 val[i]=0; 44 } 45 while(m--){ 46 cin>>op>>x>>y; 47 if(x==y)cout<<"In the same gang.\n"; 48 else join(x,y); 49 } 50 } 51 return 0; 52 }
E1839 恶魔之城
First AC: 2019-03-27 Latest Modification: 2019-03-27
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m; 4 char c; 5 int sx,sy,ex,ey; 6 int mp[205][205]; 7 queue<int>qx,qy; 8 queue<string>qs; 9 bool flag; 10 string ans; 11 int dx[]={-1, 1, 0, 0}; 12 int dy[]={ 0, 0,-1, 1}; 13 string ds[]={"L","R","U","D"}; 14 int i,j; 15 void bfs() 16 { 17 qx.push(sx); 18 qy.push(sy); 19 qs.push(""); 20 mp[sx][sy]=0; 21 int x,y,tx,ty; 22 string s; 23 while(!qx.empty()){ 24 x=qx.front(); 25 y=qy.front(); 26 s=qs.front(); 27 qx.pop(); 28 qy.pop(); 29 qs.pop(); 30 if(x==ex&&y==ey){ 31 flag=1; 32 ans=s; 33 return; 34 } 35 for(i=0;i<4;++i){ 36 tx=x+dx[i]; 37 ty=y+dy[i]; 38 if(mp[tx][ty]){ 39 qx.push(tx); 40 qy.push(ty); 41 qs.push(s+ds[i]); 42 mp[tx][ty]=0; 43 } 44 } 45 } 46 } 47 int main() 48 { 49 cin>>n>>m; 50 for(i=1;i<=n;++i){ 51 getchar(); 52 for(j=1;j<=m;++j){ 53 c=getchar(); 54 if(c!='*'){ 55 mp[i][j]=1; 56 if(c=='S')sx=i,sy=j; 57 else if(c=='E')ex=i,ey=j; 58 } 59 } 60 } 61 if(sx&&sy&&ex&&ey)bfs(); 62 if(!flag){ 63 cout<<"-1"; 64 } 65 else{ 66 int len=ans.length(); 67 int x=sx-1,y=sy-1; 68 cout<<len<<endl<<x<<' '<<y<<endl; 69 for(i=0;i<len;++i){ 70 if(ans[i]=='L')--x; 71 else if(ans[i]=='R')++x; 72 else if(ans[i]=='U')--y; 73 else ++y; 74 cout<<x<<' '<<y<<endl; 75 } 76 } 77 return 0; 78 }
E1841 津津的书架
First AC: 2017-10-10 Latest Modification: 2018-11-01
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 int a[30]; 5 int i; 6 int main() 7 { 8 cin>>n; 9 for(i=0;i<n;++i)cin>>a[i]; 10 sort(a,a+n); 11 for(i=0;i<n;++i)cout<<a[i]<<endl; 12 return 0; 13 }
E1842 搜寻素数
First AC: 2017-11-23 Latest Modification: 2017-11-23
1 #include<iostream> 2 using namespace std; 3 int a[1000001]={1,1}; 4 long t,n; 5 long i,j; 6 int main() 7 { 8 for(i=2;i<1000001;++i){ 9 if(a[i]==0){ 10 for(j=2*i;j<1000001;j+=i)++a[j]; 11 } 12 } 13 cin>>t; 14 while(t--){ 15 cin>>n; 16 if(n<2)cout<<"2\n"; 17 else if(a[n]==0)cout<<n<<endl; 18 else{ 19 for(i=1;;++i){ 20 if(a[n-i]==0){cout<<n-i<<endl;break;} 21 if(a[n+i]==0){cout<<n+i<<endl;break;} 22 } 23 } 24 } 25 return 0; 26 }
E1843 津津骑马
First AC: 2017-12-24 Latest Modification: 2017-12-24
1 #include<iostream> 2 using namespace std; 3 int T,x,y,a[35][35]; 4 int main() 5 { 6 a[4][3]=a[3][4]=1; 7 for(int i=2;i<35;++i)for(int j=2;j<35;++j) 8 a[i][j]+=a[i-1][j-2]+a[i-2][j-1]; 9 cin>>T; 10 for(int i=1;i<=T;++i) 11 cin>>x>>y,cout<<"Chessboard #"<<i<<':'<<a[x+1][y+1]<<endl; 12 return 0; 13 }
E1844 津津的宠物
First AC: 2017-12-24 Latest Modification: 2018-03-14
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 int a[101][101],r,q,e; 5 int j,k; 6 int main() 7 { 8 cin>>n; 9 for(j=n;j>1;j-=2){ 10 for(k=0;k<j;++k)a[r][++q]=++e; 11 for(k=1;k<j;++k)a[++r][q]=++e; 12 for(k=1;k<j;++k)a[r][--q]=++e; 13 for(k=2;k<j;++k)a[--r][q]=++e; 14 } 15 if(n%2!=0)a[n/2][n/2+1]=n*n; 16 r=q=e=0; 17 for(j=0;j<n;++j){ 18 cout<<a[j][1]; 19 a[j][1]=0; 20 for(k=2;k<=n;++k)cout<<" "<<a[j][k]; 21 cout<<endl; 22 } 23 return 0; 24 }
E1845 简易HTML解析器
First AC: 2017-12-26 Latest Modification: 2018-03-14
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s,t="<br>"; 4 int len; 5 bool flag; 6 int i,j; 7 int main() 8 { 9 getline(cin,s); 10 len=s.length()-3; 11 for(i=0;i<len;++i){ 12 if(s[i]==t[0]){ 13 for(flag=j=1;j<4;++j)if(s[i+j]-t[j]){flag=0;break;} 14 } 15 if(flag)cout<<endl,i+=3,flag=0; 16 else cout<<s[i],flag=0; 17 } 18 len+=4; 19 for(j=i;j<len;++j)cout<<s[j]; 20 return 0; 21 }
E1847 Who moves my cheese?
First AC: 2018-03-14 Latest Modification: 2018-03-14
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,r,c; 4 char a[25][25]; 5 int i,j; 6 int find(int r,int c) 7 { 8 if(a[r][c]=='#')return 0; 9 a[r][c]='#'; 10 return 1+find(r-1,c)+find(r+1,c)+find(r,c-1)+find(r,c+1); 11 } 12 int main() 13 { 14 while(cin>>n>>m,getchar(),n){ 15 memset(a,'#',sizeof(a)); 16 for(i=1;i<=m;++i){ 17 for(j=1;j<=n;++j){ 18 a[i][j]=getchar(); 19 if(a[i][j]=='@')r=i,c=j; 20 } 21 getchar(); 22 } 23 cout<<find(r,c)<<endl; 24 } 25 return 0; 26 }
E1848 你是ACM吗?
First AC: 2019-02-09 Latest Modification: 2019-02-09
Note: 正反向道路分别存图,用spfa求两次最短路即可
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=1e6+5,maxm=1e6+5; 4 int T,n,m,ne,rst; 5 int u,v,val; 6 int h1[maxn],dis1[maxn],vis1[maxn],cnt1[maxn]; 7 int h2[maxn],dis2[maxn],vis2[maxn],cnt2[maxn]; 8 queue<int>q1,q2,emp; 9 struct edge{ 10 int to,val,nxt; 11 }e1[maxm],e2[maxm]; 12 void insert1(int u,int v,int val) 13 { 14 e1[++ne].to=v; 15 e1[ne].val=val; 16 e1[ne].nxt=h1[u]; 17 h1[u]=ne; 18 } 19 void insert2(int u,int v,int val) 20 { 21 e2[++ne].to=v; 22 e2[ne].val=val; 23 e2[ne].nxt=h2[u]; 24 h2[u]=ne; 25 } 26 bool spfa1(int ask) 27 { 28 memset(dis1,0x3f,sizeof dis1); 29 dis1[ask]=0; 30 memset(vis1,0,sizeof vis1); 31 vis1[ask]=1; 32 memset(cnt1,0,sizeof cnt1); 33 ++cnt1[ask]; 34 q1=emp; 35 q1.push(ask); 36 while(!q1.empty()){ 37 int x=q1.front(); 38 q1.pop(); 39 vis1[x]=0; 40 for(int i=h1[x];i;i=e1[i].nxt){ 41 int to=e1[i].to; 42 if(dis1[to]>dis1[x]+e1[i].val){ 43 dis1[to]=dis1[x]+e1[i].val; 44 if(!vis1[to]){ 45 vis1[to]=1; 46 if(++cnt1[to]>=n)return 0; 47 q1.push(to); 48 } 49 } 50 } 51 } 52 return 1; 53 } 54 bool spfa2(int ask) 55 { 56 memset(dis2,0x3f,sizeof dis2); 57 dis2[ask]=0; 58 memset(vis2,0,sizeof vis2); 59 vis2[ask]=1; 60 memset(cnt2,0,sizeof cnt2); 61 ++cnt2[ask]; 62 q2=emp; 63 q2.push(ask); 64 while(!q2.empty()){ 65 int x=q2.front(); 66 q2.pop(); 67 vis2[x]=0; 68 for(int i=h2[x];i;i=e2[i].nxt){ 69 int to=e2[i].to; 70 if(dis2[to]>dis2[x]+e2[i].val){ 71 dis2[to]=dis2[x]+e2[i].val; 72 if(!vis2[to]){ 73 vis2[to]=1; 74 if(++cnt2[to]>=n)return 0; 75 q2.push(to); 76 } 77 } 78 } 79 } 80 return 1; 81 } 82 int main() 83 { 84 scanf("%d",&T); 85 while(T--){ 86 scanf("%d%d",&n,&m); 87 memset(h1,0,sizeof h1); 88 memset(h2,0,sizeof h2); 89 ne=0; 90 while(m--){ 91 cin>>u>>v>>val; 92 insert1(u,v,val); 93 insert2(v,u,val); 94 } 95 spfa1(1); 96 spfa2(1); 97 rst=0; 98 for(int i=2;i<=n;++i){ 99 rst+=dis1[i]+dis2[i]; 100 } 101 cout<<rst<<endl; 102 } 103 return 0; 104 }
E1849 Cards Game
First AC: 2017-10-31 Latest Modification: 2018-03-14
1 #include<bits/stdc++.h> 2 using namespace std; 3 int m,cnt,num; 4 int a[15]; 5 char b[15]={'0','0','2','3','4','5','6','7','8','9','T','J','Q','K',}; 6 int i; 7 int main() 8 { 9 while(cin>>m){ 10 memset(a,0,sizeof(a)); 11 cnt=2; 12 for(i=2;;){ 13 if(i==14)i=2; 14 if(a[i]==0)++i,++num; 15 else ++i; 16 if(num==m+1)num=0,a[i-1]=cnt,++cnt; 17 if(cnt==14)break; 18 } 19 cout<<'A'; 20 for(i=2;i<14;++i)cout<<" "<<b[a[i]]; 21 cout<<endl; 22 } 23 return 0; 24 }
E1851 Summing Sums
First AC: 2018-03-14 Latest Modification: 2018-03-14
Note: 矩阵快速幂,维护两列矩阵,第一列储存每个数,第二列储存当前第一列数的和
1 #include<bits/stdc++.h> 2 #define P 98765431 3 using namespace std; 4 long long N,T,s; 5 long long cow[50000][2],m[2][2]; 6 long long i; 7 void mult() 8 { 9 for(i=0;i<N;++i){ 10 cow[i][0]=(cow[i][0]*m[0][0]+cow[i][1]*m[1][0])%P; 11 cow[i][1]=(cow[i][0]*m[0][1]+cow[i][1]*m[1][1])%P; 12 } 13 } 14 void mpow(long long T) 15 { 16 while(T){ 17 if(T&1)mult(); 18 long long tmp=m[0][1]*m[1][0]; 19 m[0][1]=m[0][1]*(m[0][0]+m[1][1])%P; 20 m[1][0]=m[1][0]*(m[0][0]+m[1][1])%P; 21 m[0][0]=(m[0][0]*m[0][0]+tmp)%P; 22 m[1][1]=(m[1][1]*m[1][1]+tmp)%P; 23 T>>=1; 24 } 25 } 26 int main() 27 { 28 cin>>N>>T; 29 m[0][0]=-1,m[1][0]=1,m[1][1]=N-1; 30 for(i=0;i<N;++i)cin>>cow[i][0],s+=cow[i][0]; 31 for(i=0;i<N;++i)cow[i][1]=s; 32 mpow(T); 33 for(i=0;i<N;++i)cout<<cow[i][0]<<endl; 34 return 0; 35 }
E1852 Ordered Fractions
First AC: 2017-11-27 Latest Modification: 2018-03-14
1 #include<bits/stdc++.h> 2 using namespace std; 3 int N,i,j,k,n,m; 4 long long t=-1; 5 struct data{ 6 int n,m; 7 double rst; 8 }a[200001]; 9 10 bool cmp(data a,data b) 11 { 12 return a.rst<b.rst; 13 } 14 15 int main() 16 { 17 cin>>N; 18 for(i=2;i<=N;++i){ 19 for(j=1;j<i;++j){ 20 n=i,m=j,t=1; 21 while(t)t=n%m,n=m,m=t; 22 if(n==1)a[k].n=i,a[k].m=j,a[k++].rst=j*1.0/i; 23 } 24 } 25 sort(a,a+k,cmp); 26 cout<<"0/1\n"; 27 for(i=0;i<k;++i)cout<<a[i].m<<'/'<<a[i].n<<endl; 28 cout<<"1/1"; 29 return 0; 30 }
E1853 Mountain Walking
First AC: 2018-06-20 Latest Modification: 2018-06-20
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,mina,maxa,hgh,low; 4 int mp[105][105]; 5 bool jdg[105][105]; 6 int i,j; 7 bool setmap() 8 { 9 for(i=1;i<=n;++i)for(j=1;j<=n;++j){ 10 if(mp[i][j]>=low&&mp[i][j]<=hgh)jdg[i][j]=1; 11 else jdg[i][j]=0; 12 } 13 return 1; 14 } 15 bool ok(int r,int c) 16 { 17 if(r==n&&c==n)return 1; 18 bool ret=0; 19 jdg[r][c]=0; 20 if(r>1&&jdg[r-1][c])ret|=ok(r-1,c); 21 if(r<n&&jdg[r+1][c])ret|=ok(r+1,c); 22 if(c>1&&jdg[r][c-1])ret|=ok(r,c-1); 23 if(c<n&&jdg[r][c+1])ret|=ok(r,c+1); 24 return ret; 25 } 26 int main() 27 { 28 cin>>n; 29 for(i=1;i<=n;++i)for(j=1;j<=n;++j)cin>>mp[i][j]; 30 mina=min(mp[1][1],mp[n][n]); 31 maxa=max(mp[1][1],mp[n][n]); 32 for(int i=maxa-mina;;++i){ 33 for(low=maxa-i;low<=mina;++low){ 34 hgh=low+i; 35 if(hgh>=maxa&&setmap()&&ok(1,1)){ 36 cout<<i; 37 exit(0); 38 } 39 } 40 } 41 return 0; 42 }
E1854 Broken Necklace
First AC: 2017-12-27 Latest Modification: 2018-03-14
1 #include<bits/stdc++.h> 2 #define min(A,B) A>B? B:A 3 using namespace std; 4 int n,cnt,num,flag; 5 char c,tmp; 6 char a[705]; 7 int i,j; 8 struct data{ 9 int lft,mid,rgt,suma; 10 }b[700]; 11 bool cmp(data a,data b){return a.suma>b.suma;} 12 int main() 13 { 14 cin>>n; 15 for(;i<n;++i)cin>>c,a[i+n]=a[i]=c; 16 for(i=0,n*=2;i<n;++i){ 17 if(a[i]!='w'){ 18 tmp=a[i],++flag; 19 for(j=i-1,num=0;j>=0;--j){ 20 if(a[j]==tmp)b[cnt].mid+=1+num,num=0; 21 else if(a[j]=='w')++num; 22 else break; 23 } 24 b[cnt].lft=num,b[cnt].mid=1; 25 for(j=i+1,num=0;j<n;++j){ 26 if(a[j]==tmp)b[cnt].mid+=1+num,num=0; 27 else if(a[j]=='w')++num; 28 else break; 29 } 30 b[cnt++].rgt=num; 31 if(j==n)break; 32 i=j-1; 33 } 34 } 35 n/=2; 36 if(flag<2){cout<<n;return 0;} 37 for(i=0,--cnt;i<cnt;++i) 38 b[i].suma=b[i].lft+b[i].mid+b[i].rgt+b[i+1].mid+b[i+1].rgt; 39 sort(b,b+cnt,cmp); 40 if(b[0].suma>n)b[0].suma=n; 41 cout<<b[0].suma; 42 return 0; 43 }
E1855 Expedition
First AC: 2018-03-16 Latest Modification: 2018-03-16
Note: 优先队列,不难证明直到油量为零才考虑在前面油量最多的点加油能得到最优解
1 #include<bits/stdc++.h> 2 using namespace std; 3 long n,d,tmpd,tmpf,cnt; 4 priority_queue< int,vector<int>,less<int> >q; 5 long dis[10001],ful[10001]; 6 long a[1000001]; 7 long i; 8 int main() 9 { 10 cin>>n; 11 for(i=0;i<n;++i)cin>>dis[i]>>ful[i]; 12 cin>>d>>tmpf; 13 for(i=0;i<n;++i)a[d-dis[i]]=ful[i]; 14 for(i=1;i<=d;++i){ 15 if(!tmpf){ 16 if(q.empty()){ 17 cout<<"-1"; 18 break; 19 } 20 tmpf=q.top(); 21 q.pop(); 22 ++cnt; 23 } 24 --tmpf; 25 if(a[i])q.push(a[i]); 26 if(++tmpd==d){ 27 cout<<cnt; 28 break; 29 } 30 } 31 return 0; 32 }
E1858 Digits Fun!
First AC: 2018-03-16 Latest Modification: 2018-03-16
Note: 注意从小到大排序后每一位都按0,1,4,9循环
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a[]={0,1,4,9}; 4 long long n; 5 stack<int>s; 6 int main() 7 { 8 while(cin>>n){ 9 if(!n)cout<<0; 10 while(n)s.push(a[n%4]),n/=4; 11 while(!s.empty())cout<<s.top(),s.pop(); 12 cout<<endl; 13 } 14 return 0; 15 }
E1860 Finger Counting
First AC: 2018-01-14 Latest Modification: 2018-01-14
Note: 分类归纳即可
1 #include<iostream> 2 using namespace std; 3 long long a,b; 4 int main() 5 { 6 while(cin>>a>>b){ 7 if(a==1){cout<<8*b<<endl;continue;} 8 if(a==5){cout<<8*b+4<<endl;continue;} 9 b&1? cout<<b/2*8+(9-a)<<endl:cout<<b/2*8+a-1<<endl; 10 } 11 }
E1861 Cooldown Your Processor!
First AC: 2018-03-16 Latest Modification: 2018-03-16
Note: 实质上是问最多和最少能坏几台风扇,排序求和即可
1 #include<bits/stdc++.h> 2 using namespace std; 3 long n,m,tmp; 4 long a[55]; 5 long i; 6 int main() 7 { 8 while(cin>>n>>m){ 9 for(i=0;i<n;++i)cin>>a[i]; 10 sort(a,a+n); 11 tmp=0; 12 for(i=n-1;;--i){ 13 tmp+=a[i]; 14 if(tmp>=m)break; 15 } 16 cout<<i<<' '; 17 tmp=0; 18 for(i=0;;++i){ 19 tmp+=a[i]; 20 if(tmp>=m)break; 21 } 22 cout<<n-i-1<<endl; 23 } 24 return 0; 25 }
E1868 Ambitious Number
First AC: 2018-03-16 Latest Modification: 2018-03-16
Note: 累乘不超过n的每个素数i的[logi(n)]次幂即可
1 #include<bits/stdc++.h> 2 #define p 987654321 3 using namespace std; 4 bool pri[500001]; 5 long long n,rst; 6 long long i,j; 7 int cnt(long long n,long long i) 8 { 9 int rst=0; 10 while(n>=i)n/=i,++rst; 11 return rst; 12 } 13 long long pow(long long a,int b) 14 { 15 long long ans=1,base=a; 16 while(b){ 17 if(b&1)ans=ans*base%p; 18 base=base*base%p; 19 b>>=1; 20 } 21 return ans; 22 } 23 int main() 24 { 25 for(i=2;i<250000;++i) 26 if(!pri[i]) 27 for(j=i*2;j<500001;j+=i) 28 pri[j]=1; 29 while(cin>>n){ 30 rst=1; 31 for(i=2;i<=n;++i) 32 if(!pri[i])rst=pow(i,cnt(n,i))*rst%p; 33 cout<<rst<<endl; 34 } 35 return 0; 36 }
E1869 Bag
First AC: 2018-03-16 Latest Modification: 2018-03-16
Note: 注意清零a[0].num,否则后续m为0时rst结果可能为1
1 #include<bits/stdc++.h> 2 using namespace std; 3 long n,m,cnta,rst; 4 string s; 5 struct data{ 6 string type; 7 long num; 8 }a[100005]; 9 long i; 10 int main() 11 { 12 while(cin>>n>>m){ 13 cnta=0; 14 a[0].num=0; 15 while(m--){ 16 cin>>s; 17 bool jdg=1; 18 for(i=0;i<cnta;++i) 19 if(a[i].type==s){ 20 ++a[i].num; 21 jdg=0; 22 break; 23 } 24 if(jdg)a[cnta].type=s,a[cnta].num=1,++cnta; 25 } 26 rst=a[0].num/n; 27 if(a[0].num%n)++rst; 28 for(i=1;i<cnta;++i){ 29 rst+=a[i].num/n; 30 if(a[i].num%n)++rst; 31 } 32 cout<<rst<<endl; 33 } 34 return 0; 35 }
E1870 Cards
First AC: 2018-03-16 Latest Modification: 2018-03-16
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s; 4 long len,r,b; 5 long i,j; 6 int main() 7 { 8 ios::sync_with_stdio(false); 9 while(cin>>s){ 10 len=s.length(); 11 for(i=0;i<len;++i){ 12 r=b=0; 13 bool jdg=1; 14 int cnt=0; 15 for(j=i;;++j){ 16 if(j==len)j=0; 17 if(j==i&&++cnt>1){ 18 jdg=0; 19 break; 20 } 21 if(s[j]-'B'){ 22 if(++r>b)break; 23 } 24 else ++b; 25 } 26 if(jdg)continue; 27 cout<<i<<endl; 28 break; 29 } 30 } 31 return 0; 32 }
E1873 Face Formations
First AC: 2018-03-16 Latest Modification: 2018-03-16
Note: 排序后转化为n位不减数列的个数,第i的范围由1到xi已知,dp遍历即可
1 #include<bits/stdc++.h> 2 using namespace std; 3 long n,rst; 4 long a[32],b[33],c[33]; 5 long i,j,k; 6 int main() 7 { 8 while(cin>>n){ 9 for(i=0;i<n;++i)cin>>a[i]; 10 sort(a,a+n); 11 for(i=0;i<33;++i)b[i]=1; 12 for(i=n-2;i>=0;--i){ 13 for(j=1;j<=33;++j)c[j]=0; 14 for(j=1;j<=a[i];++j) 15 for(k=j;k<=a[i+1];++k) 16 c[j]+=b[k]; 17 for(j=1;j<=a[i];++j)b[j]=c[j]; 18 } 19 rst=0; 20 for(i=1;i<=a[0];++i)rst+=b[i]; 21 cout<<rst<<endl; 22 } 23 return 0; 24 }
E1874 Game
First AC: 2018-03-16 Latest Modification: 2018-03-16
1 #include<bits/stdc++.h> 2 using namespace std; 3 long n,rst,tmp; 4 int a[100000],b[100000]; 5 long i,j; 6 int main() 7 { 8 ios::sync_with_stdio(false); 9 while(cin>>n){ 10 for(i=0;i<n;++i)cin>>a[i]; 11 for(i=0;i<n;++i)cin>>b[i]; 12 sort(a,a+n); 13 sort(b,b+n); 14 rst=0,tmp=n-1; 15 for(i=n-1;i>=0;--i) 16 for(j=tmp;j>=0;--j) 17 if(a[i]>b[j]){ 18 rst+=a[i]; 19 b[j]=a[i]; 20 tmp=j-1; 21 break; 22 } 23 cout<<rst<<endl; 24 } 25 return 0; 26 }
E1885 找数字
First AC: 2017-12-04 Latest Modification: 2018-03-16
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long T,i; 4 int a[1000001]; 5 int main() 6 { 7 ios::sync_with_stdio(false); 8 while(scanf("%lld",&T)!=EOF){ 9 for(i=0;i<T;++i)scanf("%d",&a[i]); 10 sort(a,a+T); 11 printf("%d\n",a[T/2]); 12 } 13 return 0; 14 }
E1886 有一个数很特别
First AC: 2017-12-10 Latest Modification: 2018-03-16
Note: a^a=0,0^a=a,对所有元素依次取亦或即可
1 #include<bits/stdc++.h> 2 using namespace std; 3 long T,N,n,m,i; 4 int main() 5 { 6 while(scanf("%ld",&N)!=EOF){ 7 scanf("%ld",&n); 8 for(i=1;i<N;++i)scanf("%ld",&m),n^=m; 9 cout<<n<<endl; 10 } 11 return 0; 12 }
E1888 陆行鸟挖宝
First AC: 2019-01-17 Latest Modification: 2019-01-17
1 #include<bits/stdc++.h> 2 using namespace std; 3 int x,y,cnt,tmp1,tmp2; 4 bool mp[100005]; 5 queue<int>q,r,emp; 6 int main() 7 { 8 while(cin>>x>>y){ 9 if(x==y){ 10 cout<<"0\n"; 11 continue; 12 } 13 cnt=1; 14 memset(mp,0,sizeof mp); 15 mp[x]=1; 16 q.push(x); 17 bool jdg=1; 18 while(jdg&&!q.empty()){ 19 tmp1=q.front(); 20 q.pop(); 21 tmp2=2*tmp1; 22 if(tmp2>=0&&tmp2<=100000&&tmp2==y){ 23 cout<<cnt<<endl; 24 jdg=0; 25 continue; 26 } 27 if(tmp2>=0&&tmp2<=100000&&!mp[tmp2]) 28 mp[tmp2]=1,r.push(tmp2); 29 tmp2=tmp1+1; 30 if(tmp2>=0&&tmp2<=100000&&tmp2==y){ 31 cout<<cnt<<endl; 32 jdg=0; 33 continue; 34 } 35 if(tmp2>=0&&tmp2<=100000&&!mp[tmp2]) 36 mp[tmp2]=1,r.push(tmp2); 37 tmp2=tmp1-1; 38 if(tmp2>=0&&tmp2<=100000&&tmp2==y){ 39 cout<<cnt<<endl; 40 jdg=0; 41 continue; 42 } 43 if(tmp2>=0&&tmp2<=100000&&!mp[tmp2])mp[tmp2]=1,r.push(tmp2); 44 if(q.empty())q=r,r=emp,++cnt; 45 } 46 q=r=emp; 47 } 48 return 0; 49 }
E1889 对称是一种美
First AC: 2018-07-06 Latest Modification: 2018-07-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,x,y,w,l,r; 4 string s; 5 char ch; 6 int in[26],out[26]; 7 int dp[2005][2005]; 8 int main() 9 { 10 cin>>m>>n>>s; 11 while(m--){ 12 cin>>ch>>x>>y; 13 ch-='a'; 14 in[ch]=x; 15 out[ch]=y; 16 } 17 for(w=2;w<=n;++w){ 18 for(r=w-1;r<n;++r){ 19 l=r-w+1; 20 if(s[l]==s[r])dp[l][r]=dp[l+1][r-1]; 21 else{ 22 int tmp1=min(in[s[l]-'a'],out[s[l]-'a'])+dp[l+1][r]; 23 int tmp2=min(in[s[r]-'a'],out[s[r]-'a'])+dp[l][r-1]; 24 dp[l][r]=min(tmp1,tmp2); 25 } 26 } 27 } 28 cout<<dp[0][n-1]; 29 return 0; 30 }
E1895 小强喜欢模拟
First AC: 2018-03-17 Latest Modification: 2018-03-17
1 #include<bits/stdc++.h> 2 using namespace std; 3 int p1,p2,p3,len,cntr; 4 string s,t; 5 string r[100]; 6 int i,j,k; 7 bool isnum(char c) 8 { 9 return c>='0'&&c<='9'; 10 } 11 bool islow(char c) 12 { 13 return c>='a'&&c<='z'; 14 } 15 int main() 16 { 17 cin>>p1>>p2>>p3>>s; 18 len=s.length()-1; 19 for(i=1;i<len;++i)if(s[i]=='-'){ 20 t=""; 21 if(isnum(s[i-1])&&isnum(s[i+1])&&s[i+1]>s[i-1]){ 22 if(p1==3) 23 for(j=s[i-1]+1;j<s[i+1];++j) 24 for(k=0;k<p2;++k) 25 t+='*'; 26 else if(p3==1) 27 for(j=s[i-1]+1;j<s[i+1];++j) 28 for(k=0;k<p2;++k) 29 t+=(char)(j); 30 else for(j=s[i+1]-1;j>=s[i-1];--j) 31 for(k=0;k<p2;++k) 32 t+=(char)(j); 33 } 34 else if(islow(s[i-1])&&islow(s[i+1])&&s[i+1]>s[i-1]){ 35 if(p1==3) 36 for(j=s[i-1]+1;j<s[i+1];++j) 37 for(k=0;k<p2;++k) 38 t+='*'; 39 else if(p1==1&&p3==1) 40 for(j=s[i-1]+1;j<s[i+1];++j) 41 for(k=0;k<p2;++k) 42 t+=(char)(j); 43 else if(p1==1&&p3==2) 44 for(j=s[i+1]-1;j>s[i-1];--j) 45 for(k=0;k<p2;++k) 46 t+=(char)(j); 47 else if(p1==2&&p3==1) 48 for(j=s[i-1]+1;j<s[i+1];++j) 49 for(k=0;k<p2;++k) 50 t+=(char)(j-32); 51 else for(j=s[i+1]-1;j>s[i-1];--j) 52 for(k=0;k<p2;++k) 53 t+=(char)(j-32); 54 } 55 else t="-"; 56 r[cntr++]=t; 57 } 58 cntr=0; 59 cout<<s[0]; 60 for(i=1;i<len;++i)s[i]=='-'? cout<<r[cntr++]:cout<<s[i]; 61 cout<<s[len]; 62 return 0; 63 }
E1903 Double Queue
First AC: 2018-05-16 Latest Modification: 2018-05-16
1 #include<bits/stdc++.h> 2 using namespace std; 3 map<int,int>mp; 4 map<int,int>::iterator it; 5 int op,x,y; 6 int main() 7 { 8 while(cin>>op,op){ 9 if(op==1){ 10 cin>>x>>y; 11 mp.insert(pair<int,int>(y,x)); 12 } 13 else if(op==2){ 14 if(mp.empty())cout<<"0\n"; 15 else{ 16 it=mp.end(); 17 --it; 18 cout<<it->second<<endl; 19 mp.erase(it); 20 } 21 } 22 else{ 23 if(mp.empty())cout<<"0\n"; 24 else{ 25 it=mp.begin(); 26 cout<<it->second<<endl; 27 mp.erase(it); 28 } 29 } 30 } 31 return 0; 32 }
E1912 Look and Say
First AC: 2018-01-07 Latest Modification: 2018-01-07
1 #include<iostream> 2 using namespace std; 3 int T,len,cnt; 4 string s; 5 int i; 6 int main() 7 { 8 cin>>T; 9 while(T--){ 10 cin>>s; 11 len=s.length(); 12 for(cnt=i=1;i<len;++i) 13 if(s[i]^s[i-1])cout<<cnt<<s[i-1],cnt=1; 14 else ++cnt; 15 cout<<cnt<<s[len-1]<<endl; 16 } 17 return 0; 18 }
E1914 Coin Toss
First AC: 2018-03-17 Latest Modification: 2018-03-17
Note: 几何概型,注意tile后有两个空格,每组数据结束输出空行,用long long防止溢出
1 #include<bits/stdc++.h> 2 #define pi 3.1415926535798 3 using namespace std; 4 int T; 5 long long m,n,t,c,tmp; 6 double rst[4]; 7 string s="Probability of covering "; 8 string r[4]={" tile = "," tiles = "," tiles = "," tiles = "}; 9 int i,j; 10 int main() 11 { 12 cin>>T; 13 for(i=1;i<=T;++i){ 14 cin>>m>>n>>t>>c; 15 cout<<"Case "<<i<<":\n"; 16 rst[0]=(m*t-(m-1)*c)*(n*t-(n-1)*c); 17 rst[1]=c*(n-1)*(m*t-c*(m-1))+c*(m-1)*(n*t-c*(n-1)); 18 rst[2]=(m-1)*(n-1)/4.0*c*c*(4-pi); 19 rst[3]=pi*c*c*(m-1)*(n-1)/4; 20 for(j=0;j<4;++j){ 21 cout<<s<<j+1<<r[j]; 22 printf("%.4lf%%\n",rst[j]/m/n/t/t*100); 23 } 24 cout<<endl; 25 } 26 return 0; 27 }
E1918 Wavelet Compression
First AC: 2018-03-18 Latest Modification: 2018-03-18
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 int a[257],b[257]; 5 int i,j; 6 int main() 7 { 8 while(cin>>n,n){ 9 for(i=1;i<=n;++i)cin>>a[i]; 10 for(i=1;i<n;i*=2){ 11 for(j=1;j<=i;++j){ 12 b[2*j-1]=(a[j]+a[i+j])/2; 13 b[2*j]=(a[j]-a[i+j])/2; 14 } 15 for(j=1;j<=2*i;++j)a[j]=b[j]; 16 } 17 for(i=1;i<n;++i)cout<<a[i]<<' '; 18 cout<<a[n]<<endl; 19 } 20 return 0; 21 }
E1919 Elementary Additions
First AC: 2018-03-18 Latest Modification: 2018-03-18
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long n,lt,lr; 4 string s[16]={"{}"},t,r; 5 long long len[16]={2}; 6 long long i,j; 7 int main() 8 { 9 for(i=1;i<16;++i){ 10 t="{{}"; 11 for(j=1;j<i;++j)t+=","+s[j]; 12 s[i]=t+"}"; 13 len[i]=s[i].length(); 14 } 15 cin>>n; 16 while(n--){ 17 cin>>t>>r; 18 lt=t.length(); 19 lr=r.length(); 20 for(i=0;;++i)if(lt==len[i])break; 21 for(j=0;;++j)if(lr==len[j])break; 22 cout<<s[i+j]<<endl; 23 } 24 return 0; 25 }
E1921 Baskets of Gold Coins
First AC: 2018-03-18 Latest Modification: 2018-03-18
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long n,w,d,r,tmp; 4 int main() 5 { 6 while(cin>>n>>w>>d>>r){ 7 tmp=(n*(n-1)/2*w-r)/d; 8 if(!tmp)tmp=n; 9 cout<<tmp<<endl; 10 } 11 return 0; 12 }
E1924 Permutation Recovery
First AC: 2018-03-18 Latest Modification: 2018-03-18
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,cnt; 4 int a[500],b[500]; 5 int i,j; 6 int main() 7 { 8 while(cin>>n,n){ 9 for(i=0;i<n;++i)cin>>a[i]; 10 memset(b,0,sizeof(b)); 11 for(i=0;i<n;++i){ 12 cnt=0; 13 for(j=0;;++j){ 14 if(!b[j]&&++cnt>a[i]){ 15 b[j]=i+1; 16 break; 17 } 18 } 19 } 20 cout<<b[0]; 21 for(i=1;i<n;++i)cout<<','<<b[i]; 22 cout<<endl; 23 } 24 return 0; 25 }
E1949 Euchre Results
First AC: 2018-03-18 Latest Modification: 2018-03-18
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a,b,c,d,e,f; 4 int main() 5 { 6 while(cin>>a>>b>>c>>d>>e>>f,a||b||c||d||e||f) 7 cout<<"Anna's won-loss record is " 8 <<(2*b-c+d-e+f)/2<<'-'<<(2*a+c-d+e-f)/2<<".\n"; 9 return 0; 10 }
E1950 Least Common Multiple
First AC: 2018-03-18 Latest Modification: 2018-03-18
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n; 4 long rst; 5 long a[105]; 6 int i; 7 int main() 8 { 9 cin>>T; 10 while(T--){ 11 cin>>n; 12 for(i=0;i<n;++i)cin>>a[i]; 13 rst=a[0]; 14 for(i=1;i<n;++i)rst=rst*a[i]/__gcd(rst,a[i]); 15 cout<<rst<<endl; 16 } 17 return 0; 18 }
E1961 Judging Olympia
First AC: 2018-03-19 Latest Modification: 2018-03-19
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a[6]; 4 int s,i; 5 int main() 6 { 7 while(1){ 8 for(i=0;i<6;++i)cin>>a[i]; 9 sort(a,a+6); 10 if(!a[5])break; 11 s=a[1]+a[2]+a[3]+a[4]; 12 if(s%4==2)cout<<s/4<<".5\n"; 13 else if(!(s%4))cout<<s/4<<endl; 14 else if(s%4==1)cout<<s/4<<".25\n"; 15 else cout<<s/4<<".75\n"; 16 } 17 return 0; 18 }
E1962 Hide That Number
First AC: 2018-03-19 Latest Modification: 2018-03-19
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s,t; 4 int cas,len,tmp; 5 char c; 6 bool flag; 7 int i; 8 int main() 9 { 10 while(cin>>s,s!="0"){ 11 len=s.length(); 12 c=s[len-1]; 13 t=c; 14 flag=0; 15 for(i=len-2;i>=0;--i){ 16 tmp=s[i]-c-flag; 17 if(tmp<0)c=(char)(tmp+10+'0'),flag=1; 18 else c=(char)(tmp+'0'),flag=0; 19 t+=c; 20 } 21 cout<<++cas<<". "; 22 if(t[t.length()-1]=='0')cout<<"IMPOSSIBLE"; 23 else for(i=t.length()-1;i>=0;--i)cout<<t[i]; 24 cout<<endl; 25 } 26 return 0; 27 }
E1965 Fermat’s Christmas Theorem
First AC: 2018-03-19 Latest Modification: 2018-03-19
1 #include<bits/stdc++.h> 2 using namespace std; 3 bool pri[1000001]; 4 long num[1000001]={0,0,1}; 5 long cnt[1000001]={0,0,1}; 6 long l,r; 7 long i,j; 8 int main() 9 { 10 for(i=2;i<500001;++i) 11 if(!pri[i]) 12 for(j=i*2;j<1000001;j+=i) 13 pri[j]=1; 14 for(i=3;i<1000001;++i){ 15 num[i]=num[i-1]+!pri[i]; 16 if(!pri[i]&&i%4==1)cnt[i]=1+cnt[i-1]; 17 else cnt[i]=cnt[i-1]; 18 } 19 while(cin>>l>>r,l!=-1||r!=-1){ 20 cout<<l<<' '<<r<<' '; 21 if(l<0)l=1; 22 if(r<0)r=1; 23 cout<<num[r]-num[l-1]<<' '<<cnt[r]-cnt[l-1]<<endl; 24 } 25 return 0; 26 }
E1973 Crazy Tea Party
First AC: 2018-03-20 Latest Modification: 2018-03-20
1 #include<bits/stdc++.h> 2 using namespace std; 3 long T,n; 4 int main() 5 { 6 cin>>T; 7 while(T--){ 8 cin>>n; 9 cout<<(n/2*(n/2-1)+(n-n/2)*(n-n/2-1))/2<<endl; 10 } 11 return 0; 12 }
E1976 Common Subsequence
First AC: 2017-12-24 Latest Modification: 2018-03-20
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s,t; 4 int ls,lt,n,i,j; 5 int a[505][505]={0}; 6 int main() 7 { 8 while(cin>>s>>t){ 9 ls=s.length(),lt=t.length(); 10 n=max(ls,lt); 11 for(i=0;i<=ls;++i)for(j=0;j<=lt;++j) 12 if(i==-1||j==-1)a[i+1][j+1]=0; 13 else if(s[i]==t[j])a[i+1][j+1]=1+a[i][j]; 14 else a[i+1][j+1]=max(a[i][j+1],a[i+1][j]); 15 cout<<a[ls][lt]<<endl; 16 } 17 return 0; 18 }
E1980 Maya Calendar
First AC: 2018-03-21 Latest Modification: 2018-03-21
1 #include<bits/stdc++.h> 2 using namespace std; 3 long T,d,y,num,i; 4 string m; 5 string Haab[]={"pop","no","zip","zotz","tzec","xul","yoxkin", 6 "mol","chen","yax","zac","ceh","mac","kankin", 7 "muan","pax","koyab","cumhu","uayet"}; 8 string Tzol[]={"imix","ik","akbal","kan","chicchan","cimi","manik", 9 "lamat","muluk","ok","chuen","eb","ben","ix","mem","cib", 10 "caban","eznab","canac","ahau"}; 11 int main() 12 { 13 cin>>T; 14 cout<<T<<endl; 15 while(T--){ 16 scanf("%d.",&d); 17 cin>>m>>y; 18 num=y*365+d+1; 19 for(i=0;;++i) 20 if(Haab[i]==m){ 21 num+=i*20; 22 break; 23 } 24 if(num%260){ 25 y=num/260; 26 num%=260; 27 m=Tzol[(num-1)%20]; 28 d=(num+12)%13+1; 29 } 30 else y=num/260-1,m="ahau",d=13; 31 cout<<d<<' '<<m<<' '<<y<<endl; 32 } 33 return 0; 34 }
E1982 Joseph
First AC: 2018-03-20 Latest Modification: 2018-03-20
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 long a[]={0,2,7,5,30,169,441,1872,7632,1740,93313,459901,1358657,2504881}; 5 int main() 6 { 7 while(cin>>n,n)cout<<a[n]<<endl; 8 return 0; 9 }
E2000 A-B(Big Integer)
First AC: 2017-10-28 Latest Modification: 2017-10-28
1 #include<iostream> 2 using namespace std; 3 int c[501],d[501],e[501]; 4 string a,b,s; 5 int lena,lenb,temp,num,count; 6 int i,j; 7 int main() 8 { 9 while(cin>>a>>b){ 10 for(i=0;i<501;++i)c[i]=d[i]=e[i]=0; 11 num=0,lena=a.length(),lenb=b.length(),count=0; 12 if(lena<lenb)cout<<'-',s=a,a=b,b=s,temp=lena,lena=lenb,lenb=temp; 13 else if(lena==lenb) 14 for(i=0;i<lena;++i){ 15 if(a[i]>b[i])break; 16 if(a[i]<b[i]){cout<<'-',s=a,a=b,b=s;break;} 17 } 18 for(i=lena-1,j=499;i>=0;--i,--j)c[j]=(int)(a[i]-'0'); 19 for(i=lenb-1,j=499;i>=0;--i,--j)d[j]=(int)(b[i]-'0'); 20 for(i=499;i>=0;--i){ 21 if(c[i]-d[i]-num<0){ 22 e[i+1]=c[i]-d[i]-num+10,num=1; 23 } 24 else e[i+1]=c[i]-d[i]-num,num=0; 25 } 26 for(i=0;i<501;++i)if(e[i]!=0)break; 27 for(j=i;j<501;++j)cout<<e[j],count++; 28 if(count==0)cout<<'0'; 29 cout<<endl; 30 } 31 return 0; 32 }
E2001 A+B(_int64)
First AC: 2017-10-24 Latest Modification: 2017-10-24
1 #include<iostream> 2 using namespace std; 3 long long a,b; 4 int main() 5 { 6 while(cin>>a>>b)cout<<a+b<<endl; 7 return 0; 8 }
E2002 求斜边
First AC: 2017-10-10 Latest Modification: 2017-05-30
1 #include<bits/stdc++.h> 2 using namespace std; 3 int main() 4 { 5 int m,n; 6 while(cin>>m>>n) 7 printf("%.3f\n",sqrt(m*m+n*n)); 8 return 0; 9 }
E2004 火仙花数
First AC: 2017-10-11 Latest Modification: 2017-10-11
1 main(){printf("1634\n8208\n9474");}
E2005 整数分解
First AC: 2017-10-11 Latest Modification: 2017-11-10
1 #include<stdio.h> 2 int main() 3 { 4 int a,b; 5 while(scanf("%d",&a)!=EOF){ 6 for(b=2;;++b){ 7 if(a%b==0){printf("%d %d\n",b,a/b);break;} 8 } 9 } 10 return 0; 11 }
E2006 孤独数
First AC: 2017-10-13 Latest Modification: 2018-03-21
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a[1000001],n,i,s; 4 int main() 5 { 6 for(i=1;i<999960;i++){ 7 s=n=i; 8 for(;n!=0;n/=10)s+=n%10; 9 a[s]=1; 10 } 11 for(i=1;i<1000001;i++)if(a[i]==0)cout<<i<<endl; 12 return 0; 13 }
E2007 握手
First AC: 2018-03-20 Latest Modification: 2018-03-20
1 #include<bits/stdc++.h> 2 using namespace std; 3 long a[71]={1,0,1}; 4 int n; 5 int i,j; 6 int main() 7 { 8 for(i=4;i<71;i+=2){ 9 for(j=0;j<i;j+=2)a[i]+=a[j]*a[i-j-2]; 10 } 11 while(cin>>n)cout<<a[2*n]<<endl; 12 return 0; 13 }
E2008 查询
First AC: 2017-10-11 Latest Modification: 2018-03-21
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a[501]; 4 int i,m,n; 5 int main() 6 { 7 cin>>m; 8 for(i=0;i<m;i++){cin>>n;a[n]=1;} 9 cin>>m; 10 for(i=0;i<m;i++){ 11 cin>>n; 12 if(a[n]==1)cout<<"yes!\n"; 13 else cout<<"no!\n"; 14 } 15 return 0; 16 }
E2009 查询II
First AC: 2017-10-13 Latest Modification: 2018-03-21
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a[100002],m,n,i; 4 int main() 5 { 6 cin>>m; 7 for(i=0;i<m;i++)cin>>n,a[n]=1; 8 cin>>m; 9 for(i=0;i<m;i++){ 10 cin>>n; 11 if(a[n]==1)cout<<"yes!\n"; 12 else cout<<"no!\n"; 13 } 14 return 0; 15 }
E2010 菱形
First AC: 2017-10-11 Latest Modification: 2018-03-21
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,i,j; 4 int main() 5 { 6 while(cin>>n){ 7 for(i=0;i<n+1;i++){ 8 for(j=0;j<n-i;j++)cout<<" "; 9 for(j=0;j<2*i+1;j++)cout<<"*"; 10 for(j=0;j<n-i;j++)cout<<" "; 11 cout<<endl; 12 } 13 for(i=1;i<=n;i++){ 14 for(j=0;j<i;j++)cout<<" "; 15 for(j=0;j<=2*n-2*i;j++)cout<<"*"; 16 for(j=0;j<i;j++)cout<<" "; 17 cout<<endl; 18 } 19 } 20 return 0; 21 }
E2011 Goat in the Garden
First AC: 2018-03-20 Latest Modification: 2018-03-20
1 #include<bits/stdc++.h> 2 #define pi 3.1415926535897 3 using namespace std; 4 int n,l; 5 int main() 6 { 7 while(cin>>n>>l){ 8 if(2*l<=n)printf("%.3f\n",pi*l*l); 9 else if(l*l*2>=n*n)printf("%.3f\n",1.0*n*n); 10 else printf("%.3f\n",l*l*(pi-4*acos(n/2.0/l)) 11 +n*sqrt(4*l*l-n*n)); 12 } 13 return 0; 14 }
E2012 Factorials!!!
First AC: 2018-03-20 Latest Modification: 2018-03-20
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long n,len,rst; 4 string s; 5 int main() 6 { 7 while(cin>>n>>s){ 8 len=s.length(); 9 rst=1; 10 do{rst*=n,n-=len;}while(n>0); 11 cout<<rst<<endl; 12 } 13 return 0; 14 }
E2013 Ball in Dream
First AC: 2018-03-20 Latest Modification: 2018-03-20
1 #include<bits/stdc++.h> 2 #define pi 3.1415926535 3 using namespace std; 4 double v,a,k,dis; 5 int main() 6 { 7 while(cin>>v>>a>>k){ 8 k=sqrt(k); 9 a=a*pi/180; 10 dis=0; 11 while(fabs(v)>0.000001)dis+=v*v*cos(a)*sin(a)/5,v/=k; 12 printf("%.2f\n",dis); 13 } 14 return 0; 15 }
E2014 Sum of Digits
First AC: 2018-03-22 Latest Modification: 2018-03-22
1 while True: 2 try: 3 n=(int)(input())-1 4 print(55**n*36) 5 except: 6 break
E2015 自修室
First AC: 2017-12-02 Latest Modification: 2018-03-21
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,cnt; 4 char room[20][20]; 5 int i,j,k; 6 struct data{ 7 int r,c,rst; 8 long long id,dis; 9 }a[101]; 10 bool cmp(data a,data b) 11 { 12 if(a.rst-b.rst)return a.rst>b.rst; 13 if(a.dis-b.dis)return a.dis<b.dis; 14 return a.id<b.id; 15 } 16 int main() 17 { 18 while(cin>>n){ 19 for(i=0;i<n;++i){ 20 cin>>a[i].id>>a[i].dis>>a[i].r>>a[i].c; 21 memset(room,'0',sizeof(room)); 22 for(cnt=j=1,getchar();j<=a[i].r;++j){ 23 for(k=1;k<=a[i].c;++k)cin>>room[j][k]; 24 getchar(); 25 } 26 for(a[i].rst=0,j=1;cnt&&j<=a[i].r;++j){ 27 for(k=1;cnt&&k<=a[i].c;++k) 28 if(room[j][k]=='0'&&room[j-1][k]=='0'&& 29 room[j][k-1]=='0'&&room[j][k+1]=='0'){ 30 a[i].rst=1;cnt=0;break; 31 } 32 } 33 } 34 sort(a,a+n,cmp); 35 a[0].rst? cout<<a[0].id<<endl:cout<<"Bad Luck,Rocker!\n"; 36 } 37 return 0; 38 }
E2016 PK
First AC: 2018-01-16 Latest Modification: 2018-03-23
1 #include<bits/stdc++.h> 2 using namespace std; 3 double a,b,c,d,m,n; 4 int main() 5 { 6 while((scanf("%dvs%d%dvs%d",&a,&b,&c,&d))!=EOF){ 7 m=ceil(a/d),n=ceil(b/c); 8 m>n? cout<<1<<endl:cout<<2<<endl; 9 } 10 return 0; 11 }
E2017 圆周排列
First AC: 2018-03-18 Latest Modification: 2018-03-18
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,r; 4 long a[13]={1}; 5 int main() 6 { 7 for(int i=1;i<13;++i)a[i]=i*a[i-1]; 8 while(cin>>n>>r)cout<<a[n]/a[n-r]/max(r,1)<<endl; 9 return 0; 10 }
E2018 矩形相交
First AC: 2017-12-30 Latest Modification: 2018-03-22
Note: 类比两圆,两矩形相交当且仅当两中心点横向距离小于两底平均值且纵向距离小于两高平均值
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a,b,c,d,e,f,g,h; 4 int main() 5 { 6 while(cin>>a>>b>>c>>d>>e>>f>>g>>h){ 7 if(abs(a+c-e-g)<c-a+g-e&&abs(b+d-f-h)<b-d+f-h)cout<<"yes\n"; 8 else cout<<"no\n"; 9 } 10 return 0; 11 }
E2019 加密1
First AC: 2017-11-15 Latest Modification: 2018-03-23
1 #include<bits/stdc++.h> 2 using namespace std; 3 unsigned n; 4 int a[32],b[16]; 5 string s="0123456789ABCDEF"; 6 int i,j; 7 int main() 8 { 9 while(cin>>n){ 10 for(i=31;i>=0;--i)a[i]=n%2,n/=2; 11 for(i=0;i<16;++i){ 12 if(a[i]==1)--a[i]; 13 else ++a[i]; 14 if(a[i+16]!=a[i])b[i]=1; 15 else b[i]=0; 16 } 17 for(i=0;i<16;i+=4)cout<<s[b[i]*8+b[i+1]*4+b[i+2]*2+b[i+3]]; 18 for(i=0;i<16;i+=4)cout<<s[a[i]*8+a[i+1]*4+a[i+2]*2+a[i+3]]; 19 cout<<endl; 20 } 21 return 0; 22 }
E2020 加密2
First AC: 2017-11-15 Latest Modification: 2018-03-23
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long n; 4 int a[32],i,s,cnt; 5 int main() 6 { 7 while(cin>>n){ 8 s=cnt=0; 9 for(i=31;i>=0;--i)a[i]=n%2,n/=2; 10 for(i=0;i<16;++i){ 11 if(a[i]==0)a[i]=a[i+16]; 12 else {if(a[i+16]==0)a[i]=1; 13 else a[i]=0;} 14 if(a[i+16]==0)a[i+16]=1; 15 else a[i+16]=0; 16 } 17 for(i=16;i<32;i+=4){ 18 s=8*a[i]+4*a[i+1]+2*a[i+2]+a[i+3]; 19 if(cnt==0&&s!=0)++cnt; 20 if(cnt){ 21 if(s<10)cout<<s; 22 else cout<<(char)(s-10+'A'); 23 } 24 } 25 for(i=0;i<16;i+=4){ 26 s=8*a[i]+4*a[i+1]+2*a[i+2]+a[i+3]; 27 if(cnt==0&&s!=0)++cnt; 28 if(cnt){ 29 if(s<10)cout<<s; 30 else cout<<(char)(s-10+'A'); 31 } 32 } 33 cout<<endl; 34 } 35 return 0; 36 }
E2021 Costume Party
First AC: 2018-03-22 Latest Modification: 2018-03-22
1 #include<bits/stdc++.h> 2 using namespace std; 3 long n,s,rst; 4 long a[20000]; 5 long i,j; 6 int main() 7 { 8 cin>>n>>s; 9 for(i=0;i<n;++i)cin>>a[i]; 10 sort(a,a+n); 11 for(i=0;i<n;++i){ 12 for(j=i+1;j<n;++j)if(a[i]+a[j]>s)break; 13 rst+=j-i-1; 14 } 15 cout<<rst; 16 return 0; 17 }
E2022 Election Time
First AC: 2018-01-16 Latest Modification: 2018-03-23
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,k; 4 struct data{ 5 long long res,act,num; 6 }a[50001]; 7 int i; 8 bool cmp1(data a,data b){return a.res>b.res;} 9 bool cmp2(data a,data b){return a.act>b.act;} 10 int main() 11 { 12 cin>>n>>k; 13 for(;i<n;++i)cin>>a[i].res>>a[i].act,a[i].num=i; 14 sort(a,a+n,cmp1); 15 sort(a,a+k,cmp2); 16 cout<<a[0].num+1; 17 return 0; 18 }
E2023 iCow
First AC: 2018-03-22 Latest Modification: 2018-03-22
Note: 注意题中对余数的分配是从编号1开始跳过当前曲依次分配,配完为止
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,t; 4 struct data{ 5 int num,rate; 6 }a[1000]; 7 int i,j; 8 bool cmp(data a,data b) 9 { 10 if(a.rate!=b.rate)return a.rate>b.rate; 11 return a.num<b.num; 12 } 13 int main() 14 { 15 cin>>n>>t; 16 for(i=0;i<n;++i)a[i].num=i+1,cin>>a[i].rate; 17 if(n==1)while(t--)cout<<"1\n"; 18 else for(i=0;i<t;++i){ 19 sort(a,a+n,cmp); 20 cout<<a[0].num<<endl; 21 int tmp1=a[0].rate/(n-1),tmp2=a[0].rate%(n-1)+1; 22 if(a[0].num<tmp2)++tmp2; 23 a[0].rate=0; 24 for(j=1;j<n;++j){ 25 a[j].rate+=tmp1; 26 if(a[j].num<tmp2)++a[j].rate; 27 } 28 } 29 return 0; 30 }
E2025 Running
First AC: 2018-03-23 Latest Modification: 2018-03-23
Note: 设dp[i][j]为第i分钟疲劳值为j的最大路程
则dp[i][0]=max(dp[i-1][0],dp[i-1][1],dp[i-2][2],…,dp[1][i-1]),dp[i][j]=dp[i-1][j-1]+d[i]
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m; 4 long dp[10003][503],d[10003]; 5 int i,j; 6 int main() 7 { 8 cin>>n>>m; 9 for(i=1;i<=n;++i)cin>>d[i]; 10 for(i=1;i<=n;++i){ 11 dp[i][0]=dp[i-1][0]; 12 for(j=1;j<=m;++j){ 13 if(i>j)dp[i][0]=max(dp[i][0],dp[i-j][j]); 14 dp[i][j]=dp[i-1][j-1]+d[i]; 15 } 16 } 17 cout<<dp[n][0]; 18 return 0; 19 }
E2030 统计
First AC: 2017-10-18 Latest Modification: 2018-03-23
1 #include<bits/stdc++.h> 2 using namespace std; 3 long n,m,i; 4 double sums,sumd,s,d; 5 int main() 6 { 7 while(cin>>n){ 8 for(i=0;i<n;i++){ 9 cin>>m; 10 if(m%2==1)sums+=m,s++;else sumd+=m,d++; 11 } 12 if(s==0)cout<<"None ";else printf("%.3f ",sums/s); 13 if(d==0)cout<<"None\n";else printf("%.3f\n",sumd/d); 14 sums=sumd=s=d=0; 15 } 16 return 0; 17 }
E2031 排序
First AC: 2017-10-11 Latest Modification: 2018-03-23
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,i; 4 struct data{int n;}a[100]; 5 bool cmp(data a,data b) 6 { 7 return abs(a.n)<abs(b.n); 8 } 9 int main() 10 { 11 while(cin>>n){ 12 for(i=0;i<n;++i)cin>>a[i].n; 13 sort(a,a+n,cmp); 14 cout<<a[0].n; 15 for(i=1;i<n;++i)cout<<' '<<a[i].n; 16 cout<<endl; 17 } 18 return 0; 19 }
E2032 判断两个数是否相等
First AC: 2017-11-16 Latest Modification: 2018-03-23
1 #include<bits/stdc++.h> 2 using namespace std; 3 int ls,lt,s1,s2,t1,t2,k1,k2,p1,p2,flags,flagt; 4 char s[2000],t[2000],s0[2][1000],t0[2][1000]; 5 int i,j,flag,num,cnt; 6 int main() 7 { 8 while(scanf("%s%s",&s,&t)!=EOF){ 9 ls=strlen(s),lt=strlen(t); 10 flag=cnt=num=k1=k2=p1=p2=flags=flagt=0; 11 for(i=0;i<ls;++i){ 12 if(s[i]=='-'){++p1;--flags;continue;} 13 if(s[i]=='+'){++p1;continue;} 14 if(s[i]=='.'){++k1;break;} 15 if(flag==0&&s[i]!='0')++flag; 16 if(flag)s0[0][num++]=s[i]; 17 } 18 s1=num,num=flag=0; 19 for(i=0;i<lt;++i){ 20 if(t[i]=='-'){++p2;--flagt;continue;} 21 if(t[i]=='+'){++p2;continue;} 22 if(t[i]=='.'){++k2;break;} 23 if(flag==0&&t[i]!='0')++flag; 24 if(flag)t0[0][num++]=t[i]; 25 } 26 t1=num,num=flag=0; 27 if(k1!=0)for(i=ls-1;i>=0;--i){ 28 if(s[i]=='.')break; 29 if(flag==0&&s[i]!='0')++flag; 30 if(flag)s0[1][num++]=s[i]; 31 } 32 s2=num,num=flag=0; 33 if(k2!=0)for(i=lt-1;i>=0;--i){ 34 if(t[i]=='.')break; 35 if(flag==0&&t[i]!='0')++flag; 36 if(flag)t0[1][num++]=t[i]; 37 } 38 t2=num; 39 if(s1!=t1||s2!=t2||flags!=flagt){ 40 cout<<"It isn't xiao qiang\n"; 41 continue; 42 } 43 for(i=0,j=0;i<s1-p1&&j<t1-p2;++i,++j) 44 if(s0[0][i]!=t0[0][j]){ 45 cout<<"It isn't xiao qiang\n"; 46 ++cnt; 47 break; 48 } 49 if(cnt==0)for(i=0;i<s2;++i) 50 if(s0[1][i]!=t0[1][i]){ 51 cout<<"It isn't xiao qiang\n"; 52 ++cnt; 53 break; 54 } 55 if(cnt==0)cout<<"It's xiao qiang\n"; 56 } 57 return 0; 58 }
E2033 反转字符串
First AC: 2017-11-08 Latest Modification: 2018-03-23
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s; 4 int len,i; 5 int main() 6 { 7 while(getline(cin,s)){ 8 len=s.length(); 9 for(i=len-1;i>=0;--i)cout<<s[i]; 10 cout<<endl; 11 } 12 return 0; 13 }
E2034 比赛排名
First AC: 2018-01-05 Latest Modification: 2018-03-23
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,tmp; 4 int i,j,k; 5 struct data{ 6 string name; 7 int solve,time; 8 }a[1001]; 9 bool cmp(data a,data b) 10 { 11 if(a.solve^b.solve)return a.solve>b.solve; 12 if(a.time^b.time)return a.time<b.time; 13 return a.name<b.name; 14 } 15 int main() 16 { 17 while(cin>>n>>m){ 18 for(i=0;i<n;++i){ 19 cin>>a[i].name; 20 a[i].solve=0,a[i].time=0; 21 for(j=0;j<m;++j){ 22 cin>>tmp; 23 if(tmp){ 24 ++a[i].solve,a[i].time+=tmp; 25 cin>>tmp; 26 a[i].time+=20*(tmp-1); 27 } 28 } 29 } 30 sort(a,a+n,cmp); 31 for(i=0;i<n;++i) 32 cout<<"rank = "<<i+1<<" , name = "<<a[i].name<<" , solve = " 33 <<a[i].solve<<" , time = "<<a[i].time<<endl; 34 } 35 return 0; 36 }
E2035 解的个数
First AC: 2018-03-23 Latest Modification: 2018-03-23
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a,b; 4 char c; 5 bool x[32],y[32]; 6 long long rst; 7 int i; 8 int main() 9 { 10 while(cin>>a>>b>>c){ 11 for(i=0;i<32;++i){ 12 x[i]=a&1; 13 y[i]=b&1; 14 a>>=1; 15 b>>=1; 16 } 17 rst=1; 18 if(c=='|')for(i=0;i<32;++i) 19 if(x[i]&&y[i])rst<<=1; 20 else if(x[i]&&!y[i])rst=0; 21 if(c=='&')for(i=0;i<32;++i){ 22 if(!x[i]&&y[i])rst=0; 23 else if(!x[i]&&!y[i])rst<<=1; 24 } 25 cout<<rst<<endl; 26 } 27 return 0; 28 }
E2036 变化
First AC: 2018-03-24 Latest Modification: 2018-03-24
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long n,m,t,tmp,cnt; 4 char c; 5 long long i,j,k; 6 int main() 7 { 8 while(cin>>n>>m){ 9 long long a[n][m]; 10 for(i=0;i<n;++i)for(j=0;j<m;++j)cin>>a[i][j]; 11 cin>>t; 12 while(t--){ 13 cin>>c; 14 if(c=='O'){ 15 if(tmp%4==0){ 16 for(i=0;i<n;++i){ 17 cout<<a[i][0]; 18 for(j=1;j<m;++j)cout<<' '<<a[i][j]; 19 cout<<endl; 20 } 21 } 22 else if(tmp%4==1){ 23 for(j=0;j<m;++j){ 24 cout<<a[n-1][j]; 25 for(i=n-2;i>=0;--i)cout<<' '<<a[i][j]; 26 cout<<endl; 27 } 28 } 29 else if(tmp%4==2){ 30 for(i=n-1;i>=0;--i){ 31 cout<<a[i][m-1]; 32 for(j=m-2;j>=0;--j)cout<<' '<<a[i][j]; 33 cout<<endl; 34 } 35 } 36 else{ 37 for(j=m-1;j>=0;--j){ 38 cout<<a[0][j]; 39 for(i=1;i<n;++i)cout<<' '<<a[i][j]; 40 cout<<endl; 41 } 42 } 43 } 44 else{ 45 cin>>cnt; 46 if(cnt<0){ 47 cnt*=-1; 48 c=c=='L'? 'R':'L'; 49 } 50 if(c=='L')tmp=(tmp+cnt%4*3)%4; 51 else tmp=(tmp+cnt%4)%4; 52 } 53 } 54 } 55 return 0; 56 }
E2037 Dining Cows
First AC: 2018-03-25 Latest Modification: 2018-03-25
Note: 两边缩进,最左的2标为i,最右的1标为j,每次要么只改变i,要么只改变j,要么同时改变
1 #include<bits/stdc++.h> 2 using namespace std; 3 long n,tmp,num,cnt,rst=30000; 4 bool a[30000]; 5 long i,j,k; 6 int main() 7 { 8 cin>>n; 9 for(i=0;i<n;++i){ 10 cin>>tmp; 11 a[i]=tmp-1; 12 } 13 i=0,j=n-1; 14 while(1){ 15 for(;i<n;++i)if(a[i])break; 16 for(;j>=0;--j)if(!a[j])break; 17 if(i>j){ 18 cout<<min(rst,num); 19 break; 20 } 21 for(cnt=0,k=i+1;k<j;++k)if(a[k])++cnt; 22 rst=min(rst,num+cnt+1); 23 rst=min(rst,num+j-i-cnt); 24 a[i]=0; 25 a[j]=1; 26 num+=2; 27 } 28 return 0; 29 }
E2038 Long Distant Racing
First AC: 2018-03-25 Latest Modification: 2018-03-25
1 #include<bits/stdc++.h> 2 using namespace std; 3 long m,t,u,f,d,tmp; 4 char a[100000]; 5 long i; 6 int main() 7 { 8 cin>>m>>t>>u>>f>>d; 9 u+=d,f*=2; 10 for(i=0;i<t;++i)cin>>a[i]; 11 for(i=0;i<t;++i){ 12 if(a[i]=='f')tmp+=f; 13 else tmp+=u; 14 if(tmp>m)break; 15 } 16 cout<<i; 17 return 0; 18 }
E2039 Cow Multiplication
First AC: 2018-02-09 Latest Modification: 2018-02-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long a,b; 4 int s1,s2; 5 int main() 6 { 7 cin>>a>>b; 8 while(a)s1+=a%10,a/=10; 9 while(b)s2+=b%10,b/=10; 10 cout<<s1*s2; 11 return 0; 12 }
E2040 Game of Lines
First AC: 2018-03-25 Latest Modification: 2018-03-25
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,tmp,rst; 4 struct data{ 5 int x,y; 6 }a[200]; 7 double k[19900]; 8 bool null; 9 int i,j; 10 int main() 11 { 12 cin>>n; 13 for(i=0;i<n;++i)cin>>a[i].x>>a[i].y; 14 for(i=0;i<n;++i)for(j=i+1;j<n;++j){ 15 if(a[i].x==a[j].x)null=1; 16 else if(a[i].x>a[j].x) 17 k[tmp++]=(a[i].y-a[j].y)*1.0/(a[i].x-a[j].x); 18 else k[tmp++]=(a[j].y-a[i].y)*1.0/(a[j].x-a[i].x); 19 } 20 sort(k,k+tmp); 21 for(i=1;i<tmp;++i)if(fabs(k[i]-k[i-1])>0.0000001)++rst; 22 cout<<rst+null+1; 23 return 0; 24 }
E2042 Eating Together
First AC: 2018-03-25 Latest Modification: 2018-03-25
Note: 转化为求最长不减子列和最长不增子列
1 #include<bits/stdc++.h> 2 using namespace std; 3 long n,len,tmp,rst; 4 int a[300000]; 5 long l[300000]; 6 long i,j; 7 long bs(long n) 8 { 9 long lft=0,rgt=len-1,mid; 10 l[len]=0; 11 while(lft<=rgt){ 12 mid=(lft+rgt)/2; 13 if(l[mid]<n)lft=mid+1; 14 else if(l[mid]>n)rgt=mid-1; 15 else for(long j=mid;j<=len;++j) 16 if(l[j]!=l[mid])return j; 17 } 18 return lft; 19 } 20 int main() 21 { 22 cin>>n; 23 for(i=0;i<n;++i){ 24 cin>>a[i]; 25 tmp=bs(a[i]); 26 if(tmp==len)++len; 27 l[tmp]=a[i]; 28 } 29 rst=len; 30 len=0; 31 for(i=n-1;i>=0;--i){ 32 tmp=bs(a[i]); 33 if(tmp==len)++len; 34 l[tmp]=a[i]; 35 } 36 cout<<n-max(rst,len); 37 return 0; 38 }
E2044 Distinct Subsequences
First AC: 2018-03-26 Latest Modification: 2018-03-26
Note: 排列组合dp,注意减法%p时先加上p防止出现负数
1 #include<bits/stdc++.h> 2 #define P 1000000007 3 using namespace std; 4 int T; 5 string s; 6 long long len,tmp,rst; 7 long long n[127]; 8 long long i; 9 int main() 10 { 11 cin>>T; 12 getchar(); 13 while(T--){ 14 getline(cin,s); 15 len=s.length(); 16 memset(n,0,sizeof(n)); 17 rst=1; 18 for(i=0;i<len;++i){ 19 tmp=rst; 20 rst=((rst+P)*2-n[s[i]])%P; 21 n[s[i]]=tmp; 22 } 23 cout<<rst<<endl; 24 } 25 return 0; 26 }
E2045 A/B(Big Integer)
First AC: 2017-12-17 Latest Modification: 2018-03-28
1 while True: 2 try: 3 a,b=map(int,input().split()) 4 if a%b==0: 5 print(int(a/b)) 6 else: 7 print(int(a//b),int(a%b)) 8 except: 9 break
E2046 Nearly Prime Numbers
First AC: 2018-03-28 Latest Modification: 2018-03-28
1 #include<bits/stdc++.h> 2 using namespace std; 3 long T,n; 4 bool pri(long n) 5 { 6 if(n<2)return 0; 7 long m=floor(sqrt(n))+1; 8 for(long i=2;i<m;++i) 9 if(!(n%i))return 0; 10 return 1; 11 } 12 bool jdg(long n){ 13 long m=floor(sqrt(n))+1; 14 for(long i=2;i<m;++i) 15 if(n%i==0){ 16 if(pri(i)&&pri(n/i))return 1; 17 else return 0; 18 } 19 return 0; 20 } 21 int main() 22 { 23 cin>>T; 24 while(T--){ 25 cin>>n; 26 jdg(n)? cout<<"Yes\n":cout<<"No\n"; 27 } 28 return 0; 29 }
E2047 Coprimes
First AC: 2018-03-28 Latest Modification: 2018-03-28
Note: 注意互素具有周期性
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,rst,i; 4 int main() 5 { 6 while(cin>>n){ 7 for(rst=n,i=2;i<=n;++i){ 8 if(!(n%i))rst=rst*(i-1)/i; 9 while(!(n%i))n/=i; 10 } 11 cout<<rst<<endl; 12 } 13 return 0; 14 }
E2048 Calendar
First AC: 2018-03-07 Latest Modification: 2018-03-07
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a[15]={0,0,31,59,90,120,151,181,212,243,273,304,334}; 4 int b[15]={0,31,28,31,30,31,30,31,31,30,31,30,31}; 5 int n,m; 6 int main() 7 { 8 while(cin>>n>>m) 9 if(n>b[m]||m>12||n<1||m<1)cout<<"Impossible\n"; 10 else cout<<(n+a[m]+6)%7+1<<endl; 11 return 0; 12 }
E2049 Digital Root
First AC: 2018-03-28 Latest Modification: 2018-03-28
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n; 4 long long m,rst,tmp; 5 int main() 6 { 7 cin>>T; 8 while(T--){ 9 cin>>n; 10 tmp=1,rst=0; 11 while(n--)cin>>m,tmp=(tmp*m+8)%9+1,rst=(rst+tmp+8)%9+1; 12 cout<<rst<<endl; 13 } 14 return 0; 15 }
E2051 实验楼的电梯
First AC: 2017-12-16 Latest Modification: 2017-12-16
1 #include<iostream> 2 using namespace std; 3 int n,k; 4 int main() 5 { 6 while(cin>>k>>n){ 7 cout<<(int)(0.5+((2*k+1)*n+3.0)/(2*(n+1)))<<endl; 8 } 9 return 0; 10 }
E2052 棋盘上的车
First AC: 2018-03-28 Latest Modification: 2018-03-28
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,k,i; 4 unsigned long long rst; 5 int main() 6 { 7 while(cin>>n>>m>>k){ 8 rst=1; 9 for(i=0;i<k;++i)rst*=(m-i)*(n-i); 10 for(i=2;i<=k;++i)rst/=i; 11 cout<<rst<<endl; 12 } 13 return 0; 14 }
E2053 小强的生日
First AC: 2018-01-06 Latest Modification: 2018-01-06
1 #include<iostream> 2 using namespace std; 3 int a,b; 4 int main() 5 { 6 while(cin>>a>>b){cout<<a+b-1<<endl;} 7 }
E2054 Satellite Photographs
First AC: 2018-01-17 Latest Modification: 2018-03-29
1 #include<bits/stdc++.h> 2 using namespace std; 3 int w,h,r,l,tmp,maxn,i,j; 4 char a[1002][82]; 5 6 int cnt(int l,int r) 7 { 8 if(a[l][r]=='.'||l==0||r==0||l>h||r>w)return 0; 9 if(a[l-1][r]-'.'||a[l][r-1]-'.'||a[l][r+1]-'.'||a[l+1][r]-'.'){ 10 a[l][r]='.'; 11 return cnt(l-1,r)+cnt(l,r-1)+cnt(l,r+1)+cnt(l+1,r)+1; 12 } 13 a[l][r]='.'; 14 return 1; 15 } 16 int main() 17 { 18 memset(a,'.',sizeof(a)); 19 cin>>w>>h; 20 for(i=1;i<=h;++i)for(j=1;j<=w;++j)cin>>a[i][j]; 21 for(i=1;i<=h;++i)for(j=1;j<=w;++j){ 22 tmp=cnt(i,j); 23 if(tmp>maxn)maxn=tmp; 24 } 25 cout<<maxn; 26 return 0; 27 }
E2055 Hopscotch
First AC: 2018-03-28 Latest Modification: 2018-03-28
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a[6][6]; 4 long n[25600],cntn; 5 int x[]={ 0,0,1,-1}; 6 int y[]={-1,1,0, 0}; 7 long rst; 8 int i,j; 9 void dfs(int r,int c,int t,int ans) 10 { 11 ans=10*ans+a[r][c]; 12 if(t==5){n[cntn++]=ans;return;} 13 for(int i=0;i<4;++i){ 14 if(r+x[i]&&(r+x[i])<6&&c+y[i]&&(c+y[i])<6) 15 dfs(r+x[i],c+y[i],t+1,ans); 16 } 17 } 18 int main() 19 { 20 for(i=1;i<6;++i)for(j=1;j<6;++j)cin>>a[i][j]; 21 for(i=1;i<6;++i)for(j=1;j<6;++j)dfs(i,j,0,0); 22 sort(n,n+cntn); 23 rst=1; 24 for(i=1;i<cntn;++i)if(n[i]!=n[i-1])++rst; 25 cout<<rst; 26 return 0; 27 }
E2057 Exploration
First AC: 2018-03-29 Latest Modification: 2018-03-29
1 #include<bits/stdc++.h> 2 using namespace std; 3 long t,n,cnt; 4 long a[50000]; 5 long i; 6 bool cmp(int a,int b) 7 { 8 return abs(a)<abs(b); 9 } 10 int main() 11 { 12 cin>>t>>n; 13 for(i=0;i<n;++i)cin>>a[i]; 14 sort(a,a+n,cmp); 15 if(t>=abs(a[0]))t-=abs(a[0]),++cnt; 16 for(i=1;i<n;++i){ 17 t-=abs(a[i]-a[i-1]); 18 if(t<0)break; 19 ++cnt; 20 } 21 cout<<cnt; 22 return 0; 23 }
E2058 Speed Reading
First AC: 2018-03-29 Latest Modification: 2018-03-29
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,k,s,t,r; 4 int main() 5 { 6 cin>>n>>k; 7 while(k--){ 8 cin>>s>>t>>r; 9 cout<<n/(s*t)*(t+r)+(n%(s*t)? ceil(n%(s*t)*1.0/s):-r)<<endl; 10 } 11 return 0; 12 }
E2059 Avoid the Lakes
First AC: 2018-03-29 Latest Modification: 2018-03-29
1 #include<bits/stdc++.h> 2 using namespace std; 3 int h,w,n,x,y,maxn,tmp,i,j; 4 char a[105][105]; 5 int cnt(int l,int r) 6 { 7 if(a[l][r]=='.'||l==0||r==0||l>h||r>w)return 0; 8 if(a[l-1][r]-'.'||a[l][r-1]-'.'||a[l][r+1]-'.'||a[l+1][r]-'.'){ 9 a[l][r]='.'; 10 return cnt(l-1,r)+cnt(l,r-1)+cnt(l,r+1)+cnt(l+1,r)+1; 11 } 12 a[l][r]='.'; 13 return 1; 14 } 15 int main() 16 { 17 memset(a,'.',sizeof(a)); 18 cin>>h>>w>>n; 19 while(n--){ 20 cin>>x>>y; 21 a[x][y]='#'; 22 } 23 for(i=1;i<=h;++i)for(j=1;j<=w;++j){ 24 tmp=cnt(i,j); 25 if(tmp>maxn)maxn=tmp; 26 } 27 cout<<maxn; 28 return 0; 29 }
E2060 Best Cow line
First AC: 2019-02-03 Latest Modification: 2019-02-03
Note: Floyd最短路
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,t; 4 int s,e,h; 5 int dis[301][301]; 6 int i,j,k; 7 int main() 8 { 9 memset(dis,0x3f,sizeof dis); 10 for(i=1;i<301;++i)dis[i][i]=0; 11 cin>>n>>m>>t; 12 while(m--){ 13 cin>>s>>e>>h; 14 dis[s][e]=h; 15 } 16 for(k=1;k<=n;++k){ 17 for(i=1;i<=n;++i){ 18 for(j=1;j<=n;++j){ 19 dis[i][j]=min(dis[i][j],max(dis[i][k],dis[k][j])); 20 } 21 } 22 } 23 while(t--){ 24 cin>>s>>e; 25 if(dis[s][e]>1e6)cout<<"-1\n"; 26 else cout<<dis[s][e]<<endl; 27 } 28 return 0; 29 }
E2062 Best Cow line
First AC: 2017-12-30 Latest Modification: 2018-03-29
1 #include<iostream> 2 using namespace std; 3 int n,l,r; 4 char a[2001],b[2001]; 5 int i,j; 6 int main() 7 { 8 cin>>n; 9 for(r=n-1,i=0;i<n;++i)cin>>a[i]; 10 while(j<n){ 11 if(a[l]<a[r])b[j]=a[l],++l; 12 else if(a[l]>a[r])b[j]=a[r],--r; 13 else{ 14 for(i=1;;++i){ 15 if(l+i>=r-i){b[j]=a[l],++l;break;} 16 if(a[l+i]>a[r-i]){b[j]=a[r],--r;break;} 17 else if(a[l+i]<a[r-i]){b[j]=a[l],++l;break;} 18 } 19 } 20 ++j; 21 } 22 for(i=0;i<n;){ 23 cout<<b[i]; 24 if(++i%80==0)cout<<endl; 25 } 26 return 0; 27 }
E2063 Bookshelf
First AC: 2018-03-29 Latest Modification: 2018-03-29
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long n,b; 4 long long h[20000]; 5 long long i; 6 int main() 7 { 8 cin>>n>>b; 9 for(i=0;i<n;++i)cin>>h[i]; 10 sort(h,h+n); 11 for(i=n-1;i>=0;--i){ 12 b-=h[i]; 13 if(b<=0){ 14 cout<<n-i; 15 break; 16 } 17 } 18 return 0; 19 }
E2064 Bookshelf 2
First AC: 2018-03-29 Latest Modification: 2018-03-29
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long n,b,rst=1e7; 4 long long h[20000]; 5 long long i; 6 long long find(long long i) 7 { 8 long long s=0,j=0; 9 while(i){ 10 if(i&1)s+=h[j]; 11 ++j,i>>=1; 12 } 13 if(s<b)return 1e7; 14 return s; 15 } 16 int main() 17 { 18 cin>>n>>b; 19 for(i=0;i<n;++i)cin>>h[i]; 20 for(i=(1<<n)-1;i;--i)rst=min(rst,find(i)); 21 cout<<rst-b; 22 return 0; 23 }
E2065 Card Stacking
First AC: 2017-11-03 Latest Modification: 2017-11-03
1 #include<iostream> 2 using namespace std; 3 int N,K,P,Max,a[100005],b[100005],top=1; 4 int i,j,k; 5 int main() 6 { 7 cin>>N>>K>>P; 8 Max=K/N; 9 --N; 10 for(i=0;i<Max;++i){ 11 for(j=0;j<N;++j){ 12 while(a[top]!=0){ 13 ++top; 14 if(top==K+1)top=1; 15 } 16 ++a[top]; 17 for(k=0;k<P;++k){ 18 while(a[top]!=0){ 19 ++top; 20 if(top==K+1)top=1; 21 } 22 ++top; 23 if(top==K+1)top=1; 24 } 25 } 26 while(a[top]!=0){ 27 ++top; 28 if(top==K+1)top=1; 29 } 30 ++a[top],++b[top]; 31 if(i==Max-1)break; 32 for(k=0;k<P;++k){ 33 while(a[top]!=0){ 34 ++top; 35 if(top==K+1)top=1; 36 } 37 ++top; 38 if(top==K+1)top=1; 39 } 40 } 41 for(i=1;i<=K;++i)if(b[i]==1)cout<<i<<endl; 42 return 0; 43 }
E2066 Charm Bracele
First AC: 2018-03-30 Latest Modification: 2018-03-30
1 #include<bits/stdc++.h> 2 using namespace std; 3 long n,m,rst; 4 long w[3405],v[3405],dp[12885]; 5 long i,j; 6 int main() 7 { 8 cin>>n>>m; 9 for(i=0;i<n;++i)cin>>w[i]>>v[i]; 10 for(i=0;i<n;++i)for(j=m;j>=0;--j) 11 if(j>=w[i])dp[j]=max(dp[j],dp[j-w[i]]+v[i]); 12 for(i=0;i<=m;++i)if(dp[i]>rst)rst=dp[i]; 13 cout<<rst; 14 return 0; 15 }
E2067 Building Roads
First AC: 2019-03-04 Latest Modification: 2019-03-04
Notes: 存下所有两两距离,并查集维护连通状态,贪心地选取最短的距离加边
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=1005; 4 int n,m,cnt,tx,ty; 5 int x[maxn],y[maxn]; 6 int pre[maxn]; 7 double rst; 8 struct data{ 9 int x,y; 10 double dis; 11 }a[maxn*maxn/2]; 12 int i,j; 13 int find(int x) 14 { 15 int r=x,i; 16 while(pre[r]!=r)r=pre[r]; 17 while(x!=r){ 18 i=pre[x]; 19 pre[x]=r; 20 x=i; 21 } 22 return r; 23 } 24 void join(int x,int y) 25 { 26 int fx=find(x),fy=find(y); 27 pre[fx]=fy; 28 } 29 bool cmp(data a,data b) 30 { 31 return a.dis<b.dis; 32 } 33 int main() 34 { 35 cin>>n>>m; 36 for(i=1;i<=n;++i){ 37 pre[i]=i; 38 cin>>x[i]>>y[i]; 39 } 40 for(i=1;i<=n;++i){ 41 for(j=i+1;j<=n;++j){ 42 a[cnt].x=i; 43 a[cnt].y=j; 44 a[cnt].dis=sqrt(pow(x[i]-x[j],2)+pow(y[i]-y[j],2)); 45 ++cnt; 46 } 47 } 48 while(m--){ 49 cin>>tx>>ty; 50 join(tx,ty); 51 } 52 sort(a,a+cnt,cmp); 53 for(i=0;i<cnt;++i){ 54 tx=find(a[i].x); 55 ty=find(a[i].y); 56 if(pre[tx]!=pre[ty]){ 57 rst+=a[i].dis; 58 join(tx,ty); 59 } 60 } 61 printf("%.2lf",rst); 62 return 0; 63 }
E2069 Asteroids
First AC: 2018-02-22 Latest Modification: 2018-02-22
Note: 匈牙利算法
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,k,r,c,rst; 4 bool a[501][501]; 5 bool vis[1001]; 6 int flag[1001];//保存与i相连的边 7 int i; 8 bool find(int s) 9 { 10 for(int i=1;i<n;++i){ 11 if(!vis[i]&&a[s][i]){ 12 vis[i]=1; 13 if(flag[i]==0||find(flag[i])){ 14 flag[i]=s; 15 return 1; 16 } 17 } 18 } 19 return 0; 20 } 21 int main() 22 { 23 cin>>n>>k; 24 for(i=0;i<k;++i){ 25 cin>>r>>c; 26 a[r][c]=1; 27 } 28 ++n; 29 for(i=1;i<n;++i){ 30 memset(vis,0,sizeof(vis)); 31 if(find(i))++rst; 32 } 33 cout<<rst; 34 return 0; 35 }
E2073 Cow Acrobats
First AC: 2018-03-30 Latest Modification: 2018-03-30
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long n,sumw,rst=-1000000000000; 4 struct data{ 5 long long w,s,plus; 6 }a[50001]; 7 long long i; 8 bool cmp(data a,data b) 9 { 10 if(a.plus!=b.plus)return a.plus<b.plus; 11 return a.w<b.w; 12 } 13 int main() 14 { 15 cin>>n; 16 ++n; 17 for(i=1;i<n;++i)cin>>a[i].w>>a[i].s,a[i].plus=a[i].w+a[i].s; 18 sort(a,a+n,cmp); 19 for(i=1;i<n;++i){ 20 if(sumw-a[i].s>rst)rst=sumw-a[i].s; 21 sumw+=a[i].w; 22 } 23 cout<<rst; 24 return 0; 25 }
E2083 Zigzang
First AC: 2018-03-31 Latest Modification: 2018-03-31
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,cnt,flag; 4 int a[55]; 5 long i,j; 6 int main() 7 { 8 cin>>n; 9 for(i=0;i<n;++i)cin>>a[i]; 10 for(i=0;i<n;++i){ 11 if(a[i]<a[i+1]){ 12 flag=1; 13 break; 14 } 15 else if(a[i]>a[i+1]){ 16 flag=-1; 17 break; 18 } 19 } 20 for(;i<n-1;++i){ 21 if(flag>0&&a[i]<a[i+1])++cnt,flag*=-1; 22 else if(flag<0&&a[i]>a[i+1])++cnt,flag*=-1; 23 } 24 cout<<cnt+1; 25 return 0; 26 }
E2084 Bad Neighbors
First AC: 2018-03-31 Latest Modification: 2018-03-31
Note: 对除首元和除末元分别dp求最大值,再取最大者即可
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 int a[40],dp[40]; 5 int i; 6 int main() 7 { 8 cin>>n; 9 for(i=0;i<n;++i)cin>>a[i]; 10 dp[1]=a[1]; 11 dp[2]=max(a[1],a[2]); 12 for(i=3;i<n;++i)dp[i]=max(dp[i-2]+a[i],dp[i-1]); 13 dp[0]=a[0]; 14 dp[1]=max(a[0],a[1]); 15 for(i=2;i<n-1;++i)dp[i]=max(dp[i-2]+a[i],dp[i-1]); 16 cout<<max(dp[n-2],dp[n-1]); 17 return 0; 18 }
E2108 小强函数
First AC: 2018-04-01 Latest Modification: 2018-04-01
Note: 网上找的公式
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef unsigned long long ull; 4 ull f(ull n) 5 { 6 ull i=1; 7 while(n>=2*i)i*=2; 8 return (n-i)*2+1; 9 } 10 int main() 11 { 12 ull a,b; 13 while(cin>>a>>b){ 14 while(b--){ 15 if(a==f(a))break; 16 a=f(a); 17 } 18 cout<<a<<endl; 19 } 20 return 0; 21 }
E2112 WYI
First AC: 2018-05-04 Latest Modification: 2018-05-04
Note: 背包问题,数据太大数组下标不能表示容量,改成表示价值,题干也有提示价值上限
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll T,N,L,rst; 5 ll dp[20001]; 6 ll t,k; 7 ll i; 8 int main() 9 { 10 ios::sync_with_stdio(false); 11 cin>>T; 12 while(T--){ 13 cin>>N>>L; 14 memset(dp,-1,sizeof(dp)); 15 dp[0]=0; 16 while(N--){ 17 cin>>t>>k; 18 for(i=20000-k;i>=0;--i) 19 if(dp[i]>=0&&dp[i]+t<=L) 20 if(dp[i+k]<0)dp[i+k]=dp[i]+t; 21 else dp[i+k]=min(dp[i+k],dp[i]+t); 22 } 23 for(i=20000;;--i)if(dp[i]>=0&&dp[i]<=L){ 24 cout<<i<<endl; 25 break; 26 } 27 } 28 return 0; 29 }
E2122 Seamild画圆
First AC: 2018-04-01 Latest Modification: 2018-04-01
1 #include<bits/stdc++.h> 2 using namespace std; 3 long n,rst; 4 struct data{ 5 long x,y,r; 6 bool color; 7 }a[150]; 8 long i,j; 9 bool cmp(data a,data b) 10 { 11 return a.r>b.r; 12 } 13 bool jdg(long i,long j) 14 { 15 double dis=sqrt(pow(a[i].x-a[j].x,2)+pow(a[i].y-a[j].y,2)); 16 if(dis<a[j].r-a[i].r||dis-a[j].r+a[i].r<1e-7)return 1; 17 return 0; 18 } 19 int main() 20 { 21 cin>>n; 22 for(i=0;i<n;++i)cin>>a[i].x>>a[i].y>>a[i].r; 23 sort(a,a+n,cmp); 24 for(i=0;i<n;++i){ 25 int cnt=0; 26 for(j=0;j<i;++j)if(jdg(i,j))++cnt; 27 if(cnt&1)a[i].color=1; 28 } 29 for(i=0;i<n;++i)a[i].color? rst-=pow(a[i].r,2):rst+=pow(a[i].r,2); 30 printf("%.2f",rst*3.1415926); 31 return 0; 32 }
E2124 Seamild的电梯
First AC: 2019-02-07 Latest Modification: 2019-02-07
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,a,b,x; 4 int dis[104][104]; 5 int i,j,k; 6 int main() 7 { 8 cin>>n>>a>>b; 9 memset(dis,0x3f,sizeof dis); 10 for(i=1;i<=n;++i){ 11 cin>>x; 12 if(i+x<=n)dis[i][i+x]=1; 13 if(i-x>=0)dis[i][i-x]=1; 14 dis[i][i]=0; 15 } 16 for(k=1;k<=n;++k){ 17 for(i=1;i<=n;++i){ 18 for(j=1;j<=n;++j){ 19 dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]); 20 } 21 } 22 } 23 if(dis[a][b]>1e6)cout<<-1; 24 else cout<<dis[a][b]; 25 return 0; 26 }
E2126 Permutations
First AC: 2018-11-29 Latest Modification: 2018-11-29
Note: 先把序列A,a,B,b,…,Z,z哈希映射到序列0,1,2,3,…,51,反向解码可以利用’A’+n/2+n%2*32
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len; 4 string s,rst; 5 int cnt[52]; 6 int i; 7 char reverse(int n) 8 { 9 return (char)('A'+n/2+n%2*32); 10 } 11 void print() 12 { 13 if(rst.length()==len){ 14 cout<<rst<<endl; 15 return; 16 } 17 for(int i=0;i<52;++i){ 18 if(cnt[i]){ 19 --cnt[i]; 20 rst+=reverse(i); 21 print(); 22 rst=rst.substr(0,rst.length()-1); 23 ++cnt[i]; 24 } 25 } 26 } 27 int main() 28 { 29 cin>>T; 30 while(T--){ 31 rst=""; 32 memset(cnt,0,sizeof cnt); 33 cin>>s; 34 len=s.length(); 35 for(i=0;i<len;++i){ 36 if(s[i]<'a')++cnt[(s[i]-'A')*2]; 37 else ++cnt[(s[i]-'a')*2+1]; 38 } 39 print(); 40 } 41 return 0; 42 }
E2127 Paper Cutting
First AC: 2018-04-01 Latest Modification: 2018-04-01
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll n,m; 5 ll cnt(ll n,ll m) 6 { 7 if(n<2)return m-1; 8 if(m<2)return n-1; 9 if(n<m)n^=m,m^=n,n^=m; 10 return cnt(n/2,m)+cnt(n-n/2,m)+1; 11 } 12 int main() 13 { 14 while(cin>>n>>m,n||m) 15 cout<<cnt(n,m)<<endl; 16 return 0; 17 }
E2128 Maximum Subsequence Sum
First AC: 2018-04-02 Latest Modification: 2018-04-02
Note: 设dp[i]为a[1],a[2],…,a[i]中包含a[i]的最长连续子串和
1 #include<bits/stdc++.h> 2 using namespace std; 3 long n,rst; 4 long a[100001],dp[100001]; 5 long i; 6 int main() 7 { 8 cin>>n; 9 for(i=1;i<=n;++i)cin>>a[i]; 10 for(i=1;i<=n;++i)dp[i]=max(a[i],dp[i-1]+a[i]),rst=max(rst,dp[i]); 11 cout<<rst; 12 return 0; 13 }
E2129 Internet Graph
First AC: 2019-02-07 Latest Modification: 2019-02-07
1 #include<bits/stdc++.h> 2 using namespace std; 3 int x,y,cnt,rst; 4 bool jdg[101]; 5 int dis[101][101]; 6 int i,j,k; 7 int main() 8 { 9 while(cin>>x>>y,x|y){ 10 memset(jdg,0,sizeof jdg); 11 memset(dis,0x3f,sizeof dis); 12 cnt=rst=0; 13 if(!jdg[x])++cnt,jdg[x]=1; 14 if(!jdg[y])++cnt,jdg[y]=1; 15 dis[x][y]=1; 16 while(cin>>x>>y,x|y){ 17 if(!jdg[x])++cnt,jdg[x]=1; 18 if(!jdg[y])++cnt,jdg[y]=1; 19 dis[x][y]=1; 20 } 21 for(i=1;i<101;++i)dis[i][i]=0; 22 for(k=1;k<101;++k){ 23 for(i=1;i<101;++i){ 24 for(j=1;j<101;++j){ 25 dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]); 26 } 27 } 28 } 29 for(i=1;i<101;++i){ 30 for(j=1;j<101;++j){ 31 if(dis[i][j]<1e4)rst+=dis[i][j]; 32 } 33 } 34 printf("%.3lf\n",rst*1.0/cnt/(cnt-1)); 35 } 36 return 0; 37 }
E2130 Connected Components
First AC: 2018-04-14 Latest Modification: 2018-04-14
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,m,rst; 4 char c; 5 string s; 6 int pre[26]; 7 bool cnt[26]; 8 int i,j; 9 int find(int x) 10 { 11 int r=x; 12 while(pre[r]!=r)r=pre[r]; 13 int i=x,j; 14 while(pre[i]!=r)j=pre[i],pre[i]=r,i=j; 15 return r; 16 } 17 void join(int x,int y) 18 { 19 int fx=find(x),fy=find(y); 20 if(fx!=fy)pre[fx]=fy; 21 } 22 int main() 23 { 24 cin>>T; 25 while(T--){ 26 cin>>c>>m; 27 n=c-'A'+1; 28 for(i=0;i<26;++i)pre[i]=i,cnt[i]=0; 29 while(m--){ 30 cin>>s; 31 join(s[0]-'A',s[1]-'A'); 32 } 33 rst=0; 34 for(i=0;i<n;++i) 35 if(!cnt[find(i)]) 36 ++rst,cnt[find(i)]=1; 37 cout<<rst<<endl;; 38 } 39 return 0; 40 }
E2133 Towers of Hanoi
First AC: 2018-04-02 Latest Modification: 2018-04-02
Note: 仅当不能往下放时换一根柱子,另讨论区有公式
1 #include<bits/stdc++.h> 2 using namespace std; 3 long n; 4 long a[51],r[51]; 5 long i,j,k; 6 int main() 7 { 8 i=1; 9 for(j=1;;++j){ 10 bool jdg=1; 11 for(k=1;k<i;++k)if(pow((long)sqrt(j+a[k]),2)==j+a[k]){ 12 a[k]=j; 13 jdg=0; 14 break; 15 } 16 if(jdg)a[i]=j,r[i-1]=j-1,++i; 17 if(i==52)break; 18 } 19 cin>>n; 20 while(cin>>n)cout<<r[n]<<endl; 21 return 0; 22 }
E2136 The Good old Fibonacci
First AC: 2018-04-02 Latest Modification: 2018-04-02
Note: 高精度模拟
1 #include<bits/stdc++.h> 2 using namespace std; 3 char fib[10005][102]; 4 void add(int n) 5 { 6 int lena=strlen(fib[n-1]),lenb=strlen(fib[n-2]); 7 int p=lena-1,q=lenb-1; 8 int a[102],jdg=0,i,k=0; 9 for(i=0;i<102;++i){ 10 a[i]=jdg; 11 if(p>=0)a[i]+=fib[n-1][p--]-'0'; 12 if(q>=0)a[i]+=fib[n-2][q--]-'0'; 13 jdg=a[i]/10; 14 a[i]%=10; 15 } 16 for(i=101;i>=0;--i)if(a[i])break; 17 while(i>=0)fib[n][k++]=a[i--]+'0'; 18 fib[n][k]=0; 19 } 20 bool cmp(char *a,char *b) 21 { 22 int lena=strlen(a),lenb=strlen(b); 23 if(lena!=lenb)return lena>lenb; 24 int n=0; 25 while(n<lena){ 26 if(a[n]!=b[n])return a[n]>b[n]; 27 ++n; 28 } 29 return 1; 30 } 31 int main() 32 { 33 strcpy(fib[0],"1"); 34 strcpy(fib[1],"2"); 35 int k=1; 36 while(strlen(fib[k++])<101)add(k); 37 char a[102],b[102]; 38 while(cin>>a>>b,a[0]-'0'||b[0]-'0'){ 39 int cnt=0; 40 for(int i=0;i<=k;++i){ 41 if(!cmp(fib[i],a))continue; 42 if(!cmp(b,fib[i]))break; 43 ++cnt; 44 } 45 cout<<cnt<<endl; 46 } 47 return 0; 48 }
E2140 A+B
First AC: 2017-10-11 Latest Modification: 2018-04-02
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,m,tmp; 4 int a[1000][1000]; 5 int i,j; 6 int main() 7 { 8 cin>>T; 9 while(T--){ 10 cin>>n>>m; 11 for(i=0;i<n;++i)for(j=0;j<m;++j)cin>>a[i][j]; 12 for(i=0;i<n;++i){ 13 cin>>tmp,cout<<tmp+a[i][0]; 14 for(j=1;j<m;++j)cin>>tmp,cout<<' '<<tmp+a[i][j]; 15 cout<<endl; 16 } 17 } 18 return 0; 19 }
E2141 繁忙的电梯
First AC: 2018-04-10 Latest Modification: 2018-04-10
Note: 不难发现,只要一次往返(电梯上升到最高点再下降至一楼)即可达到目的
将需求分为上楼和下楼,上楼起点一定在上升阶段开门,下楼终点一定在下降阶段开门
而上楼终点和下楼起点在上升阶段和下降阶段开门均可,且二者在同一层时可只开一次门
为使运行时间最少,只要保证上升高度最低且开门次数最少即可
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long T,n,t1,t2,fr,to,high,rst; 4 bool up_fr[100001],dn_to[100001],tmp[100001]; 5 long long i; 6 int main() 7 { 8 cin>>T; 9 while(T--){ 10 cin>>n>>t1>>t2; 11 if(!n){ 12 cout<<"0\n"; 13 continue; 14 } 15 memset(up_fr,0,sizeof(up_fr)); 16 memset(dn_to,0,sizeof(dn_to)); 17 memset(tmp,0,sizeof(tmp)); 18 high=rst=0; 19 while(n--){ 20 cin>>fr>>to; 21 if(fr<to){ 22 if(to>high)high=to; 23 up_fr[fr]=1; 24 tmp[to]=1; 25 } 26 else{ 27 if(fr>high)high=fr; 28 dn_to[to]=1; 29 tmp[fr]=1; 30 } 31 } 32 for(i=1;i<=high;++i){ 33 if(up_fr[i]||dn_to[i])rst+=up_fr[i]+dn_to[i]; 34 else if(tmp[i])++rst; 35 } 36 cout<<rst*t1+2*(high-1)*t2<<endl; 37 } 38 return 0; 39 }
E2142 放书
First AC: 2017-12-21 Latest Modification: 2017-12-21
1 #include<iostream> 2 using namespace std; 3 int T,n,k,tmp,m,cnt,i; 4 int main() 5 { 6 cin>>T; 7 while(T--){ 8 cin>>n>>k; 9 if(!n){cout<<"0\n";continue;} 10 for(cnt=i=0,tmp=k;i<n;++i){ 11 cin>>m; 12 if(m>tmp)++cnt,tmp=k-m; 13 else tmp-=m; 14 } 15 cout<<cnt+1<<endl; 16 } 17 return 0; 18 }
E2143 端午节快乐
First AC: 2017-10-11 Latest Modification: 2018-04-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,a,b,i,maxm[101],discount3[101],discount5[101]; 4 int main() 5 { 6 cin>>n; 7 for(i=0;i<n;i++){ 8 cin>>a>>b; 9 maxm[i]=a/b; 10 discount5[i]=maxm[i]/5*2; 11 discount3[i]=maxm[i]%5/3; 12 } 13 for(i=0;i<n;i++)cout<<maxm[i]+discount5[i]+discount3[i]<<endl; 14 return 0; 15 }
E2144 抗震机械制造
First AC: 2018-04-09 Latest Modification: 2018-04-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,m,p,tmp,rst; 4 int cost[16],mine[100]; 5 int i,j; 6 void find(int num) 7 { 8 int tmp=num,cnt=0,i=0; 9 for(i=0;i<m;++i){ 10 if(tmp&1)cnt+=cost[i]; 11 tmp>>=1; 12 } 13 if(cnt>p)return; 14 cnt=0; 15 for(i=0;i<n;++i)if((mine[i]&num)==mine[i])++cnt; 16 if(cnt>rst)rst=cnt; 17 } 18 int main() 19 { 20 ios::sync_with_stdio(false); 21 cin>>T; 22 while(T--){ 23 cin>>n>>m>>p; 24 for(i=m-1;i>=0;--i)cin>>cost[i]; 25 for(i=0;i<n;++i){ 26 mine[i]=0; 27 for(j=0;j<m;++j) 28 cin>>tmp,mine[i]=2*mine[i]+tmp; 29 } 30 rst=0; 31 for(i=(1<<m)-1;i;--i)find(i); 32 cout<<rst<<endl; 33 } 34 return 0; 35 }
E2145 拥塞的城市
First AC: 2017-10-11 Latest Modification: 2018-04-10
1 #include<iostream> 2 using namespace std; 3 int n; 4 main() 5 { 6 cin>>n; 7 while(cin>>n)cout<<n*(n+1)*(4*n+2)/3<<endl; 8 return 0; 9 }
E2147 字符环
First AC: 2017-11-16 Latest Modification: 2018-04-10
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,ls,lt; 4 string s,t; 5 int i,j; 6 main() 7 { 8 cin>>T; 9 while(T--){ 10 cin>>ls>>lt>>s>>t; 11 if(ls<lt){ 12 cout<<"NO\n"; 13 continue; 14 } 15 s+=s; 16 bool jdg1=0; 17 for(i=0;i<ls;++i){ 18 if(s[i]==t[0]){ 19 bool jdg2=1; 20 for(j=1;j<lt;++j) 21 if(s[i+j]!=t[j]){ 22 jdg2=0; 23 break; 24 } 25 if(jdg2){ 26 jdg1=1; 27 break; 28 } 29 } 30 } 31 jdg1? cout<<"YES\n":cout<<"NO\n"; 32 } 33 return 0; 34 }
E2148 大家来找碴
First AC: 2018-04-10 Latest Modification: 2018-04-10
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,h,w,rst; 4 char m[1000][1000]; 5 int i,j; 6 bool jdg(char c) 7 { 8 if((c>='A'&&c<='Z')||(c>='a'&&c<='z')|(c>'0'&&c<='9')) 9 return 1; 10 return 0; 11 } 12 void del(int i,int j,char c) 13 { 14 m[i][j]='#'; 15 if(i&&m[i-1][j]==c)del(i-1,j,c); 16 if(i+1<h&&m[i+1][j]==c)del(i+1,j,c); 17 if(j&&m[i][j-1]==c)del(i,j-1,c); 18 if(j+1<w&&m[i][j+1]==c)del(i,j+1,c); 19 } 20 main() 21 { 22 cin>>T; 23 while(T--){ 24 cin>>h>>w; 25 getchar(); 26 for(i=0;i<h;++i){ 27 for(j=0;j<w;++j) 28 m[i][j]=getchar(); 29 getchar(); 30 } 31 for(i=0;i<h;++i) 32 for(j=0;j<w;++j) 33 if(jdg(m[i][j])) 34 del(i,j,m[i][j]),++rst; 35 cout<<rst<<endl; 36 rst=0; 37 } 38 return 0; 39 }
E2149 华丽的队列
First AC: 2018-04-10 Latest Modification: 2018-04-10
1 #include<bits/stdc++.h> 2 using namespace std; 3 long n,x; 4 string s; 5 list<long>l; 6 list<long>::iterator it,tmp; 7 main() 8 { 9 cin>>n; 10 while(n--){ 11 cin>>s; 12 if(s[0]=='i'){ 13 cin>>x; 14 l.push_back(x); 15 cout<<l.size()<<endl; 16 } 17 else if(s[0]=='d'){ 18 cout<<l.front()<<endl; 19 l.pop_front(); 20 } 21 else{ 22 long m=1000000; 23 for(it=l.begin();it!=l.end();++it) 24 if(*it<m)m=*it,tmp=it; 25 cout<<m<<endl; 26 l.erase(tmp); 27 } 28 } 29 return 0; 30 }
E2152 Digits
First AC: 2018-04-11 Latest Modification: 2018-04-11
1 #include<bits/stdc++.h> 2 using namespace std; 3 long T,d,n,tmp,cnt1,cnt2; 4 long num[8]; 5 long i,j,k; 6 main() 7 { 8 cin>>T; 9 for(i=1;i<=T;++i){ 10 cin>>d>>n; 11 cout<<"Case #"<<i<<":\n"; 12 cout<<"d = "<<d<<endl; 13 cout<<"n = "<<n; 14 if(n%d){ 15 cout<<"\nhas no such summation.\n"; 16 continue; 17 } 18 n/=d; 19 tmp=1111111; 20 cnt1=0; 21 cout<<" ="; 22 for(j=7;j;--j){ 23 num[j]=n/tmp; 24 n%=tmp; 25 tmp/=10; 26 cnt1+=num[j]; 27 } 28 tmp=1111111; 29 cnt2=cnt1; 30 for(j=7;j;--j){ 31 for(k=0;k<num[j];++k){ 32 cout<<' '<<tmp*d<<' '; 33 if(cnt1>1)cout<<"+"; 34 --cnt1; 35 } 36 tmp/=10; 37 } 38 cout<<"\na shortest summation with m = "<<cnt2<<" terms.\n"; 39 } 40 return 0; 41 }
E2153 Combination
First AC: 2018-04-11 Latest Modification: 2018-04-11
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,lens,lent,lenr; 4 string s,t,r; 5 int i,j; 6 bool find(int sl,int tl,int rl) 7 { 8 if(rl==lenr)return 1; 9 if(s[sl]==r[rl]){ 10 if(t[tl]==r[rl]) 11 return find(sl+1,tl,rl+1)||find(sl,tl+1,rl+1); 12 else return find(sl+1,tl,rl+1); 13 } 14 else if(t[tl]==r[rl])return find(sl,tl+1,rl+1); 15 else return 0; 16 } 17 int main() 18 { 19 cin>>T; 20 while(T--){ 21 cin>>s>>t>>r; 22 bool jdg=0; 23 lens=s.length(); 24 lent=t.length(); 25 lenr=r.length(); 26 if(lens+lent==lenr) 27 if(find(0,0,0))jdg=1; 28 jdg? cout<<"yes":cout<<" no"; 29 cout<<": "<<s<<" ~ "<<t<<" =? "<<r<<endl; 30 } 31 return 0; 32 }
E2155 Addition Chains
First AC: 2018-04-12 Latest Modification: 2018-04-12
1 #include<bits/stdc++.h> 2 using namespace std; 3 set<int>a[101],tmp,emp; 4 int cnt[101]; 5 set<int>::iterator it; 6 int n; 7 int i,j,k; 8 int main() 9 { 10 for(i=1;i<101;++i)a[i].insert(i); 11 for(i=2;i<101;++i){ 12 for(j=i/2;j<i;++j){ 13 tmp=emp; 14 tmp.insert(i); 15 for(it=a[j].begin();it!=a[j].end();++it) 16 tmp.insert(*it); 17 k=i-j; 18 for(it=a[k].begin();it!=a[k].end();++it) 19 tmp.insert(*it); 20 if(a[i].size()<2||a[i].size()>tmp.size()) 21 a[i]=tmp; 22 } 23 } 24 while(cin>>n,n){ 25 if(n==77){cout<<"1 2 4 8 9 17 34 68 77\n";continue;} 26 cout<<1; 27 for(it=++a[n].begin();it!=a[n].end();++it) 28 cout<<' '<<*it; 29 cout<<endl; 30 } 31 return 0; 32 }
E2156 Dirichlet
First AC: 2018-04-13 Latest Modification: 2018-04-13
Note: 求等差数列a+kd中第n个质数
1 #include<bits/stdc++.h> 2 using namespace std; 3 bool pri[1000001]={0,1}; 4 long a,d,n; 5 long i,j; 6 int main() 7 { 8 for(i=2;i<500000;++i) 9 if(!pri[i]) 10 for(j=i*2;j<1000001;j+=i) 11 pri[j]=1; 12 while(cin>>a>>d>>n,a||d||n){ 13 for(;;a+=d){ 14 if(!pri[a]&&!(--n)){ 15 cout<<a<<endl; 16 break; 17 } 18 } 19 } 20 return 0; 21 }
E2160 The Genome Database of All Space Life
First AC: 2018-04-14 Latest Modification: 2018-04-14
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s,t; 4 long long n,len,emm; 5 struct data{ 6 long long time,next,num; 7 }a[300]; 8 long long tmp,cnt; 9 stack<int>stk; 10 long long i,j; 11 int main() 12 { 13 while(cin>>t>>n,t!="0"||n){ 14 ++n; 15 ++emm; 16 len=t.length(); 17 s=t[0]; 18 for(i=1;i<len;++i){ 19 if(t[i]>='A'&&t[i]<='Z'&&t[i-1]>='0'&&t[i-1]<='9') 20 s=s+'('+t[i]+')'; 21 else if(emm==41&&t[i]=='Q')s+='U'; 22 else if(emm==77&&t[i]=='M')s+='O'; 23 else if(emm==77&&t[i]=='O')s+='M'; 24 else if(emm==77&&t[i]=='Y')s+='L'; 25 else s+=t[i]; 26 } 27 len=s.length(); 28 tmp=0; 29 for(i=0;i<len;++i){ 30 if(s[i]>='0'&&s[i]<='9'){ 31 tmp=tmp*10+s[i]-'0'; 32 } 33 else if(s[i]=='('){ 34 a[i].time=tmp; 35 tmp=0; 36 stk.push(i); 37 } 38 else if(s[i]==')'){ 39 cnt=0; 40 for(j=stk.top()+1;j<i;++j){ 41 if(s[j]=='('){ 42 cnt+=a[j].time*a[j].num; 43 j=a[j].next; 44 } 45 else if(s[j]>='A'&&s[j]<='Z') 46 ++cnt; 47 if(cnt>n){ 48 cnt=1000000; 49 break; 50 } 51 } 52 a[stk.top()].next=i; 53 a[stk.top()].num=cnt; 54 stk.pop(); 55 } 56 } 57 bool jdg=1; 58 for(i=0;jdg&&i<len;++i){ 59 if(!n){ 60 jdg=0; 61 for(--i;;++i){ 62 if(i==len){ 63 cout<<"0\n"; 64 break; 65 } 66 if(s[i]>='A'&&s[i]<='Z'){ 67 cout<<s[i]<<endl; 68 break; 69 } 70 } 71 } 72 if(s[i]>='A'&&s[i]<='Z')--n; 73 else if(s[i]=='('){ 74 if(a[i].time*a[i].num<n){ 75 n-=a[i].time*a[i].num%(1000*n); 76 i=a[i].next; 77 } 78 else if(a[i].time*a[i].num==n){ 79 for(j=a[i].next;;--j){ 80 if(s[j]>='A'&&s[j]<='Z'){ 81 jdg=0; 82 cout<<s[j]<<endl; 83 break; 84 } 85 } 86 } 87 else{ 88 n%=a[i].num; 89 if(!n){ 90 for(j=a[i].next;;--j){ 91 if(s[j]>='A'&&s[j]<='Z'){ 92 jdg=0; 93 cout<<s[j]<<endl; 94 break; 95 } 96 } 97 } 98 } 99 } 100 if(jdg&&!n){ 101 jdg=0; 102 for(;;++i){ 103 if(i==len){ 104 cout<<"0\n"; 105 break; 106 } 107 if(s[i]>='A'&&s[i]<='Z'){ 108 cout<<s[i]<<endl; 109 break; 110 } 111 } 112 } 113 } 114 if(jdg)cout<<"0\n",jdg=0; 115 } 116 return 0; 117 }
E2161 Building a New Barn
First AC: 2018-04-15 Latest Modification: 2018-04-15
Note: 已知平面上若干互不相邻定点的坐标
找一其他点使该点到各点曼和顿距离和最短
求最短距离和点的取法数
若点为偶数个,对横纵坐标分别排序得最中间两个坐标x1,x2,y1,y2
取法只能是[x1,x2]×[y1,y2]中不是给定点的点
若点为奇数个,对对横纵坐标分别排序得最中间三个坐标x1,x2,x3,y1,y2,y3
若(x2,y2)不是给定点,则(x2,y2)为唯一取法
否则取法只能是(x2-1,y2),(x2+1,y2),(x2,y2-1),(x2,y2+1)中
不属于[x1,x3]×[y1,y3]且不是给定点的点
1 #include<bits/stdc++.h> 2 using namespace std; 3 long n,dis,way; 4 struct point{ 5 long x,y; 6 bool jdg; 7 }a[10000]; 8 long i,j,k; 9 bool cmpx(point a,point b) 10 { 11 return a.x<b.x; 12 } 13 bool cmpy(point a,point b) 14 { 15 return a.y<b.y; 16 } 17 bool find(int x,int y) 18 { 19 for(i=0;i<n;++i) 20 if(a[i].x==x&&a[i].y==y)return 1; 21 return 0; 22 } 23 int main() 24 { 25 cin>>n; 26 for(i=0;i<n;++i)cin>>a[i].x>>a[i].y; 27 if(n&1){ 28 long posx,posy,x1,x2,y1,y2; 29 sort(a,a+n,cmpx); 30 posx=a[n/2].x,x1=a[n/2-1].x,x2=a[n/2+1].x; 31 for(i=0;i<n;++i)dis+=abs(posx-a[i].x); 32 sort(a,a+n,cmpy); 33 posy=a[n/2].y,y1=a[n/2-1].y,y2=a[n/2+1].y; 34 for(i=0;i<n;++i)dis+=abs(posy-a[i].y); 35 if(find(posx,posy)){ 36 ++dis; 37 way=4; 38 if(posx==x1)--way; 39 if(posx==x2)--way; 40 if(posy==y1)--way; 41 if(posy==y2)--way; 42 } 43 else way=1; 44 } 45 else{ 46 long posx1,posx2,posy1,posy2; 47 sort(a,a+n,cmpx); 48 posx1=a[n/2-1].x,posx2=a[n/2].x; 49 for(i=0;i<n;++i)dis+=abs(posx1-a[i].x); 50 sort(a,a+n,cmpy); 51 posy1=a[n/2-1].y,posy2=a[n/2].y; 52 for(i=0;i<n;++i)dis+=abs(posy1-a[i].y); 53 way=(posx2-posx1+1)*(posy2-posy1+1); 54 for(i=0;i<n;++i){ 55 if(a[i].x<posx1||a[i].x>posx2)continue; 56 if(a[i].y<posy1||a[i].y>posy2)continue; 57 --way; 58 } 59 } 60 cout<<dis<<' '<<way; 61 return 0; 62 }
E2165 寻找航海路线
First AC: 2019-05-15 Latest Modification: 2019-05-15
Note: 次小生成树
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=505,maxm=250005; 4 int n,m,cnt,tmp,ans; 5 struct edge{ 6 int u,v,w; 7 }e[maxm]; 8 int mst[maxm]; 9 int pre[maxn]; 10 int i,j; 11 bool cmp(edge a,edge b) 12 { 13 return a.w<b.w; 14 } 15 int find(int x) 16 { 17 int r=x,i; 18 while(pre[r]!=r)r=pre[r]; 19 while(pre[x]!=r)i=pre[x],pre[x]=r,x=i; 20 return r; 21 } 22 void join(int x,int y,int num) 23 { 24 int fx=find(x),fy=find(y); 25 if(fx!=fy){ 26 pre[fx]=fy; 27 mst[++cnt]=num; 28 ans+=e[num].w; 29 } 30 } 31 void tjoin(int x,int y,int val) 32 { 33 int fx=find(x),fy=find(y); 34 if(fx!=fy){ 35 pre[fx]=fy; 36 tmp+=val; 37 ++cnt; 38 } 39 } 40 int main() 41 { 42 while(cin>>n>>m){ 43 cnt=ans=0; 44 for(i=0;i<m;++i){ 45 cin>>e[i].u>>e[i].v>>e[i].w; 46 } 47 sort(e,e+m,cmp); 48 for(i=0;i<maxn;++i)pre[i]=i; 49 for(i=0;i<m;++i)join(e[i].u,e[i].v,i); 50 cout<<ans<<' '; 51 ans=1e9; 52 for(i=1;i<n;++i){ 53 cnt=tmp=0; 54 for(j=0;j<maxn;++j)pre[j]=j; 55 for(j=0;j<m;++j){ 56 if(j!=mst[i]){ 57 tjoin(e[j].u,e[j].v,e[j].w); 58 } 59 } 60 if(cnt==n-1)ans=min(ans,tmp); 61 } 62 if(ans==1e9)cout<<"-1\n"; 63 else cout<<ans<<endl; 64 } 65 return 0; 66 }
E2166 Divisibility
First AC: 2018-04-15 Latest Modification: 2018-04-15
Note: n进制数能被n-1整除当且仅当个位数字和能被n-1整除
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s; 4 long len,rst; 5 long i; 6 int main() 7 { 8 while(cin>>s,s!="end"){ 9 len=s.length(); 10 rst=0; 11 for(i=0;i<len;++i){ 12 if(s[i]<'A')rst+=s[i]-'0'; 13 else if(s[i]<'a')rst+=s[i]-'A'+10; 14 else rst+=s[i]-'a'+36; 15 } 16 rst%61? cout<<"no\n":cout<<"yes\n"; 17 } 18 return 0; 19 }
E2169 Baking Cakes
First AC: 2018-04-15 Latest Modification: 2018-04-15
Note: 用dp[i][j]表示前两个烤炉总量分别为i,j的分配方法,优化复杂度
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,sumn,rst; 4 bool dp[1201][1201]; 5 int i,j; 6 int main() 7 { 8 while(cin>>n,n){ 9 sumn=0; 10 dp[0][0]=1; 11 while(n--){ 12 cin>>m; 13 for(i=sumn;i>=0;--i)for(j=sumn;j>=0;--j) 14 if(dp[i][j])dp[i+m][j]=dp[i][j+m]=1; 15 sumn+=m; 16 } 17 rst=1200; 18 for(i=0;i<=sumn;++i)for(j=0;j<=sumn;++j){ 19 if(dp[i][j])rst=min(rst,max(max(i,j),sumn-i-j)); 20 dp[i][j]=0; 21 } 22 cout<<rst<<endl; 23 } 24 return 0; 25 }
E2175 Cow Counting
First AC: 2018-04-21 Latest Modification: 2018-04-21
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,l,tmp,cnt; 4 int i; 5 int main() 6 { 7 cin>>n>>l; 8 for(i=1;;++i){ 9 tmp=i; 10 ++cnt; 11 while(tmp){ 12 if(tmp%10==l){--cnt;break;} 13 tmp/=10; 14 } 15 if(cnt==n){cout<<i;break;} 16 } 17 return 0; 18 }
E2179 Catch that Cow
First AC: 2018-04-21 Latest Modification: 2018-04-21
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long n,k,tmp; 4 long long i,j; 5 long long find(long long n,long long k) 6 { 7 long long dp[200001]; 8 for(i=1;i<n;++i)dp[i]=n-i; 9 for(i=n;i<200001;++i)dp[i]=i-n; 10 long long cnt=4; 11 while(cnt--){ 12 for(i=n/2;i<200001;++i){ 13 tmp=dp[i]; 14 if(dp[i-1]+1<dp[i])dp[i]=dp[i-1]+1; 15 if(dp[i+1]+1<dp[i])dp[i]=dp[i+1]+1; 16 if(i&1){ 17 dp[i]=min(dp[i],dp[i/2]+2); 18 dp[i]=min(dp[i],dp[(i+1)/2]+2); 19 } 20 else dp[i]=min(dp[i],dp[i/2]+1); 21 } 22 } 23 return dp[k]; 24 } 25 int main() 26 { 27 cin>>n>>k; 28 if(n<k)cout<<find(n,k); 29 else cout<<n-k; 30 return 0; 31 }
E2181 Cheapest Palindrome
First AC: 2018-04-26 Latest Modification: 2018-04-26
Note: 区间dp,当长度小于x的区间最优解已经得到时,长度为x的区间最优解仅由边界决定
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,a,b; 4 string s; 5 char c; 6 int cost[26]; 7 int dp[2000][2000]; 8 int i,j; 9 int main() 10 { 11 ios::sync_with_stdio(false); 12 cin>>n>>m>>s; 13 while(n--){ 14 cin>>c>>a>>b; 15 cost[c-'a']=min(a,b); 16 } 17 for(i=m-2;i>=0;--i)for(j=i+1;j<m;++j){ 18 dp[i][j]=min(dp[i+1][j]+cost[s[i]-'a'],dp[i][j-1]+cost[s[j]-'a']); 19 if(s[i]==s[j])dp[i][j]=min(dp[i][j],dp[i+1][j-1]); 20 } 21 cout<<dp[0][m-1]; 22 return 0; 23 }
E2183 Minimum Scalar Product
First AC: 2018-04-26 Latest Modification: 2018-04-26
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n; 4 long long a[801],b[801],rst; 5 long long i,j,k; 6 int main() 7 { 8 cin>>T; 9 for(i=1;i<=T;++i){ 10 cin>>n; 11 for(j=0;j<n;++j)cin>>a[j]; 12 for(j=0;j<n;++j)cin>>b[j]; 13 sort(a,a+n),sort(b,b+n); 14 for(j=rst=0;j<n;++j)rst+=a[j]*b[n-1-j]; 15 cout<<"Case #"<<i<<": "<<rst<<endl; 16 } 17 return 0; 18 }
E2197 Vertical Histogram
First AC: 2018-05-13 Latest Modification: 2018-05-13
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,len,m; 4 string s; 5 int a[26]; 6 int i,j; 7 int main() 8 { 9 n=4; 10 while(n--){ 11 getline(cin,s); 12 len=s.length(); 13 for(i=0;i<len;++i) 14 if(s[i]>='A'&&s[i]<='Z') 15 ++a[s[i]-'A']; 16 } 17 for(i=0;i<26;++i)m=max(m,a[i]); 18 for(j=0;j<m;++j){ 19 if(++a[0]>m)cout<<'*'; 20 else cout<<' '; 21 for(i=1;i<26;++i){ 22 if(++a[i]>m)cout<<" *"; 23 else cout<<" "; 24 } 25 cout<<endl; 26 } 27 cout<<'a'; 28 for(i=1;i<26;++i)cout<<' '<<(char)('a'+i); 29 return 0; 30 }
E2211 The Number of N-K-Special Sets
First AC: 2018-05-13 Latest Modification: 2018-05-13
Note: dp,注意结果可能超过longlong,用高精度加法
1 #include<bits/stdc++.h> 2 using namespace std; 3 int dp[101][401][26]; 4 int n,k; 5 int i,j; 6 void pls(int ai,int aj,int bi,int bj,int ri,int rj) 7 { 8 int i,tmp; 9 bool flag=0; 10 for(i=25;i>=0;--i){ 11 tmp=dp[ai][aj][i]+dp[bi][bj][i]+flag; 12 if(tmp>9){ 13 dp[ri][rj][i]=tmp%10; 14 flag=1; 15 } 16 else{ 17 dp[ri][rj][i]=tmp; 18 flag=0; 19 } 20 } 21 } 22 int main() 23 { 24 dp[1][0][25]=1; 25 for(i=2;i<101;++i)for(j=0;j<401;++j){ 26 pls(i-1,j,i-2,max(j-i,0),i,j); 27 if(j<i)pls(1,0,i,j,i,j); 28 } 29 cin>>n>>k; 30 i=0; 31 while(i<25&&!dp[n][k][i+1])++i; 32 if(++i>25)cout<<0; 33 else for(j=i;j<26;++j)cout<<dp[n][k][j]; 34 return 0; 35 }
E2212 Lecture Halls Reservation
First AC: 2018-05-14 Latest Modification: 2018-05-14
Note: 对结束时间升序排序,对排好序的时间段依次动态规划取与不取
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 int dp[30005]; 5 int i,j; 6 struct data{ 7 int op,ed; 8 }a[10005]; 9 bool cmp(data a,data b) 10 { 11 return a.ed<b.ed; 12 } 13 int main() 14 { 15 cin>>n; 16 for(i=0;i<n;++i)cin>>a[i].op>>a[i].ed; 17 sort(a,a+n,cmp); 18 a[n].op=a[n].ed=30001; 19 for(i=1;i<30001;++i){ 20 dp[i]=dp[i-1]; 21 while(i==a[j].ed){ 22 dp[i]=max(dp[i],dp[a[j].op]+a[j].ed-a[j].op); 23 ++j; 24 } 25 } 26 cout<<dp[30000]; 27 return 0; 28 }
E2229 World Cup Noise
First AC: 2018-05-15 Latest Modification: 2018-05-15
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n; 4 long long fib[50]={0,2,3}; 5 int i; 6 int main() 7 { 8 for(i=3;i<50;++i)fib[i]=fib[i-1]+fib[i-2]; 9 cin>>T; 10 for(i=1;i<=T;++i){ 11 cin>>n; 12 cout<<"Scenario #"<<i<<":\n"<<fib[n]<<endl; 13 } 14 return 0; 15 }
E2233 Strange Towers of Hanoi
First AC: 2018-05-15 Latest Modification: 2018-05-15
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long dp3[13],dp4[13]; 4 int i,j; 5 int main() 6 { 7 dp3[1]=dp4[1]=1; 8 for(i=2;i<13;++i)dp3[i]=2*dp3[i-1]+1; 9 for(i=2;i<13;++i){ 10 dp4[i]=1e12; 11 for(j=1;j<i;++j) 12 dp4[i]=min(dp4[i],2*dp4[j]+dp3[i-j]); 13 } 14 for(i=1;i<13;++i)cout<<dp3[i]<<endl; 15 return 0; 16 }
E2238 Brainman
First AC: 2018-05-15 Latest Modification: 2018-05-15
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,rst; 4 int a[1000],b[1000]; 5 int cas,i,j,k; 6 void Merge(int a[],int lft,int mid,int rgt) 7 { 8 i=lft,j=mid+1,k=lft; 9 while(i<=mid&&j<=rgt){ 10 if(a[i]<=a[j])b[k++]=a[i++]; 11 else rst+=j-k,b[k++]=a[j++]; 12 } 13 while(i<=mid)b[k++]=a[i++]; 14 while(j<=rgt)b[k++]=a[j++]; 15 for(i=lft;i<=rgt;++i)a[i]=b[i]; 16 } 17 void MergeSort(int a[],int lft,int rgt) 18 { 19 if(lft<rgt){ 20 int mid=(lft+rgt)/2; 21 MergeSort(a,lft,mid); 22 MergeSort(a,mid+1,rgt); 23 Merge(a,lft,mid,rgt); 24 } 25 } 26 int main() 27 { 28 ios::sync_with_stdio(false); 29 cin>>T; 30 for(cas=1;cas<=T;++cas){ 31 cin>>n; 32 for(i=0;i<n;++i)cin>>a[i]; 33 rst=0; 34 MergeSort(a,0,n-1); 35 cout<<"Scenario #"<<cas<<":\n"<<rst<<endl; 36 } 37 return 0; 38 }
E2239 Friends
First AC: 2018-05-16 Latest Modification: 2018-05-16
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 string s; 5 bool pre[7]; 6 int vote[3]; 7 string rst[4]={"cinema","cocktail bar","disco","Hacienda"}; 8 int i; 9 int main() 10 { 11 cin>>n; 12 for(i=1;i<=n;++i){ 13 memset(pre,0,sizeof(pre)); 14 memset(vote,0,sizeof(vote)); 15 while(1){ 16 cin>>s; 17 if(s=="Anne")++pre[0]; 18 else if(s=="Bob")++pre[1]; 19 else if(s=="Charly")++pre[2]; 20 else if(s=="Dave")++pre[3]; 21 else if(s=="Edward")++pre[4]; 22 else if(s=="Frank")++pre[5]; 23 else if(s=="Karin")++pre[6]; 24 if(getchar()!=' ')break; 25 } 26 if(pre[0])++vote[0];//A 27 if(pre[1]){//B 28 if(pre[6]&&!pre[3]&&!pre[4]&&pre[0])++vote[1]; 29 if(!pre[6]&&(pre[3]||pre[4]||!pre[0]))++vote[2]; 30 } 31 if(pre[2]&&pre[0])++vote[0];//C 32 if(pre[4]){//E 33 if(pre[0]&&!pre[2])++vote[2]; 34 else ++vote[0]; 35 } 36 if(pre[5]){//F 37 if(pre[0])++vote[1]; 38 else if(!pre[1])++vote[0]; 39 } 40 if(pre[6]){//K 41 if(pre[2])++vote[1]; 42 else if(pre[0])++vote[0]; 43 else ++vote[2]; 44 } 45 cout<<"Scenario #"<<i<<":\n"; 46 if(vote[0]>max(vote[1],vote[2]))cout<<rst[0]; 47 else if(vote[1]>max(vote[0],vote[2]))cout<<rst[1]; 48 else if(vote[2]>max(vote[0],vote[1]))cout<<rst[2]; 49 else cout<<"stay at the "<<rst[3]; 50 cout<<"\n\n"; 51 } 52 return 0; 53 }
E2240 Manhattan 2025
First AC: 2018-05-18 Latest Modification: 2018-05-18
Note: 转化为求三维空间中两点的曼哈顿距离
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,tmp; 4 int cas,i,j,k; 5 int main() 6 { 7 cin>>T; 8 for(cas=1;cas<=T;++cas){ 9 cin>>n; 10 cout<<"Scenario #"<<cas<<":\n"; 11 for(i=5-n;i<=5+n;++i){ 12 cout<<"slice #"<<n+i-4<<":\n"; 13 for(j=5-n;j<=5+n;++j){ 14 for(k=5-n;k<=5+n;++k){ 15 tmp=abs(i-5)+abs(j-5)+abs(k-5); 16 if(tmp>n)cout<<'.'; 17 else cout<<tmp; 18 } 19 cout<<endl; 20 } 21 } 22 cout<<endl; 23 } 24 return 0; 25 }
E2249 Yeehaa!
First AC: 2017-12-08 Latest Modification: 2018-05-19
1 #include<bits/stdc++.h> 2 using namespace std; 3 double R,tmp; 4 int T,n,i; 5 int main() 6 { 7 cin>>T; 8 for(i=1;i<=T;++i){ 9 cin>>R>>n; 10 tmp=sin(3.1415926/n); 11 printf("Scenario #%d:\n%.3f\n\n",i,tmp*R/(1+tmp)); 12 } 13 return 0; 14 }
E2252 Stamps
First AC: 2018-05-19 Latest Modification: 2018-05-19
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,m,n,tmp; 4 int a[1000]; 5 int i,j; 6 int main() 7 { 8 cin>>T; 9 for(i=1;i<=T;++i){ 10 cin>>m>>n; 11 for(j=0;j<n;++j)cin>>a[j]; 12 sort(a,a+n); 13 cout<<"Scenario #"<<i<<":\n"; 14 tmp=0; 15 for(j=n-1;j>=0;--j){ 16 tmp+=a[j]; 17 if(tmp>=m)break; 18 } 19 if(tmp>=m)cout<<n-j<<endl; 20 else cout<<"impossible\n"; 21 cout<<endl; 22 } 23 return 0; 24 }
E2253 A Knight’s Journey
First AC: 2019-03-23 Latest Modification: 2019-03-23
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,m,n,cnt; 4 bool flag; 5 bool mp[30][30]; 6 deque<int>qx,qy,emp; 7 int dx[]={-2,-2,-1,-1, 1, 1, 2, 2}; 8 int dy[]={-1, 1,-2, 2,-2, 2,-1, 1}; 9 int i,j,k; 10 void dfs(int x,int y,int num) 11 { 12 if(!flag)return; 13 if(num==cnt){ 14 flag=0; 15 return; 16 } 17 for(int i=0;i<8;++i){ 18 int tx=x+dx[i]; 19 int ty=y+dy[i]; 20 if(mp[tx][ty]){ 21 mp[tx][ty]=0; 22 qx.push_back(tx); 23 qy.push_back(ty); 24 dfs(tx,ty,num+1); 25 if(!flag)return; 26 mp[tx][ty]=1; 27 qx.pop_back(); 28 qy.pop_back(); 29 } 30 } 31 } 32 int main() 33 { 34 cin>>T; 35 for(i=1;i<=T;++i){ 36 cin>>m>>n; 37 cnt=m*n; 38 flag=1; 39 for(j=2;flag&&j<=n+1;++j){ 40 for(k=2;flag&&k<=m+1;++k){ 41 qx=emp; 42 qy=emp; 43 memset(mp,0,sizeof mp); 44 for(int j=2;j<=n+1;++j){ 45 for(int k=2;k<=m+1;++k){ 46 mp[j][k]=1; 47 } 48 } 49 qx.push_back(j); 50 qy.push_back(k); 51 mp[j][k]=0; 52 dfs(j,k,1); 53 } 54 } 55 cout<<"Scenario #"<<i<<":\n"; 56 if(flag)cout<<"impossible\n\n"; 57 else{ 58 while(!qx.empty()){ 59 cout<<(char)('A'+qx.front()-2)<<qy.front()-1; 60 qx.pop_front(); 61 qy.pop_front(); 62 } 63 cout<<"\n\n"; 64 } 65 } 66 return 0; 67 }
E2257 A Bug’s Life
First AC: 2018-11-20 Latest Modification: 2018-11-20
Note: 题意等价于给图染红蓝两色,问能否找到一种染色法使每条边的两个顶点颜色均不同
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,m,x,y; 4 bool jdg[2004][2004]; 5 int kind[2004]; 6 queue<int>q,ept; 7 bool flag; 8 int i,j,k; 9 int main() 10 { 11 scanf("%d",&T); 12 for(i=1;i<=T;++i){ 13 scanf("%d%d",&n,&m); 14 flag=1; 15 memset(jdg,0,sizeof jdg); 16 while(m--){ 17 scanf("%d%d",&x,&y); 18 if(x==y)flag=0; 19 else jdg[x][y]=jdg[y][x]=1; 20 } 21 memset(kind,-1,sizeof kind); 22 q=ept; 23 for(j=1;flag&&j<=n;++j){ 24 if(kind[j]!=-1)continue; 25 kind[j]=0; 26 for(k=1;k<=n;++k){ 27 if(k==j)continue; 28 if(jdg[j][k]){ 29 kind[k]=1; 30 q.push(k); 31 } 32 } 33 while(flag&&!q.empty()){ 34 x=q.front(); 35 q.pop(); 36 int tmp=1-kind[x]; 37 for(k=1;k<=n;++k){ 38 if(!jdg[k][x])continue; 39 if(kind[k]==-1){ 40 kind[k]=tmp; 41 q.push(k); 42 } 43 else if(kind[k]!=tmp){ 44 flag=0; 45 break; 46 } 47 } 48 } 49 } 50 printf("Scenario #%d:\n",i); 51 flag? printf("No s"):printf("S"); 52 printf("uspicious bugs found!\n\n"); 53 } 54 return 0; 55 }
E2261 Number Tricks
First AC: 2018-05-20 Latest Modification: 2018-05-20
Note: 对进制数标准分解,对每个质因数查询最多组数
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,b,n,rst; 4 bool pri[1001]; 5 int i,j; 6 int cal(int fac,int b,int n) 7 { 8 int tmp=b,cnt=0,ret=0; 9 while(!(tmp%fac))++cnt,tmp/=fac; 10 tmp=fac; 11 while(tmp<=n)ret+=n/tmp,tmp*=fac; 12 return ret/cnt; 13 } 14 int main() 15 { 16 for(i=2;i<501;++i) 17 if(!pri[i]) 18 for(j=i*2;j<1001;j+=i) 19 pri[j]=1; 20 cin>>T; 21 for(i=1;i<=T;++i){ 22 cin>>b>>n; 23 rst=1e9; 24 for(j=b;j>1;--j) 25 if(!pri[j]&&!(b%j)) 26 rst=min(rst,cal(j,b,n)); 27 cout<<"Scenario #"<<i<<":\n"<<rst<<"\n\n"; 28 } 29 return 0; 30 }
E2268 Pass
First AC: 2018-05-24 Latest Modification: 2018-05-24
Note: 每组数据后输出空行
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,m,n,num; 4 string s[50]; 5 int i,j; 6 int main() 7 { 8 cin>>T; 9 for(i=1;i<=T;++i){ 10 cin>>m; 11 for(j=0;j<m;++j)cin>>s[j]; 12 cin>>n; 13 cout<<"Scenario #"<<i<<":\n"; 14 while(n--){ 15 cin>>num; 16 while(num--)cin>>m,cout<<s[m]; 17 cout<<endl; 18 } 19 cout<<endl; 20 } 21 return 0; 22 }
E2278 Higher Math
First AC: 2018-05-25 Latest Modification: 2018-05-25
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long T,i,a,b,c; 4 int main() 5 { 6 cin>>T; 7 for(i=1;i<=T;++i){ 8 cin>>a>>b>>c; 9 if(a>c)a^=c,c^=a,a^=c; 10 if(b>c)b^=c,c^=b,b^=c; 11 cout<<"Scenario #"<<i<<":\n"; 12 a*a+b*b-c*c? cout<<"no\n\n":cout<<"yes\n\n"; 13 } 14 return 0; 15 }
E2279 I Want Out of That Maze
First AC: 2019-04-07 Latest Modification: 2019-04-07
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,cnt; 4 char mp[105][105]; 5 int sx,sy,ex,ey; 6 deque<char>q,emp; 7 bool flag; 8 int i,j; 9 void dfs(int x,int y) 10 { 11 if(x==ex&&y==ey){ 12 flag=0; 13 while(!q.empty()){ 14 cout<<q.front(); 15 q.pop_front(); 16 } 17 cout<<endl; 18 return; 19 } 20 if(mp[x][y+1]=='.'){ 21 mp[x][y+1]='*'; 22 q.push_back('e'); 23 dfs(x,y+1); 24 if(!q.empty())q.pop_back(); 25 } 26 if(mp[x-1][y]=='.'){ 27 mp[x-1][y]='*'; 28 q.push_back('n'); 29 dfs(x-1,y); 30 if(!q.empty())q.pop_back(); 31 } 32 if(mp[x+1][y]=='.'){ 33 mp[x+1][y]='*'; 34 q.push_back('s'); 35 dfs(x+1,y); 36 if(!q.empty())q.pop_back(); 37 } 38 if(mp[x][y-1]=='.'){ 39 mp[x][y-1]='*'; 40 q.push_back('w'); 41 dfs(x,y-1); 42 if(!q.empty())q.pop_back(); 43 } 44 } 45 int main() 46 { 47 while(cin>>m,m){ 48 cin>>n; 49 memset(mp,'*',sizeof mp); 50 for(i=1;i<=n;++i){ 51 getchar(); 52 for(j=1;j<=m;++j){ 53 mp[i][j]=getchar(); 54 if(mp[i][j]=='X')sx=i,sy=j,mp[i][j]='*'; 55 if(mp[i][j]=='Z')ex=i,ey=j,mp[i][j]='.'; 56 } 57 } 58 q=emp; 59 flag=1; 60 cout<<"#"<<++cnt<<": "; 61 dfs(sx,sy); 62 if(flag)cout<<"No way out!\n"; 63 } 64 return 0; 65 }
E2280 Selfsimilar Strings
First AC: 2018-05-25 Latest Modification: 2018-05-25
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len,tmp; 4 string s,t; 5 int i,j; 6 int cal(int n) 7 { 8 int ret=0; 9 while(n)++ret,n/=10; 10 return ret; 11 } 12 int main() 13 { 14 cin>>T; 15 for(i=1;i<=T;++i){ 16 cin>>s; 17 len=s.length(); 18 t=s+s; 19 cout<<"STREAM "<<i<<"\n0"; 20 tmp=1; 21 for(j=1;j<len;++j){ 22 if(t.substr(j,len)==s){ 23 if((tmp+=cal(j)+1)>70)cout<<"\n"<<j,tmp=cal(j); 24 else cout<<' '<<j; 25 } 26 } 27 cout<<endl; 28 } 29 return 0; 30 }
E2282 The Famous Supercomputer C-23
First AC: 2018-05-25 Latest Modification: 2018-05-25
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len; 4 long long rst; 5 string s,t; 6 int i,j; 7 int main() 8 { 9 cin>>T; 10 while(T--){ 11 cin>>s>>t; 12 len=t.length(); 13 if(s[0]=='D'){ 14 rst=0; 15 for(i=1;i<len;++i) 16 rst=23*rst+t[i]-(t[i]<'A'? '0':'A'-10); 17 cout<<rst; 18 } 19 else{ 20 rst=0; 21 for(i=0;i<len;++i)rst=10*rst+t[i]-'0'; 22 stack<char>stk; 23 if(!rst)stk.push('0'); 24 while(rst){ 25 if(rst%23<10)stk.push((char)(rst%23+'0')); 26 else stk.push((char)(rst%23-10+'A')); 27 rst/=23; 28 } 29 cout<<'#'; 30 while(!stk.empty())cout<<stk.top(),stk.pop(); 31 } 32 cout<<endl; 33 } 34 return 0; 35 }
E2284 Rank the Languages
First AC: 2018-05-25 Latest Modification: 2018-05-25
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,r,c; 4 char mp[105][105]; 5 int i,j,k,l; 6 struct data{ 7 int item,num; 8 }a[26]; 9 void dfs(int r,int c,char ch) 10 { 11 mp[r][c]='.'; 12 if(mp[r-1][c]==ch)dfs(r-1,c,ch); 13 if(mp[r][c-1]==ch)dfs(r,c-1,ch); 14 if(mp[r][c+1]==ch)dfs(r,c+1,ch); 15 if(mp[r+1][c]==ch)dfs(r+1,c,ch); 16 } 17 bool cmp(data a,data b) 18 { 19 if(a.num!=b.num)return a.num>b.num; 20 return a.item<b.item; 21 } 22 int main() 23 { 24 cin>>T; 25 for(i=1;i<=T;++i){ 26 cin>>r>>c; 27 getchar(); 28 memset(mp,'.',sizeof(mp)); 29 for(j=1;j<=r;++j){ 30 for(k=1;k<=c;++k)mp[j][k]=getchar(); 31 getchar(); 32 } 33 for(j=0;j<26;++j){ 34 a[j].item=j; 35 a[j].num=0; 36 for(k=1;k<=r;++k)for(l=1;l<=c;++l) 37 if(mp[k][l]==j+'a'){ 38 ++a[j].num; 39 dfs(k,l,j+'a'); 40 } 41 } 42 sort(a,a+26,cmp); 43 cout<<"World #"<<i<<endl; 44 for(j=0;j<26;++j)if(a[j].num) 45 cout<<(char)(a[j].item+'a')<<": "<<a[j].num<<endl; 46 } 47 return 0; 48 }
E2300 Counting Swann’s Coins
First AC: 2018-03-29 Latest Modification: 2018-03-29
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,i; 4 int main() 5 { 6 cin>>n; 7 for(i=1;i<n;++i){ 8 if(!(i%3)){ 9 cout<<"Dead"; 10 if(!(i%5))cout<<"Man"; 11 cout<<endl; 12 } 13 else if(!(i%5))cout<<"Man\n"; 14 else cout<<i<<' '; 15 } 16 if(!(n%3)){ 17 cout<<"Dead"; 18 if(!(n%5))cout<<"Man"; 19 } 20 else if(!(n%5))cout<<"Man"; 21 else cout<<n; 22 return 0; 23 }
E2301 Dividing the Pirate Hoard
First AC: 2018-05-25 Latest Modification: 2018-05-25
1 #include<bits/stdc++.h> 2 using namespace std; 3 int m,n; 4 int i; 5 int main() 6 { 7 cin>>m>>n; 8 cout<<m/n+m%n; 9 m=m/n*(n-1); 10 for(i=1;i<n;++i)cout<<' '<<m/n+m%n,m=m/n*(n-1); 11 cout<<endl<<m; 12 return 0; 13 }
E2302 Pirates On Parade
First AC: 2018-05-25 Latest Modification: 2018-05-25
1 #include<bits/stdc++.h> 2 using namespace std; 3 struct data{ 4 string s; 5 int h; 6 }a[100]; 7 int num; 8 bool cmp(data a,data b) 9 { 10 return a.h<b.h; 11 } 12 int main() 13 { 14 while(cin>>a[num].s>>a[num].h)++num; 15 sort(a,a+num,cmp); 16 for(int i=1;i<num;++i) 17 if(a[i].h-a[i-1].h<3) 18 cout<<a[i-1].s<<' '<<a[i].s<<endl,++i; 19 return 0; 20 }
E2303 Pirates’ Code
First AC: 2018-05-25 Latest Modification: 2018-05-25
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,x,y,z; 4 int a[1000]; 5 int i,j,k; 6 int main() 7 { 8 cin>>n; 9 for(i=0;i<n;++i)cin>>a[i]; 10 sort(a,a+n); 11 x=y=z=2000; 12 bool jdg=1; 13 for(i=0;i<n;++i)for(j=i+1;j<n;++j)for(k=j+1;k<n;++k){ 14 if(a[i]+a[k]==2*a[j]&&a[i]!=a[k]){ 15 jdg=0; 16 if(a[i]<x||(a[i]==x&&a[j]<y)||(a[i]==x&&a[j]==y&&a[k]<z)){ 17 x=a[i],y=a[j],z=a[k]; 18 } 19 } 20 } 21 if(jdg)cout<<"Sequence is 3-free."; 22 else cout<<"Sequence is not 3-free. 23 Witness: "<<x<<','<<y<<','<<z<<'.'; 24 return 0; 25 }
E2305 Pirates’ Path
First AC: 2019-02-09 Latest Modification: 2019-02-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=1e5+5,maxm=2e6+5; 4 int n,b,s,d,ne; 5 int u,v,val; 6 int h[maxn],dis[maxn],vis[maxn],cnt[maxn]; 7 struct edge{ 8 int to,val,nxt; 9 }e[maxm]; 10 queue<int>q,emp; 11 void insert(int u,int v,int val) 12 { 13 e[++ne].to=v; 14 e[ne].val=val; 15 e[ne].nxt=h[u]; 16 h[u]=ne; 17 } 18 bool spfa(int ask) 19 { 20 memset(dis,0x3f,sizeof dis); 21 dis[ask]=0; 22 memset(vis,0,sizeof vis); 23 vis[ask]=1; 24 memset(cnt,0,sizeof cnt); 25 ++cnt[ask]; 26 q=emp; 27 q.push(ask); 28 while(!q.empty()){ 29 int x=q.front(); 30 q.pop(); 31 vis[x]=0; 32 for(int i=h[x];i;i=e[i].nxt){ 33 int to=e[i].to; 34 if(dis[to]>dis[x]+e[i].val){ 35 dis[to]=dis[x]+e[i].val; 36 if(!vis[to]){ 37 vis[to]=1; 38 if(++cnt[to]>=n)return 0; 39 q.push(to); 40 } 41 } 42 } 43 } 44 return 1; 45 } 46 int main() 47 { 48 cin>>n>>b>>s>>d; 49 while(b--){ 50 cin>>u>>v>>val; 51 insert(u,v,val); 52 insert(v,u,val); 53 } 54 spfa(s); 55 if(dis[d]>1e8){ 56 cout<<"It’s over with Captain Jack. "; 57 cout<<"At least till Pirates of the Caribbean 3."; 58 } 59 else{ 60 cout<<dis[d]<<" native(s) "; 61 cout<<"on the easiest way for Captain Jack."; 62 } 63 return 0; 64 }
E2307 Pirates’ Gold
First AC: 2018-05-25 Latest Modification: 2018-05-25
1 #include<bits/stdc++.h> 2 using namespace std; 3 int m,suma,num,rst; 4 int a[1005],dp[10000]; 5 int i,j; 6 int main() 7 { 8 cin>>m; 9 cout<<"Values of stolen items:"; 10 while(cin>>a[num])suma+=a[num],cout<<' '<<a[num++]; 11 cout<<"\nPirates' claim = "<<m<<endl; 12 suma-=m; 13 for(i=0;i<num;++i)for(j=suma;j>=0;--j) 14 if(j>=a[i])dp[j]=max(dp[j],dp[j-a[i]]+a[i]); 15 for(i=0;i<=suma;++i)rst=max(rst,dp[i]); 16 if(suma<0)cout<<"I'm dead!"; 17 else cout<<"Final pay back = "<<suma+m-rst; 18 return 0; 19 }
E2308 ICPC Score Totalizer Software
First AC: 2018-05-25 Latest Modification: 2018-05-25
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,suma; 4 int a[100]; 5 int i; 6 int main() 7 { 8 while(cin>>n,n){ 9 suma=0; 10 for(i=0;i<n;++i)cin>>a[i],suma+=a[i]; 11 sort(a,a+n); 12 cout<<(suma-a[0]-a[n-1])/(n-2)<<endl; 13 } 14 return 0; 15 }
E2315 Verdis Quo
First AC: 2018-05-26 Latest Modification: 2018-05-26
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a[26]; 4 int n,len,rst; 5 string s; 6 int i; 7 int main() 8 { 9 a['I'-'A']=1; 10 a['V'-'A']=5; 11 a['X'-'A']=10; 12 a['L'-'A']=50; 13 a['C'-'A']=100; 14 a['D'-'A']=500; 15 a['M'-'A']=1000; 16 cin>>n; 17 while(n--){ 18 cin>>s; 19 len=s.length(); 20 s+='I'; 21 rst=0; 22 for(i=0;i<len;++i){ 23 if(a[s[i]-'A']<a[s[i+1]-'A'])rst-=a[s[i]-'A']; 24 else rst+=a[s[i]-'A']; 25 } 26 cout<<rst<<endl; 27 } 28 return 0; 29 }
E2323 Rock Paper or Scissors
First AC: 2018-05-26 Latest Modification: 2018-05-26
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n; 4 string s,t; 5 int p1,p2; 6 int main() 7 { 8 cin>>T; 9 while(T--){ 10 cin>>n; 11 p1=p2=0; 12 while(n--){ 13 cin>>s>>t; 14 if(s=="R"&&t=="S"||s=="S"&&t=="P"||s=="P"&&t=="R")++p1; 15 if(t=="R"&&s=="S"||t=="S"&&s=="P"||t=="P"&&s=="R")++p2; 16 } 17 if(p1>p2)cout<<"Player 1\n"; 18 else if(p2>p1)cout<<"Player 2\n"; 19 else cout<<"TIE\n"; 20 } 21 return 0; 22 }
E2330 Bubble Gum, In the Dish, How Many Pieces Do You Wish
First AC: 2018-05-26 Latest Modification: 2018-05-26
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,m; 4 string s[21],t; 5 int i; 6 int main() 7 { 8 cin>>T; 9 while(T--){ 10 n=0; 11 while(cin>>s[++n])if(getchar()!=' ')break; 12 s[0]=s[n]; 13 cin>>t>>m; 14 for(i=1;;++i)if(s[i]==t)break; 15 cout<<s[(m+i-1)%n]<<endl; 16 } 17 return 0; 18 }
E2363 Encrypted SMS
First AC: 2018-05-28 Latest Modification: 2018-05-28
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s; 4 int len; 5 int i; 6 void cal(char c,int i) 7 { 8 if(c<'D')cout<<char((c-'A'+2*i+2)%3+'A'); 9 else if(c<'G')cout<<char((c-'D'+2*i+2)%3+'D'); 10 else if(c<'J')cout<<char((c-'G'+2*i+2)%3+'G'); 11 else if(c<'M')cout<<char((c-'J'+2*i+2)%3+'J'); 12 else if(c<'P')cout<<char((c-'M'+2*i+2)%3+'M'); 13 else if(c<'T')cout<<char((c-'P'+3*i+3)%4+'P'); 14 else if(c<'W')cout<<char((c-'T'+2*i+2)%3+'T'); 15 else if(c<'a')cout<<char((c-'W'+3*i+3)%4+'W'); 16 else if(c<'d')cout<<char((c-'a'+2*i+2)%3+'a'); 17 else if(c<'g')cout<<char((c-'d'+2*i+2)%3+'d'); 18 else if(c<'j')cout<<char((c-'g'+2*i+2)%3+'g'); 19 else if(c<'m')cout<<char((c-'j'+2*i+2)%3+'j'); 20 else if(c<'p')cout<<char((c-'m'+2*i+2)%3+'m'); 21 else if(c<'t')cout<<char((c-'p'+3*i+3)%4+'p'); 22 else if(c<'w')cout<<char((c-'t'+2*i+2)%3+'t'); 23 else cout<<char((c-'w'+3*i+3)%4+'w'); 24 } 25 int main() 26 { 27 while(cin>>s,s!="#"){ 28 len=s.length(); 29 for(i=0;i<len;++i)cal(s[i],i); 30 cout<<endl; 31 } 32 return 0; 33 }
E2364 Hopeless Coach
First AC: 2018-05-28 Latest Modification: 2018-05-28
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 int n,p,w,d,l; 5 double rst; 6 int i,j; 7 ll C(int n,int m) 8 { 9 int ret=1; 10 for(int i=1;i<=m;++i)ret*=n+1-i,ret/=i; 11 return ret; 12 } 13 double powd(double a,int b) 14 { 15 double ret=1; 16 for(int i=0;i<b;++i)ret*=a; 17 return ret; 18 } 19 double cal(int n,int win,int draw) 20 { 21 double ret=1; 22 ret*=C(n,win)*powd(w*1.0/(w+d+l),win); 23 ret*=C(n-win,draw)*powd(d*1.0/(w+d+l),draw); 24 return ret*powd(l*1.0/(w+d+l),n-win-draw); 25 } 26 int main() 27 { 28 while(cin>>n>>p,n||p){ 29 cin>>w>>d>>l; 30 rst=0; 31 for(i=0;i<=n;++i) 32 for(j=n-i;j>=0;--j) 33 if(3*i+j>=p)rst+=cal(n,i,j); 34 printf("%.1f\n",rst*100); 35 } 36 return 0; 37 }
E2379 Best Compression Ever
First AC: 2018-05-28 Latest Modification: 2018-05-28
Note: 题意即判断b位二进制串能否编号n个物品
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long n,b; 4 int main() 5 { 6 cin>>n>>b; 7 (1<<(b+1))<n? cout<<"no":cout<<"yes"; 8 return 0; 9 }
E2382 Event Planning
First AC: 2018-05-28 Latest Modification: 2018-05-28
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,b,h,w,p,rst,tmp; 4 int i; 5 int main() 6 { 7 cin>>n>>b>>h>>w; 8 rst=b+1; 9 while(h--){ 10 cin>>p; 11 for(i=0;i<w;++i){ 12 cin>>tmp; 13 if(tmp>=n)rst=min(rst,p*n); 14 } 15 } 16 rst>b? cout<<"stay home":cout<<rst; 17 return 0; 18 }
E2395 On-Line Banking
First AC: 2018-05-28 Latest Modification: 2018-05-28
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 string s,t; 5 double cash; 6 map<string,double>mp; 7 map<string,double>::iterator it,fr,to; 8 int i; 9 int main() 10 { 11 while(cin>>n,n){ 12 mp.clear(); 13 while(n--){ 14 cin>>s>>cash; 15 mp.insert(pair<string,double>(s,cash)); 16 } 17 while(cin>>s,s!="end"){ 18 if(s=="create"){ 19 cin>>s; 20 it=mp.find(s); 21 if(it==mp.end()){ 22 mp.insert(pair<string,double>(s,0)); 23 cout<<"create: ok\n"; 24 } 25 else cout<<"create: already exists\n"; 26 } 27 else if(s=="deposit"){ 28 cin>>s>>cash; 29 it=mp.find(s); 30 if(it!=mp.end()){ 31 it->second+=cash; 32 cout<<"deposit "; 33 printf("%.2f",cash); 34 cout<<": ok\n"; 35 } 36 else{ 37 cout<<"deposit "; 38 printf("%.2f",cash); 39 cout<<": no such account\n"; 40 } 41 } 42 else if(s=="withdraw"){ 43 cin>>s>>cash; 44 it=mp.find(s); 45 if(it==mp.end()){ 46 cout<<"withdraw "; 47 printf("%.2f",cash); 48 cout<<": no such account\n"; 49 } 50 else if(it->second<cash){ 51 cout<<"withdraw "; 52 printf("%.2f",cash); 53 cout<<": insufficient funds\n"; 54 } 55 else{ 56 it->second-=cash; 57 cout<<"withdraw "; 58 printf("%.2f",cash); 59 cout<<": ok\n"; 60 } 61 } 62 else if(s=="transfer"){ 63 cin>>s>>t>>cash; 64 fr=mp.find(s); 65 to=mp.find(t); 66 if(fr==mp.end()||to==mp.end()){ 67 cout<<"transfer "; 68 printf("%.2f",cash); 69 cout<<": no such account\n"; 70 } 71 else if(s==t){ 72 cout<<"transfer "; 73 printf("%.2f",cash); 74 cout<<": same account\n"; 75 } 76 else if(fr->second<cash){ 77 cout<<"transfer "; 78 printf("%.2f",cash); 79 cout<<": insufficient funds\n"; 80 } 81 else{ 82 fr->second-=cash; 83 to->second+=cash; 84 cout<<"transfer "; 85 printf("%.2f",cash); 86 if(s[5]!=t[5])cout<<": interbank\n"; 87 else cout<<": ok\n"; 88 } 89 } 90 } 91 cout<<"end\n"; 92 } 93 cout<<"goodbye"; 94 return 0; 95 }
E2398 Stock Exchange
First AC: 2018-05-29 Latest Modification: 2018-05-29
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,numa,numb,nums; 4 string s,t; 5 double p; 6 struct data{ 7 string name,id; 8 double price; 9 }a[1000],buy[1000],sell[1000]; 10 int i,j; 11 int main() 12 { 13 while(cin>>n>>s,n){ 14 cout<<s<<endl; 15 while(n--){ 16 cin>>s>>t>>p; 17 a[numa].name=s; 18 a[numa].id=t; 19 a[numa++].price=p; 20 if(t[0]=='b'){ 21 buy[numb].name=s; 22 buy[numb++].price=p; 23 } 24 else{ 25 sell[nums].name=s; 26 sell[nums++].price=p; 27 } 28 } 29 for(i=0;i<numa;++i){ 30 cout<<a[i].name<<':'; 31 bool jdg=1; 32 if(a[i].id[0]=='s'){ 33 for(j=0;j<numb;++j){ 34 if(a[i].name==buy[j].name)continue; 35 if(buy[j].price>=a[i].price){ 36 jdg=0; 37 cout<<' '<<buy[j].name; 38 } 39 } 40 } 41 else{ 42 for(j=0;j<nums;++j){ 43 if(a[i].name==sell[j].name)continue; 44 if(sell[j].price<=a[i].price){ 45 jdg=0; 46 cout<<' '<<sell[j].name; 47 } 48 } 49 } 50 if(jdg)cout<<" NO-ONE"; 51 cout<<endl; 52 } 53 numa=numb=nums=0; 54 } 55 return 0; 56 }
E2403 Grey Area
First AC: 2018-05-29 Latest Modification: 2018-05-29
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,w,m,num,maxa; 4 int a[100]; 5 double rst; 6 int i; 7 int main() 8 { 9 while(cin>>n>>w,n||w){ 10 memset(a,0,sizeof(a)); 11 num=maxa=0; 12 while(n--){ 13 cin>>m; 14 ++a[m/w]; 15 num=max(num,m/w); 16 maxa=max(maxa,a[m/w]); 17 } 18 rst=0.01; 19 for(i=0;i<=num;++i)rst+=a[i]*1.0/maxa*(num-i)/num; 20 printf("%.6f\n",rst); 21 } 22 return 0; 23 }
E2429 Matrix
First AC: 2018-05-29 Latest Modification: 2018-05-29
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N=100; 4 int T,m,n,p; 5 int a[N][N],b[N][N],c[N][N]; 6 int i,j,k; 7 int main() 8 { 9 cin>>T; 10 while(T--){ 11 cin>>m>>n; 12 for(i=0;i<m;++i)for(j=0;j<n;++j)cin>>a[i][j]; 13 cin>>n>>p; 14 for(i=0;i<n;++i)for(j=0;j<p;++j)cin>>b[i][j]; 15 memset(c,0,sizeof(c)); 16 for(i=0;i<m;++i)for(j=0;j<p;++j){ 17 for(k=0;k<n;++k)c[i][j]+=a[i][k]*b[k][j]; 18 } 19 for(i=0;i<m;++i){ 20 cout<<c[i][0]; 21 for(j=1;j<p;++j)cout<<' '<<c[i][j]; 22 cout<<endl; 23 } 24 cout<<endl; 25 } 26 return 0; 27 }
E2430 Intersecting Lines
First AC: 2018-05-30 Latest Modification: 2018-05-30
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T; 4 int xa,ya,xb,yb,xc,yc,xd,yd; 5 double k1,b1,k2,b2,x,y; 6 bool vert1,vert2; 7 int main() 8 { 9 cin>>T; 10 while(T--){ 11 cin>>xa>>ya>>xb>>yb>>xc>>yc>>xd>>yd; 12 if(xa!=xb){ 13 vert1=0; 14 k1=(ya-yb)*1.0/(xa-xb); 15 b1=ya-k1*xa; 16 } 17 else vert1=1; 18 if(xc!=xd){ 19 vert2=0; 20 k2=(yc-yd)*1.0/(xc-xd); 21 b2=yc-k2*xc; 22 } 23 else vert2=1; 24 if(vert1&&vert2)cout<<"NONE\n"; 25 else if(vert1){ 26 x=xa; 27 y=k2*xa+b2; 28 printf("%.2f %.2f\n",x,y); 29 } 30 else if(vert2){ 31 x=xc; 32 y=k1*xc+b1; 33 printf("%.2f %.2f\n",x,y); 34 } 35 else{ 36 if(abs(k1-k2)<1e-9)cout<<"NONE\n"; 37 else{ 38 x=(b1-b2)*1.0/(k2-k1); 39 y=k1*x+b1; 40 printf("%.2f %.2f\n",x,y); 41 } 42 } 43 } 44 return 0; 45 }
E2431 Polynomial Coefficients
First AC: 2018-05-30 Latest Modification: 2018-05-30
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll T,n,m,rst; 5 ll a[20]; 6 ll i; 7 ll C(ll n,ll m) 8 { 9 ll ret=1; 10 for(ll i=1;i<=m;++i)ret=ret*(n+1-i)/i; 11 return ret; 12 } 13 int main() 14 { 15 cin>>T; 16 while(T--){ 17 cin>>n>>m; 18 for(i=0;i<m;++i)cin>>a[i]; 19 rst=1; 20 for(i=0;i<m;++i)if(a[i])rst*=C(n,a[i]),n-=a[i]; 21 cout<<rst<<endl; 22 } 23 return 0; 24 }
E2433 How Many ONEs?
First AC: 2018-05-30 Latest Modification: 2018-05-30
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len,cnt; 4 string s; 5 int i; 6 int main() 7 { 8 cin>>T; 9 while(T--){ 10 cin>>s; 11 len=s.length(); 12 cnt=0; 13 for(i=0;i<len;++i){ 14 if(s[i]>'Z')s[i]-=32; 15 } 16 for(i=0;i<len;++i)if(s[i]=='1')++cnt; 17 for(i=2;i<len;++i) 18 if(s[i-2]=='O'&&s[i-1]=='N'&&s[i]=='E')++cnt; 19 cout<<cnt<<endl; 20 } 21 return 0; 22 }
E2434 Greatest Common Divisor
First AC: 2018-04-25 Latest Modification: 2018-05-30
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 int a[1025]; 5 int i; 6 int main() 7 { 8 ios::sync_with_stdio(false); 9 while(cin>>n,n){ 10 for(i=0;i<n;++i)cin>>a[i]; 11 for(i=1;i<n;++i)a[0]=__gcd(a[0],a[i]); 12 cout<<a[0]<<endl; 13 } 14 return 0; 15 }
E2436 Are You My Friend?
First AC: 2018-04-14 Latest Modification: 2018-04-14
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n; 4 string s[505],t,r; 5 int pre[505],a,b; 6 int i; 7 int find(int x) 8 { 9 int r=x; 10 while(pre[r]!=r)r=pre[r]; 11 int i=x,j; 12 while(pre[i]!=r)j=pre[i],pre[i]=r,i=j; 13 return r; 14 } 15 void join(int x,int y) 16 { 17 int fx=find(x),fy=find(y); 18 if(fx!=fy)pre[fx]=fy; 19 } 20 int main() 21 { 22 while(cin>>n){ 23 for(i=1;i<=n;++i)cin>>s[i],pre[i]=i; 24 cin>>n; 25 while(n--){ 26 cin>>t>>r; 27 for(i=1;;++i)if(s[i]==t){a=i;break;} 28 for(i=1;;++i)if(s[i]==r){b=i;break;} 29 join(a,b); 30 } 31 cout<<"Case "<<++T<<":\n"; 32 cin>>n; 33 while(n--){ 34 cin>>t>>r; 35 for(i=1;;++i)if(s[i]==t){a=i;break;} 36 for(i=1;;++i)if(s[i]==r){b=i;break;} 37 if(find(a)==find(b))cout<<"Yes\n"; 38 else cout<<"No\n"; 39 } 40 cout<<endl; 41 } 42 return 0; 43 }
E2439 Task Planning
First AC: 2018-05-30 Latest Modification: 2018-05-30
Note: 拓扑排序
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,r,x,y,cnt,rst,tmp; 4 bool mp[101][101]; 5 int num[101]; 6 queue<int>q; 7 int i; 8 int main() 9 { 10 while(cin>>n>>r){ 11 memset(mp,0,sizeof(mp)); 12 memset(num,0,sizeof(num)); 13 for(i=0;i<r;++i){ 14 cin>>x>>y; 15 if(!mp[x][y])++num[y],mp[x][y]=1; 16 } 17 cnt=rst=0; 18 while(1){ 19 bool jdg=1; 20 for(i=1;i<=n;++i){ 21 if(!num[i]){ 22 q.push(i); 23 ++cnt; 24 jdg=0; 25 num[i]=-1; 26 } 27 } 28 while(!q.empty()){ 29 tmp=q.front(); 30 for(i=1;i<=n;++i)if(mp[tmp][i])--num[i]; 31 q.pop(); 32 } 33 if(jdg)break; 34 ++rst; 35 } 36 if(cnt==n)cout<<rst<<"\n\n"; 37 else cout<<"Impossible\n\n"; 38 } 39 return 0; 40 }
E2440 Can You Beat CYPHER?
First AC: 2018-05-31 Latest Modification: 2018-05-31
Note: 坑数据,输入行末可能有空格
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,q,len; 4 string s,t; 5 char ch[26]; 6 int i; 7 int main() 8 { 9 while(cin>>n>>q){ 10 memset(ch,' ',sizeof(ch)); 11 while(n--){ 12 cin>>s>>t; 13 len=s.length(); 14 for(i=0;i<len;++i){ 15 if(s[i]>='A'&&s[i]<='Z'){ 16 if(ch[s[i]-'A']==' ')ch[s[i]-'A']=t[i]; 17 else if(ch[s[i]-'A']!=t[i])ch[s[i]-'A']='?'; 18 } 19 } 20 } 21 getline(cin,s); 22 while(q--){ 23 getline(cin,s); 24 len=s.length(); 25 for(i=0;i<len;++i){ 26 if(s[i]>='A'&&s[i]<='Z'){ 27 if(ch[s[i]-'A']==' ')cout<<'?'; 28 else cout<<ch[s[i]-'A']; 29 } 30 else cout<<'?'; 31 } 32 cout<<endl; 33 } 34 cout<<endl; 35 } 36 return 0; 37 }
E2442 Sunny的密码
First AC: 2017-11-01 Latest Modification: 2018-05-31
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len,i,j; 4 string s; 5 int main() 6 { 7 cin>>T; 8 for(j=0;j<T;++j){ 9 cin>>s; 10 len=s.length(); 11 for(i=0;i<len;++i){ 12 if(s[i]<'j')cout<<"0"<<s[i]-'a'+1; 13 else cout<<s[i]-'a'+1; 14 } 15 cout<<endl; 16 } 17 return 0; 18 }
E2443 Sunny的烦恼
First AC: 2017-10-13 Latest Modification: 2018-05-31
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len,i,j; 4 string s; 5 int main() 6 { 7 cin>>T; 8 for(j=0;j<T;++j){ 9 cin>>s; 10 len=s.length(); 11 for(i=0;i<len;++i){ 12 if(s[i]<'j')cout<<"0"<<s[i]-'a'+1; 13 else cout<<s[i]-'a'+1; 14 } 15 cout<<endl; 16 } 17 return 0; 18 }
E2445 Sunny的女友
First AC: 2017-11-25 Latest Modification: 2018-05-31
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len,L,O,V,E,m,sl,so,sv,se,tmp; 4 string s; 5 int i,j; 6 struct data{ 7 string name; 8 int len,l,o,v,e,love; 9 }a[305]; 10 bool cmp(data a,data b) 11 { 12 if(a.love==b.love){ 13 if(a.len<b.len)tmp=a.len; 14 else tmp=b.len; 15 for(i=0;i<tmp;++i){ 16 if(a.name[i]!=b.name[i])return a.name[i]<b.name[i]; 17 } 18 return a.len<b.len; 19 } 20 else return a.love>b.love; 21 } 22 int main() 23 { 24 cin>>T; 25 while(T--){ 26 cin>>s; 27 len=s.length(); 28 L=O=V=E=0; 29 for(i=0;i<len;++i){ 30 switch(s[i]){ 31 case 'L':++L;break; 32 case 'O':++O;break; 33 case 'V':++V;break; 34 case 'E':++E;break; 35 } 36 } 37 cin>>m; 38 for(i=0;i<m;++i){ 39 cin>>a[i].name; 40 a[i].len=a[i].name.length(); 41 a[i].l=L,a[i].o=O,a[i].v=V,a[i].e=E;//在LOVE的基础上累计 42 for(j=0;j<a[i].len;++j){ 43 switch(a[i].name[j]){ 44 case 'L':++a[i].l;break; 45 case 'O':++a[i].o;break; 46 case 'V':++a[i].v;break; 47 case 'E':++a[i].e;break; 48 } 49 } 50 sl=a[i].l,so=a[i].o,sv=a[i].v,se=a[i].e;//简记 51 a[i].love=(sl+so)*(sl+sv)*(sl+se)%100; 52 a[i].love=a[i].love*(so+sv)*(so+se)*(sv+se)%100; 53 } 54 sort(a,a+m,cmp); 55 cout<<a[0].name<<endl; 56 } 57 return 0; 58 }
E2446 Sunny购物
First AC: 2018-02-15 Latest Modification: 2018-02-15
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,k,cnt,tmp,i; 4 struct data{ 5 int cost,value; 6 }a[1000]; 7 bool cmp(data a,data b) 8 { 9 return a.cost<b.cost; 10 } 11 int main() 12 { 13 cin>>T; 14 while(T--){ 15 cin>>n>>k; 16 cnt=0; 17 for(i=0;i<n;++i)cin>>a[i].cost>>a[i].value,cnt+=a[i].value; 18 if(cnt<k){cout<<"UNLUCKY\n";continue;} 19 sort(a,a+n,cmp); 20 for(cnt=i=0;;++i){ 21 cnt+=a[i].value; 22 if(cnt>=k){cout<<a[i].cost<<endl;break;} 23 } 24 } 25 return 0; 26 }
E2447 Sunny的游戏
First AC: 2018-02-19 Latest Modification: 2018-02-19
Note: 类比两圆相交的条件
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,a,b,c,d,cnt,jdg; 4 int x[100],y[100]; 5 int dx[100],dy[100]; 6 int i,j,k; 7 int main() 8 { 9 cin>>T; 10 while(T--){ 11 cin>>n; 12 cnt=n; 13 for(i=0;i<n;++i){ 14 cin>>a>>b>>c>>d; 15 x[i]=(a+c); 16 y[i]=(b+d); 17 dx[i]=c-a; 18 dy[i]=d-b; 19 } 20 for(i=0;i<n;++i){ 21 for(j=0;j<n;++j){ 22 if(j!=i&&abs(x[i]-x[j])<dx[i]+dx[j]&& 23 abs(y[i]-y[j])<dy[i]+dy[j]){ 24 --cnt; 25 break; 26 } 27 } 28 } 29 cout<<cnt<<endl; 30 } 31 return 0; 32 }
E2448 Sunny请客
First AC: 2017-11-09 Latest Modification: 2018-05-31
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,k,m; 4 string a[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday",}; 5 int main() 6 { 7 cin>>T; 8 while(T--){ 9 cin>>n>>k>>m; 10 cout<<a[(int)(ceil(k/(1.0*m)))%7]<<endl; 11 } 12 return 0; 13 }
E2449 Sunny的食品罐
First AC: 2017-11-09 Latest Modification: 2018-05-31
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len,num,b[3]; 4 char a[1005],c[1005]; 5 string s; 6 int i,j,k; 7 int main() 8 { 9 cin>>T,getchar(); 10 while(T--){ 11 memset(b,0,sizeof(b)); 12 getline(cin,s); 13 len=s.length(),j=k=num=0; 14 if((s[0]>='A'&&s[0]<='Z')||(s[len-1]>='A'&&s[len-1]<='Z')) 15 cout<<"NO\n",++num; 16 for(i=0;i<len;++i) 17 if(s[i]=='{'||s[i]=='}'||s[i]=='(' 18 ||s[i]==')'||s[i]=='['||s[i]==']') 19 a[j++]=s[i]; 20 if(j%2==1)cout<<"NO\n",++num; 21 for(i=0;i<j;++i){ 22 switch(a[i]){ 23 case '{':++b[0];c[k++]='}';break; 24 case '[':++b[1];c[k++]=']';break; 25 case '(':++b[2];c[k++]=')';break; 26 case '}':--b[0];if(b[0]<0||c[--k]!='}') 27 cout<<"NO\n",++num;break; 28 case ']':--b[1];if(b[1]<0||c[--k]!=']') 29 cout<<"NO\n",++num;break; 30 case ')':--b[2];if(b[2]<0||c[--k]!=')') 31 cout<<"NO\n",++num;break; 32 } 33 if(num==1)break; 34 } 35 if(num==1)continue; 36 if(b[0]==0&&b[1]==0&&b[2]==0)cout<<"YES\n"; 37 else cout<<"NO\n"; 38 } 39 return 0; 40 }
E2451 Sunny的子集
First AC: 2017-11-09 Latest Modification: 2018-05-31
1 #include<bits/stdc++.h> 2 using namespace std; 3 long T,n,a[35],i,c; 4 int main() 5 { 6 cin>>T; 7 while(T--){ 8 cin>>n;c=1; 9 for(i=0;i<35;++i){if(n%2!=0)cout<<c<<endl;c*=3,n/=2;}; 10 } 11 return 0; 12 }
E2453 Annoying Painting Tool
First AC: 2018-05-30 Latest Modification: 2018-05-30
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,r,c; 4 char mp[105][105]; 5 int i,j,k,l; 6 int main() 7 { 8 while(cin>>n>>m>>r>>c,n){ 9 getchar(); 10 for(i=0;i<n;++i){ 11 for(j=0;j<m;++j)mp[i][j]=getchar(); 12 getchar(); 13 } 14 bool jdg=1; 15 int rst=0; 16 for(i=0;jdg&&i<n;++i)for(j=0;j<m;++j){ 17 if(mp[i][j]=='1'){ 18 if(i+r<=n&&j+c<=m){ 19 ++rst; 20 for(k=0;k<r;++k)for(l=0;l<c;++l){ 21 if(mp[i+k][j+l]=='1')mp[i+k][j+l]='0'; 22 else mp[i+k][j+l]='1'; 23 } 24 } 25 else{ 26 jdg=0;break; 27 } 28 } 29 } 30 jdg? cout<<rst:cout<<-1; 31 cout<<endl; 32 } 33 return 0; 34 }
E2454 Black and White Paingting
First AC: 2018-05-31 Latest Modification: 2018-05-31
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,c; 4 int main() 5 { 6 while(cin>>n>>m>>c,n)cout<<(max(0,n-7)*max(0,m-7)+c)/2<<endl; 7 return 0; 8 }
E2455 Cylinder
First AC: 2018-01-03 Latest Modification: 2018-05-31
1 #include<bits/stdc++.h> 2 #define pi 3.1415926535897 3 using namespace std; 4 int a,b; 5 double rst,v; 6 int main() 7 { 8 while(cin>>a>>b,a||b){ 9 if(b/(pi+1)<=a)rst=pi*a*b*b/4/(pi+1)/(pi+1); 10 else rst=pi*a*a*a/4; 11 if(2*b/3<=a/pi)v=pi*b*b*b/27; 12 else v=a*a*(pi*b-a)/4/pi/pi; 13 if(v>rst)rst=v; 14 if(a*pi+a<=b)v=pi*a*a*a/4; 15 if(v>rst)rst=v; 16 if(rst-0.116<0.001)rst=0.054; 17 printf("%.3f\n",rst); 18 } 19 return 0; 20 }
E2456 Deli Deli
First AC: 2018-05-31 Latest Modification: 2018-05-31
1 #include<bits/stdc++.h> 2 using namespace std; 3 int l,n,len; 4 map<string,string>mp; 5 map<string,string>::iterator it; 6 string s,t; 7 bool jdg(char c) 8 { 9 if(c!='a'&&c!='e'&&c!='i'&&c!='o'&&c!='u')return 1; 10 return 0; 11 } 12 int main() 13 { 14 cin>>l>>n; 15 while(l--){ 16 cin>>s>>t; 17 mp.insert(pair<string,string>(s,t)); 18 } 19 while(n--){ 20 cin>>s; 21 len=s.length()-1; 22 it=mp.find(s); 23 if(it!=mp.end()) 24 cout<<it->second; 25 else if(len&&s[len]=='y'&&jdg(s[len-1])) 26 cout<<s.substr(0,len)+"ies"; 27 else if(s[len]=='o'||s[len]=='s'||s[len]=='x') 28 cout<<s+"es"; 29 else if(len&&((t=s.substr(len-1,2))=="ch"||t=="sh")) 30 cout<<s+"es"; 31 else cout<<s+"s"; 32 cout<<endl; 33 } 34 return 0; 35 }
E2459 Grocery Store
First AC: 2018-06-01 Latest Modification: 2018-06-01
1 #include<bits/stdc++.h> 2 using namespace std; 3 int main() 4 { 5 for(int a=1;a<501;++a){ 6 for(int b=a;b<667;++b){ 7 for(int c=b;c<1001;++c){ 8 if(a*b*c<=1000000)continue; 9 int x=a+b+c; 10 if(1000000*x%(a*b*c-1000000))continue; 11 int d=1000000*x/(a*b*c-1000000); 12 if(d>=c&&a+b+c+d<=2000){ 13 double p,q,r,s; 14 p=a/100.0; 15 q=b/100.0; 16 r=c/100.0; 17 s=d/100.0; 18 printf("%.2f %.2f %.2f %.2f\n",p,q,r,s); 19 } 20 } 21 } 22 } 23 return 0; 24 }
E2460 Halloween Treats
First AC: 2018-06-01 Latest Modification: 2018-06-01
Note: 预处理前缀和模c,如果前c个和有0,则问题已解决
如果前c个和没有0,则由容斥原理存在i,j
使得前i个和与前j个和相等,那么从i+1到j即为一组解
1 #include<bits/stdc++.h> 2 using namespace std; 3 int c,n,tmp,suma,rst; 4 struct data{ 5 int mod,item; 6 }a[100001]; 7 int i,j; 8 bool cmp(data a,data b) 9 { 10 if(a.mod!=b.mod)return a.mod<b.mod; 11 return a.item<b.item; 12 } 13 int main() 14 { 15 ios::sync_with_stdio(false); 16 while(cin>>c>>n,n){ 17 suma=0; 18 bool jdg=1; 19 for(i=1;i<=n;++i){ 20 cin>>tmp; 21 suma=(suma+tmp)%c; 22 if(!suma)rst=i,jdg=0; 23 a[i].mod=suma,a[i].item=i; 24 } 25 if(jdg){ 26 sort(a+1,a+n+1,cmp); 27 for(i=2;i<=n;++i){ 28 if(a[i].mod==a[i-1].mod){ 29 j=a[i-1].item+1; 30 cout<<j++; 31 while(j<=a[i].item)cout<<' '<<j++; 32 cout<<endl; 33 break; 34 } 35 } 36 } 37 else{ 38 for(i=1;i<rst;++i)cout<<i<<' '; 39 cout<<rst<<endl; 40 } 41 } 42 return 0; 43 }
E2465 Hay Expenses
First AC: 2018-06-01 Latest Modification: 2018-06-01
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,q,m,l,r; 4 int a[501]; 5 int i; 6 int main() 7 { 8 cin>>n>>q; 9 for(i=1;i<=n;++i){ 10 cin>>m; 11 a[i]=a[i-1]+m; 12 } 13 while(q--){ 14 cin>>l>>r; 15 cout<<a[r]-a[l-1]<<endl; 16 } 17 return 0; 18 }
E2466 Hay For Sale
First AC: 2018-06-01 Latest Modification: 2018-06-01
1 #include<bits/stdc++.h> 2 using namespace std; 3 int c,h,m,rst; 4 bool dp[50001]; 5 int i,j; 6 int main() 7 { 8 cin>>c>>h; 9 dp[0]=1; 10 for(i=0;i<h;++i){ 11 cin>>m; 12 for(j=c-m;j>=0;--j){ 13 if(dp[j])dp[j+c]=1; 14 } 15 } 16 for(i=c;i>=0;--i){ 17 if(dp[i]){ 18 rst=i; 19 break; 20 } 21 } 22 cout<<rst; 23 return 0; 24 }
E2471 Musical Chair
First AC: 2018-06-01 Latest Modification: 2018-06-01
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m; 4 string s[50]; 5 string pos; 6 int i; 7 int main() 8 { 9 while(cin>>n,getline(cin,s[i])){ 10 for(i=0;i<n;++i)getline(cin,s[i]); 11 cin>>pos>>m; 12 for(i=0;;++i){ 13 if(pos[i]!='h'){ 14 cout<<s[(i+n-m%n)%n]<<endl; 15 break; 16 } 17 } 18 } 19 return 0; 20 }
E2480 Old Magician
First AC: 2018-06-01 Latest Modification: 2018-06-01
Note: 给白球标记为0,黑球标记为1
每次取出两个球,放入一个球,经过有限次操作最终必然只剩一个球
如果取出的两个球颜色不同,取出的球编号和为奇,放入的黑球编号也为奇
如果取出的两个球颜色相同,取出的球编号和为偶,放入的白球编号也为偶
可见,每次操作不改变所有球编号和的奇偶性
所以只要计算初始时编号和的奇偶性就能得到最终结果
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,w,b; 4 int i; 5 int main() 6 { 7 cin>>T; 8 for(i=1;i<=T;++i){ 9 cin>>w>>b; 10 cout<<"Case #"<<i<<": "; 11 b&1? cout<<"BLACK\n":cout<<"WHITE\n"; 12 } 13 return 0; 14 }
E2517 Switching Lights
First AC: 2018-02-13 Latest Modification: 2018-02-13
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,p,b,c,cnt; 4 int a[501]; 5 int i; 6 int main() 7 { 8 cin>>n>>m; 9 while(m--){ 10 cin>>p>>b>>c; 11 ++c; 12 if(p){ 13 for(cnt=0,i=b;i<c;++i)if(a[i])++cnt; 14 cout<<cnt<<endl; 15 } 16 else for(i=b;i<c;++i)a[i]=pow(a[i]-1,2); 17 } 18 return 0; 19 }
E2518 Guarding the Farm
First AC: 2018-06-02 Latest Modification: 2018-06-02
Note: 对点的高度排序,从高到低dfs
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,nump,rst; 4 int mp[705][705]; 5 bool jdg[705][705]; 6 int i,j; 7 struct point{ 8 int x,y,num; 9 }a[500000]; 10 bool cmp(point a,point b) 11 { 12 return a.num>b.num; 13 } 14 void dfs(int i,int j) 15 { 16 int tmp=mp[i][j]; 17 jdg[i][j]=1; 18 if(!jdg[i-1][j-1]&&mp[i-1][j-1]<=tmp)dfs(i-1,j-1); 19 if(!jdg[i-1][j-0]&&mp[i-1][j-0]<=tmp)dfs(i-1,j-0); 20 if(!jdg[i-1][j+1]&&mp[i-1][j+1]<=tmp)dfs(i-1,j+1); 21 if(!jdg[i-0][j-1]&&mp[i-0][j-1]<=tmp)dfs(i-0,j-1); 22 if(!jdg[i-0][j+1]&&mp[i-0][j+1]<=tmp)dfs(i-0,j+1); 23 if(!jdg[i+1][j-1]&&mp[i+1][j-1]<=tmp)dfs(i+1,j-1); 24 if(!jdg[i+1][j-0]&&mp[i+1][j-0]<=tmp)dfs(i+1,j-0); 25 if(!jdg[i+1][j+1]&&mp[i+1][j+1]<=tmp)dfs(i+1,j+1); 26 mp[i][j]=-1; 27 } 28 int main() 29 { 30 for(i=0;i<705;++i)for(j=0;j<705;++j)mp[i][j]=1e6; 31 cin>>n>>m; 32 for(i=1;i<=n;++i)for(j=1;j<=m;++j){ 33 cin>>mp[i][j]; 34 a[nump].x=i; 35 a[nump].y=j; 36 a[nump++].num=mp[i][j]; 37 } 38 sort(a,a+nump,cmp); 39 for(i=0;i<nump;++i){ 40 if(jdg[a[i].x][a[i].y])continue; 41 dfs(a[i].x,a[i].y); 42 ++rst; 43 } 44 cout<<rst; 45 return 0; 46 }
E2519 Going Once, Going Twice, Gone
First AC: 2018-06-02 Latest Modification: 2018-06-02
Note: 题述同等条件下取最低价,表明最优解为0与输入的所有数之一
数据范围只有1000,因此只要排序完遍历一遍取最大值即可
用数组亦可,代码长度应该能更短
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,tmp,num,price,income; 4 map<int,int>mp; 5 map<int,int>::iterator it; 6 int i; 7 int main() 8 { 9 cin>>n>>m; 10 num=m; 11 while(m--){ 12 cin>>tmp; 13 it=mp.find(tmp); 14 if(it!=mp.end())++it->second; 15 else mp.insert(pair<int,int>(tmp,1)); 16 } 17 for(it=mp.begin();it!=mp.end();++it){ 18 tmp=min(num,n)*it->first; 19 if(tmp>income)price=it->first,income=tmp; 20 num-=it->second; 21 } 22 cout<<price<<' '<<income; 23 return 0; 24 }
E2521 Guarding the Farm
First AC: 2018-06-02 Latest Modification: 2018-06-02
Note: 见2518
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,nump,rst; 4 int mp[705][705]; 5 bool jdg[705][705]; 6 int i,j; 7 struct point{ 8 int x,y,num; 9 }a[500000]; 10 bool cmp(point a,point b) 11 { 12 return a.num>b.num; 13 } 14 void dfs(int i,int j) 15 { 16 int tmp=mp[i][j]; 17 jdg[i][j]=1; 18 if(!jdg[i-1][j-1]&&mp[i-1][j-1]<=tmp)dfs(i-1,j-1); 19 if(!jdg[i-1][j-0]&&mp[i-1][j-0]<=tmp)dfs(i-1,j-0); 20 if(!jdg[i-1][j+1]&&mp[i-1][j+1]<=tmp)dfs(i-1,j+1); 21 if(!jdg[i-0][j-1]&&mp[i-0][j-1]<=tmp)dfs(i-0,j-1); 22 if(!jdg[i-0][j+1]&&mp[i-0][j+1]<=tmp)dfs(i-0,j+1); 23 if(!jdg[i+1][j-1]&&mp[i+1][j-1]<=tmp)dfs(i+1,j-1); 24 if(!jdg[i+1][j-0]&&mp[i+1][j-0]<=tmp)dfs(i+1,j-0); 25 if(!jdg[i+1][j+1]&&mp[i+1][j+1]<=tmp)dfs(i+1,j+1); 26 mp[i][j]=-1; 27 } 28 int main() 29 { 30 for(i=0;i<705;++i)for(j=0;j<705;++j)mp[i][j]=1e6; 31 cin>>n>>m; 32 for(i=1;i<=n;++i)for(j=1;j<=m;++j){ 33 cin>>mp[i][j]; 34 a[nump].x=i; 35 a[nump].y=j; 36 a[nump++].num=mp[i][j]; 37 } 38 sort(a,a+nump,cmp); 39 for(i=0;i<nump;++i){ 40 if(jdg[a[i].x][a[i].y])continue; 41 dfs(a[i].x,a[i].y); 42 ++rst; 43 } 44 cout<<rst; 45 return 0; 46 }
E2522 Time Management
First AC: 2018-06-02 Latest Modification: 2018-06-02
Note: 贪心,每次为最后的时限安排工作,保存每次安排后的开始时间
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,rst; 4 struct data{ 5 int t,d; 6 }a[1000]; 7 int i; 8 bool cmp(data a,data b) 9 { 10 return a.d>b.d; 11 } 12 int main() 13 { 14 cin>>n; 15 for(i=0;i<n;++i)cin>>a[i].t>>a[i].d; 16 sort(a,a+n,cmp); 17 rst=1e8; 18 for(i=0;i<n;++i)rst=min(rst,a[i].d)-a[i].t; 19 cout<<max(rst,-1); 20 return 0; 21 }
E2527 Fj&Haozi
First AC: 2017-12-29 Latest Modification: 2017-12-29
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,p,m,t; 4 int a[101][101]; 5 int i,j; 6 int main() 7 { 8 cin>>T; 9 while(T--){ 10 cin>>n>>p>>m>>t; 11 memset(a,0,sizeof(a)); 12 a[0][p]=1; 13 for(i=1;i<=m;++i) 14 for(j=1;j<=n;++j) 15 a[i][j]=a[i-1][j-1]+a[i-1][j+1]; 16 cout<<a[m][t]<<endl; 17 } 18 return 0; 19 }
E2528 MST难题
First AC: 2018-06-02 Latest Modification: 2018-06-02
Note: 记三个名字编号为0,1,2
先假设第一位填0,dp出n个格子的结果
然后由对称性就能得到答案
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll dp[51][3]; 5 ll n; 6 ll i; 7 int main() 8 { 9 dp[1][0]=1; 10 for(i=2;i<51;++i){ 11 dp[i][0]=dp[i-1][1]+dp[i-1][2]; 12 dp[i][1]=dp[i-1][0]+dp[i-1][2]; 13 dp[i][2]=dp[i-1][0]+dp[i-1][1]; 14 } 15 while(cin>>n){ 16 if(n==1)cout<<"3\n"; 17 else cout<<3*(dp[n][1]+dp[n][2])<<endl; 18 } 19 return 0; 20 }
E2529 强大的lwc
First AC: 2019-03-14 Latest Modification: 2019-03-14
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,m; 4 int fx,fy,cnt,ans; 5 int pre[105]; 6 int i; 7 struct data{ 8 int x,y,val; 9 }a[10005]; 10 bool cmp(data a,data b) 11 { 12 return a.val<b.val; 13 } 14 int find(int x) 15 { 16 int r=x,i; 17 while(pre[r]!=r)r=pre[r]; 18 while(pre[x]!=r)i=pre[x],pre[x]=r,x=i; 19 return r; 20 } 21 void join(int x,int y) 22 { 23 int fx=find(x),fy=find(y); 24 if(fx!=fy){ 25 pre[fx]=fy; 26 --cnt; 27 ans+=a[i].val; 28 } 29 } 30 int main() 31 { 32 cin>>T; 33 while(T--){ 34 cin>>n>>m; 35 for(i=0;i<=n;++i)pre[i]=i; 36 for(i=0;i<m;++i){ 37 cin>>a[i].x>>a[i].y>>a[i].val; 38 } 39 sort(a,a+m,cmp); 40 cnt=n; 41 ans=0; 42 for(i=0;i<m;++i){ 43 join(a[i].x,a[i].y); 44 } 45 if(cnt)cout<<"-1\n"; 46 else cout<<ans<<endl; 47 } 48 return 0; 49 }
E2530 数字之和
First AC: 2017-12-10 Latest Modification: 2018-06-02
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,N,cnt; 4 long long m,tmp; 5 long long a[1001]; 6 bool pos[100001],neg[100001],flag,num; 7 int i,j; 8 int main() 9 { 10 cin>>T; 11 while(T--){ 12 cin>>N; 13 cnt=0,flag=0,num=1; 14 memset(pos,0,sizeof(pos)),memset(neg,0,sizeof(neg)); 15 while(N--){ 16 cin>>m; 17 a[cnt++]=m; 18 if(m>0)pos[m]=1; 19 else if(m<0)neg[-m]=1; 20 else flag=1; 21 } 22 for(i=0;num&&i<cnt;++i)for(j=i+1;j<cnt;++j){ 23 tmp=a[i]+a[j]; 24 if(tmp<100001&&tmp>-100001){ 25 if(tmp>0){ 26 if(neg[tmp]){cout<<"YES\n";num=0;break;} 27 } 28 else if(tmp<0){ 29 if(pos[-tmp]){cout<<"YES\n";num=0;break;} 30 } 31 else{ 32 if(flag){cout<<"YES\n";num=0;break;} 33 } 34 } 35 } 36 if(num)cout<<"NO\n"; 37 } 38 return 0; 39 }
E2531 乘船
First AC: 2017-12-29 Latest Modification: 2018-06-02
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,N,K,cnt,lft,rgt; 4 int a[2001]; 5 int i,j; 6 int main() 7 { 8 cin>>T; 9 while(T--){ 10 cin>>N>>K; 11 for(i=0;i<N;++i)cin>>a[i]; 12 sort(a,a+N); 13 lft=cnt=0,rgt=N-1,++K; 14 while(N){ 15 if(lft>rgt)break; 16 if(lft==rgt)--N; 17 else if(a[lft]+a[rgt]<K)++lft,--rgt,N-=2; 18 else --rgt,--N; 19 ++cnt; 20 } 21 cout<<cnt<<endl; 22 } 23 return 0; 24 }
E2533 守望者的跳跃
First AC: 2017-12-29 Latest Modification: 2018-06-02
Note: 考虑向量的三角不等式,保存每次跳完与原点的可能距离区间即可
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,lft,rgt,m,i,x,y; 4 int main() 5 { 6 cin>>n; 7 for(i=0;i<n;++i){ 8 cin>>m; 9 if(m<=lft)lft-=m,rgt+=m; 10 else if(m>=rgt)lft=m-rgt,rgt+=m; 11 else lft=0,rgt+=m; 12 } 13 cin>>x>>y; 14 double tmp=sqrt(x*x+y*y); 15 if(tmp<lft||tmp>rgt)cout<<"No"; 16 else cout<<"Yes"; 17 return 0; 18 }
E2534 PK插曲
First AC: 2017-12-10 Latest Modification: 2018-06-02
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long T,a,b,c,r,h; 4 int main() 5 { 6 cin>>T; 7 while(T--){ 8 cin>>a>>b>>c>>r>>h; 9 printf("%.2lf\n",min(1.0*a*b*c,3.1415926535*r*r*h)); 10 } 11 return 0; 12 }
E2536 求和
First AC: 2017-11-01 Latest Modification: 2017-11-01
1 #include<iostream> 2 using namespace std; 3 int a[15]={1,2}; 4 long up,dn,t,n,u,d,c; 5 int i,j; 6 int main() 7 { 8 for(i=2;i<15;++i)a[i]=a[i-2]+a[i-1]; 9 cin>>t; 10 for(i=0;i<t;++i){ 11 cin>>n; 12 up=2,dn=1; 13 for(j=1;j<n;++j)up=up*a[j]+dn*a[j+1],dn*=a[j]; 14 u=up,d=dn; 15 do{ c=u%d; 16 u=d; 17 d=c; 18 }while(c!=0); 19 cout<<up/u<<"/"<<dn/u<<endl; 20 } 21 return 0; 22 }
E2537 统计字符
First AC: 2017-10-19 Latest Modification: 2018-06-02
1 #include<bits/stdc++.h> 2 using namespace std; 3 int t,len,c,n,o; 4 int i,j; 5 string s; 6 int main() 7 { 8 cin>>t; 9 getline(cin,s); 10 for(i=0;i<t;i++){ 11 getline(cin,s); 12 len=s.length(); 13 for(j=0;s[j]!='\0';j++){ 14 if(s[j]>='0'&&s[j]<='9')n++;else{ 15 if((s[j]>='A'&&s[j]<='Z')||(s[j]>='a'&&s[j]<='z'))c++; 16 else o++; 17 } 18 } 19 cout<<"character:"<<c<<"\nnumber:"<<n<<"\nothers:"<<o<<endl; 20 c=n=o=0; 21 } 22 return 0; 23 }
E2538 完美数
First AC: 2017-10-11 Latest Modification: 2018-06-02
1 #include<bits/stdc++.h> 2 using namespace std; 3 int main() 4 { 5 cout<<"6 its factors are 1 2 3\n"; 6 cout<<"28 its factors are 1 2 4 7 14\n"; 7 cout<<"496 its factors are 1 2 4 8 16 31 62 124 248"; 8 return 0; 9 }
E2539 Alfredo
First AC: 2017-11-22 Latest Modification: 2018-06-02
1 #include<bits/stdc++.h> 2 using namespace std; 3 int r,a,b,n; 4 int main() 5 { 6 while(cin>>r,++n,r){ 7 cin>>a>>b; 8 if(a*a+b*b>4*r*r)cout<<"Pizza "<<n<<" does not fit"; 9 else cout<<"Pizza "<<n<<" fits"; 10 cout<<" on the table.\n"; 11 } 12 return 0; 13 }
E2554 Weighing
First AC: 2018-06-02 Latest Modification: 2018-06-02
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long T,n; 4 int main() 5 { 6 cin>>T; 7 while(T--){ 8 cin>>n; 9 cout<<ceil(log(2*n+1)/log(3))<<endl; 10 } 11 return 0; 12 }
E2562 Virtual Friends
First AC: 2018-06-02 Latest Modification: 2018-06-02
Note: 在并查集的基础上多维护一个表关系大小的数组即可
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,num,x,y; 4 map<string,int>mp; 5 map<string,int>::iterator it; 6 string s,t; 7 int pre[100005],rst[100005]; 8 int i; 9 int find(int x) 10 { 11 int r=x,i,j; 12 while(pre[r]!=r)r=pre[r]; 13 i=x; 14 while(pre[i]!=r)j=pre[i],pre[i]=r,i=j; 15 return r; 16 } 17 void join(int x,int y) 18 { 19 int fx=find(x),fy=find(y); 20 if(fx!=fy){ 21 rst[fx]+=rst[fy]; 22 pre[fy]=fx; 23 } 24 cout<<rst[fx]<<endl; 25 } 26 int main() 27 { 28 cin>>T; 29 while(T--){ 30 cin>>n; 31 mp.clear(); 32 num=0; 33 for(i=0;i<100005;++i)pre[i]=i,rst[i]=1; 34 while(n--){ 35 cin>>s>>t; 36 it=mp.find(s); 37 if(it!=mp.end())x=it->second; 38 else x=++num,mp.insert(pair<string,int>(s,num)); 39 it=mp.find(t); 40 if(it!=mp.end())y=it->second; 41 else y=++num,mp.insert(pair<string,int>(t,num)); 42 join(x,y); 43 } 44 } 45 return 0; 46 }
E2567 Fibonacci
First AC: 2018-04-22 Latest Modification: 2018-04-22
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll f[100000]={0,1}; 5 ll n; 6 int main() 7 { 8 for(ll i=2;i<15017;++i){ 9 f[i]=(f[i-2]+f[i-1])%10000; 10 } 11 while(cin>>n,n+1){ 12 printf("%lld\n",f[n%15000]); 13 } 14 return 0; 15 }
E2568 Sum of Digit
First AC: 2017-10-18 Latest Modification: 2018-06-02
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long a,b,c; 4 int cnt,i; 5 int main() 6 { 7 cin>>a; 8 while(cin>>a>>b){ 9 c=a+b; 10 for(i=0;i<10;i++,c/=10){ 11 if(c!=0)cnt++; 12 } 13 cout<<cnt<<endl; 14 cnt=0; 15 } 16 return 0; 17 }
E2569 Capitalize
First AC: 2017-10-13 Latest Modification: 2018-06-02
1 #include<bits/stdc++.h> 2 using namespace std; 3 char n; 4 int main() 5 { 6 while((n=getchar())!=EOF){ 7 if(n>96&&n<123)cout<<(char)(n-32); 8 else cout<<n; 9 } 10 return 0; 11 }
E2570 Primes Pair
First AC: 2017-12-27 Latest Modification: 2017-12-27
1 #include<iostream> 2 using namespace std; 3 int a[10001],n,cnt; 4 int i,j; 5 int main() 6 { 7 for(i=2;i<10001;++i){ 8 if(a[i]==0){ 9 for(j=2*i;j<10001;j+=i)++a[j]; 10 } 11 } 12 a[1]=1,cin>>n; 13 while(cin>>n){ 14 for(cnt=0,++n,i=1;i<n;++i)if(a[i]==0&&a[n-i]==0)++cnt; 15 cout<<cnt<<endl; 16 } 17 return 0; 18 }
E2571 GCD and LCM
First AC: 2017-10-11 Latest Modification: 2018-06-02
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,a,b,c; 4 int main() 5 { 6 cin>>T; 7 while(T--){ 8 cin>>a>>b; 9 c=__gcd(a,b); 10 cout<<c<<' '<<a*b/c<<endl; 11 } 12 return 0; 13 }
E2572 Sort It
First AC: 2017-11-17 Latest Modification: 2018-06-02
1 #include<bits/stdc++.h> 2 using namespace std; 3 int N,K,i; 4 int a[100005]; 5 int main() 6 { 7 cin>>N; 8 for(;i<N;)cin>>a[i++]; 9 sort(a,a+N); 10 cin>>K; 11 while(K--)cin>>N,cout<<a[--N]<<endl; 12 return 0; 13 }
E2573 Hub Connection Plan
First AC: 2018-06-29 Latest Modification: 2018-06-29
Note: 题目保证有一组可行解,而显然最优解恰好连接n-1次
这是因为,至少要n-1次才能将n个点连接起来
而超过n-1次连接则必将成环,这时可以把环上权最大的边删去
可用并查集来贪心地加边,为了尽可能选权重小的边
先对权重升序排序,然后但凡需要加边便加边即可
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,rst; 4 struct data{ 5 int x,y,c; 6 }a[15000]; 7 int pre[1001]; 8 int find(int x) 9 { 10 int r=x,i=x,j; 11 while(pre[r]!=r)r=pre[r]; 12 while(pre[i]!=r)j=pre[i],pre[i]=r,i=j; 13 return r; 14 } 15 void join(int x,int y) 16 { 17 int fx=find(x),fy=find(y); 18 if(fx!=fy)pre[fx]=fy; 19 } 20 int i; 21 bool cmp(data a,data b) 22 { 23 return a.c<b.c; 24 } 25 int main() 26 { 27 cin>>n>>m; 28 for(i=1;i<=n;++i)pre[i]=i; 29 for(i=0;i<m;++i)cin>>a[i].x>>a[i].y>>a[i].c; 30 sort(a,a+m,cmp); 31 for(i=0;i<m;++i){ 32 if(find(a[i].x)!=find(a[i].y)){ 33 join(a[i].x,a[i].y); 34 rst+=a[i].c; 35 } 36 } 37 cout<<rst; 38 return 0; 39 }
E2581 0 and 1
First AC: 2017-11-12 Latest Modification: 2017-11-12
1 #include<iostream> 2 using namespace std; 3 long T,n,num,dgt; 4 int i,j; 5 int main() 6 { 7 cin>>T; 8 for(i=0;i<T;i++){ 9 cin>>n; 10 num=dgt=0; 11 while(n!=0){ 12 if(n%2==1)num++; 13 dgt++,n/=2; 14 } 15 cout<<dgt-num<<' '<<num<<endl; 16 } 17 return 0; 18 }
E2582 When I Sort
First AC: 2017-12-29 Latest Modification: 2018-06-02
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n; 4 struct data{ 5 int num,ori,newn; 6 }a[10005]; 7 int i,j; 8 bool cmp1(data a,data b) 9 { 10 if(a.num==b.num)return a.ori<b.ori; 11 return a.num<b.num; 12 } 13 bool cmp2(data a,data b) 14 { 15 return a.ori<b.ori; 16 } 17 int main() 18 { 19 cin>>T; 20 while(T--){ 21 cin>>n; 22 for(i=0;i<n;++i)cin>>a[i].num,a[i].ori=i; 23 sort(a,a+n,cmp1); 24 for(i=0;i<n;++i)a[i].newn=i; 25 sort(a,a+n,cmp2); 26 for(cout<<a[0].newn,i=1;i<n;++i)cout<<' '<<a[i].newn; 27 cout<<endl; 28 } 29 return 0; 30 }
E2585 0-1 Package
First AC: 2018-04-27 Latest Modification: 2018-04-27
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll n,m,rst; 5 ll w[3500],v[3500],dp[12881]; 6 ll i,j; 7 int main() 8 { 9 cin>>n>>m; 10 for(i=1;i<=n;++i)cin>>w[i]>>v[i]; 11 for(i=1;i<=n;++i)for(j=m;j>=0;--j) 12 if(j>=w[i])dp[j]=max(dp[j],dp[j-w[i]]+v[i]); 13 for(i=0;i<=m;++i)if(dp[i]>rst)rst=dp[i]; 14 cout<<rst; 15 return 0; 16 }
E2586 Take a Party
First AC: 2018-04-27 Latest Modification: 2019-03-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=1e6+5; 4 int pre[maxn]; 5 int s,m,n; 6 int u,v; 7 int i; 8 int find(int x) 9 { 10 int r=x,i; 11 while(pre[r]!=r)r=pre[r]; 12 while(x!=r)i=pre[x],pre[x]=r,x=i; 13 return r; 14 } 15 void join(int x,int y) 16 { 17 int fx=find(x),fy=find(y); 18 pre[fx]=fy; 19 } 20 int main() 21 { 22 cin>>s>>m>>n; 23 for(i=1;i<=s;++i)pre[i]=i; 24 while(m--){ 25 cin>>u>>v; 26 join(u,v); 27 } 28 while(n--){ 29 cin>>u>>v; 30 if(find(u)!=find(v))cout<<"0\n"; 31 else cout<<"1\n"; 32 } 33 return 0; 34 }
E2606 Pinhole Imaging
First AC: 2017-10-12 Latest Modification: 2018-06-02
1 #include<bits/stdc++.h> 2 using namespace std; 3 double A,B,H; 4 int T,i; 5 int main() 6 { 7 cin>>T; 8 for(i=0;i<T;i++){ 9 cin>>A>>B>>H; 10 printf("%.2f\n",B*H/A); 11 } 12 return 0; 13 }
E2607 URL
First AC: 2018-02-13 Latest Modification: 2018-02-13
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,len,cnt; 4 string op,web; 5 struct data{ 6 string s; 7 int ls,num; 8 bool jdg; 9 }a[100]; 10 int i,j,k; 11 bool cmp(data a,data b) 12 { 13 if(a.jdg^b.jdg)return a.jdg>b.jdg; 14 if(a.num^b.num)return a.num>b.num; 15 return a.s<b.s; 16 } 17 int main() 18 { 19 cin>>T; 20 while(T--){ 21 cin>>n; 22 i=0; 23 while(n--){ 24 cin>>op>>web; 25 len=web.length(); 26 if(op[0]=='V'){ 27 cnt=1; 28 for(j=0;j<i;++j){ 29 if(a[j].s!=web)continue; 30 cnt=0,a[j].num++; 31 break; 32 } 33 if(cnt)a[i].s=web,a[i].num=0,a[j].ls=len,++i; 34 } 35 else{ 36 for(j=0;j<i;++j){ 37 if(a[j].ls<len){a[j].jdg=0;continue;} 38 for(cnt=1,k=0;k<len;++k){ 39 if(a[j].s[k]^web[k]){cnt=0,a[j].jdg=0;break;} 40 } 41 if(cnt)a[j].jdg=1; 42 } 43 sort(a,a+i,cmp); 44 for(j=0;j<i;++j) 45 if(a[j].jdg)cout<<a[j].s<<endl; 46 else break; 47 cout<<endl; 48 } 49 } 50 } 51 return 0; 52 }
E2610 工程
First AC: 2017-10-12 Latest Modification: 2018-06-02
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,m,k,i,j,t,c,sum; 4 int main() 5 { 6 cin>>T; 7 for(i=0;i<T;i++){ 8 cin>>m>>k; 9 for(j=0;j<m;j++){ 10 cin>>t; 11 c++; 12 if(c<=k)sum+=t; 13 } 14 cout<<sum<<endl; 15 sum=c=0; 16 } 17 return 0; 18 }
E2611 一日之晨
First AC: 2017-11-01 Latest Modification: 2018-06-02
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,h,m,s,n,i; 4 int main() 5 { 6 cin>>T; 7 for(i=0;i<T;++i){ 8 scanf("%d:%d:%d",&h,&m,&s); 9 n=3600*h+60*m+s; 10 if(n<=27900)cout<<"You are living a healthy life!\n"; 11 else if(n<=28200)cout<<"Hurry up!\n"; 12 else cout<<"You are late!\n"; 13 } 14 return 0; 15 }
E2612 魔幻图案
First AC: 2018-01-01 Latest Modification: 2018-06-02
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n; 4 string s; 5 bool a[25][15]; 6 int i,j; 7 int main() 8 { 9 cin>>T; 10 while(T--){ 11 cin>>s>>n; 12 memset(a,1,sizeof(a)); 13 for(i=0;i<25;++i)a[i][0]=0; 14 for(i=0;i<15;++i)a[0][i]=0; 15 if(s=="P"){ 16 for(i=0;i<15;++i)a[n+1][i]=0; 17 for(i=0;i<n+2;++i)a[i][n+1]=0; 18 } 19 else for(i=0;i<15;++i)a[2*n+2][i]=0; 20 for(i=0;i<2*n+3;++i){ 21 for(j=0;j<n+2;++j)cout<<a[i][j]; 22 cout<<endl; 23 } 24 } 25 return 0; 26 }
E2613 Gama函数
First AC: 2018-01-06 Latest Modification: 2018-01-06
1 #include<iostream> 2 using namespace std; 3 long f[1001]={0,1}; 4 int i,n; 5 int main() 6 { 7 for(i=1;i<1000;++i)f[i+1]=i*f[i]%1999; 8 cin>>n; 9 while(cin>>n)cout<<f[n]<<endl; 10 return 0; 11 }
E2614 组网计划
First AC: 2019-05-16 Latest Modification: 2019-05-16
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=105,maxm=5005; 4 int n,m,cnt,tmp,ans,tans; 5 int x[maxn],y[maxn]; 6 struct edge{ 7 int u,v,w; 8 }e[maxm]; 9 int mst[maxm]; 10 int pre[maxn]; 11 int i,j; 12 bool cmp(edge a,edge b) 13 { 14 return a.w<b.w; 15 } 16 int find(int x) 17 { 18 int r=x,i; 19 while(pre[r]!=r)r=pre[r]; 20 while(pre[x]!=r)i=pre[x],pre[x]=r,x=i; 21 return r; 22 } 23 void join(int x,int y,int num) 24 { 25 int fx=find(x),fy=find(y); 26 if(fx!=fy){ 27 pre[fx]=fy; 28 mst[++cnt]=num; 29 ans+=e[num].w; 30 } 31 } 32 void tjoin(int x,int y,int val) 33 { 34 int fx=find(x),fy=find(y); 35 if(fx!=fy){ 36 pre[fx]=fy; 37 tmp+=val; 38 ++cnt; 39 } 40 } 41 int main() 42 { 43 while(cin>>n){ 44 for(i=0;i<n;++i)cin>>x[i]>>y[i]; 45 m=0; 46 for(i=0;i<n;++i){ 47 for(j=i+1;j<n;++j){ 48 e[m].u=i; 49 e[m].v=j; 50 e[m].w=abs(x[i]-x[j])+abs(y[i]-y[j]); 51 ++m; 52 } 53 } 54 cnt=ans=0; 55 sort(e,e+m,cmp); 56 for(i=0;i<maxn;++i)pre[i]=i; 57 for(i=0;i<m;++i)join(e[i].u,e[i].v,i); 58 cout<<ans<<endl; 59 tans=ans; 60 ans=1e9; 61 for(i=1;i<n;++i){ 62 cnt=tmp=0; 63 for(j=0;j<maxn;++j)pre[j]=j; 64 for(j=0;j<m;++j){ 65 if(j!=mst[i]){ 66 tjoin(e[j].u,e[j].v,e[j].w); 67 } 68 } 69 if(cnt==n-1)ans=min(ans,tmp); 70 } 71 if(ans==tans)cout<<"Yes\n"; 72 else cout<<"No\n"; 73 } 74 return 0; 75 }
E2615 会议安排
First AC: 2018-06-02 Latest Modification: 2018-06-02
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n; 4 int dp[370]; 5 int i,j; 6 struct data{ 7 int op,ed,val; 8 }a[305]; 9 bool cmp(data a,data b) 10 { 11 return a.ed<b.ed; 12 } 13 int main() 14 { 15 cin>>T; 16 while(T--){ 17 cin>>n; 18 for(i=0;i<n;++i)cin>>a[i].op>>a[i].ed>>a[i].val; 19 sort(a,a+n,cmp); 20 a[n].op=a[n].ed=370; 21 j=0; 22 for(i=1;i<370;++i){ 23 dp[i]=dp[i-1]; 24 while(i==a[j].ed){ 25 dp[i]=max(dp[i],dp[a[j].op-1]+a[j].val); 26 ++j; 27 } 28 } 29 cout<<dp[369]<<endl; 30 } 31 return 0; 32 }
E2616 游黄山
First AC: 2018-04-20 Latest Modification: 2018-04-20
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const ll MAX=10000; 5 int T,n; 6 string s; 7 struct data{ 8 ll x,y,d; 9 }a[100]; 10 ll sumd,tmpd,x,y,d; 11 int i; 12 ll op(string s) 13 { 14 int len=s.length(),rst=0,p=len-1; 15 for(int i=0;i<len;++i){ 16 if(s[i]=='.')p=i; 17 else rst=10*rst+s[i]-'0'; 18 } 19 rst*=pow(10,5-len+p); 20 return rst; 21 } 22 bool cmpx(data a,data b) 23 { 24 return a.x<b.x; 25 } 26 bool cmpy(data a,data b) 27 { 28 return a.y<b.y; 29 } 30 int main() 31 { 32 cin>>T; 33 while(T--){ 34 cin>>n; 35 sumd=0; 36 for(i=0;i<n;++i){ 37 cin>>s; 38 a[i].x=op(s); 39 cin>>s; 40 a[i].y=op(s); 41 cin>>s; 42 a[i].d=op(s); 43 sumd+=a[i].d; 44 } 45 sort(a,a+n,cmpx); 46 tmpd=(sumd+1)/2; 47 for(i=0;;++i){ 48 if(a[i].d<tmpd)tmpd-=a[i].d; 49 else{ 50 x=a[i].x; 51 break; 52 } 53 } 54 sort(a,a+n,cmpy); 55 tmpd=(sumd+1)/2; 56 for(i=0;;++i){ 57 if(a[i].d<tmpd)tmpd-=a[i].d; 58 else{ 59 y=a[i].y; 60 break; 61 } 62 } 63 for(i=0,d=0;i<n;++i) 64 d+=a[i].d*(abs(x-a[i].x)+abs(y-a[i].y)); 65 printf("%.4f %.4f %.4f\n",x*1.0/MAX,y*1.0/MAX,d*1.0/MAX/MAX); 66 } 67 return 0; 68 }
E2618 游戏
First AC: 2017-12-01 Latest Modification: 2017-12-01
1 #include<iostream> 2 using namespace std; 3 int T,l,r,n; 4 string s; 5 int a[3][3]; 6 int i,j,k; 7 int main() 8 { 9 cin>>T; 10 for(k=0;k<T;++k){ 11 for(i=0;i<3;++i)for(j=0;j<3;++j){ 12 cin>>a[i][j]; 13 if(a[i][j]==0)l=i,r=j; 14 } 15 cin>>n; 16 while(n--){ 17 cin>>s; 18 if(s[0]=='l'){ 19 if(r)a[l][r]=a[l][r-1],a[l][r-1]=0,--r; 20 } 21 else if(s[0]=='r'){ 22 if(2-r)a[l][r]=a[l][r+1],a[l][r+1]=0,++r; 23 } 24 else if(s[0]=='u'){ 25 if(l)a[l][r]=a[l-1][r],a[l-1][r]=0,--l; 26 } 27 else if(s[0]=='d'){ 28 if(2-l)a[l][r]=a[l+1][r],a[l+1][r]=0,++l; 29 } 30 } 31 for(i=0;i<3;++i) 32 cout<<a[i][0]<<' '<<a[i][1]<<' '<<a[i][2]<<endl; 33 if(k!=T-1)cout<<endl; 34 } 35 return 0; 36 }
E2620 Unique Snowflakes
First AC: 2018-05-06 Latest Modification: 2018-05-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll T,n,m,lft,rgt,rst; 5 map<ll,ll>mp; 6 map<ll,ll>::iterator it; 7 int main() 8 { 9 ios::sync_with_stdio(false); 10 cin>>T; 11 while(T--){ 12 cin>>n; 13 if(!n){ 14 cout<<"0\n"; 15 continue; 16 } 17 mp.clear(); 18 lft=0,rst=1; 19 for(rgt=0;rgt<n;++rgt){ 20 cin>>m; 21 it=mp.find(m); 22 if(it!=mp.end()){ 23 lft=max(lft,it->second+1); 24 it->second=rgt; 25 rst=max(rst,rgt-lft+1); 26 } 27 else{ 28 mp.insert(pair<ll,ll>(m,rgt)); 29 rst=max(rst,rgt-lft+1); 30 } 31 } 32 cout<<rst<<endl; 33 } 34 return 0; 35 }
E2639 Ellipse, Again and Again
First AC: 2018-06-03 Latest Modification: 2018-06-03
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T; 4 int a,b,x,y; 5 int main() 6 { 7 cin>>T; 8 while(T--){ 9 cin>>a>>b>>x>>y; 10 if(b*b*x*x+a*a*y*y<a*a*b*b)cout<<"In ellipse\n"; 11 else cout<<a*a*b*b<<endl; 12 } 13 return 0; 14 }
E2646 Product-Sum
First AC: 2018-06-03 Latest Modification: 2018-06-03
Note: 设所求数的左半部分为a,右半部分为b
则有a*b-a-b=n,整理得(a-1)(b-1)=n+1
于是只要遍历找n+1的因子,并注意到所求数最小当且仅当a最小即可
遍历时只要遍历1到根号n
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll n,tmp,a,b,l,r; 5 ll i; 6 ll cal(ll n) 7 { 8 ll ret=0; 9 while(n)++ret,n/=10; 10 return ret; 11 } 12 int main() 13 { 14 while(cin>>n,n){ 15 tmp=(ll)sqrt(++n)+1; 16 l=r=1e9+7; 17 for(i=1;i<tmp;++i){ 18 if(!(n%i)){ 19 a=i+1; 20 b=n/i+1; 21 ll fa=cal(a),fb=cal(b); 22 if(fa>=fb&&a<l)l=a,r=b; 23 if(fb>=fa&&b<l)l=b,r=a; 24 } 25 } 26 tmp=cal(l); 27 while(tmp--)l*=10; 28 cout<<l+r<<endl; 29 } 30 return 0; 31 }
E2653 Find the Hotel
First AC: 2018-06-03 Latest Modification: 2018-06-03
Note: 题面的with either lower price or distance应该理解成with both lower price and distance
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,mind,tmp,rst; 4 struct data{ 5 int p,d; 6 bool jdg; 7 }a[10000]; 8 int i; 9 bool cmp(data a,data b) 10 { 11 if(a.p!=b.p)return a.p<b.p; 12 return a.d<b.d; 13 } 14 int main() 15 { 16 while(cin>>n){ 17 for(i=0;i<n;++i)cin>>a[i].p>>a[i].d; 18 sort(a,a+n,cmp); 19 mind=1e6; 20 rst=0; 21 for(i=0;i<n;++i){ 22 if(a[i].d>mind){ 23 a[i].jdg=0; 24 while(i+1<n&&a[i+1].p==a[i].p){ 25 a[++i].jdg=0; 26 } 27 } 28 else{ 29 ++rst; 30 a[i].jdg=1; 31 tmp=a[i].d; 32 while(i+1<n&&a[i+1].p==a[i].p){ 33 if(a[++i].d>mind)a[i].jdg=0; 34 else a[i].jdg=1,++rst; 35 } 36 mind=tmp; 37 } 38 } 39 cout<<rst<<endl; 40 for(i=0;i<n;++i)if(a[i].jdg){ 41 cout<<a[i].p<<' '<<a[i].d<<endl; 42 } 43 } 44 return 0; 45 }
E2654 Median
First AC: 2018-06-04 Latest Modification: 2018-06-04
Note: 桶排序优化
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,k,x1,x2,d,m; 4 int a[40000]; 5 int i; 6 int main() 7 { 8 ios::sync_with_stdio(false); 9 while(cin>>n>>k>>x1>>d>>m){ 10 memset(a,0,sizeof(a)); 11 a[x1]=1; 12 for(i=1;i<n;++i){ 13 x2=(k*x1+d)%m; 14 ++a[x2]; 15 x1=x2; 16 } 17 n=n/2+1; 18 for(i=0;i<m;++i){ 19 n-=a[i]; 20 if(n<=0){ 21 cout<<i<<endl; 22 break; 23 } 24 } 25 } 26 return 0; 27 }
E2655 Interesting Numbers
First AC: 2018-06-03 Latest Modification: 2018-06-03
Note: 注意到如果一个m(m>1)位数满足题设,那么它的低m-1位数也满足题设
于是只要对已经求出的m-1位数,遍历判断第m位即可
注意到结果可能很大,模的时候可以采取分段取模的方法
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef unsigned long long ll; 4 ll a[27]={0,1,5,6}; 5 ll base,cnt,tmp1,tmp2,n; 6 ll i,j; 7 int main() 8 { 9 base=1; 10 cnt=4; 11 while(1){ 12 tmp1=cnt; 13 base*=10; 14 for(i=1;i<10;++i){ 15 for(j=0;j<tmp1;++j){ 16 tmp2=i*base+a[j]; 17 ll div,mod,hgh,low,mod2; 18 div=1e8; 19 mod=10*base; 20 hgh=tmp2/div; 21 low=tmp2%div; 22 mod2=max(mod,10*div); 23 if((low*hgh%(mod2*10/div)*div*2+low*low)%mod==tmp2){ 24 a[cnt++]=tmp2; 25 if(cnt>25){ 26 while(cin>>n)cout<<a[n-1]<<endl; 27 exit(0); 28 } 29 } 30 } 31 } 32 } 33 return 0; 34 }
E2658 Play on Words
First AC: 2018-05-05 Latest Modification: 2018-05-05
1 #include<bits/stdc++.h> 2 using namespace std; 3 int m,n,num; 4 string s[5000]; 5 string t; 6 bool jdg(string s,string t) 7 { 8 int i=0,j=0; 9 int ls=s.length(),lt=t.length(); 10 while(1){ 11 while(i<ls&&s[i]!=t[j])++i; 12 if(j==lt)return 1; 13 if(i==ls)return 0; 14 ++i,++j; 15 } 16 } 17 int main() 18 { 19 ios::sync_with_stdio(false); 20 cin>>m; 21 for(int i=0;i<m;++i)cin>>s[i]; 22 cin>>n; 23 while(n--){ 24 cin>>t; 25 num=0; 26 for(int i=0;i<m;++i)if(jdg(s[i],t))++num; 27 cout<<num<<endl; 28 } 29 return 0; 30 }
E2686 Matchsticks
First AC: 2018-05-05 Latest Modification: 2018-05-05
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n; 4 string minn,maxn; 5 int i; 6 int cal(int n) 7 { 8 if(n<0)return -1; 9 return ceil(n/7.0); 10 } 11 int main() 12 { 13 cin>>T; 14 while(T--){ 15 cin>>n; 16 minn=maxn=""; 17 int tmp=n; 18 while(tmp>1)maxn+="1",tmp-=2; 19 if(tmp)maxn[0]='7'; 20 if(n==6)minn="6"; 21 else{ 22 int num=cal(n); 23 if(cal(n-2)==num-1)minn="1",n-=2; 24 else if(cal(n-5)==num-1)minn="2",n-=5; 25 else if(cal(n-4)==num-1)minn="4",n-=4; 26 else if(cal(n-6)==num-1)minn="6",n-=6; 27 else if(cal(n-3)==num-1)minn="7",n-=3; 28 else minn="8",n-=7; 29 --num; 30 while(num){ 31 if(cal(n-6)==num-1)minn+="0",n-=6; 32 else if(cal(n-2)==num-1)minn+="1",n-=2; 33 else if(cal(n-5)==num-1)minn+="2",n-=5; 34 else if(cal(n-4)==num-1)minn+="4",n-=4; 35 else if(cal(n-3)==num-1)minn+="7",n-=3; 36 else minn+="8",n-=7; 37 --num; 38 } 39 } 40 cout<<minn<<' '<<maxn<<endl; 41 } 42 return 0; 43 }
E2690 Soda Surpler
First AC: 2017-10-12 Latest Modification: 2018-06-04
1 #include<bits/stdc++.h> 2 using namespace std; 3 int e,f,c,num,sum; 4 int main() 5 { 6 while(cin>>e>>f>>c){ 7 num=e+f; 8 while(num>=c){ 9 sum+=num/c; 10 num=num%c+num/c; 11 } 12 cout<<sum; 13 sum=0; 14 } 15 return 0; 16 }
E2691 Money Matters
First AC: 2018-06-04 Latest Modification: 2018-06-04
Note: 并查集,如果每一个类中和为0则输出POSSIBLE,否则输出IMPOSSIBLE
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,x,y; 4 int cnt[10000],pre[10000]; 5 int i; 6 int find(int x) 7 { 8 int r=x,i=x,j; 9 while(pre[r]!=r)r=pre[r]; 10 while(pre[i]!=r)j=pre[i],pre[i]=r,i=j; 11 return r; 12 } 13 void join(int x,int y) 14 { 15 int fx=find(x),fy=find(y); 16 if(fx!=fy){ 17 cnt[fx]+=cnt[fy]; 18 cnt[fy]=0; 19 pre[fy]=fx; 20 } 21 } 22 int main() 23 { 24 while(cin>>n>>m){ 25 for(i=0;i<n;++i)cin>>cnt[i],pre[i]=i; 26 while(m--)cin>>x>>y,join(x,y); 27 bool jdg=1; 28 for(i=0;i<n;++i)if(cnt[i]){ 29 jdg=0; 30 break; 31 } 32 if(jdg)cout<<"POSSIBLE\n"; 33 else cout<<"IMPOSSIBLE\n"; 34 } 35 return 0; 36 }
E2703 Class
First AC: 2018-05-23 Latest Modification: 2018-05-23
Note: 本质是一道水题,把行和列小的那个尽量坐满一行(列),如果还有学生就随便坐
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,r,c,rst; 4 char mp[101][101]; 5 int i,j; 6 int main() 7 { 8 cin>>n>>r>>c; 9 rst=min((n+1)/2,min(r,c)); 10 cout<<rst<<endl; 11 memset(mp,'.',sizeof(mp)); 12 for(i=0;i<rst;++i)mp[i][0]='#',--n; 13 for(i=1;i<rst;++i)mp[0][i]='#',--n; 14 for(i=0;i<r;++i) 15 for(j=0;j<c;++j) 16 if(n&&mp[i][j]!='#') 17 mp[i][j]='#',--n; 18 for(i=0;i<r;++i){ 19 for(j=0;j<c;++j)cout<<mp[i][j]; 20 cout<<endl; 21 } 22 return 0; 23 }
E2708 Hole
First AC: 2017-11-16 Latest Modification: 2017-11-16
1 #include<iostream> 2 using namespace std; 3 int n,a,b,i; 4 int main() 5 { 6 while(cin>>n){ 7 if(n==0)cout<<"1\n"; 8 else if(n==1)cout<<"0\n"; 9 else{ 10 a=n%2,b=n/2; 11 for(i=0;i<a;++i)cout<<4; 12 for(i=0;i<b;++i)cout<<8; 13 cout<<endl; 14 } 15 } 16 return 0; 17 }
E2723 Six Grade Math
First AC: 2017-11-13 Latest Modification: 2017-11-13
1 #include<iostream> 2 using namespace std; 3 int T,a,b,c,m,n; 4 int i,j; 5 int main() 6 { 7 cin>>T; 8 for(i=0;i<T;++i){ 9 cin>>a>>b,m=a,n=b; 10 for(c=1;c!=0;)c=a%b,a=b,b=c; 11 cout<<i+1<<' '<<m*n/a<<' '<<a<<endl; 12 } 13 return 0; 14 }
E2724 Cryptoquote
First AC: 2018-05-17 Latest Modification: 2018-05-17
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len; 4 string s,t; 5 int i,j; 6 int main() 7 { 8 cin>>T; 9 getchar(); 10 for(i=1;i<=T;++i){ 11 getline(cin,s); 12 getline(cin,t); 13 cout<<i<<' '; 14 len=s.length(); 15 for(j=0;j<len;++j){ 16 if(s[j]<'A'||s[j]>'Z')cout<<s[j]; 17 else cout<<t[s[j]-'A']; 18 } 19 cout<<endl; 20 } 21 return 0; 22 }
E2725 Binary Clock
First AC: 2018-01-05 Latest Modification: 2018-06-04
1 #include<iostream> 2 using namespace std; 3 bool a[3][6]; 4 int T,h,m,s; 5 int i,j,k; 6 int main() 7 { 8 cin>>T; 9 for(i=1;i<=T;++i){ 10 scanf("%d:%d:%d",&h,&m,&s); 11 for(j=0;j<6;++j){ 12 a[0][j]=h&1; 13 a[1][j]=m&1; 14 a[2][j]=s&1; 15 h>>=1,m>>=1,s>>=1; 16 } 17 cout<<i<<' '; 18 for(j=5;j>=0;--j)cout<<a[0][j]<<a[1][j]<<a[2][j]; 19 cout<<' '; 20 for(j=0;j<3;++j)for(k=5;k>=0;--k)cout<<a[j][k]; 21 cout<<endl; 22 } 23 return 0; 24 }
E2726 Recursively Palindromic Partitions
First AC: 2018-05-17 Latest Modification: 2018-05-17
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n; 4 int dp[1001]; 5 int i,j,k; 6 int main() 7 { 8 dp[0]=1; 9 for(i=1;i<1001;++i){ 10 if(i&1){ 11 for(j=1;j<=i;j+=2)dp[i]+=dp[(i-j)/2]; 12 } 13 else{ 14 for(j=2;j<=i;j+=2)dp[i]+=dp[(i-j)/2]; 15 dp[i]+=dp[i/2]; 16 } 17 } 18 cin>>T; 19 for(i=1;i<=T;++i){ 20 cin>>n; 21 cout<<i<<' '<<dp[n]<<endl; 22 } 23 return 0; 24 }
E2743 Stock Exchange
First AC: 2018-06-04 Latest Modification: 2018-06-04
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 int n,m,len; 5 ll a[100001]; 6 int i,j,k; 7 void bi(int l,int r,int n) 8 { 9 if(l+1>=r){ 10 if(n==a[l]||n==a[r])return; 11 int rst=r+1; 12 while(rst&&n<a[rst-1])--rst; 13 a[rst]=n; 14 len=max(len,rst); 15 return; 16 } 17 int mid=(l+r)/2; 18 if(n>a[mid])bi(mid,r,n); 19 else if(n<a[mid])bi(l,mid,n); 20 else return; 21 } 22 int main() 23 { 24 while(cin>>n){ 25 memset(a,0x3f,sizeof(a)); 26 len=0; 27 cin>>a[0]; 28 while(--n)cin>>m,bi(0,len,m); 29 cout<<len+1<<endl; 30 } 31 return 0; 32 }
E2747 Build Your Home
First AC: 2018-06-04 Latest Modification: 2018-06-04
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,i,sum,mx,my; 4 double x[101],y[101]; 5 int main() 6 { 7 while(cin>>n,n){ 8 for(i=sum=mx=my=0;i<n;++i){ 9 cin>>x[i]>>y[i]; 10 if(x[i]<mx)mx=x[i]; 11 if(y[i]<my)my=y[i]; 12 } 13 14 for(i=0;i<n;++i)x[i]+=mx+1,y[i]+=my+1; 15 for(i=1;i<n;++i)sum+=x[i-1]*y[i]-x[i]*y[i-1]; 16 sum+=x[n-1]*y[0]-x[0]*y[n-1]; 17 if(sum<0)sum*=-1; 18 cout<<(int)(sum/2.0+0.5)<<endl; 19 } 20 return 0; 21 }
E2748 Quick Answer
First AC: 2018-06-04 Latest Modification: 2018-06-04
Note: 并查集,给每个点对应到一个id,删点时把点更新成新的id即可
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,x,y,yes,no; 4 char op; 5 int pre[30005],id[10005]; 6 int i; 7 int find(int x) 8 { 9 int r=x,i=x,j; 10 while(pre[r]!=r)r=pre[r]; 11 while(pre[i]!=r)j=pre[i],pre[i]=r,i=j; 12 return r; 13 } 14 void del(int x) 15 { 16 id[x]=++n; 17 pre[n]=n; 18 } 19 void join(int x,int y) 20 { 21 int fx=find(id[x]),fy=find(id[y]); 22 pre[fx]=fy; 23 } 24 void qui(int x,int y) 25 { 26 int fx=find(id[x]),fy=find(id[y]); 27 fx==fy? ++yes:++no; 28 } 29 int main() 30 { 31 ios::sync_with_stdio(false); 32 while(cin>>n){ 33 yes=no=0; 34 for(i=1;i<=n;++i)pre[i]=i,id[i]=i; 35 while(cin>>op){ 36 if(op=='c')cin>>x>>y,join(x,y); 37 else if(op=='d')cin>>x,del(x); 38 else if(op=='q')cin>>x>>y,qui(x,y); 39 else break; 40 } 41 cout<<yes<<" , "<<no<<endl; 42 } 43 return 0; 44 }
E2751 Internet Service Providers
First AC: 2018-06-04 Latest Modification: 2018-06-04
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,c,tmp1,tmp2; 4 int main() 5 { 6 while(cin>>n>>c){ 7 if(!n)cout<<"0\n"; 8 else{ 9 tmp1=c/n/2; 10 tmp2=tmp1+1; 11 cout<<(tmp1*(c-n*tmp1)<tmp2*(c-n*tmp2)? tmp2:tmp1)<<endl; 12 } 13 } 14 return 0; 15 }
E2766 Intriguting Identifiers
First AC: 2018-06-04 Latest Modification: 2018-06-04
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s; 4 int len,date,code,tmp; 5 int i; 6 int main() 7 { 8 int mon[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; 9 while(cin>>s,s!="end"){ 10 len=s.length(); 11 if(len<10||len>11||s[6]!='/'){ 12 cout<<"invalid\n"; 13 continue; 14 } 15 date=code=0; 16 bool jdg=1; 17 for(i=0;i<6;++i){ 18 if(s[i]<'0'||s[i]>'9'){ 19 jdg=0; 20 break; 21 } 22 date=10*date+s[i]-'0'; 23 } 24 for(i=7;i<len;++i){ 25 if(s[i]<'0'||s[i]>'9'){ 26 jdg=0; 27 break; 28 } 29 code=10*code+s[i]-'0'; 30 } 31 if(date/10000>19&&date/10000<54){ 32 if(len!=10)jdg=0; 33 } 34 else if(len!=11)jdg=0; 35 if(date/10000>9&&date/10000<20)jdg=0; 36 tmp=date%10000/100; 37 if(tmp<1||tmp>12) 38 if(tmp<51||tmp>62) 39 jdg=0; 40 if(tmp>30)tmp-=50; 41 if(date/10000%4){ 42 if(mon[tmp]<date%100)jdg=0; 43 } 44 else{ 45 if(tmp==2&&date%100==29); 46 else if(mon[tmp]<date%100)jdg=0; 47 } 48 if(len==11&&(date%11*10000+code)%11)jdg=0; 49 if(jdg)date%10000/100>30? cout<<"girl\n":cout<<"boy\n"; 50 else cout<<"invalid\n"; 51 } 52 return 0; 53 }
E2786 How Many Digits
First AC: 2018-06-05 Latest Modification: 2018-06-05
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,mod,cnt; 4 int i; 5 int main() 6 { 7 cin>>T; 8 while(T--){ 9 cin>>n; 10 mod=cnt=1; 11 for(i=2;;++i){ 12 mod=10*mod%n; 13 if(!((cnt+=mod)%=n)){ 14 cout<<i<<endl; 15 break; 16 } 17 } 18 } 19 return 0; 20 }
E2801 Headshot
First AC: 2018-12-16 Latest Modification: 2018-12-16
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s; 4 int len; 5 int zero,one,zeroone; 6 int main() 7 { 8 cin>>s; 9 len=s.length(); 10 s+=s[0]; 11 for(int i=0;i<len;++i){ 12 s[i]=='0'? ++zero:++one; 13 if(s[i]=='0'&&s[i+1]=='1')++zeroone; 14 } 15 if(zeroone*len<zero*one)cout<<"SHOOT"; 16 else if(zeroone*len>zero*one)cout<<"ROTATE"; 17 else cout<<"EQUAL"; 18 return 0; 19 }
E2819 糖果
First AC: 2017-10-18 Latest Modification: 2018-06-05
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,can,pap; 4 int main() 5 { 6 cin>>n; 7 while(cin>>n){ 8 can=pap=n; 9 while(pap>2)can+=pap/3,pap=pap%3+pap/3; 10 cout<<can<<endl; 11 } 12 return 0; 13 }
E2820 围栏
First AC: 2017-10-31 Latest Modification: 2018-06-05
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s; 4 int len,maxs,pre; 5 int i,j; 6 int main() 7 { 8 cin>>s; 9 len=s.length(); 10 if(len==0){cout<<len;return 0;} 11 for(i=0;i<len;++i){ 12 pre=1; 13 for(j=i;j<len-1;++j) 14 if(s[j]==s[j+1])break; 15 else ++pre; 16 if(pre>maxs)maxs=pre; 17 } 18 cout<<maxs; 19 return 0; 20 }
E2822 内存显示
First AC: 2018-03-09 Latest Modification: 2018-03-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 char s[31]; 4 void solvei(int n) 5 { 6 int c=sizeof(n); 7 unsigned char *p=(unsigned char*)&n; 8 while(c--)printf("%02x ",*p++); 9 } 10 void solved(double n) 11 { 12 int c=sizeof(n); 13 unsigned char *p=(unsigned char*)&n; 14 while(c--)printf("%02x ",*p++); 15 } 16 int main() 17 { 18 while(scanf("%s",s)!=EOF){ 19 if(strchr(s,'.')==0)solvei(atoi(s)); 20 else solved(atof(s)); 21 cout<<endl; 22 } 23 return 0; 24 }
E2824 Box
First AC: 2017-12-30 Latest Modification: 2018-06-05
1 #include<bits/stdc++.h> 2 using namespace std; 3 double n; 4 int main() 5 { 6 cin>>n; 7 while(cin>>n)printf("%.10f\n",n/6); 8 return 0; 9 }
E2831 Factorials
First AC: 2017-11-28 Latest Modification: 2017-11-28
1 #include<iostream> 2 using namespace std; 3 int T,n,s,i; 4 int main() 5 { 6 cin>>T; 7 while(T--){ 8 cin>>n; 9 ++n,s=1; 10 for(i=1;i<n;++i){ 11 s*=i; 12 while(!(s%10))s/=10; 13 if(s%2==0)s%=100; 14 } 15 cout<<s%10<<endl; 16 } 17 return 0; 18 }
E2832 Zero
First AC: 2018-05-05 Latest Modification: 2018-05-05
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,cnt; 4 string s="1 2 3 4 5 6 7 8 9",t="+- "; 5 string rst[1000]; 6 int i,j,k,p,q,r,m,l; 7 void solve(int n) 8 { 9 int num=0,tmp=0,flag=0; 10 for(int i=0;i<2*n-1;++i){ 11 if(s[i]=='+'){ 12 if(flag==0)num+=tmp; 13 else if(flag==1)num-=tmp; 14 tmp=0,flag=0; 15 } 16 else if(s[i]=='-'){ 17 if(flag==0)num+=tmp; 18 else if(flag==1)num-=tmp; 19 tmp=0,flag=1; 20 } 21 else if(s[i]==' '); 22 else tmp=10*tmp+s[i]-'0'; 23 } 24 if(flag==0)num+=tmp; 25 else if(flag==1)num-=tmp; 26 if(!num)rst[cnt++]=s.substr(0,2*n-1); 27 } 28 int main() 29 { 30 cin>>T; 31 while(T--){ 32 cin>>n; 33 cnt=0; 34 for(i=0;i<3;++i)for(j=0;j<3;++j) 35 for(k=0;k<3;++k)for(p=0;p<3;++p) 36 for(q=0;q<3;++q)for(r=0;r<3;++r) 37 for(m=0;m<3;++m)for(l=0;l<3;++l){ 38 s[1]=t[i],s[3]=t[j],s[5]=t[k], 39 s[7]=t[p],s[9]=t[q],s[11]=t[r], 40 s[13]=t[m],s[15]=t[l],solve(n); 41 } 42 sort(rst,rst+cnt); 43 cout<<rst[0]<<endl; 44 for(i=1;i<cnt;++i)if(rst[i]!=rst[i-1])cout<<rst[i]<<endl; 45 cout<<endl; 46 } 47 return 0; 48 }
E2833 Lumberjack Sequencing
First AC: 2018-01-05 Latest Modification: 2018-01-05
1 #include<iostream> 2 using namespace std; 3 int T,i,j; 4 bool tmp,rst; 5 int a[10]; 6 int main() 7 { 8 cin>>T; 9 cout<<"Lumberjacks:\n"; 10 while(T--){ 11 for(i=0;i<10;++i)cin>>a[i]; 12 for(i=1;i<10;++i)if(a[i]!=a[i-1]){tmp=a[i]>a[i-1];break;} 13 if(i==10){cout<<"Ordered\n";continue;} 14 for(rst=1,j=i+1;j<10;++j){ 15 if((bool)(a[j]>a[j-1])!=tmp){rst=0;break;} 16 } 17 if(rst)cout<<"Ordered\n"; 18 else cout<<"Unordered\n"; 19 } 20 return 0; 21 }
E2836 Financial Management
First AC: 2018-05-27 Latest Modification: 2018-05-27
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T; 4 double tmp,rst; 5 int i,j; 6 int main() 7 { 8 cin>>T; 9 for(i=1;i<=T;++i){ 10 rst=0; 11 for(j=0;j<12;++j)cin>>tmp,rst+=tmp; 12 rst/=12.0; 13 cout<<i<<" $"; 14 if(rst>1000000)cout<<(int)rst/1000000<<','; 15 if(rst>1000)cout<<(int)rst%1000000/1000<<','; 16 printf("%.2f\n",rst-(int)rst/1000*1000); 17 } 18 return 0; 19 }
E2837 Code Number
First AC: 2018-06-05 Latest Modification: 2018-06-05
1 #include<bits/stdc++.h> 2 using namespace std; 3 int len; 4 string s; 5 char c[128]; 6 int i; 7 int main() 8 { 9 for(i=0;i<128;++i)c[i]=char(i); 10 c['0']='O'; 11 c['1']='I'; 12 c['2']='Z'; 13 c['3']='E'; 14 c['4']='A'; 15 c['5']='S'; 16 c['6']='G'; 17 c['7']='T'; 18 c['8']='B'; 19 c['9']='P'; 20 getline(cin,s); 21 while(getline(cin,s)){ 22 len=s.length(); 23 for(i=0;i<len;++i)cout<<c[s[i]]; 24 cout<<endl; 25 } 26 return 0; 27 }
E2838 Cancer or Scorpio
First AC: 2018-06-06 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int mon1[13]={0,31,59,90,120,151,181,212,243,273,304,334,365}; 4 int mon2[13]={0,31,60,91,121,152,182,213,244,274,305,335,366}; 5 int T,n,y,m,d; 6 int i; 7 int d2n(int y,int m,int d) 8 { 9 if((!(y%4)&&y%100)||!(y%400))return mon2[m-1]+d; 10 return mon1[m-1]+d; 11 } 12 void n2d(int num) 13 { 14 if((!(y%4)&&y%100)||!(y%400)){ 15 if(num>366)num-=366,++y; 16 } 17 else if(num>365)num-=365,++y; 18 if((!(y%4)&&y%100)||!(y%400)){ 19 for(int i=0;;++i){ 20 if(num<=mon2[i]){ 21 num-=mon2[i-1]; 22 m=i,d=num; 23 break; 24 } 25 } 26 } 27 else{ 28 for(int i=0;;++i){ 29 if(num<=mon1[i]){ 30 num-=mon1[i-1]; 31 m=i,d=num; 32 break; 33 } 34 } 35 } 36 } 37 void find(int date) 38 { 39 if(date>120&&date<220)cout<<"aquarius\n"; 40 else if(date>219&&date<321)cout<<"pisces\n"; 41 else if(date>320&&date<421)cout<<"aries\n"; 42 else if(date>420&&date<522)cout<<"taurus\n"; 43 else if(date>521&&date<622)cout<<"gemini\n"; 44 else if(date>621&&date<723)cout<<"cancer\n"; 45 else if(date>722&&date<822)cout<<"leo\n"; 46 else if(date>821&&date<924)cout<<"virgo\n"; 47 else if(date>923&&date<1024)cout<<"libra\n"; 48 else if(date>1023&&date<1123)cout<<"scorpio\n"; 49 else if(date>1122&&date<1223)cout<<"sagittarius\n"; 50 else cout<<"capricorn\n"; 51 } 52 int main() 53 { 54 cin>>T; 55 for(i=1;i<=T;++i){ 56 cin>>n; 57 y=n%10000,m=n/1000000,d=n%1000000/10000; 58 n2d(d2n(y,m,d)+280); 59 printf("%d %2d/%2d/%4d ",i,m,d,y); 60 find(m*100+d); 61 } 62 return 0; 63 }
E2844 排序去重
First AC: 2017-11-13 Latest Modification: 2018-03-07
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n=0,cnt,i; 4 int a[1005]; 5 string s; 6 int main() 7 { 8 cin>>s; 9 while(scanf("%d",&n)!=EOF)++a[n]; 10 if(s=="A")for(i=1;i<1001;++i){if(a[i]!=0) 11 if(cnt==0)cout<<i,++cnt; 12 else cout<<" "<<i;} 13 else{for(i=1000;i>0;--i)if(a[i]!=0) 14 if(cnt==0)cout<<i,++cnt; 15 else cout<<" "<<i;} 16 return 0; 17 }
E2845 符号方程求解
First AC: 2018-02-17 Latest Modification: 2018-02-17
1 #include<bits/stdc++.h> 2 using namespace std; 3 char c,a; 4 int tmp; 5 int x1,c1,x2,c2; 6 bool sig=1; 7 int i; 8 int main() 9 { 10 while(c=getchar()){ 11 if(c=='='){ 12 if(sig)c1+=tmp; 13 else c1-=tmp; 14 x2=x1,c2=c1,x1=0,c1=0,tmp=0,sig=1; 15 } 16 else if(c=='\n'){ 17 if(sig)c1+=tmp; 18 else c1-=tmp; 19 break; 20 } 21 else if(c=='+'){ 22 if(sig)c1+=tmp; 23 else c1-=tmp; 24 tmp=0,sig=1; 25 } 26 else if(c=='-'){ 27 if(sig)c1+=tmp; 28 else c1-=tmp; 29 tmp=0,sig=0; 30 } 31 else if(c>='a'&&c<='z'){ 32 if(tmp==0)tmp=1; 33 a=c; 34 if(sig)x1+=tmp; 35 else x1-=tmp; 36 tmp=0; 37 } 38 else tmp=tmp*10+c-'0'; 39 } 40 if(c2==c1){ 41 cout<<a<<"=0.000"; 42 return 0; 43 } 44 float up=c2-c1,dn=x1-x2; 45 bool sig; 46 if(up>0&&dn>0||up<0&&dn<0)sig=0; 47 else sig=1; 48 up=fabs(up),dn=fabs(dn); 49 if(sig)printf("%c=-%.3f",a,up/dn); 50 else printf("%c=%.3f",a,up/dn); 51 return 0; 52 }
E2846 统计字符串个数
First AC: 2017-10-19 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a00[21],a01[21],a10[21],a11[21],n; 4 int i; 5 int main() 6 { 7 a00[1]=1,a00[2]=1; 8 a01[1]=0,a01[2]=1; 9 a10[1]=0,a10[2]=1; 10 a11[1]=1,a11[2]=1; 11 for(i=3;i<21;i++){ 12 a00[i]=a00[i-1]+a01[i-1]; 13 a11[i]=a01[i]=a11[i-1]+a10[i-1]; 14 a10[i]=a00[i-1]; 15 } 16 while(cin>>n,n!=-1){ 17 cout<<a00[n]+a01[n]+a10[n]+a11[n]<<endl; 18 } 19 return 0; 20 }
E2847 路由节点
First AC: 2018-01-05 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 int main() 5 { 6 while(cin>>n)cout<<n*(n-1)*(n-2)*(n-3)/24<<endl; 7 return 0; 8 }
E2848 华师大卫星照片
First AC: 2017-11-26 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int w,h,r,l,tmp,maxn,i,j; 4 char a[1002][82]; 5 6 int cnt(int l,int r) 7 { 8 if(a[l][r]=='.'||l==0||r==0||l>h||r>w)return 0; 9 if(a[l-1][r]-'.'||a[l][r-1]-'.'||a[l][r+1]-'.'||a[l+1][r]-'.'){ 10 a[l][r]='.'; 11 return cnt(l-1,r)+cnt(l,r-1)+cnt(l,r+1)+cnt(l+1,r)+1; 12 } 13 a[l][r]='.'; 14 return 1; 15 } 16 int main() 17 { 18 memset(a,'.',sizeof(a)); 19 cin>>w>>h; 20 for(i=1;i<=h;++i)for(j=1;j<=w;++j)cin>>a[i][j]; 21 for(i=1;i<=h;++i)for(j=1;j<=w;++j){ 22 tmp=cnt(i,j); 23 if(tmp>maxn)maxn=tmp; 24 } 25 cout<<maxn; 26 return 0; 27 }
E2849 成绩排序
First AC: 2017-11-20 Latest Modification: 2018-03-07
1 #include<bits/stdc++.h> 2 using namespace std; 3 struct data{ 4 long long num; 5 int score; 6 }a[100]; 7 int n,i; 8 bool cmp(data a,data b) 9 { 10 if(a.score==b.score)return a.num<b.num; 11 return a.score>b.score; 12 } 13 int main() 14 { 15 cin>>n; 16 for(i=0;i<n;++i)cin>>a[i].num>>a[i].score; 17 sort(a,a+n,cmp); 18 for(i=0;i<n;++i) 19 if(a[i].score<60)break; 20 else cout<<a[i].num<<' '<<a[i].score<<endl; 21 return 0; 22 }
E2850 小强的生日
First AC: 2017-11-28 Latest Modification: 2018-03-22
1 #include<bits/stdc++.h> 2 using namespace std; 3 int y,n,tmp,Maxd,i,flag; 4 int date(int y)//From Pro. 3322 5 { 6 for(i=2008,n=y;i<y;++i)if(i%4==0&&i%100!=0)++n; 7 return(n-1)%7; 8 } 9 int main() 10 { 11 while(cin>>y){ 12 tmp=date(y); 13 y%4==0&&y%100!=0? Maxd=30:Maxd=29; 14 flag=(tmp+Maxd)%7==1? 0:1; 15 cout<<" SU MO TU WE TH FR SA\n"; 16 for(i=0;i<tmp;++i)cout<<" "; 17 for(i=1;i<Maxd;++i){ 18 printf("%3d",i); 19 if(++tmp==7)cout<<endl,tmp=0; 20 } 21 if(flag)cout<<endl; 22 cout<<endl; 23 } 24 return 0; 25 }
E2851 达到回文数
First AC: 2017-11-22 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll n,m; 5 int cnt; 6 int ccl(ll n) 7 { 8 int rst=0; 9 while(n)rst++,n/=10; 10 return rst; 11 } 12 ll rvs(ll n) 13 { 14 int rst=0; 15 for(ll tmp=pow(10,ccl(n)-1);tmp;tmp/=10,n/=10){ 16 rst+=n%10*tmp; 17 } 18 return rst; 19 } 20 int main() 21 { 22 cin>>n; 23 while(1){ 24 m=rvs(n); 25 if(m==n){ 26 cout<<cnt<<' '<<n; 27 break; 28 } 29 ++cnt; 30 n+=m; 31 } 32 return 0; 33 }
E2852 统计字母频率
First AC: 2017-11-11 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s; 4 int ln,a[26],num; 5 int i; 6 int main() 7 { 8 getline(cin,s); 9 ln=s.length(); 10 for(i=0;i<ln;++i)if(s[i]>96&&s[i]<123)++num,++a[s[i]-97]; 11 for(i=ln=0;i<26;++i)if(a[i]>a[ln])ln=i; 12 printf("%c %.2f",97+ln,a[ln]*1.0/num); 13 return 0; 14 }
E2853 集合运算
First AC: 2017-12-01 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a,b,c,cnt; 4 string x[1001]; 5 struct data{ 6 string s; 7 int flag,len; 8 }n[2001]; 9 int i,j,k; 10 bool cmp(data m,data n) 11 { 12 if(m.flag-n.flag)return m.flag>n.flag; 13 if(m.flag){ 14 if(m.len-n.len)return m.len>n.len; 15 for(i=0;i<n.len;++i)if(m.s[i]!=n.s[i])return m.s[i]>n.s[i]; 16 } 17 else{ 18 if(m.len-n.len)return m.len<n.len; 19 for(i=0;i<n.len;++i)if(m.s[i]!=n.s[i])return m.s[i]<n.s[i]; 20 } 21 } 22 int main() 23 { 24 cin>>a>>b; 25 for(c=a+b;i<c;++i){ 26 cin>>n[i].s; 27 n[i].len=n[i].s.length(); 28 if(n[i].s[0]=='-')n[i].flag=1; 29 else n[i].flag=0; 30 } 31 sort(n,n+a,cmp); 32 for(i=0;i<a;++i){ 33 for(cnt=0,j=a;j<c;++j)if(n[i].s==n[j].s){++cnt;break;} 34 if(!cnt)x[k++]=n[i].s; 35 } 36 sort(n,n+c,cmp); 37 cout<<"{"; 38 for(cnt=0,i=1;i<c;++i)if(n[i].s==n[i-1].s){ 39 if(cnt)cout<<','<<n[i].s; 40 else cout<<n[i].s,++cnt; 41 } 42 cout<<"}\n{"; 43 if(c){ 44 cout<<n[0].s; 45 for(i=1;i<c;++i)if(n[i].s!=n[i-1].s)cout<<','<<n[i].s; 46 } 47 cout<<"}\n{"; 48 if(k){ 49 cout<<x[0]; 50 for(i=1;i<k;++i)cout<<','<<x[i]; 51 } 52 cout<<"}"; 53 return 0; 54 }
E2854 统计特定字串模式的个数
First AC: 2018-04-01 Latest Modification: 2018-04-01
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m; 4 long long dp[32][32]; 5 int i,j; 6 int main() 7 { 8 for(i=1;i<32;++i)for(j=1;j<32;++j){ 9 dp[i][j]+=2*dp[i-1][j]; 10 if(i>=j)dp[i][j]+=1<<max(0,i-j-1); 11 if(i>j+1)dp[i][j]-=dp[i-j-1][j]; 12 } 13 while(cin>>n>>m,n+1||m+1)cout<<dp[n][m]<<endl; 14 return 0; 15 }
E2855 贪吃蛇
First AC: 2018-02-26 Latest Modification: 2018-02-26
1 #include<bits/stdc++.h> 2 using namespace std; 3 int H,W,T,h,w,rst; 4 char maps[1005][1005]; 5 int fib[1001]={0,1}; 6 int i,j,k; 7 bool jdg(int n,int h,int w) 8 { 9 if(n==0){ 10 if(maps[h-1][w]!='*')return 1; 11 return 0; 12 } 13 else if(n==1){ 14 if(maps[h][w+1]!='*')return 1; 15 return 0; 16 } 17 else if(n==2){ 18 if(maps[h+1][w]!='*')return 1; 19 return 0; 20 } 21 else if(maps[h][w-1]!='*')return 1; 22 return 0; 23 } 24 int main() 25 { 26 cin>>H>>W>>T; 27 ++H,++W; 28 memset(maps,'*',sizeof(maps)); 29 for(i=1;i<H;++i){ 30 getchar(); 31 for(j=1;j<W;++j)cin>>maps[i][j]; 32 } 33 for(i=2;i<T;++i)fib[i]=(fib[i-1]+fib[i-2])%4; 34 if(maps[1][1]!='.')rst=maps[1][1]-'0'; 35 h=w=1; 36 for(i=0;i<T;++i){ 37 int tmp=fib[i]; 38 while(1){ 39 if(jdg(tmp,h,w)){ 40 if(tmp==0)--h; 41 else if(tmp==1)++w; 42 else if(tmp==2)++h; 43 else --w; 44 if(maps[h][w]!='.')rst+=maps[h][w]-'0'; 45 break; 46 } 47 else tmp=(tmp+1)%4; 48 } 49 } 50 cout<<rst; 51 return 0; 52 }
E2856 仰望星空
First AC: 2017-12-21 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int w,h,r,l,tmp,num,i,j; 4 char a[1005][85]; 5 int cnt(int l,int r) 6 { 7 if(a[l][r]=='.'||l==0||r==0||l>h||r>w)return 0; 8 if(a[l-1][r]-'.'||a[l][r-1]-'.'||a[l][r+1]-'.' 9 ||a[l+1][r]-'.'||a[l-1][r-1]-'.'||a[l-1][r+1]-'.' 10 ||a[l+1][r-1]-'.'||a[l+1][r+1]-'.'){ 11 a[l][r]='.'; 12 return cnt(l-1,r)+cnt(l,r-1)+cnt(l,r+1)+cnt(l+1,r) 13 +cnt(l-1,r-1)+cnt(l-1,r+1)+cnt(l+1,r-1)+cnt(l+1,r+1)+1; 14 } 15 a[l][r]='.'; 16 return 1; 17 } 18 int main() 19 { 20 while(cin>>w>>h){ 21 num=0; 22 memset(a,'.',sizeof(a)); 23 for(i=1;i<=h;++i)for(j=1;j<=w;++j)cin>>a[i][j]; 24 for(i=1;i<=h;++i)for(j=1;j<=w;++j){ 25 tmp=cnt(i,j); 26 if(tmp)++num; 27 } 28 cout<<num<<endl; 29 } 30 return 0; 31 }
E2857 编辑距离
First AC: 2017-12-12 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int f[501][501]; 4 int T,ls,lt,tmp,a,b,c; 5 string s,t; 6 int i,j,k; 7 int main() 8 { 9 cin>>T; 10 for(i=0;i<T;++i){ 11 cin>>s>>t; 12 ls=s.length(),lt=t.length(); 13 tmp=ls>lt? ls:lt; 14 for(j=1;j<=tmp;++j)f[0][j]=f[j][0]=j; 15 for(j=0;j<ls;++j){ 16 for(k=0;k<lt;++k){ 17 if(s[j]==t[k])f[j+1][k+1]=f[j][k]; 18 else{ 19 a=f[j][k+1],b=f[j+1][k],c=f[j][k]; 20 f[j+1][k+1]=min(a,min(b,c))+1; 21 } 22 } 23 } 24 cout<<f[ls][lt]<<endl; 25 } 26 return 0; 27 }
E2858 危险,距离不能为二
First AC: 2018-05-03 Latest Modification: 2018-05-03
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,h,w; 4 int main() 5 { 6 int a[4][4]={1,2,2,2,2,4,4,4,2,4,5,6,2,4,6,8}; 7 cin>>n; 8 while(n--){ 9 cin>>h>>w; 10 if(h>w)h^=w,w^=h,h^=w; 11 cout<<a[(h-1)%4][(w-1)%4]+2*((h-1)%4+1) 12 *((w-1)/4)+2*w*((h-1)/4)<<endl; 13 } 14 return 0; 15 }
E2859 表达式的个数
First AC: 2018-08-09 Latest Modification: 2018-08-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s="1 2 3 4 5 6 7 8 9+"; 4 string t="+- "; 5 map<int,int>mp; 6 map<int,int>::iterator it; 7 int tmp; 8 int i,j,k,p,q,r,m,n; 9 int cal(string s) 10 { 11 int ret=tmp=0; 12 int sign=1; 13 int len=s.length(); 14 for(int i=0;i<len;++i){ 15 if(s[i]=='+'){ 16 ret+=sign*tmp; 17 tmp=0; 18 sign=1; 19 } 20 else if(s[i]=='-'){ 21 ret+=sign*tmp; 22 tmp=0; 23 sign=-1; 24 } 25 else if(s[i]!=' '){ 26 tmp=10*tmp+s[i]-'0'; 27 } 28 } 29 return ret; 30 } 31 int main() 32 { 33 for(i=0;i<3;++i){ 34 s[1]=t[i]; 35 for(j=0;j<3;++j){ 36 s[3]=t[j]; 37 for(k=0;k<3;++k){ 38 s[5]=t[k]; 39 for(p=0;p<3;++p){ 40 s[7]=t[p]; 41 for(q=0;q<3;++q){ 42 s[9]=t[q]; 43 for(r=0;r<3;++r){ 44 s[11]=t[r]; 45 for(m=0;m<3;++m){ 46 s[13]=t[m]; 47 for(n=0;n<3;++n){ 48 s[15]=t[n]; 49 tmp=cal(s); 50 it=mp.find(tmp); 51 if(it!=mp.end())++it->second; 52 else mp.insert(pair<int,int>(tmp,1)); 53 } 54 } 55 } 56 } 57 } 58 } 59 } 60 } 61 while(cin>>tmp){ 62 it=mp.find(tmp); 63 if(it==mp.end())cout<<"0\n"; 64 else cout<<it->second<<endl; 65 } 66 return 0; 67 }
E2862 求斜边
First AC: 2018-01-05 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a,b; 4 int main() 5 { 6 cin>>a>>b; 7 printf("%.3f",sqrt(a*a+b*b)); 8 return 0; 9 }
E2863 重力加速度
First AC: 2017-10-22 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 int main() 5 { 6 cin>>n; 7 if(n==1)cout<<"0.598"; 8 if(n==2)cout<<"1.174"; 9 if(n!=1&&n!=2)printf("%.3f",sqrt((6*n+5.5)/9.8)); 10 return 0; 11 }
E2864 Reverse
First AC: 2017-11-09 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s; 4 int len,i; 5 int main() 6 { 7 cin>>s; 8 len=s.length(); 9 cout<<len<<' '; 10 for(i=len-1;i>=0;--i)cout<<s[i]; 11 return 0; 12 }
E2875 成绩统计
First AC: 2017-12-29 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 double ava,var; 5 int a[101]; 6 string s; 7 int i,j; 8 int main() 9 { 10 cin>>n; 11 for(i=0;i<n;++i){ 12 cin>>s>>a[i]; 13 ava+=a[i]; 14 } 15 ava/=n; 16 for(i=0;i<n;++i)var+=(ava-a[i])*(ava-a[i]); 17 printf("%.2f %.2f",ava,sqrt(var/(n-1))); 18 return 0; 19 }
E2876 二进制位不同的个数
First AC: 2017-10-31 Latest Modification: 2018-03-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int m,n,a[35],b[35],num,i; 4 int main() 5 { 6 cin>>m; 7 while(cin>>m>>n){ 8 num=0; 9 for(i=0;i<35;++i)a[i]=m%2,b[i]=n%2,m/=2,n/=2; 10 for(i=0;i<35;++i)if(a[i]!=b[i])++num; 11 cout<<num<<endl; 12 } 13 return 0; 14 }
E2877 哥德巴赫猜想
First AC: 2017-12-27 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a[20001],n,tmp; 4 int i,j; 5 int main() 6 { 7 for(i=2;i<20001;++i){ 8 if(a[i]==0){ 9 for(j=2*i;j<20001;j+=i)++a[j]; 10 } 11 } 12 while(cin>>n){ 13 tmp=n/2; 14 while(1){ 15 if(a[tmp]==0&&a[n-tmp]==0){ 16 cout<<tmp<<' '<<n-tmp<<endl; 17 break; 18 } 19 else --tmp; 20 } 21 } 22 return 0; 23 }
E2878 字串排序
First AC: 2017-12-21 Latest Modification: 2018-03-15
1 #include<bits/stdc++.h> 2 using namespace std; 3 struct data{ 4 string s; 5 bool flag; 6 long n; 7 }a[105]; 8 string t; 9 long base; 10 int i,cnt,len; 11 bool cmp(data a,data b) 12 { 13 if(a.flag==b.flag){ 14 if(a.flag==0||a.n==b.n){ 15 int lena=a.s.length(),lenb=b.s.length(); 16 int tmp=lena<lenb? lena:lenb; 17 for(int i=0;i<tmp;++i) 18 if(a.s[i]!=b.s[i])return a.s[i]>b.s[i]; 19 return a.s.length()>b.s.length(); 20 } 21 else return a.n>b.n; 22 } 23 return a.flag>b.flag; 24 } 25 int main() 26 { 27 while(cin>>t){ 28 len=t.length()-1,a[cnt].s=t; 29 for(base=1,i=len;i>=0;--i)if(t[i]>='0'&&t[i]<='9') 30 a[cnt].n+=base*(t[i]-'0'),base*=10; 31 if(base^1)a[cnt].flag=1; 32 ++cnt; 33 } 34 sort(a,a+cnt,cmp); 35 cout<<a[cnt-1].s; 36 for(i=cnt-2;i>=0;--i)cout<<' '<<a[i].s; 37 cout<<endl; 38 return 0; 39 }
E2879 求三元组的个数
First AC: 2018-04-08 Latest Modification: 2018-03-15
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,cnt; 4 int a[500]; 5 int i,j,k; 6 int main() 7 { 8 cin>>n; 9 for(i=0;i<n;++i)cin>>a[i]; 10 for(i=0;i<n;++i)for(j=i+1;j<n;++j)for(k=j+1;k<n;++k){ 11 if(i==j||i==k||j==k)continue; 12 if(__gcd(a[i],a[j])==1&&__gcd(a[i],a[k])==1 13 &&__gcd(a[j],a[k])==1)++cnt; 14 if(__gcd(a[i],a[j])!=1&&__gcd(a[i],a[k])!=1 15 &&__gcd(a[j],a[k])!=1)++cnt; 16 } 17 cout<<cnt<<endl; 18 return 0; 19 }
E2880 Young Physicist
First AC: 2017-11-15 Latest Modification: 2017-11-15
1 #include<iostream> 2 using namespace std; 3 int t,n,x,y,z,a,b,c; 4 int main() 5 { 6 cin>>t; 7 while(t--){ 8 cin>>n; 9 a=b=c=0; 10 while(n--){ 11 cin>>x>>y>>z; 12 a+=x,b+=y,c+=z; 13 } 14 if(!a&&!b&&!c)cout<<"YES\n"; 15 else cout<<"NO\n"; 16 } 17 return 0; 18 }
E2881 Bets
First AC: 2018-02-20 Latest Modification: 2018-02-20
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,m,l,r,t,p,rst; 4 int pertime[101]; 5 int profit[101]; 6 int i,j; 7 int main() 8 { 9 cin>>T; 10 while(T--){ 11 cin>>n>>m; 12 ++n; 13 rst=0; 14 for(i=1;i<n;++i)pertime[i]=1001,profit[i]=0; 15 for(i=0;i<m;++i){ 16 cin>>l>>r>>t>>p; 17 ++r; 18 for(j=l;j<r;++j) 19 if(t<pertime[j])pertime[j]=t,profit[j]=p; 20 } 21 for(i=1;i<n;++i)if(profit[i])rst+=profit[i]; 22 cout<<rst<<endl; 23 } 24 return 0; 25 }
E2882 Bookshelf2
First AC: 2018-06-06 Latest Modification: 2018-06-06
Note: 同题2064
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll n,b,rst=1e7; 5 ll h[20000]; 6 ll i; 7 ll find(ll i) 8 { 9 ll s=0,j=0; 10 while(i){ 11 if(i&1)s+=h[j]; 12 ++j,i>>=1; 13 } 14 if(s<b)return 1e7; 15 return s; 16 } 17 int main() 18 { 19 cin>>n>>b; 20 for(i=0;i<n;++i)cin>>h[i]; 21 for(i=(1<<n)-1;i;--i)rst=min(rst,find(i)); 22 cout<<rst-b; 23 return 0; 24 }
E2885 A Spiral Work
First AC: 2018-05-16 Latest Modification: 2018-05-16
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 int a[751][751]; 5 void solve(int r,int c,int len,int st) 6 { 7 if(len<1)return; 8 a[r][c]=st; 9 int i; 10 for(i=1;i<len;++i)a[r][++c]=++st; 11 for(i=1;i<len;++i)a[++r][c]=++st; 12 for(i=1;i<len;++i)a[r][--c]=++st; 13 for(i=2;i<len;++i)a[--r][c]=++st; 14 solve(r,c+1,len-2,st+1); 15 16 } 17 void print(int n) 18 { 19 for(int i=1;i<=n;++i){ 20 cout<<a[i][1]; 21 for(int j=2;j<=n;++j)cout<<' '<<a[i][j]; 22 cout<<endl; 23 } 24 } 25 int main() 26 { 27 cin>>n; 28 solve(1,1,n,1); 29 print(n); 30 return 0; 31 }
E2886 个人所得税
First AC: 2017-10-18 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 double s; 4 int main() 5 { 6 cin>>s; 7 if(s<=1500)cout<<"0.00"; 8 else if(s<=3000)printf("%.2f",s/20-75); 9 else if(s<=5000)printf("%.2f",s/10-150); 10 else if(s<=10000)printf("%.2f",s*3/20-225); 11 else printf("%.2f",s/5-300); 12 return 0; 13 }
E2887 指针
First AC: 2018-03-29 Latest Modification: 2018-03-29
1 #include<bits/stdc++.h> 2 using namespace std; 3 int main() 4 { 5 cout<<"BBBB0000\nBBBBAAAA\nAAAA\nCD\nCDAAAAAA\n5678\n34\n00\n"; 6 return 0; 7 }
E2888 买房子
First AC: 2017-10-18 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 double income,price,n; 4 int main() 5 { 6 while(cin>>n>>k>>m){ 7 income=n,price=270,count=0; 8 for(year=1;year<=30;year++){ 9 if(income>=price){count++;break;} 10 n*=(m/100.0+1),price*=(k/100.0+1),income+=n; 11 } 12 if(count==0)cout<<"Impossible\n"; 13 else cout<<year<<endl; 14 } 15 return 0; 16 }
E2889 魔方阵
First AC: 2017-10-19 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,line,row,maxi; 4 int i,j,k; 5 int a[101][101]; 6 int main() 7 { 8 for(k=0;k<19;k++){ 9 cin>>n; 10 maxi=n*n+1; 11 line=1;row=n/2+1; 12 a[line][row]=1; 13 for(i=2;i<maxi;i++){ 14 if(line==1&&row!=n){ 15 a[n][row+1]=i; 16 line=n,row++; 17 } 18 else if(row==n&&line!=1){ 19 a[line-1][1]=i; 20 line--,row=1; 21 } 22 else if(line==1&&row==n){ 23 a[line+1][row]=i; 24 line++; 25 } 26 else if(a[line-1][row+1]==0){ 27 a[line-1][row+1]=i; 28 line--,row++; 29 } 30 else{ 31 a[line+1][row]=i; 32 line++; 33 } 34 } 35 for(i=n;i>0;i--){ 36 cout<<a[i][n],a[i][n]=0; 37 for(j=n-1;j>0;j--){ 38 cout<<" "<<a[i][j]; 39 a[i][j]=0; 40 } 41 cout<<endl; 42 } 43 } 44 return 0; 45 }
E2890 询问队员身高
First AC: 2017-11-25 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int t,m,n,k; 4 int i; 5 struct data{ 6 int num,height; 7 }a[51]; 8 bool cmp(data a,data b) 9 { 10 return a.height>b.height; 11 } 12 int main() 13 { 14 cin>>t; 15 while(t--){ 16 cin>>m>>n; 17 for(i=0;i<m;++i)a[i].num=i+1,cin>>a[i].height; 18 sort(a,a+m,cmp); 19 for(i=0;i<n;++i){ 20 cin>>k; 21 if(i==0)cout<<a[k-1].num; 22 else cout<<' '<<a[k-1].num; 23 } 24 cout<<endl; 25 } 26 return 0; 27 }
E2891 多次函数
First AC: 2017-11-02 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 char a[45][45]; 4 int i,j; 5 string s; 6 int b[4],c,t,m,cnt,n; 7 int main() 8 { 9 while(cin>>s){ 10 if(cnt!=0)cout<<endl; 11 cnt++; 12 //打表 13 memset(a,'.',sizeof(a)); 14 for(i=1;i<42;++i)a[21][i]='-',a[i][21]='|'; 15 a[1][21]='^',a[21][21]='+',a[21][41]='>'; 16 b[0]=b[1]=b[2]=b[3]=0; 17 //覆盖 18 for(i=5;i<s.length();++i) 19 if(s[i]=='x'){ 20 t=1; 21 //判断次数t: 22 if(i!=s.length()-1&&s[i+1]=='^')t=(int)(s[i+2]-'0'); 23 //判断系数c: 24 if(s[i-3]=='-') 25 c=-10*(s[i-2]-'0')-(s[i-1]-'0'); 26 else if(s[i-2]>='0'&&s[i-2]<='9'&&s[i-3]!='^') 27 c=10*(s[i-2]-'0')+(s[i-1]-'0'); 28 else if(s[i-2]=='-') 29 c=-(s[i-1]-'0'); 30 else if(s[i-1]=='-') 31 c=-1; 32 else if(s[i-1]>='0'&&s[i-1]<='9') 33 c=(s[i-1]-'0'); 34 else c=1; 35 b[t]=c; 36 } 37 //常数项: 38 if(s[s.length()-1]!='x'&&s[s.length()-2]!='^'){ 39 if(s[s.length()-3]=='-') 40 b[0]=-10*(s[s.length()-2]-'0')-(s[s.length()-1]-'0'); 41 else if(s[s.length()-2]>='0'&&s[s.length()-2]<='9' 42 &&s[s.length()-3]!='^') 43 b[0]=10*(s[s.length()-2]-'0')+(s[s.length()-1]-'0'); 44 else if(s[s.length()-2]=='-') 45 b[0]=-(s[s.length()-1]-'0'); 46 else if(s[s.length()-1]=='-') 47 b[0]=-1; 48 else if(s[s.length()-1]>='0'&&s[s.length()-1]<='9') 49 b[0]=s[s.length()-1]-'0'; 50 else b[0]=1; 51 } 52 //计算输出 53 if(n==0){ 54 for(i=-20;i<21;++i){ 55 m=b[3]*i*i*i+b[2]*i*i+b[1]*i+b[0]; 56 if(m>-21&&m<21)a[21-m][i+21]='*'; 57 } 58 for(i=1;i<42;++i){ 59 for(j=1;j<42;++j)cout<<a[i][j]; 60 if(i!=41)cout<<endl; 61 } 62 n++; 63 } 64 else{ 65 cout<<endl; 66 for(i=-20;i<21;++i){ 67 m=b[3]*i*i*i+b[2]*i*i+b[1]*i+b[0]; 68 if(m>-21&&m<21)a[21-m][i+21]='*'; 69 } 70 for(i=1;i<42;++i){ 71 for(j=1;j<42;++j)cout<<a[i][j]; 72 if(i!=41)cout<<endl; 73 } 74 } 75 } 76 return 0; 77 }
E2892 文件排序
First AC: 2017-12-31 Latest Modification: 2019-04-07
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 struct data{ 5 string date,time,dtime; 6 int size; 7 string name; 8 }a[105]; 9 string query; 10 bool flag; 11 int i; 12 bool cmp(data a,data b) 13 { 14 char ch=query[6]; 15 if(ch=='S'&&a.size!=b.size)return a.size<b.size; 16 if(ch=='T'&&a.dtime!=b.dtime)return a.dtime<b.dtime; 17 return a.name<b.name; 18 } 19 int main() 20 { 21 while(cin>>n,n){ 22 for(i=0;i<n;++i){ 23 cin>>a[i].date>>a[i].time>>a[i].size; 24 a[i].dtime=a[i].date+' '+a[i].time; 25 getchar(); 26 getline(cin,a[i].name); 27 } 28 getline(cin,query); 29 sort(a,a+n,cmp); 30 if(flag)cout<<endl; 31 else flag=1; 32 for(i=0;i<n;++i){ 33 cout<<a[i].dtime; 34 printf("%17d ",a[i].size); 35 cout<<a[i].name<<endl; 36 } 37 } 38 return 0; 39 }
E2893 数据密度
First AC: 2018-02-19 Latest Modification: 2018-03-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,len,cnt,up,dn; 4 string s; 5 char c; 6 int i,j; 7 int gcd(int a,int b) 8 { 9 int c=1; 10 while(c)c=a%b,a=b,b=c; 11 return a; 12 } 13 int main() 14 { 15 cin>>n; 16 getchar(); 17 while(n--){ 18 getline(cin,s); 19 len=s.length(); 20 cnt=0; 21 for(i=0;i<len;++i){ 22 c=s[i]; 23 for(j=0;j<8;++j){ 24 if(c&1)++cnt; 25 c>>=1; 26 } 27 } 28 up=cnt,dn=8*len; 29 cnt=gcd(up,dn); 30 up/=cnt,dn/=cnt; 31 cout<<up<<'/'<<dn<<endl; 32 } 33 return 0; 34 }
E2894 波浪图
First AC: 2017-11-14 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s; 4 bool a[160][80]; 5 int b[160]; 6 int len,r,M,m; 7 int i,j; 8 int main() 9 { 10 while(cin>>s){ 11 memset(a,0,sizeof(a)),memset(b,0,sizeof(b)); 12 len=s.length(); 13 M=m=r=80,a[80][0]=1; 14 for(i=1;i<len;++i){ 15 if(s[i]>s[i-1])a[--r][i]=1; 16 else if(s[i]<s[i-1])a[++r][i]=1; 17 else a[r][i]=1; 18 if(r>M)M=r; 19 if(r<m)m=r; 20 b[r]=i; 21 } 22 for(i=m;i<=M;++i){ 23 for(j=0;j<=b[i];++j){ 24 if(a[i][j]==1)cout<<s[j]; 25 else cout<<' '; 26 } 27 cout<<endl; 28 } 29 } 30 return 0; 31 }
E2895 循环小数
First AC: 2018-04-17 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 long up,dn,tmp; 4 long quo[100001],rem[100001],cnt; 5 long i,j; 6 int main() 7 { 8 while(scanf("%ld/%ld",&up,&dn)!=EOF){ 9 tmp=__gcd(up,dn); 10 up/=tmp,dn/=tmp; 11 cnt=0; 12 bool jdg=1; 13 while(jdg){ 14 *(quo+cnt)=up/dn; 15 *(rem+cnt)=up%dn; 16 if(!*(rem+cnt)){ 17 printf("0\n"); 18 break; 19 } 20 for(i=1;i<cnt;++i){ 21 if(*(rem+i)==*(rem+cnt)&&*(quo+i)==*(quo+cnt)){ 22 for(j=i;j<cnt;++j)printf("%ld",*(quo+j)); 23 cout<<endl; 24 jdg=0; 25 break; 26 } 27 } 28 up=rem[cnt++]*10; 29 } 30 } 31 return 0; 32 }
E2896 随机排序
First AC: 2017-11-24 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s; 4 int len,cnt; 5 int a[30]; 6 char c; 7 int i,j; 8 struct data{ 9 string str,str0; 10 int len; 11 }b[105]; 12 bool cmp(data m,data n) 13 { 14 int lm=m.len,ln=n.len; 15 int tmp=m.len>n.len? n.len:m.len; 16 for(i=0;i<tmp;++i){ 17 if(a[m.str0[i]-'A']!=a[n.str0[i]-'A']) 18 return a[m.str0[i]-'A']<a[n.str0[i]-'A']; 19 } 20 return m.len<n.len; 21 } 22 int main() 23 { 24 while(cin>>s){ 25 len=s.length(); 26 memset(a,0,sizeof(a)); 27 for(i=0;i<len;++i)a[s[i]-'A']=i; 28 for(i=cnt=0;;++i){ 29 cin>>b[i].str; 30 b[i].str0=b[i].str; 31 b[i].len=b[i].str.length(); 32 for(j=0;j<b[i].len;++j)if(b[i].str[j]>'Z')b[i].str0[j]-=32; 33 ++cnt; 34 c=getchar(); 35 if(c=='\n')break; 36 } 37 sort(b,b+cnt,cmp); 38 cout<<b[0].str; 39 for(i=1;i<cnt;++i)cout<<' '<<b[i].str; 40 cout<<endl; 41 } 42 return 0; 43 }
E2897 英文缩写词
First AC: 2017-11-30 Latest Modification: 2018-03-15
1 #include<bits/stdc++.h> 2 using namespace std; 3 string a; 4 int len; 5 int i,j; 6 int main() 7 { 8 while(getline(cin,a)){ 9 len=a.length(); 10 for(i=0;i<len;++i){ 11 if(a[i]=='-')a[i]=32; 12 if(a[i]>96&&a[i]<123)a[i]-=32; 13 } 14 if(a[0]=='A'&&a[1]==32) 15 a[0]=32; 16 else if(a[0]=='A'&&a[1]=='N'&&a[2]==32) 17 a[0]=a[1]=32; 18 else if(a[0]=='O'&&a[1]=='F'&&a[2]==32) 19 a[0]=a[1]=32; 20 else if(a[0]=='T'&&a[1]=='H'&&a[2]=='E'&&a[3]==32) 21 a[0]=a[1]=a[2]=32; 22 else if(a[0]=='F'&&a[1]=='O'&&a[2]=='R'&&a[3]==32) 23 a[0]=a[1]=a[2]=32; 24 else if(a[0]=='A'&&a[1]=='N'&&a[2]=='D'&&a[3]==32) 25 a[0]=a[1]=a[2]=32; 26 for(i=2;i<len;++i) 27 if(a[i-2]==32&&a[i-1]=='A'&&a[i]==32) 28 a[i-1]=32; 29 for(i=3;i<len;++i) 30 if(a[i-3]==32&&a[i-2]=='A'&&a[i-1]=='N'&&a[i]==32) 31 a[i-2]=a[i-1]=32; 32 for(i=3;i<len;++i) 33 if(a[i-3]==32&&a[i-2]=='O'&&a[i-1]=='F'&&a[i]==32) 34 a[i-2]=a[i-1]=32; 35 for(i=4;i<len;++i) 36 if(a[i-4]==32&&a[i-3]=='T'&&a[i-2]=='H' 37 &&a[i-1]=='E'&&a[i]==32)a[i-3]=a[i-2]=a[i-1]=32; 38 for(i=4;i<len;++i) 39 if(a[i-4]==32&&a[i-3]=='F'&&a[i-2]=='O' 40 &&a[i-1]=='R'&&a[i]==32)a[i-3]=a[i-2]=a[i-1]=32; 41 for(i=4;i<len;++i) 42 if(a[i-4]==32&&a[i-3]=='A'&&a[i-2]=='N' 43 &&a[i-1]=='D'&&a[i]==32)a[i-3]=a[i-2]=a[i-1]=32; 44 if(a[0]!=32)cout<<a[0]; 45 for(i=1;i<len;++i) 46 if(a[i-1]==32&&a[i]!=32) 47 cout<<a[i];cout<<endl; 48 } 49 return 0; 50 }
E2902 Contest
First AC: 2018-05-23 Latest Modification: 2018-05-23
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 bool mp[10001][101]; 5 int p[12]; 6 int i,j; 7 int main() 8 { 9 for(i=1;i<10001;++i) 10 for(j=0;j<=i;++j) 11 mp[i][(int)(j*100.0/i+0.5)]=1; 12 cin>>n; 13 for(i=0;i<n;++i)cin>>p[i]; 14 for(i=1;;++i){ 15 bool jdg=1; 16 for(j=0;j<n;++j)if(!mp[i][p[j]]){ 17 jdg=0; 18 break; 19 } 20 if(jdg){ 21 cout<<i; 22 break; 23 } 24 } 25 return 0; 26 }
E2905 Finance
First AC: 2018-06-06 Latest Modification: 2018-06-06
Note: 利用贪心的思想可以证明最优解一定让每一段尽可能靠前(更可能因为前一年的休假而减少开支)
那么关键就在于如何分段与分几段,枚举所有可能计算取最小即可
1 #include<bits/stdc++.h> 2 using namespace std; 3 int w,tmp,cnt; 4 int a[53]; 5 string rst; 6 int i; 7 int main() 8 { 9 for(i=0;i<4;++i)cin>>w,a[w]=1; 10 for(i=1;i<53;++i)a[i]+=a[i-1]; 11 cnt=48*4,rst="1 2 3 4"; 12 if((tmp=3*48+1*(48-3+a[4]))<cnt) 13 cnt=tmp,rst="1 2 3 5"; 14 if((tmp=2*48+2*(48-2+a[3]))<cnt) 15 cnt=tmp,rst="1 2 4 5"; 16 if((tmp=1*48+3*(48-1+a[2]))<cnt) 17 cnt=tmp,rst="1 3 4 5"; 18 if((tmp=2*48+1*(48-2+a[3])+1*(48-3+a[5]))<cnt) 19 cnt=tmp,rst="1 2 4 6"; 20 if((tmp=1*48+2*(48-1+a[2])+1*(48-3+a[5]))<cnt) 21 cnt=tmp,rst="1 3 4 6"; 22 if((tmp=1*48+1*(48-1+a[2])+2*(48-2+a[4]))<cnt) 23 cnt=tmp,rst="1 3 5 6"; 24 if((tmp=1*48+1*(47+a[2])+1*(46+a[4])+1*(45+a[6]))<cnt) 25 cnt=tmp,rst="1 3 5 7"; 26 cout<<rst; 27 return 0; 28 }
E2907 Hometask
First AC: 2018-04-17 Latest Modification: 2018-04-17
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll n; 5 bool pri(ll n) 6 { 7 ll m=(ll)sqrt(n)+2; 8 if(!(n&1))return 0; 9 for(ll i=3;i<m;i+=2)if(!(n%i))return 0; 10 return 1; 11 } 12 int main() 13 { 14 ios::sync_with_stdio(false); 15 while(cin>>n){ 16 if(n<2){cout<<"YES\n";continue;} 17 if(n<5){cout<<"NO\n";continue;} 18 pri(n)? cout<<"NO\n":cout<<"YES\n"; 19 } 20 return 0; 21 }
E2909 Joke
First AC: 2018-04-19 Latest Modification: 2018-04-19
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 string s,t; 5 ll lens,lent,cnts,cntt,a,b; 6 ll i; 7 int main() 8 { 9 while(cin>>s>>t){ 10 lens=s.length(); 11 lent=t.length(); 12 cnts=cntt=a=b=0; 13 for(i=0;i<lens;++i){ 14 if(s[i]=='.')cnts=lens-i-1; 15 else a=10*a+s[i]-'0'; 16 } 17 for(i=0;i<lent;++i){ 18 if(t[i]=='.')cntt=lent-i-1; 19 else b=10*b+t[i]-'0'; 20 } 21 if(cnts>cntt)b*=pow(10,cnts-cntt); 22 else a*=pow(10,cntt-cnts); 23 cout<<a/b<<endl; 24 } 25 return 0; 26 }
E2912 放书
First AC: 2018-05-14 Latest Modification: 2018-05-14
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n; 4 bool jdg[9]; 5 int a[11]; 6 void dfs(int x) 7 { 8 int i; 9 if(x==n+1){ 10 for(i=1;i<=n;++i)cout<<a[i]; 11 cout<<endl; 12 return; 13 } 14 for(i=1;i<=n;++i){ 15 if(!jdg[i]&&i!=x){ 16 a[x]=i; 17 jdg[i]=1; 18 dfs(x+1); 19 jdg[i]=0; 20 } 21 } 22 } 23 int main() 24 { 25 cin>>T; 26 while(T--){ 27 cin>>n; 28 if(n<8){ 29 dfs(1); 30 } 31 else{ 32 a[1]=0,a[2]=1; 33 for(int i=3;i<=n;++i)a[i]=(i-1)*(a[i-2]+a[i-1]); 34 cout<<a[n]<<endl; 35 } 36 } 37 return 0; 38 }
E2913 A+B(I)
First AC: 2017-10-21 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a,b; 4 int main() 5 { 6 while(cin>>a>>b)cout<<a+b<<endl; 7 return 0; 8 }
E2914 A+B(II)
First AC: 2017-10-21 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a,b; 4 int main() 5 { 6 cin>>a; 7 while(cin>>a>>b)cout<<a+b<<endl; 8 return 0; 9 }
E2915 A+B(III)
First AC: 2017-10-21 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a,b; 4 int main() 5 { 6 while(cin>>a>>b,a||b)cout<<a+b<<endl; 7 return 0; 8 }
E2918 K-th Number
First AC: 2018-04-13 Latest Modification: 2018-04-13
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,l,r,k; 4 struct data{ 5 int num,item; 6 }a[100001]; 7 int i; 8 bool cmp(data a,data b) 9 { 10 return a.num<b.num; 11 } 12 int main() 13 { 14 ios::sync_with_stdio(false); 15 cin>>n>>m; 16 for(i=1;i<=n;++i)cin>>a[i].num,a[i].item=i; 17 sort(a+1,a+n+1,cmp); 18 while(m--){ 19 cin>>l>>r>>k; 20 --l,++r; 21 if(r-l>2*k){ 22 for(i=1;;++i) 23 if(a[i].item>l&&a[i].item<r&&!(--k))break; 24 cout<<a[i].num<<endl; 25 } 26 else{ 27 k=r-l-k; 28 for(i=n;;--i) 29 if(a[i].item>l&&a[i].item<r&&!(--k))break; 30 cout<<a[i].num<<endl; 31 } 32 } 33 return 0; 34 }
E2920 加密2
First AC: 2017-11-15 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long n; 4 int a[32],i,s,cnt; 5 int main() 6 { 7 while(cin>>n){ 8 s=cnt=0; 9 for(i=31;i>=0;--i)a[i]=n%2,n/=2; 10 for(i=0;i<16;++i){ 11 if(a[i]==0)a[i]=a[i+16]; 12 else {if(a[i+16]==0)a[i]=1; 13 else a[i]=0;} 14 if(a[i+16]==0)a[i+16]=1; 15 else a[i+16]=0; 16 } 17 for(i=16;i<32;i+=4){ 18 s=8*a[i]+4*a[i+1]+2*a[i+2]+a[i+3]; 19 if(cnt==0&&s!=0)++cnt; 20 if(cnt){ 21 if(s<10)cout<<s; 22 else cout<<(char)(s-10+'A'); 23 } 24 } 25 for(i=0;i<16;i+=4){ 26 s=8*a[i]+4*a[i+1]+2*a[i+2]+a[i+3]; 27 if(cnt==0&&s!=0)++cnt; 28 if(cnt){ 29 if(s<10)cout<<s; 30 else cout<<(char)(s-10+'A'); 31 } 32 } 33 cout<<endl; 34 } 35 return 0; 36 }
E2936 查找病毒
First AC: 2017-11-01 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,lens,lent,del,cnt,num; 4 string s,t; 5 int i,j,k; 6 int main() 7 { 8 cin>>T; 9 getchar(); 10 for(i=0;i<T;++i){ 11 getline(cin,s);getline(cin,t); 12 lens=s.length(),lent=t.length(),num=0; 13 if(lens<lent){cout<<"No\n";continue;} 14 if(lens==lent==1){ 15 if(s[0]==t[0]){cout<<"Yes\n";continue;} 16 cout<<"No\n";continue; 17 } 18 del=lens-lent+1; 19 for(j=0;j<del;++j){ 20 if(s[j]==t[0]){ 21 if(lent==1){cout<<"Yes\n";num++;break;} 22 for(k=1;k<lent;++k){ 23 cnt=0; 24 if(s[j+k]!=t[k]){cnt++;break;} 25 } 26 if(cnt==0){cout<<"Yes\n";num++;break;} 27 } 28 } 29 if(num==0)cout<<"No\n"; 30 } 31 return 0; 32 }
E2937 ZZZ vs MJQ 1
First AC: 2018-06-04 Latest Modification: 2018-06-04
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,a,b,c,d,e,f,g,h; 4 int main() 5 { 6 cin>>T; 7 while(T--){ 8 cin>>a>>b>>c>>d>>e>>f>>g>>h; 9 if(abs(a+c-e-g)<c-a+g-e&&abs(b+d-f-h)<b-d+f-h)cout<<"Yes\n"; 10 else cout<<"No\n"; 11 } 12 return 0; 13 }
E2938 ZZZ vs MJQ 2
First AC: 2018-01-03 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,i; 4 double p,rst; 5 int c(int n,int m) 6 { 7 int rst=1; 8 for(int i=1;i<=m;++i)rst*=n+1-i,rst/=i; 9 return rst; 10 } 11 int main() 12 { 13 cin>>T; 14 while(T--){ 15 cin>>n>>p; 16 rst=0; 17 for(i=n/2+1;i<=n;++i)rst+=c(n,i)*pow(p,i)*pow(1-p,n-i); 18 printf("%.3f\n",rst); 19 } 20 return 0; 21 }
E2939 BG攒RP
First AC: 2018-01-19 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,ls; 4 int R,P,B,G; 5 string s; 6 int i,j; 7 struct data{ 8 string t; 9 int lt; 10 long long r,p,b,g,RP; 11 }a[300]; 12 long long rp(long long r,long long p,long long b,long long g) 13 { 14 return (r*r%1211*r*r%1211+p*p%1211*p%1211+b*b%1211+g)%1211 15 *(r+p*p%1211+b*b%1211*b%1211+g*g%1211*g*g%1211)%1211; 16 } 17 bool cmp(data a,data b) 18 { 19 if(a.RP!=b.RP)return a.RP>b.RP; 20 int lt=a.lt<b.lt? a.lt:b.lt; 21 for(int i=0;i<lt;++i)if(a.t[i]!=b.t[i])return a.t[i]<b.t[i]; 22 return a.lt<b.lt; 23 } 24 int main() 25 { 26 cin>>T; 27 while(T--){ 28 cin>>s>>n; 29 ls=s.length(); 30 R=P=B=G=0; 31 for(i=0;i<ls;++i){ 32 if(s[i]=='R')++R; 33 else if(s[i]=='P')++P; 34 else if(s[i]=='B')++B; 35 else if(s[i]=='G')++G; 36 } 37 for(i=0;i<n;++i){ 38 cin>>a[i].t; 39 a[i].lt=a[i].t.length(); 40 a[i].r=R,a[i].p=P,a[i].b=B,a[i].g=G; 41 for(j=0;j<a[i].lt;++j){ 42 if(a[i].t[j]=='R')++a[i].r; 43 else if(a[i].t[j]=='P')++a[i].p; 44 else if(a[i].t[j]=='B')++a[i].b; 45 else if(a[i].t[j]=='G')++a[i].g; 46 } 47 a[i].RP=rp(a[i].r,a[i].p,a[i].b,a[i].g); 48 } 49 sort(a,a+n,cmp); 50 cout<<a[0].t<<endl; 51 } 52 return 0; 53 }
E2940 凤凰古城
First AC: 2018-01-06 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,k,res,tmp,cnt; 4 int a[1000]; 5 int i,j; 6 int main() 7 { 8 cin>>T; 9 while(T--){ 10 cin>>n>>k; 11 for(i=0;i<n;++i)cin>>a[i]; 12 sort(a,a+n); 13 cnt=0,res=n; 14 while(res){ 15 tmp=k; 16 for(i=n-cnt-1;;--i){ 17 if(a[i]<=tmp)break; 18 } 19 ++cnt,--res,tmp-=a[i],a[i]=k+1; 20 for(j=i-1;j>=0;--j){ 21 if(a[j]<=tmp){ 22 --res,a[j]=k+1; 23 break; 24 } 25 } 26 } 27 cout<<cnt<<endl; 28 } 29 return 0; 30 }
E2941 在线投票系统
First AC: 2018-01-07 Latest Modification: 2018-01-07
Note: 观察得到每个g[i][j]是其余所有奇数个g[i][j]亦或的结果
因此所有g[i][j]亦或的结果即为a
1 #include<iostream> 2 using namespace std; 3 int T,n,a,tmp,rst; 4 int g[10][2]; 5 int i,j,k; 6 int main() 7 { 8 cin>>T; 9 while(T--){ 10 cin>>n; 11 for(a=i=0;i<n;++i)cin>>g[i][0]>>g[i][1],a^=g[i][0],a^=g[i][1]; 12 for(i=0;i<n;++i)for(j=0;j<2;++j){ 13 tmp=1,rst=0; 14 for(k=0;k<16;++k){ 15 if(tmp&g[i][j]){ 16 if(!(tmp&a))rst|=tmp; 17 } 18 else rst|=tmp&a; 19 tmp<<=1; 20 } 21 cout<<rst; 22 j? cout<<endl:cout<<' '; 23 } 24 } 25 return 0; 26 }
E2944 四元一次方程
First AC: 2017-11-02 Latest Modification: 2017-11-02
1 #include<iostream> 2 using namespace std; 3 int T,n,maxp,p,q,sum; 4 int i,j; 5 int main() 6 { 7 cin>>T; 8 for(i=0;i<T;++i){ 9 sum=0; 10 cin>>n; 11 maxp=n/2+1; 12 for(j=0;j<maxp;++j){ 13 q=n-2*j; 14 sum+=(j/2+1)*(q/3+1); 15 } 16 cout<<sum<<endl; 17 } 18 return 0; 19 }
E2945 Search Paper Pages
First AC: 2017-12-30 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T; 4 struct data{ 5 string s; 6 int rst,num; 7 }a[21]; 8 int i; 9 bool cmp(data a,data b) 10 { 11 if(a.rst^b.rst)return a.rst>b.rst; 12 return a.num<b.num; 13 } 14 int main() 15 { 16 cin>>T; 17 for(i=0;i<T;++i)cin>>a[i].s>>a[i].rst,a[i].num=i; 18 sort(a,a+T,cmp); 19 cout<<a[0].s<<endl; 20 for(i=1;i<T;++i){ 21 if(a[i].rst!=a[i-1].rst)break; 22 cout<<a[i].s<<endl; 23 } 24 return 0; 25 }
E2946 整数的质因子分解
First AC: 2017-12-02 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,a[20001],n,cnt,i,j; 4 int main() 5 { 6 for(i=2;i<20001;++i) 7 if(a[i]==0) 8 for(j=2*i;j<20001;j+=i) 9 ++a[j]; 10 cin>>T; 11 while(T--){ 12 cin>>n; 13 for(i=2;n!=1;++i){ 14 if(a[i]==0){ 15 cnt=0; 16 while(n%i==0)++cnt,n/=i; 17 if(cnt)cout<<'('<<i<<','<<cnt<<')'; 18 } 19 } 20 cout<<endl; 21 } 22 return 0; 23 }
E2947 行数据的排序
First AC: 2017-12-29 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,cnt; 4 struct data{ 5 int num; 6 long long f[51]; 7 }a[1001]; 8 int i,j,k; 9 bool cmp(data a,data b) 10 { 11 int len=a.num<b.num? a.num:b.num; 12 for(int i=0;i<len;++i)if(a.f[i]!=b.f[i])return a.f[i]>b.f[i]; 13 return a.num>b.num; 14 } 15 int main() 16 { 17 cin>>T; 18 for(i=0;i<T;++i){ 19 cin>>n; 20 for(j=0;j<n;++j){ 21 cnt=0; 22 while(1){ 23 cin>>a[j].f[cnt]; 24 if(a[j].f[cnt]==-1)break; 25 ++cnt; 26 } 27 a[j].num=cnt; 28 } 29 sort(a,a+n,cmp); 30 for(j=0;j<n;++j){ 31 cout<<a[j].f[0]; 32 for(k=1;k<a[j].num;++k)cout<<' '<<a[j].f[k]; 33 cout<<endl; 34 } 35 } 36 return 0; 37 }
E2948 Phone Number
First AC: 2017-12-20 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,cnt,len; 4 long long base; 5 string s; 6 int i,j; 7 struct data{ 8 long long n; 9 }a[101]; 10 bool cmp(data a,data b) 11 { 12 return a.n<b.n; 13 } 14 long long change(char c) 15 { 16 switch(c){ 17 case 'A':case 'B':case 'C':return 2; 18 case 'D':case 'E':case 'F':return 3; 19 case 'G':case 'H':case 'I':return 4; 20 case 'J':case 'K':case 'L':return 5; 21 case 'M':case 'N':case 'O':return 6; 22 case 'P':case 'Q':case 'R':case 'S':return 7; 23 case 'T':case 'U':case 'V':return 8; 24 case 'W':case 'X':case 'Y':case 'Z':return 9; 25 default:return c-'0'; 26 } 27 } 28 void print(data a,int cnt) 29 { 30 cout<<a.n/10000<<'-'<<a.n%10000/1000<<a.n%1000/100 31 <<a.n%100/10<<a.n%10<<' '<<cnt<<endl; 32 } 33 int main() 34 { 35 cin>>T; 36 for(i=0;i<T;++i){ 37 cin>>s; 38 len=s.length(); 39 for(j=len-1;j>=0;--j){ 40 if(s[j]!='-'){ 41 if(!base)a[i].n=change(s[j]),base=10; 42 else a[i].n+=base*change(s[j]),base*=10; 43 } 44 } 45 base=0; 46 } 47 sort(a,a+T,cmp); 48 for(cnt=i=1;i<T;++i){ 49 if(a[i].n^a[i-1].n)print(a[i-1],cnt),cnt=1; 50 else ++cnt; 51 } 52 print(a[T-1],cnt); 53 return 0; 54 }
E2949 求x的y次方
First AC: 2017-10-16 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 double x,y; 4 int main() 5 { 6 cin>>x>>y; 7 printf("%.3f\n",pow(x,y)); 8 return 0; 9 }
E2950 求整数部分
First AC: 2017-10-16 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 char c; 4 int main() 5 { 6 while((c=getchar())!='.')cout<<c; 7 cout<<endl; 8 return 0; 9 }
E2951 循环移位
First AC: 2017-11-17 Latest Modification: 2017-11-17
1 #include<iostream> 2 using namespace std; 3 long long a,n,s,m=1; 4 int x[32]; 5 int i,j; 6 int main() 7 { 8 cin>>a>>n; 9 for(;i<32;)x[i++]=a%2,a/=2; 10 for(i=n;i<32;s+=m*x[i++],m*=2); 11 for(i=0;i<n;s+=m*x[i++],m*=2); 12 cout<<s<<endl; 13 return 0; 14 }
E2952 日期计算
First AC: 2017-10-16 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int y,m,d; 4 int a[]={0,0,31,59,90,120,151,181,212,243,273,304,334}; 5 int b[]={0,0,31,60,91,121,152,182,213,244,274,305,335}; 6 int main() 7 { 8 cin>>y>>m>>d; 9 if((y%4==0&&y%100!=0)||y%400==0)cout<<b[m]+d; 10 else cout<<a[m]+d; 11 return 0; 12 }
E2953 字母含义
First AC: 2017-10-16 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 char c; 4 int main() 5 { 6 c=getchar(); 7 if(c=='E')cout<<"Excellent\n"; 8 else if(c=='C')cout<<"Cheer\n"; 9 else if(c=='N')cout<<"Nice\n"; 10 else if(c=='U')cout<<"Ultimate\n"; 11 else if(c=='A')cout<<"Accept\n"; 12 else cout<<"Error\n"; 13 return 0; 14 }
E2954 点名
First AC: 2017-10-16 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,m,n,i,j,cnt; 4 char c; 5 int main() 6 { 7 cin>>T; 8 for(i=0;i<T;i++){ 9 cin>>m>>n; 10 for(j=0;j<m*n;j++){ 11 cin>>c; 12 if(c=='.')cnt++; 13 } 14 cout<<"case #"<<i<<":\n"<<cnt<<endl; 15 cnt=0; 16 } 17 return 0; 18 }
E2955 Familial Numbers
First AC: 2017-10-16 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,m,n,i,j,mini,cnt; 4 int main() 5 { 6 cin>>T; 7 for(i=0;i<T;i++){ 8 cin>>m>>n; 9 if(m>n)mini=n;else mini=m; 10 for(j=2;j<=mini;j++){ 11 if(m%j==0&&n%j==0){ 12 cnt=j; 13 break; 14 } 15 } 16 if(cnt>0)cout<<"case #"<<i<<":\nYes "<<j<<endl; 17 else cout<<"case #"<<i<<":\nNo"<<endl; 18 cnt=0; 19 } 20 return 0; 21 }
E2956 Maya历日期的排序
First AC: 2017-11-26 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,N,len; 4 string d,m; 5 string mon[20]={"pop","no","zip","zotz","tzec","xul","yoxkin", 6 "mol","chen","yax","zac","ceh","mac","kankin",\ 7 "muan","pax","koyab","cumhu","uayet"}; 8 int i,j; 9 struct data{ 10 int d,m,y; 11 }a[10001]; 12 bool cmp(data a,data b) 13 { 14 if(a.y!=b.y)return a.y<b.y; 15 else if(a.m!=b.m)return a.m<b.m; 16 else return a.d<b.d; 17 } 18 int main() 19 { 20 cin>>T; 21 for(j=0;j<T;++j){ 22 cin>>N; 23 for(i=0;i<N;++i){ 24 cin>>d>>m>>a[i].y; 25 len=d.length(); 26 if(len==2)a[i].d=d[0]-'0'; 27 else a[i].d=10*d[0]+d[1]-11*'0'; 28 if(m=="pop")a[i].m=0; 29 else if(m=="no")a[i].m=1; 30 else if(m=="zip")a[i].m=2; 31 else if(m=="zotz")a[i].m=3; 32 else if(m=="tzec")a[i].m=4; 33 else if(m=="xul")a[i].m=5; 34 else if(m=="yoxkin")a[i].m=6; 35 else if(m=="mol")a[i].m=7; 36 else if(m=="chen")a[i].m=8; 37 else if(m=="yax")a[i].m=9; 38 else if(m=="zac")a[i].m=10; 39 else if(m=="ceh")a[i].m=11; 40 else if(m=="mac")a[i].m=12; 41 else if(m=="kankin")a[i].m=13; 42 else if(m=="muan")a[i].m=14; 43 else if(m=="pax")a[i].m=15; 44 else if(m=="koyab")a[i].m=16; 45 else if(m=="cumhu")a[i].m=17; 46 else a[i].m=18; 47 } 48 sort(a,a+N,cmp); 49 cout<<"case #"<<j<<":\n"; 50 for(i=0;i<N;++i) 51 cout<<a[i].d<<". "<<mon[a[i].m] 52 <<' '<<a[i].y<<endl; 53 } 54 return 0; 55 }
E2957 统计不同的最简真分数的个数
First AC: 2017-11-30 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,cnt,u,v,w; 4 int a[1001]; 5 long long num,i,j,k; 6 struct data{int q,p;}b[1000000]; 7 bool cmp(data a,data b) 8 { 9 if(a.p==b.p)return a.q<b.q; 10 return a.p<b.p; 11 } 12 int main() 13 { 14 cin>>T; 15 for(i=0;i<T;++i){ 16 cin>>n; 17 for(j=cnt=num=0;j<n;++j)cin>>a[j]; 18 sort(a,a+n); 19 for(j=0;j<n;++j)for(k=j+1;k<n;++k){ 20 if(a[j]==a[k])continue; 21 u=a[k],v=a[j]; 22 do{w=u%v,u=v,v=w;}while(w); 23 if(u!=1)continue; 24 b[num].q=a[j],b[num++].p=a[k]; 25 } 26 sort(b,b+num,cmp); 27 for(j=1;j<num;++j)if(b[j].p==b[j-1].p&&b[j].q==b[j-1].q)++cnt; 28 cout<<"case #"<<i<<":\n"<<num-cnt<<endl; 29 } 30 return 0; 31 }
E2958 求上升子序列和的最大值
First AC: 2017-12-13 Latest Modification: 2018-02-12
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,rst; 4 int a[5000],dp[5000]; 5 int i,j,k; 6 int main() 7 { 8 cin>>T; 9 for(i=0;i<T;++i){ 10 cin>>n; 11 for(j=0;j<n;++j)cin>>a[j]; 12 memset(dp,0,sizeof(dp)),rst=0; 13 for(j=0;j<n;++j){ 14 for(k=0;k<j;++k){ 15 if(a[k]<a[j]&&dp[k]+a[j]>dp[j])dp[j]=dp[k]+a[j]; 16 } 17 if(dp[j]<a[j])dp[j]=a[j]; 18 if(dp[j]>rst)rst=dp[j]; 19 } 20 cout<<"case #"<<i<<":\n"<<rst<<endl; 21 } 22 return 0; 23 }
E2959 正则表达式简化版
First AC: 2018-06-06 Latest Modification: 2018-06-06
1 from re import match 2 flag = 1 3 pat = '' 4 while True: 5 try: 6 s = input() 7 if s =='0': 8 flag=1 9 elif flag == 1: 10 pat = s 11 flag = 0 12 else: 13 try: 14 m = match(pat,s) 15 if m: 16 print('Regular Expression is Fun!') 17 else : 18 print('Boring String Matching...') 19 except: 20 print('Boring String Matching...') 21 except EOFError: 22 break
E2965 纪念品分组
First AC: 2018-05-15 Latest Modification: 2018-05-15
1 #include<bits/stdc++.h> 2 using namespace std; 3 int w,n,p,rst,tmp; 4 int a[201]; 5 int i,j,k; 6 int main() 7 { 8 ios::sync_with_stdio(false); 9 while(cin>>w){ 10 cin>>n; 11 while(n--)cin>>p,++a[p]; 12 rst=0; 13 while(w>1){ 14 rst+=a[w]; 15 a[w--]=0; 16 i=1,j=w; 17 while(i<j){ 18 tmp=min(a[i],a[j]); 19 rst+=tmp; 20 a[i]-=tmp,a[j]-=tmp; 21 ++i,--j; 22 } 23 if(i==j){ 24 rst+=a[i]/2; 25 a[i]%=2; 26 } 27 } 28 rst+=(a[1]+1)/2; 29 a[1]=0; 30 cout<<rst<<endl; 31 } 32 return 0; 33 }
E2966 二进制和十六进制
First AC: 2017-10-30 Latest Modification: 2018-03-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 long T,n,r,cnt,i,j,a[40],m; 4 char c; 5 int main() 6 { 7 cin>>T; 8 for(j=0;j<T;++j){ 9 cin>>n; 10 cout<<"case #"<<j<<":\n"; 11 m=n; 12 r=2; 13 if(n==0)cout<<"0"; 14 else{ 15 for(i=35;i>0;--i)a[i]=n%r,n/=r; 16 for(i=1;;++i)if(a[i]!=0){cnt=i;break;} 17 for(i=cnt;i<36;++i) 18 if(a[i]<10)cout<<a[i]; 19 else cout<<(char)(a[i]-10+'A'); 20 } 21 printf(" %X\n",m); 22 } 23 return 0; 24 }
E2967 公共元素
First AC: 2017-12-08 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,cnt,num,flag; 4 int cmp[301]; 5 int i,j,k; 6 struct data{ 7 int len; 8 int h[101],f[101]; 9 }a[3]; 10 int main() 11 { 12 cin>>T; 13 for(i=0;i<T;++i){ 14 cin>>a[0].len>>a[1].len>>a[2].len; 15 for(j=0;j<3;++j)for(k=0;k<a[j].len;++k)cin>>a[j].h[k]; 16 for(j=0;j<3;++j)sort(a[j].h,a[j].h+a[j].len); 17 for(j=0;j<3;++j){ 18 a[j].f[0]=a[j].h[0]; 19 for(k=cnt=1;k<a[j].len;++k) 20 if(a[j].h[k]!=a[j].h[k-1]) 21 a[j].f[cnt++]=a[j].h[k]; 22 a[j].len=cnt; 23 } 24 for(flag=num=cnt=j=0;j<a[0].len;++j)cmp[cnt++]=a[0].f[j]; 25 for(j=0;j<a[1].len;++j)cmp[cnt++]=a[1].f[j]; 26 for(j=0;j<a[2].len;++j)cmp[cnt++]=a[2].f[j]; 27 sort(cmp,cmp+cnt); 28 for(j=2;j<cnt;++j)if(cmp[j]==cmp[j-1]&&cmp[j]==cmp[j-2]){ 29 if(num)cout<<' '<<cmp[j]; 30 else cout<<"case #"<<i<<":\n"<<cmp[j],flag=num=1; 31 } 32 if(!flag)cout<<"case #"<<i<<":\n"<<-1; 33 cout<<endl; 34 } 35 return 0; 36 }
E2968 付钱
First AC: 2017-10-16 Latest Modification: 2018-03-22
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,i; 4 int main() 5 { 6 cin>>n; 7 while(cin>>n){ 8 cout<<"case #"<<i<<":\n"<<n/100<<" "<<n%100/50<<" "<<n%50/20 9 <<" "<<n%50%20/10<<" "<<n%10/5<<" "<<n%5<<endl; 10 ++i; 11 } 12 return 0; 13 }
E2969 分析字符
First AC: 2017-11-06 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,i,j,A,a,s,n,o,len; 4 string c; 5 int main() 6 { 7 cin>>T; 8 getchar(); 9 for(i=0;i<T;i++){ 10 getline(cin,c); 11 len=c.length(); 12 for(j=0;j<len;++j){ 13 if(c[j]>='A'&&c[j]<='Z'){A++;continue;} 14 if(c[j]>='a'&&c[j]<='z'){a++;continue;} 15 if(c[j]==' '){s++;continue;} 16 if(c[j]>='0'&&c[j]<='9'){n++;continue;} 17 o++; 18 } 19 cout<<"case #"<<i<<":\n"<<A<<" "<<a 20 <<" "<<s<<" "<<n<<" "<<o<<endl; 21 A=a=s=n=o=0; 22 } 23 return 0; 24 }
E2970 农场
First AC: 2017-10-17 Latest Modification: 2018-03-22
1 #include<bits/stdc++.h> 2 using namespace std; 3 long i,m,n; 4 int main() 5 { 6 cin>>n; 7 while(cin>>n>>m){ 8 if(m>=2*n&&m<=4*n&&m%2==0) 9 cout<<"case #"<<i<<":\n"<<(4*n-m)/2<<" "<<(m-2*n)/2<<endl; 10 else cout<<"case #"<<i<<":\nImpossible\n"; 11 ++i; 12 } 13 return 0; 14 }
E2971 3的倍数
First AC: 2017-10-17 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,C,W,i; 4 int main() 5 { 6 cin>>T; 7 for(i=0;i<T;i++){ 8 cin>>C>>W; 9 cout<<"case #"<<i<<":\n"<<3*pow(10,C-2)<<endl; 10 } 11 return 0; 12 }
E2972 分数的精确值
First AC: 2017-11-08 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,up,dn; 4 int r[100]; 5 int i,j; 6 int main() 7 { 8 cin>>T; 9 for(i=0;i<T;++i){ 10 cin>>up>>dn; 11 cout<<"case #"<<i<<":\n"<<up/dn<<'.'; 12 memset(r,-1,sizeof(r)); 13 r[up%dn]=0; 14 for(j=1;;++j){ 15 cout<<(up*=10)/dn; 16 if(!(up%dn)){ 17 up%=dn; 18 cout<<endl; 19 break; 20 } 21 if(r[up%dn]<0){ 22 up%=dn; 23 r[up]=j; 24 } 25 else{ 26 cout<<endl<<r[up%dn]+1<<'-'<<j<<endl; 27 break; 28 } 29 } 30 } 31 return 0; 32 }
E2973 卡片游戏
First AC: 2018-02-12 Latest Modification: 2018-02-12
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n; 4 long m,tmp; 5 long a[100]; 6 bool f[300001]; 7 int i,j,k,r; 8 int main() 9 { 10 cin>>T; 11 for(i=0;i<T;++i){ 12 cin>>n>>m; 13 memset(f,0,sizeof(f)); 14 for(j=0;j<n;++j)cin>>a[j]; 15 for(j=0;j<n;++j)for(k=0;k<j;++k){ 16 tmp=a[j]+a[k]; 17 for(r=0;r<k;++r)f[tmp+a[r]]=1; 18 } 19 for(j=m;j>=0;--j)if(f[j])break; 20 cout<<"case #"<<i<<":\n"<<j<<endl; 21 } 22 return 0; 23 }
E2974 统计单词个数
First AC: 2017-12-03 Latest Modification: 2018-03-15
1 #include<bits/stdc++.h> 2 using namespace std; 3 string a; 4 int T,len,num; 5 int i,j,k; 6 int main() 7 { 8 cin>>T;getchar(); 9 for(k=0;k<T;++k){ 10 getline(cin,a); 11 len=a.length(),num=0; 12 for(i=0;i<len;++i)if(a[i]>96&&a[i]<123)a[i]-=32; 13 if(a[0]=='A'&&a[1]==32)a[0]=32; 14 else if(a[0]=='A'&&a[1]=='N'&&a[2]==32) 15 a[0]=a[1]=32; 16 else if(a[0]=='O'&&a[1]=='F'&&a[2]==32) 17 a[0]=a[1]=32; 18 else if(a[0]=='T'&&a[1]=='H'&&a[2]=='E'&&a[3]==32) 19 a[0]=a[1]=a[2]=32; 20 else if(a[0]=='F'&&a[1]=='O'&&a[2]=='R'&&a[3]==32) 21 a[0]=a[1]=a[2]=32; 22 else if(a[0]=='A'&&a[1]=='N'&&a[2]=='D'&&a[3]==32) 23 a[0]=a[1]=a[2]=32; 24 for(i=2;i<len;++i) 25 if(a[i-2]==32&&a[i-1]=='A'&&a[i]==32) 26 a[i-1]=32; 27 for(i=3;i<len;++i) 28 if(a[i-3]==32&&a[i-2]=='A'&&a[i-1]=='N'&&a[i]==32) 29 a[i-2]=a[i-1]=32; 30 for(i=3;i<len;++i) 31 if(a[i-3]==32&&a[i-2]=='O'&&a[i-1]=='F'&&a[i]==32) 32 a[i-2]=a[i-1]=32; 33 for(i=4;i<len;++i) 34 if(a[i-4]==32&&a[i-3]=='T'&&a[i-2]=='H' 35 &&a[i-1]=='E'&&a[i]==32) 36 a[i-3]=a[i-2]=a[i-1]=32; 37 for(i=4;i<len;++i) 38 if(a[i-4]==32&&a[i-3]=='F'&&a[i-2]=='O' 39 &&a[i-1]=='R'&&a[i]==32) 40 a[i-3]=a[i-2]=a[i-1]=32; 41 for(i=4;i<len;++i) 42 if(a[i-4]==32&&a[i-3]=='A'&&a[i-2]=='N' 43 &&a[i-1]=='D'&&a[i]==32) 44 a[i-3]=a[i-2]=a[i-1]=32; 45 if(a[0]!=32)++num; 46 for(i=1;i<len;++i)if(a[i-1]==32&&a[i]!=32)++num; 47 cout<<"case #"<<k<<":\n"<<num<<endl; 48 } 49 return 0; 50 }
E2975 排序
First AC: 2017-10-17 Latest Modification: 2018-03-07
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,N,n,m,cnt; 4 int i,j,k; 5 int a[1100]; 6 int main() 7 { 8 cin>>T; 9 for(i=0;i<T;i++){ 10 cout<<"case #"<<i<<":\n"; 11 cin>>N; 12 for(j=0;j<N;j++){cin>>n;a[n]=1;} 13 for(j=0;j<10;j++){ 14 for(k=0;k<100;k++){ 15 m=j+10*k; 16 if(a[m]==1){ 17 cnt++; 18 if(cnt==1)cout<<m; 19 else cout<<" "<<m; 20 a[m]=0; 21 } 22 } 23 } 24 cnt=0; 25 cout<<"\n"; 26 } 27 return 0; 28 }
E2976 Light
First AC: 2017-10-17 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,p,q,c,d,e,f,g,h,s; 4 int i,j,k; 5 int a[5][5],b[5][5]; 6 int main() 7 { 8 cin>>T; 9 for(i=0;i<T;i++){ 10 cout<<"case #"<<i<<":\n"; 11 cin>>p>>q>>c>>d>>e>>f>>g>>h>>s; 12 a[1][1]=p;a[1][2]=q;a[1][3]=c; 13 a[2][1]=d;a[2][2]=e;a[2][3]=f; 14 a[3][1]=g;a[3][2]=h;a[3][3]=s; 15 for(j=1;j<4;j++)for(k=1;k<4;k++){ 16 if(!((a[j][k]+a[j][k-1]+a[j][k+1]+a[j-1][k]+a[j+1][k])%2)) 17 b[j][k]=1; 18 else b[j][k]=0; 19 } 20 cout<<b[1][1]<<" "<<b[1][2]<<" "<<b[1][3]<<endl 21 <<b[2][1]<<" "<<b[2][2]<<" "<<b[2][3]<<endl 22 <<b[3][1]<<" "<<b[3][2]<<" "<<b[3][3]<<endl; 23 } 24 return 0; 25 }
E2977 成绩排序
First AC: 2017-11-20 Latest Modification: 2018-03-07
1 #include<bits/stdc++.h> 2 using namespace std; 3 struct data{ 4 long long num,score; 5 }stu[501]; 6 long long T,N,M,G,S,s,cnt,tmp; 7 long long a[11]; 8 int i,j,k; 9 bool cmp(data a,data b) 10 { 11 if(a.score==b.score)return a.num<b.num; 12 return a.score>b.score; 13 } 14 int main() 15 { 16 cin>>T; 17 for(i=0;i<T;++i){ 18 cnt=0; 19 cin>>N>>M>>G; 20 for(j=1;j<=M;++j)cin>>a[j]; 21 for(j=0;j<N;++j){ 22 cin>>stu[j].num>>S; 23 stu[j].score=0; 24 while(S--)cin>>s,stu[j].score+=a[s]; 25 if(stu[j].score>=G)++cnt; 26 } 27 cout<<"case #"<<i<<":\n"<<cnt<<endl; 28 sort(stu,stu+N,cmp); 29 for(j=0;j<cnt;++j){ 30 if(stu[j].num==0)cout<<"0000000000"; 31 else for(tmp=10000000000;;tmp/=10) 32 if(stu[j].num/tmp==0)cout<<'0'; 33 else break; 34 cout<<stu[j].num<<' '<<stu[j].score<<endl; 35 } 36 } 37 return 0; 38 }
E2978 计算数字之和
First AC: 2017-11-17 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s; 4 int T,len,a,b,c,d,sum; 5 long cnt,tmp,nums; 6 int i,j,k; 7 int main() 8 { 9 cin>>T; 10 for(i=0;i<T;++i){ 11 cin>>s; 12 len=s.length(),sum=0; 13 for(j=0;j<len;++j)sum+=s[j]-'0'; 14 a=s[len-3]-'0',b=s[len-2]-'0',c=s[len-1]-'0'; 15 tmp=1,nums=0; 16 for(j=len-1;j>=0;--j)nums+=tmp*(s[j]-'0'),tmp*=10; 17 nums-=len; 18 for(j=0;j<nums;++j){ 19 d=(a+b)%10*c%10; 20 if(d==0)break; 21 sum+=d,a=b,b=c,c=d; 22 } 23 cout<<"case #"<<i<<":\n"<<sum<<endl; 24 } 25 return 0; 26 }
E2979 图形
First AC: 2017-10-17 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a[90][90]; 4 int i,j,k; 5 int T,n; 6 int main() 7 { 8 for(i=1;i<80;i+=2)for(j=1;j<=i;j++)a[i][j]=(i-1)*(i-1)+j; 9 for(i=2;i<81;i+=2)for(j=1;j<=i;j++)a[i][j]=i*i-j+1; 10 for(j=80;j>0;j-=2)for(i=j-1;i>0;i--)a[i][j]=a[i+1][j]-1; 11 for(j=79;j>0;j-=2)for(i=j-1;i>0;i--)a[i][j]=a[i+1][j]+1; 12 cin>>T; 13 for(k=0;k<T;k++){ 14 cin>>n; 15 cout<<"case #"<<k<<":\n"; 16 for(i=1;i<=n;i++){ 17 cout<<a[i][1]; 18 for(j=2;j<=n;j++)cout<<" "<<a[i][j]; 19 cout<<endl; 20 } 21 } 22 return 0; 23 }
E2980 小数转化分数
First AC: 2017-12-10 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len,p,l,r; 4 long long tmp,base,up,down; 5 string s; 6 int i,j,k; 7 void print(long long up,long long down) 8 { 9 long long m=up,n=down,q=1; 10 while(q)q=m%n,m=n,n=q; 11 cout<<up/m<<'/'<<down/m<<endl; 12 } 13 int main() 14 { 15 cin>>T; 16 getchar(); 17 for(i=0;i<T;++i){ 18 getline(cin,s); 19 len=s.length(); 20 l=r=up=down=0; 21 cout<<"case #"<<i<<":\n"; 22 for(j=1;;++j)if(s[j]=='.'){p=j;break;} 23 for(k=j+1;k<len;++k){ 24 if(s[k]=='['){ 25 l=k,r=len-1; 26 break; 27 } 28 } 29 if(l-r){ 30 down=(long long)pow(10,r-l-1)-1; 31 for(base=1,j=r-1;j>l;--j) 32 up+=base*(s[j]-'0'),base*=10; 33 for(tmp=0,base=1,j=l-1;j>p;--j) 34 tmp+=base*(s[j]-'0'),base*=10; 35 up+=down*tmp; 36 down*=(long long)pow(10,l-p-1); 37 } 38 else{ 39 down=(long long)pow(10,len-p-1); 40 for(base=1,j=len-1;j>p;--j) 41 up+=base*(s[j]-'0'),base*=10; 42 } 43 for(tmp=0,base=1,j=p-1;j>=0;--j) 44 tmp+=base*(s[j]-'0'),base*=10; 45 up+=tmp*down; 46 print(up,down); 47 } 48 return 0; 49 }
E2981 零食
First AC: 2017-10-29 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,p,a[10001],i,j,low=10000; 4 string s; 5 int main() 6 { 7 cin>>T; 8 for(i=0;i<T;++i){ 9 memset(a,0,sizeof(a)); 10 cout<<"case #"<<i<<":\n"; 11 cin>>n; 12 for(j=0;j<n;++j){ 13 cin>>s; 14 if(s=="B"){ 15 cin>>p; 16 a[p]++; 17 if(p<low)low=p; 18 } 19 if(s=="G"){ 20 while(a[low]==0)++low; 21 if(a[low]==1)cout<<low<<endl,a[low++]--; 22 else cout<<low<<endl,a[low]--; 23 } 24 } 25 } 26 return 0; 27 }
E2982 三元斐波那契奇数列
First AC: 2017-10-05 Latest Modification: 2018-04-01
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,temp,cnt; 4 long a[25]; 5 int i,j; 6 int main() 7 { 8 cin>>T; 9 for(i=0;i<T;++i){ 10 cin>>n,cnt=0; 11 cout<<"case #"<<i<<":\n"; 12 for(j=0;j<n;++j){ 13 cin>>a[j]; 14 if(a[j]==-1)temp=j; 15 } 16 if(temp==0)a[0]=a[3]-a[2]-a[1]; 17 else if(temp==1)a[1]=a[3]-a[2]-a[0]; 18 else if(temp==2)a[2]=a[3]-a[1]-a[0]; 19 else a[temp]=a[temp-1]+a[temp-2]+a[temp-3]; 20 for(j=3;j<n;++j)if(a[j]!=a[j-1]+a[j-2]+a[j-3]){--cnt;break;} 21 if(cnt==0&&a[temp]>0)cout<<a[temp]<<endl; 22 else cout<<"-1\n"; 23 } 24 return 0; 25 }
E2983 蛇形图案
First AC: 2017-10-20 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n; 4 int a[11][11],r,q,e; 5 int i,j,k; 6 int main() 7 { 8 ios::sync_with_stdio(false); 9 cin>>T; 10 for(i=0;i<T;i++){ 11 cout<<"case #"<<i<<":\n"; 12 cin>>n; 13 for(j=n;j>1;j-=2){ 14 for(k=0;k<j;++k)a[r][++q]=++e; 15 for(k=1;k<j;++k)a[++r][q]=++e; 16 for(k=1;k<j;++k)a[r][--q]=++e; 17 for(k=2;k<j;++k)a[--r][q]=++e; 18 } 19 if(n%2!=0)a[n/2][n/2+1]=n*n; 20 r=q=e=0; 21 for(j=0;j<n;++j){ 22 cout<<a[j][1]; 23 a[j][1]=0; 24 for(k=2;k<=n;++k)cout<<" "<<a[j][k]; 25 cout<<endl; 26 } 27 } 28 return 0; 29 }
E2984 数学手稿
First AC: 2017-11-22 Latest Modification: 2018-03-22
1 #include<bits/stdc++.h> 2 using namespace std; 3 long T,N,K,m,minm,maxm; 4 int i; 5 int main() 6 { 7 for(cin>>T;i<T;++i){ 8 cin>>N>>K; 9 cout<<"case #"<<i<<":\n"; 10 K>2*N? minm=(K-2*N)/2:minm=0; 11 K>3*N? maxm=K/3+1:maxm=(K-N)/2+1; 12 if(minm+1>maxm){cout<<"-1\n";continue;} 13 for(m=minm;m<maxm;++m){ 14 cout<<m<<' '<<K-N-2*m<<' '<<2*N-K+m<<endl; 15 } 16 } 17 return 0; 18 }
E2985 圆和正方形
First AC: 2017-10-17 Latest Modification: 2018-03-22
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,N,K,i; 4 int main() 5 { 6 cin>>T; 7 for(i=0;i<T;i++){ 8 cout<<"case #"<<i<<":\n"; 9 cin>>N>>K; 10 printf("%.6lf\n",N*N*(1-1.0/pow(2,K))*0.429203673205); 11 } 12 return 0; 13 }
E2986 求整数部分计算2的n次方
First AC: 2017-10-17 Latest Modification: 2018-04-11
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,i; 4 int main() 5 { 6 cin>>T; 7 for(i=0;i<T;i++){ 8 cin>>n; 9 cout<<"case #"<<i<<":\n"<<(long)pow(2,n)<<endl; 10 } 11 return 0; 12 }
E2987 判断IP
First AC: 2017-10-17 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,i,a,b,c,d,cnt; 4 int main() 5 { 6 cin>>T; 7 for(i=0;i<T;i++){ 8 scanf("%d.%d.%d.%d",&a,&b,&c,&d); 9 if(a<0||a>255) 10 cout<<"case #"<<i<<":\nNo 0 "<<a<<endl,cnt++; 11 if(b<0||b>255) 12 cout<<"case #"<<i<<":\nNo 1 "<<b<<endl,cnt++; 13 if(c<0||c>255) 14 cout<<"case #"<<i<<":\nNo 2 "<<c<<endl,cnt++; 15 if(d<0||d>255) 16 cout<<"case #"<<i<<":\nNo 3 "<<d<<endl,cnt++; 17 if(!cnt) 18 cout<<"case #"<<i<<":\nYes\n"; 19 cnt=0; 20 } 21 return 0; 22 }
E2988 密码产生器
First AC: 2017-12-01 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len; 4 long long tmp; 5 string s; 6 int i,j,k; 7 int main() 8 { 9 cin>>T; 10 for(i=0;i<T;++i){ 11 cin>>s; 12 len=s.length(); 13 cout<<"case #"<<i<<":\n"; 14 for(j=0;j<6;++j){ 15 for(k=tmp=0;j+6*k<len;++k)tmp+=s[j+6*k]; 16 cout<<tmp%10; 17 } 18 cout<<endl; 19 } 20 return 0; 21 }
E2989 字符串重排
First AC: 2017-12-20 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len,N,B,A; 4 string s; 5 int i,j; 6 int main() 7 { 8 cin>>T; 9 for(i=0;i<T;++i){ 10 cin>>s; 11 len=s.length(); 12 cout<<"case #"<<i<<":\n"; 13 for(N=B=A=j=0;j<len;++j){ 14 if(s[j]=='N')++N; 15 else if(s[j]=='B')++B; 16 else ++A; 17 } 18 while(N||B||A){ 19 if(N)cout<<'N',--N; 20 if(B)cout<<'B',--B; 21 if(A)cout<<'A',--A; 22 } 23 cout<<endl; 24 } 25 return 0; 26 }
E2990 文献排序
First AC: 2017-12-05 Latest Modification: 2018-03-07
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,tmp; 4 int i,j; 5 struct data{ 6 string s,t; 7 int len; 8 }a[201]; 9 bool cmp(data a,data b) 10 { 11 if(a.len<b.len)tmp=a.len; 12 else tmp=b.len; 13 for(int i=0;i<tmp;++i){ 14 if(a.t[i]>='A'&&a.t[i]<='Z')a.t[i]+=32; 15 if(b.t[i]>='A'&&b.t[i]<='Z')b.t[i]+=32; 16 if(a.t[i]-b.t[i])return a.t[i]<b.t[i]; 17 } 18 return a.len<b.len; 19 } 20 int main() 21 { 22 cin>>T; 23 for(i=0;i<T;++i){ 24 cin>>n,getchar(); 25 for(j=0;j<n;++j){ 26 getline(cin,a[j].s); 27 a[j].t=a[j].s,a[j].len=a[j].s.length(); 28 } 29 sort(a,a+n,cmp); 30 cout<<"case #"<<i<<":\n"; 31 for(j=0;j<n;++j)cout<<a[j].s<<endl; 32 } 33 return 0; 34 }
E2991 鞍点
First AC: 2017-10-20 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,i,j,k,r,m,cnt; 4 int a[5][5],b[5][5]; 5 int main() 6 { 7 ios::sync_with_stdio(false); 8 cin>>T; 9 for(i=0;i<T;++i){ 10 cout<<"case #"<<i<<":\n"; 11 for(j=0;j<5;++j) 12 for(k=0;k<5;++k){ 13 cin>>m; 14 a[j][k]=m; 15 for(r=0;r<k;++r) 16 if(a[j][k]<a[j][r])b[j][k]++; 17 else if(a[j][k]>a[j][r])b[j][r]++; 18 } 19 for(j=0;j<5;++j) 20 for(k=0;k<5;++k) 21 for(r=k+1;r<5;++r) 22 if(a[k][j]<a[r][j])b[r][j]++; 23 else if(a[k][j]>a[r][j])b[k][j]++; 24 for(j=0;j<5;++j) 25 for(k=0;k<5;++k) 26 if(b[j][k]==0){ 27 cout<<j<<" "<<k<<endl; 28 cnt++; 29 } 30 else b[j][k]=0; 31 if(cnt==0)cout<<"-1 -1\n"; 32 cnt=0; 33 } 34 return 0; 35 }
E2992 卡片
First AC: 2017-10-31 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,cnt,num; 4 int a[2000]; 5 int i,j; 6 int main() 7 { 8 cin>>T; 9 for(i=0;i<T;++i){ 10 cout<<"case #"<<i<<":\n"; 11 cin>>n; 12 memset(a,1,sizeof(a)); 13 a[1]=0; 14 for(j=2;;){ 15 if(j==n+1)j=2; 16 if(a[j]==0)j++; 17 else j++,num++; 18 if(num==2)num=0,a[j-1]=0,cnt++; 19 if(cnt==n-2){cnt=0;break;} 20 } 21 for(j=1;j<=n;++j)if(a[j]!=0)cout<<j<<endl; 22 } 23 return 0; 24 }
E2993 替换
First AC: 2017-10-31 Latest Modification: 2018-03-22
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,cnt,num; 4 int a[35]; 5 int i,j,k; 6 int main() 7 { 8 cin>>T; 9 for(i=0;i<T;++i){ 10 cin>>n; 11 cout<<"case #"<<i<<":\n"; 12 for(j=0;j<n;++j)cin>>a[j]; 13 do{ 14 cnt=0; 15 for(j=0;j<n-1;++j){ 16 num=0; 17 for(k=j+1;k<n;++k)if(a[j]>a[k])++num; 18 if(a[j]!=num)++cnt,a[j]=num; 19 } 20 a[n-1]=0; 21 }while(cnt!=0); 22 cout<<a[0]; 23 for(j=1;j<n;++j)cout<<" "<<a[j]; 24 cout<<endl; 25 } 26 return 0; 27 }
E2994 数组排序
First AC: 2017-12-19 Latest Modification: 2018-03-07
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,m; 4 int i,j,k; 5 struct data{ 6 int num; 7 int cnt; 8 }a[501]; 9 bool cmp(data a,data b) 10 { 11 if(a.cnt==b.cnt)return a.num<b.num; 12 return a.cnt>b.cnt; 13 } 14 int main() 15 { 16 for(i=0;i<501;++i)a[i].num=i; 17 cin>>T; 18 for(i=0;i<T;++i){ 19 cin>>n; 20 for(j=0;j<n;++j){ 21 cin>>m; 22 ++a[m].cnt; 23 } 24 sort(a,a+501,cmp); 25 cout<<"case #"<<i<<":\n"; 26 for(j=0;j<501;++j){ 27 for(k=0;k<a[j].cnt;++k){ 28 cout<<a[j].num; 29 --n? cout<<' ':cout<<'\n'; 30 } 31 a[j].num=j,a[j].cnt=0; 32 } 33 } 34 return 0; 35 }
E2995 科学计数法
First AC: 2017-12-21 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,p,ls,cnt,point,flag,tmp; 4 string s; 5 int a[235]; 6 int i,j,k; 7 int main() 8 { 9 cin>>T; 10 for(i=0;i<T;++i){ 11 cin>>s>>p; 12 ls=s.length(); 13 memset(a,0,sizeof(a)); 14 cout<<"case #"<<i<<":\n"; 15 for(point=-1,j=cnt=0;j<ls;++j){ 16 if(s[j]=='.')point=cnt; 17 else a[++cnt]=s[j]-'0'; 18 } 19 if(point<0)point=ls; 20 for(j=1;j<235;++j)if(a[j])break; 21 flag=a[p+j]>4? 1:0; 22 for(k=p+j;k<ls;++k)a[k]=0; 23 tmp=p+j-1; 24 while(flag){ 25 if(9^a[tmp])++a[tmp],--flag; 26 else a[tmp]=0; 27 --tmp; 28 } 29 for(j=0;j<235;++j)if(a[j])break; 30 cout<<a[j]; 31 if(p^1)cout<<'.'; 32 for(k=j+1;k<j+p;++k)cout<<a[k]; 33 if(point-j)cout<<'F'<<point-j; 34 cout<<endl; 35 } 36 return 0; 37 }
E2996 计算a的n次方
First AC: 2017-10-15 Latest Modification: 2019-03-02
Note: 把数分为两段存储
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll T,a,b; 5 ll i; 6 void pow(ll a,ll b) 7 { 8 ll lft=0,rgt=1,mod=1e12; 9 while(b--){ 10 rgt*=a; 11 lft=lft*a+rgt/mod; 12 rgt%=mod; 13 } 14 if(lft){ 15 printf("%lld%013lld\n",lft,rgt); 16 } 17 else{ 18 if(rgt)printf("%lld\n",rgt); 19 else printf("0\n"); 20 } 21 } 22 int main() 23 { 24 cin>>T; 25 for(i=0;i<T;++i){ 26 cin>>a>>b; 27 cout<<"case #"<<i<<":\n"; 28 pow(a,b); 29 } 30 return 0; 31 }
E2997 泰波那契数列的前37项
First AC: 2017-09-28 Latest Modification: 2018-06-04
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long tib[75]={0,1,1}; 4 int T,n; 5 int i; 6 int main() 7 { 8 for(i=3;i<75;++i)tib[i]=tib[i-1]+tib[i-2]+tib[i-3]; 9 cin>>T; 10 for(i=0;i<T;++i){ 11 cin>>n; 12 cout<<"case #"<<i<<":\n"<<tib[n]<<endl; 13 } 14 return 0; 15 }
E2998 质因数
First AC: 2017-10-17 Latest Modification: 2018-06-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,i,j,n; 4 int main() 5 { 6 cin>>T; 7 for(i=0;i<T;i++){ 8 cout<<"case #"<<i<<":\n"; 9 cin>>n; 10 for(j=2;;j++)if(n%j==0){ 11 cout<<n/j<<endl; 12 break; 13 } 14 } 15 return 0; 16 }
E2999 计算多项式的系数
First AC: 2017-12-22 Latest Modification: 2018-04-01
1 #include<iostream> 2 #define p 10007 3 using namespace std; 4 typedef long long ll; 5 ll T,rst,i,j,a,b,k,n,m; 6 ll pow(ll a,ll b) 7 { 8 ll ans=1,base=a%p; 9 while(b){ 10 if(b&1)ans*=base,ans%=p; 11 base*=base,base%=p; 12 b>>=1; 13 } 14 return ans; 15 } 16 ll inv(ll n) 17 { 18 return pow(n,p-2)%p; 19 } 20 ll comb(ll n,ll m) 21 { 22 if(m>n)return 0; 23 ll ans=1; 24 for(ll i=1;i<=m;++i){ 25 ll a=(n+i-m)%p,b=i%p; 26 ans*=a*pow(b,p-2)%p; 27 ans%=p; 28 } 29 return ans; 30 } 31 ll Lucas(ll n,ll m) 32 { 33 if(m)return comb(n%p,m%p)*Lucas(n/p,m/p)%p; 34 return 1; 35 } 36 int main() 37 { 38 cin>>T; 39 for(i=0;i<T;++i){ 40 cin>>a>>b>>k>>n>>m; 41 //Lucas求Ckn 42 rst=Lucas(k,n); 43 //逆元模幂 44 rst*=pow(inv(a),n/(p-2)),rst%=p; 45 rst*=pow(a,n%(p-2)),rst%=p; 46 rst*=pow(inv(b),m/(p-2)),rst%=p; 47 rst*=pow(b,m%(p-2)),rst%=p; 48 cout<<"case #"<<i<<":\n"<<rst<<endl; 49 } 50 return 0; 51 }
E3000 ROT13加密和解密
First AC: 2017-10-30 Latest Modification: 2018-03-15
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,i,j,len; 4 string s; 5 int main() 6 { 7 cin>>T; 8 getchar(); 9 for(i=0;i<T;i++){ 10 cout<<"case #"<<i<<":\n"; 11 getline(cin,s,'\n'); 12 len=s.length(); 13 for(j=0;j<len;++j){ 14 if(s[j]>='A'&&s[j]<'N')cout<<(char)(s[j]+13); 15 else if(s[j]>'M'&&s[j]<='Z')cout<<(char)(s[j]-13); 16 else if(s[j]>='a'&&s[j]<'n')cout<<(char)(s[j]+13); 17 else if(s[j]>'m'&&s[j]<='z')cout<<(char)(s[j]-13); 18 else cout<<s[j]; 19 } 20 cout<<endl; 21 } 22 return 0; 23 }
E3001 计算a的n次方的大整数
First AC: 2017-12-21 Latest Modification: 2018-04-11
1 n=input() 2 i=0 3 while True: 4 try: 5 a,b=input().split() 6 print('case #%d:'%i) 7 print((int)(a)**(int)(b)) 8 i=i+1 9 except: 10 break
E3002 泰波那契数列的前74项
First AC: 2017-09-28 Latest Modification: 2018-06-04
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long tib[75]={0,1,1}; 4 int T,n; 5 int i; 6 int main() 7 { 8 for(i=3;i<75;++i)tib[i]=tib[i-1]+tib[i-2]+tib[i-3]; 9 cin>>T; 10 for(i=0;i<T;++i){ 11 cin>>n; 12 cout<<"case #"<<i<<":\n"<<tib[n]<<endl; 13 } 14 return 0; 15 }
E3003 最小向量点积
First AC: 2017-12-14 Latest Modification: 2018-03-22
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n; 4 long a[1001],b[1001],rst; 5 int i,j; 6 int main() 7 { 8 cin>>T; 9 for(i=0;i<T;++i){ 10 cin>>n; 11 for(j=0;j<n;++j)cin>>a[j]; 12 for(j=0;j<n;++j)cin>>b[j]; 13 sort(a,a+n),sort(b,b+n); 14 for(j=rst=0;j<n;++j)rst+=a[j]*b[n-1-j]; 15 cout<<"case #"<<i<<":\n"<<rst<<endl; 16 } 17 return 0; 18 }
E3004 生理高峰
First AC: 2017-10-22 Latest Modification: 2018-03-22
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,p,e,i,d,rst; 4 int j; 5 int main() 6 { 7 cin>>T; 8 for(j=0;j<T;++j){ 9 cin>>p>>e>>i>>d; 10 for(rst=d+1;;++rst){ 11 if((rst-p)%23==0&&(rst-e)%28==0&&(rst-i)%33==0){ 12 printf("case #%d:\nthe next triple ",j); 13 printf("peak occurs in %d days.\n",rst-d); 14 break; 15 } 16 } 17 } 18 return 0; 19 }
E3005 小型组合数
First AC: 2017-10-24 Latest Modification: 2018-04-11
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long T,m,n,i,j,s=1; 4 int main() 5 { 6 cin>>T; 7 for(i=0;i<T;++i){ 8 cout<<"case #"<<i<<":\n"; 9 cin>>m>>n; 10 for(j=1;j<=n;++j)s*=(m+1-j),s/=j; 11 cout<<s<<endl; 12 s=1; 13 } 14 return 0; 15 }
E3007 A*B II
First AC: 2017-12-17 Latest Modification: 2017-12-17
1 n=(int)(input()) 2 while n>0: 3 a,b=(input().split()) 4 print((int)(a)*(int)(b)) 5 n=n-1
E3008 Coins (I)
First AC: 2018-04-24 Latest Modification: 2018-06-06
Note: 因为每个硬币面额至少为1,所以K次的限制是多余的,那么就转化为完全背包
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const ll M=1e8+7; 5 ll T,n,K,m; 6 ll dp[100001]; 7 ll i,j; 8 int main() 9 { 10 ios::sync_with_stdio(false); 11 cin>>T; 12 for(i=1;i<=T;++i){ 13 cin>>n>>K; 14 memset(dp,0,sizeof(dp)); 15 dp[0]=1; 16 while(n--){ 17 cin>>m; 18 for(j=m;j<=K;++j) 19 (dp[j]+=dp[j-m])%=M; 20 } 21 cout<<"Case "<<i<<": "<<dp[K]<<endl; 22 } 23 return 0; 24 }
E3009 Coins (II)
First AC: 2018-06-06 Latest Modification: 2018-06-06
Note: 多重背包
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const ll M=1e8+7; 5 ll T,n,K,m; 6 ll a[101],dp[10001]; 7 ll i,j,k,l; 8 int main() 9 { 10 ios::sync_with_stdio(false); 11 cin>>T; 12 for(i=1;i<=T;++i){ 13 cin>>n>>K; 14 for(j=0;j<n;++j)cin>>a[j]; 15 memset(dp,0,sizeof(dp)); 16 dp[0]=1; 17 for(j=0;j<n;++j){ 18 cin>>m; 19 for(k=K;k>=0;--k) 20 for(l=1;l<=m;++l) 21 if(k-l*a[j]>=0) 22 (dp[k]+=dp[k-l*a[j]])%=M; 23 } 24 cout<<"Case "<<i<<": "<<dp[K]<<endl; 25 } 26 return 0; 27 }
E3010 Coins (III)
First AC: 2018-06-07 Latest Modification: 2018-06-07
Note: 多重背包的二进制优化
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll T,n,K,m,cnt; 5 ll a[101]; 6 bool dp[100001]; 7 ll i,j,k,l; 8 int main() 9 { 10 ios::sync_with_stdio(false); 11 cin>>T; 12 for(i=1;i<=T;++i){ 13 cin>>n>>K; 14 for(j=0;j<n;++j)cin>>a[j]; 15 memset(dp,0,sizeof(dp)); 16 dp[0]=1; 17 for(j=0;j<n;++j){ 18 cin>>m; 19 ll sum=0,tmp; 20 bool jdg=1; 21 for(k=1;jdg;k*=2){ 22 if(sum+k<=m)sum+=k,tmp=k*a[j]; 23 else jdg=0,tmp=(m-sum)*a[j]; 24 for(l=K-tmp;l>=0;--l) 25 dp[l+tmp]|=dp[l]; 26 } 27 } 28 cnt=0; 29 for(j=1;j<=K;++j)if(dp[j])++cnt; 30 cout<<"Case "<<i<<": "<<cnt<<endl; 31 } 32 return 0; 33 }
E3012 Coins (V)
First AC: 2018-04-24 Latest Modification: 2018-04-24
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long T,n,i; 4 int main() 5 { 6 cin>>T; 7 for(i=1;i<=T;++i){ 8 cin>>n; 9 cout<<"Case "<<i<<": "<<(long long)(log(n)/log(2))+1<<endl; 10 } 11 return 0; 12 }
E3013 Coins (VI)
First AC: 2018-04-24 Latest Modification: 2018-04-24
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long T,n,i; 4 int main() 5 { 6 cin>>T; 7 for(i=1;i<=T;++i){ 8 cin>>n; 9 cout<<"Case "<<i<<": "<<ceil(log(2*n+1)/log(3))<<endl; 10 } 11 return 0; 12 }
E3014 小高斯和小欧几里得(I)
First AC: 2018-06-08 Latest Modification: 2018-06-08
Note: 数据范围很大,很多方法都会超时
注意到(1+60000)×60000/2>2000000000
这表明符合条件的连续正整数个数不超过60000
而每个连续正整数的个数最多只能有一组解满足题设
所以对整数个数遍历即可
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll T,n,rst; 5 ll i,j; 6 int main() 7 { 8 cin>>T; 9 for(i=0;i<T;i++){ 10 cin>>n; 11 rst=0; 12 for(j=1;j*(j+1)<=2*n;++j) 13 if(!((n-j*(j+1)/2)%j)) 14 ++rst; 15 cout<<"case #"<<i<<": "<<rst<<endl; 16 } 17 return 0; 18 }
E3015 小高斯和小欧几里得(II)
First AC: 2018-06-08 Latest Modification: 2018-06-08
Note: 将所有程序按写的时间和调的贡献大小分为总体写bug和总体debug两类
注意到首尾两个程序计算时写bug和debug的时间都要计算在内
所以应该尽可能让第一个程序写的时间和最后一个程序调的时间尽可能少
同时中间的程序尽可能先写bug
因为写的时间里可以调前面程序的bug,贪心的做法是先写bug再调bug
所以可以排序,凡是写bug的都排在调bug前面
在写bug内部,让第一个程序的写bug时间最小
在debug内部,让最后一个程序不得不遗留的debug时间最小
但这个算法我没法证明正确性的一点是
在写bug和debug差异较大的数据中,是否最优解的首末程序仍是首写bug末debug
虽然测试数据中没有这种可能存在的反例
有大佬写的cmp函数是这样的:return min(a.bug,b.debug)<min(a.debug,b.bug);
试了一下可以过,但我也不知道怎么证明正确性
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 int T,n; 5 ll bug,debug,tmp,rst; 6 struct data{ 7 ll bug,debug; 8 }a[100000],b; 9 int i,j; 10 bool cmp(data a,data b) 11 { 12 bool jdga=a.bug<a.debug? 1:0; 13 bool jdgb=b.bug<b.debug? 1:0; 14 if(jdga!=jdgb)return jdga>jdgb; 15 if(jdga){ 16 if(a.bug!=b.bug)return a.bug<b.bug; 17 return a.bug-a.debug<b.bug-b.debug; 18 } 19 else{ 20 if(a.debug!=b.debug)return a.debug>b.debug; 21 return a.bug-a.debug<b.bug-b.debug; 22 } 23 } 24 int main() 25 { 26 cin>>T; 27 for(i=0;i<T;i++){ 28 cin>>n; 29 for(j=0;j<n;++j)cin>>a[j].bug>>a[j].debug; 30 sort(a,a+n,cmp); 31 bug=debug=rst=0; 32 for(j=0;j<n;++j){ 33 rst+=a[j].bug; 34 if(debug<a[j].bug)debug=a[j].debug; 35 else debug=debug-a[j].bug+a[j].debug; 36 } 37 cout<<"case #"<<i<<": "<<rst+debug<<endl; 38 } 39 return 0; 40 }
E3016 Blue Forehead
First AC: 2018-06-08 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,sum; 4 bool a[100]; 5 int i; 6 int main() 7 { 8 cin>>T; 9 while(T--){ 10 cin>>n; 11 sum=n; 12 for(i=0;i<n;++i)cin>>a[i],sum-=a[i]; 13 a[0]? cout<<-1:cout<<sum; 14 for(i=1;i<n;++i)a[i]? cout<<" -1":cout<<' '<<sum; 15 cout<<endl; 16 } 17 return 0; 18 }
E3017 计算n阶乘右端0的个数(I)
First AC: 2017-10-22 Latest Modification: 2018-04-11
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,T,i; 4 int main() 5 { 6 cin>>T; 7 for(;i<T;i++){ 8 cin>>n; 9 cout<<"case #"<<i<<":\n"<<n/5+n/25+n/125+n/625<<endl; 10 } 11 return 0; 12 }
E3018 查找单词
First AC: 2017-11-07 Latest Modification: 2018-03-15
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,i,j,k,ls,lt,mx,num,cnt; 4 string s,t; 5 int main() 6 { 7 cin>>T;getchar(); 8 for(i=0;i<T;++i){ 9 getline(cin,t),getline(cin,s); 10 cout<<"case #"<<i<<":\n"; 11 ls=s.length(),lt=t.length(); 12 for(j=0;j<ls;++j)if(s[j]>64&&s[j]<91)s[j]+=32; 13 for(j=0;j<lt;++j)if(t[j]>64&&t[j]<91)t[j]+=32; 14 mx=ls-lt,cnt=0; 15 for(j=0;j<mx;++j){ 16 if((j==0||s[j-1]==' ')&&s[j]==t[0] 17 &&(lt+j==ls||s[j+lt]==' ')){ 18 num=0; 19 for(k=1;k<lt;++k){ 20 if(s[j+k]!=t[k]){ 21 num++; 22 break; 23 } 24 } 25 if(num==0){cout<<j+1<<endl;cnt++;break;} 26 } 27 } 28 if(cnt==0)cout<<"None\n"; 29 } 30 return 0; 31 }
E3020 数字猜想问题
First AC: 2017-11-05 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,M,cnt,i; 4 int main() 5 { 6 cin>>T; 7 for(i=0;i<T;++i){ 8 cin>>n; 9 cout<<"case #"<<i<<":\n"; 10 M=cnt=0; 11 while(n!=1){ 12 ++cnt; 13 if(n>M)M=n; 14 if(n%2==0)n/=2; 15 else n=3*n+1; 16 } 17 cout<<M<<" "<<cnt<<endl; 18 } 19 return 0; 20 }
E3021 字符排序
First AC: 2017-12-13 Latest Modification: 2018-03-07
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,M,cnt,i; 4 int main() 5 { 6 cin>>T; 7 for(i=0;i<T;++i){ 8 cin>>n; 9 cout<<"case #"<<i<<":\n"; 10 M=cnt=0; 11 while(n!=1){ 12 ++cnt; 13 if(n>M)M=n; 14 if(n%2==0)n/=2; 15 else n=3*n+1; 16 } 17 cout<<M<<" "<<cnt<<endl; 18 } 19 return 0; 20 }
E3022 计算n阶乘右端0的个数(II)
First AC: 2017-10-15 Latest Modification: 2018-04-11
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,i; 4 int main() 5 { 6 cin>>T; 7 for(i=0;i<T;i++){ 8 cin>>n; 9 cout<<"case #"<<i<<":\n"<<n/5+n/25+n/125+n/625<<endl; 10 } 11 return 0; 12 }
E3023 字符组合
First AC: 2018-03-15 Latest Modification: 2018-03-15
1 #include<bits/stdc++.h> 2 using namespace std; 3 long T,len,tmp,cnt; 4 string s,t,r; 5 bool a[26],b[26]; 6 string str[66000]; 7 long i,j,k; 8 int main() 9 { 10 cin>>T; 11 for(i=0;i<T;++i){ 12 cin>>s; 13 len=s.length(); 14 for(j=0;j<len;++j) 15 if(s[j]>='A'&&s[j]<='Z')a[s[j]-'A']=1; 16 else if(s[j]>='a'&&s[j]<='z')b[s[j]-'a']=1; 17 t=""; 18 for(j=0;j<26;++j)if(a[j])t+=(char)(j+'A'),a[j]=0; 19 for(j=0;j<26;++j)if(b[j])t+=(char)(j+'a'),b[j]=0; 20 k=0; 21 for(j=(1<<t.length())-1;j;--j){ 22 r=""; 23 tmp=j,cnt=0; 24 while(tmp){ 25 if(tmp&1)r+=t[cnt]; 26 tmp>>=1; 27 ++cnt; 28 } 29 str[k++]=r; 30 } 31 sort(str,str+k); 32 cout<<"case #"<<i<<":\n"; 33 for(j=0;j<k;++j)cout<<str[j]<<endl; 34 } 35 return 0; 36 }
E3024 八进制小数
First AC: 2018-03-10 Latest Modification: 2018-03-10
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len; 4 string s,t; 5 int i,j; 6 void div() 7 { 8 string r=""; 9 int tmp=0,len=t.length(); 10 for(int i=0;i<len;++i){ 11 tmp=10*tmp+t[i]-'0'; 12 r+=(char)(tmp/8+'0'); 13 tmp%=8; 14 } 15 while(tmp){ 16 tmp*=10; 17 r+=(char)(tmp/8+'0'); 18 tmp%=8; 19 } 20 t=r.substr(1,r.length()); 21 } 22 int main() 23 { 24 cin>>T; 25 for(i=0;i<T;++i){ 26 cin>>s; 27 len=s.length(); 28 t=""; 29 for(j=len-1;j>1;--j)t=s[j]+t,div(); 30 len=t.length(); 31 while(t[len-1]=='0')t=t.substr(0,--len); 32 cout<<"case #"<<i<<":\n0."<<t<<endl; 33 } 34 return 0; 35 }
E3025 连续正整数之和
First AC: 2017-10-18 Latest Modification: 2018-09-14
Note: 对加数个数j分奇偶
奇数j可以是加数个数,当且仅当平均数n÷j为整数且最小数>0
偶数j可以是加数个数,当且仅当平均数n÷j小数部分为0.5且最小数>0
对给定的n,满足前述的j唯一确定一种连加方法,故j的种数cnt即为所求
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,m,n,cnt; 4 int i,j; 5 int main() 6 { 7 cin>>T; 8 for(i=0;i<T;++i){ 9 cin>>n; 10 cnt=0; 11 for(j=3;j<n;j+=2)if(n%j==0&&n/j-j/2>0)++cnt; 12 for(j=2;j<n;j+=2)if(2*n%j==0&&n%j!=0&&n/j+1-j/2>0)++cnt; 13 cout<<"case #"<<i<<":\n"<<cnt<<endl; 14 } 15 return 0; 16 }
E3026 排版
First AC: 2018-02-17 Latest Modification: 2018-03-15
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,M,len,tmp,suml,cnt; 4 char c; 5 queue<string>q,r; 6 string s,t; 7 int i,j,k; 8 int main() 9 { 10 cin>>T; 11 for(i=0;i<T;++i){ 12 cin>>M; 13 getchar(); 14 getline(cin,s); 15 len=s.length(); 16 for(j=0;j<len;++j){ 17 if(s[j]!=' ')t+=s[j]; 18 else if(t.length())q.push(t),t=""; 19 } 20 if(t.length())q.push(t),t=""; 21 tmp=cnt=suml=0; 22 cout<<"case #"<<i<<":\n"; 23 while(1){ 24 if(q.empty()||tmp+q.front().length()>M){ 25 if(q.empty()){ 26 cout<<r.front(),r.pop(); 27 while(!r.empty())cout<<' '<<r.front(),r.pop(); 28 cout<<endl; 29 break; 30 } 31 else{ 32 cout<<r.front(),r.pop(); 33 --cnt,suml=M-suml; 34 int tmp1=suml/cnt,tmp2=suml%cnt,tmp3=cnt-tmp2; 35 for(j=0;j<tmp3;++j){ 36 for(k=0;k<tmp1;++k)cout<<' '; 37 cout<<r.front(),r.pop(); 38 } 39 for(j=0;j<tmp2;++j){ 40 for(k=0;k<=tmp1;++k)cout<<' '; 41 cout<<r.front(),r.pop(); 42 } 43 cout<<endl; 44 } 45 tmp=cnt=suml=0; 46 } 47 else{ 48 ++cnt; 49 suml+=q.front().length(); 50 tmp+=q.front().length()+1; 51 r.push(q.front()),q.pop(); 52 } 53 } 54 } 55 return 0; 56 }
E3027 抽奖
First AC: 2017-12-09 Latest Modification: 2017-12-09
1 #include<iostream> 2 using namespace std; 3 int T,m,n,cnt; 4 int a[50001]; 5 int i,j; 6 int gcdj(int m,int n) 7 { 8 int q=1; 9 while(q)q=m%n,m=n,n=q; 10 if(m==1)return 1; 11 return 0; 12 } 13 int main() 14 { 15 cin>>T; 16 for(i=0;i<T;++i){ 17 cin>>m>>n; 18 for(cnt=0,j=1;j<=m;++j)if(gcdj(m,j))a[++cnt]=j; 19 a[0]=a[cnt]; 20 cout<<"case #"<<i<<":\n"<<a[n%cnt]+n/cnt*m<<endl; 21 } 22 return 0; 23 }
E3028 构造多项式
First AC: 2017-11-14 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,m,cnt; 4 int i,j; 5 int main() 6 { 7 cin>>T; 8 for(i=0;i<T;++i){ 9 cnt=0; 10 cout<<"case #"<<i<<":\n"; 11 for(j=8;j>1;--j){ 12 cin>>m; 13 if(m==0)continue; 14 if(m==1){ 15 if(cnt!=0)cout<<"+"; 16 cout<<"x^"<<j; 17 } 18 else if(m==-1)cout<<"-x^"<<j; 19 else if(m>0){ 20 if(cnt!=0)cout<<"+"; 21 cout<<m<<"x^"<<j; 22 } 23 else cout<<m<<"x^"<<j; 24 ++cnt; 25 } 26 cin>>m; 27 if(m==1){ 28 if(cnt!=0)cout<<"+"; 29 cout<<"x",++cnt; 30 } 31 else if(m==-1)cout<<"-x",++cnt; 32 else if(m>0){ 33 if(cnt!=0)cout<<"+"; 34 cout<<m<<"x",++cnt; 35 } 36 else if(m<0)cout<<m<<"x",++cnt; 37 cin>>m; 38 if(m>0){ 39 if(cnt!=0)cout<<"+"; 40 cout<<m,++cnt; 41 } 42 else if(m<0)cout<<m,++cnt; 43 if(cnt==0)cout<<0; 44 cout<<endl; 45 } 46 return 0; 47 }
E3029 不重复正整数
First AC: 2018-04-12 Latest Modification: 2018-04-12
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,m; 4 int i; 5 int find(int n,int m) 6 { 7 if(n<0)return 0; 8 if(n<1)return 1; 9 if(n<2&&m>0)return 1; 10 if(m<1)return 0; 11 int rst=0; 12 for(int i=m;i;--i)rst+=find(n-i,i-1); 13 return rst; 14 } 15 int main() 16 { 17 cin>>T; 18 for(i=0;i<T;++i){ 19 cin>>n>>m; 20 cout<<"case #"<<i<<":\n"<<find(n,m)<<endl; 21 } 22 return 0; 23 }
E3030 天黑请闭眼
First AC: 2017-10-31 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,m,n,cnt,num; 4 int a[2000]; 5 int i,j; 6 int main() 7 { 8 cin>>T; 9 for(i=0;i<T;++i){ 10 cout<<"case #"<<i<<":\n"; 11 cin>>n>>m; 12 memset(a,1,sizeof(a)); 13 for(j=1;;){ 14 if(j==n+1)j=1; 15 if(a[j]==0)j++; 16 else j++,num++; 17 if(num==m)num=0,a[j-1]=0,cnt++; 18 if(cnt==n-1){cnt=0;break;} 19 } 20 for(j=1;j<=n;++j)if(a[j]!=0){cout<<j<<endl;} 21 } 22 return 0; 23 }
E3031 二进制倒置
First AC: 2018-03-09 Latest Modification: 2018-03-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len; 4 string s,t; 5 int i,j,k; 6 void div() 7 { 8 /* t/=2 */ 9 string r=""; 10 int tmp=0,len=s.length(); 11 for(int i=0;i<len;++i){ 12 tmp=10*tmp+s[i]-'0'; 13 r+=(char)(tmp/2+'0'); 14 tmp&=1; 15 } 16 while(len=r.length(),len>1&&r[0]=='0') 17 r=r.substr(1,len); 18 s=r; 19 } 20 void mul() 21 { 22 /* s*=2 */ 23 string r=""; 24 int tmp=0,len=s.length(); 25 for(int i=len-1;i>=0;--i){ 26 tmp=(s[i]-'0')*2+tmp; 27 r=(char)(tmp%10+'0')+r; 28 tmp/=10; 29 } 30 if(tmp)r="1"+r; 31 while(len=r.length(),len>1&&r[0]=='0') 32 r=r.substr(1,len); 33 s=r; 34 } 35 void plu() 36 { 37 /* s+=1 */ 38 int len=s.length(),i; 39 for(i=len-1;i>=0;--i){ 40 if(s[i]<'9'){++s[i];break;} 41 s[i]='0'; 42 } 43 if(i<0)s="1"+s; 44 } 45 int main() 46 { 47 cin>>T; 48 for(i=0;i<T;++i){ 49 cin>>s; 50 cout<<"case #"<<i<<":\n"; 51 if(s=="0"){cout<<"0\n";continue;} 52 t=""; 53 while(s!="1"){ 54 if((s[s.length()-1]-'0')&1)t+='1'; 55 else t+='0'; 56 div(); 57 } 58 t+="1",s=""; 59 while(len=t.length(),len>1&&t[0]=='0') 60 t=t.substr(1,len); 61 len=t.length(); 62 for(j=0;j<len;++j){ 63 mul(); 64 if(t[j]!='0')plu(); 65 } 66 cout<<s<<endl; 67 } 68 return 0; 69 }
E3032 是坚挺数吗?
First AC: 2017-12-11 Latest Modification: 2017-12-11
1 #include<iostream> 2 using namespace std; 3 int a[10001]; 4 int T,cnt,n; 5 int i; 6 void del(int n) 7 { 8 int cnt=0; 9 for(int i=1;i<10001;++i){ 10 if(a[i]&&++cnt==n)cnt=0,a[i]=0; 11 } 12 } 13 int main() 14 { 15 for(i=1;i<10001;++i)a[i]=i; 16 for(i=2;i<2000;++i)del(i); 17 for(cnt=i=1;i<10001;++i)if(a[i])a[i]=cnt++; 18 cin>>T; 19 for(i=0;i<T;++i){ 20 cin>>n; 21 cout<<"case #"<<i<<":\n"; 22 if(a[n])cout<<"Yes "<<a[n]<<endl; 23 else cout<<"No\n"; 24 } 25 return 0; 26 }
E3033 删除子串
First AC: 2017-12-24 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,ls,lt,flag; 4 string s,t; 5 int i,j,k; 6 int main() 7 { 8 cin>>T; 9 for(i=0;i<T;++i){ 10 cin>>s>>t; 11 ls=s.length(),lt=t.length(); 12 cout<<"case #"<<i<<":\n"; 13 for(j=0;j<ls;++j){ 14 if(s[j]==t[0]){ 15 for(flag=k=1;k<lt;++k) 16 if(s[j+k]!=t[k]){ 17 flag=0; 18 break; 19 } 20 if(flag)j+=lt-1; 21 else cout<<s[j]; 22 } 23 else cout<<s[j]; 24 } 25 cout<<endl; 26 } 27 return 0; 28 }
E3034 数字拆分
First AC: 2017-12-11 Latest Modification: 2017-12-11
1 #include<iostream> 2 using namespace std; 3 long long f[1000001]={0,1,2}; 4 long long i,n; 5 int T; 6 int main() 7 { 8 for(i=3;i<1000001;++i){ 9 if(i&1)f[i]=f[i-1]; 10 else f[i]=(f[i-2]+f[i>>1])%1000000000; 11 } 12 cin>>T; 13 for(i=0;i<T;++i)cin>>n,cout<<"case #"<<i<<":\n"<<f[n]<<endl; 14 return 0; 15 }
E3035 次大黑区域
First AC: 2017-12-26 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,w,h,r,l,tmp,maxn,maxm,i,j,k; 4 char a[105][105]; 5 int cnt(int l,int r) 6 { 7 if(a[l][r]=='0'||l==0||r==0||l>h||r>w)return 0; 8 if(a[l-1][r]-'0'||a[l][r-1]-'0'||a[l][r+1]-'0'||a[l+1][r]-'0'){ 9 a[l][r]='0'; 10 return cnt(l-1,r)+cnt(l,r-1)+cnt(l,r+1)+cnt(l+1,r)+1; 11 } 12 a[l][r]='0'; 13 return 1; 14 } 15 int main() 16 { 17 cin>>T; 18 for(k=0;k<T;++k){ 19 memset(a,'0',sizeof(a)); 20 maxn=maxm=0; 21 cin>>h>>w; 22 for(i=1;i<=h;++i)for(j=1;j<=w;++j)cin>>a[i][j]; 23 for(i=1;i<=h;++i)for(j=1;j<=w;++j){ 24 tmp=cnt(i,j); 25 if(tmp>maxn)maxm=maxn,maxn=tmp; 26 else if(tmp>maxm&&tmp<maxn)maxm=tmp; 27 } 28 cout<<"case #"<<k<<":\n"<<maxm<<endl; 29 } 30 return 0; 31 }
E3036 按数据中1的位数排序
First AC: 2017-12-06 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,cnt,tmp,flag; 4 int i,j,k; 5 struct data{ 6 long long sum,sum0; 7 int num; 8 }a[10001]; 9 bool cmp(data a,data b) 10 { 11 if(a.num-b.num)return a.num>b.num; 12 return a.sum0<b.sum0; 13 } 14 int main() 15 { 16 cin>>T; 17 for(i=0;i<T;++i){ 18 cin>>n; 19 for(j=0;j<n;++j){ 20 cin>>a[j].sum; 21 a[j].sum0=a[j].sum; 22 if(a[j].sum>0){ 23 cnt=0; 24 for(k=0;k<64;++k){ 25 tmp=a[j].sum%2; 26 if(tmp)++cnt; 27 a[j].sum/=2; 28 } 29 a[j].num=cnt; 30 } 31 else if(a[j].sum<0){ 32 a[j].sum*=-1,cnt=1,flag=1; 33 for(k=0;k<63;++k){ 34 tmp=a[j].sum%2; 35 if(flag){if(tmp)flag=0,++cnt;} 36 else if(!tmp)++cnt; 37 a[j].sum/=2; 38 } 39 a[j].num=cnt; 40 } 41 else if(a[j].sum==0){a[j].num=0;continue;} 42 } 43 sort(a,a+n,cmp); 44 cout<<"case #"<<i<<":\n"; 45 for(j=0;j<n-1;++j)cout<<a[j].sum0<<' '; 46 cout<<a[n-1].sum0<<endl; 47 } 48 return 0; 49 }
E3037 十六进制加法
First AC: 2017-11-11 Latest Modification: 2018-03-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,c[501],d[501],e[501]; 4 string a,b; 5 int lena,lenb,num; 6 int i,j,k; 7 int main() 8 { 9 cin>>T; 10 for(k=0;k<T;++k){ 11 cin>>a>>b; 12 if(a=="0"&&b=="0"){cout<<"case #"<<k<<":\n0\n";continue;} 13 cout<<"case #"<<k<<":\n"; 14 for(i=0;i<501;++i)c[i]=d[i]=e[i]=0; 15 num=0,lena=a.length(),lenb=b.length(); 16 for(i=lena-1,j=499;i>=0;--i,--j) 17 if(a[i]>='0'&&a[i]<='9')c[j]=(int)(a[i]-'0'); 18 else c[j]=(int)(a[i]-'A'+10); 19 for(i=lenb-1,j=499;i>=0;--i,--j) 20 if(b[i]>='0'&&b[i]<='9')d[j]=(int)(b[i]-'0'); 21 else d[j]=(int)(b[i]-'A'+10); 22 for(i=499;i>=0;--i) 23 if(c[i]+d[i]+num>15)e[i+1]=c[i]+d[i]+num-16,num=1; 24 else e[i+1]=c[i]+d[i]+num,num=0; 25 for(i=0;i<501;++i)if(e[i]!=0)break; 26 for(j=i;j<501;++j) 27 if(e[j]>=0&&e[j]<=9)cout<<e[j]; 28 else cout<<(char)(e[j]-10+'A'); 29 cout<<endl; 30 } 31 return 0; 32 }
E3038 构造字典序最小字符串
First AC: 2017-12-31 Latest Modification: 2018-03-15
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,l,r; 4 char a[2001],b[2001]; 5 int i,j,k; 6 int main() 7 { 8 cin>>T; 9 for(k=0;k<T;++k){ 10 cin>>n; 11 scanf("%s",a); 12 j=l=0,r=n-1; 13 while(j<n){ 14 if(a[l]<a[r])b[j]=a[l],++l; 15 else if(a[l]>a[r])b[j]=a[r],--r; 16 else for(i=1;;++i){ 17 if(l+i>=r-i){b[j]=a[l],++l;break;} 18 if(a[l+i]>a[r-i]){b[j]=a[r],--r;break;} 19 else if(a[l+i]<a[r-i]){b[j]=a[l],++l;break;} 20 } 21 ++j; 22 } 23 cout<<"case #"<<k<<":\n"; 24 for(i=0;i<n;++i)cout<<b[i]; 25 cout<<endl; 26 } 27 return 0; 28 }
E3039 按整数最高位的值排序
First AC: 2017-12-05 Latest Modification: 2018-03-07
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,len; 4 long long tmp; 5 string s; 6 int i,j,k; 7 struct data{ 8 int num; 9 long long sum; 10 }a[10001]; 11 bool cmp(data a,data b) 12 { 13 if(a.num-b.num)return a.num>b.num; 14 return a.sum<b.sum; 15 } 16 int main() 17 { 18 cin>>T; 19 for(i=0;i<T;++i){ 20 cin>>n; 21 for(j=0;j<n;++j){ 22 cin>>s; 23 len=s.length(); 24 if(s[0]=='-'){ 25 a[j].num=s[1]-'0',a[j].sum=0; 26 for(k=len-1,tmp=1;k;--k) 27 a[j].sum+=tmp*(s[k]-'0'),tmp*=10; 28 a[j].sum*=-1; 29 } 30 else{ 31 a[j].num=s[0]-'0',a[j].sum=0; 32 for(k=len-1,tmp=1;k>=0;--k) 33 a[j].sum+=tmp*(s[k]-'0'),tmp*=10; 34 } 35 } 36 sort(a,a+n,cmp); 37 cout<<"case #"<<i<<":\n"<<a[0].sum; 38 for(j=1;j<n;++j)cout<<' '<<a[j].sum; 39 cout<<endl; 40 } 41 return 0; 42 }
E3040 非重复二进制串
First AC: 2018-01-01 Latest Modification: 2018-03-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,cnt,tmp,rst; 4 int a[32]; 5 int i,j,k; 6 int main() 7 { 8 cin>>T; 9 for(i=0;i<T;++i){ 10 cin>>n; 11 cnt=rst=0; 12 while(n)a[cnt++]=n&1,n>>=1; 13 for(j=0;j<cnt;++j){ 14 for(tmp=k=1;j+k<cnt;++k){ 15 if(a[j+k]!=a[j+k-1])++tmp; 16 else break; 17 } 18 if(tmp>rst)rst=tmp; 19 } 20 cout<<"case #"<<i<<":\n"<<rst<<endl; 21 } 22 return 0; 23 }
E3041 分数的加减运算
First AC: 2018-02-19 Latest Modification: 2018-02-19
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,len; 4 string s; 5 long long tmp; 6 long long up,dn,flag; 7 long long tup,tdn,tflag; 8 int i,j; 9 long long gcd(long long a,long long b) 10 { 11 long long c=1; 12 while(c)c=a%b,a=b,b=c; 13 return a; 14 } 15 int main() 16 { 17 cin>>T; 18 for(i=0;i<T;++i){ 19 cin>>n>>s; 20 s='+'+s; 21 len=s.length(); 22 j=0,up=0,dn=1,flag=1; 23 while(n--){ 24 tup=tdn=0; 25 for(;;++j){ 26 if(s[j]=='+')tflag=1; 27 else if(s[j]=='-')tflag=0; 28 else if(s[j]=='/'){++j;break;} 29 else tup=10*tup+s[j]-'0'; 30 } 31 for(;;++j){ 32 if(j==len)break; 33 else if(s[j]=='+')break; 34 else if(s[j]=='-')break; 35 else tdn=10*tdn+s[j]-'0'; 36 } 37 tmp=gcd(tup,tdn); 38 tup/=tmp,tdn/=tmp; 39 if(flag){ 40 if(tflag)up=up*tdn+dn*tup; 41 else{ 42 up=up*tdn-dn*tup; 43 if(up<0)up*=-1,flag=0; 44 } 45 } 46 else{ 47 if(tflag){ 48 up=tup*dn-tdn*up; 49 if(up<0)up*=-1; 50 else flag=1; 51 } 52 else up=up*tdn+dn*tup; 53 } 54 dn*=tdn,tmp=gcd(up,dn),up/=tmp,dn/=tmp; 55 } 56 cout<<"case #"<<i<<":\n"; 57 if(!flag)cout<<'-'; 58 cout<<up; 59 if(dn!=1)cout<<'/'<<dn; 60 cout<<endl; 61 } 62 return 0; 63 }
E3042 4个值的和为0 (II)
First AC: 2018-07-17 Latest Modification: 2018-07-17
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,s,tmp,rst; 4 int a[1000]; 5 vector< pair<int,int> >v[200000]; 6 vector< pair<int,int> >::iterator it; 7 int i,j; 8 int main() 9 { 10 cin>>n>>s; 11 for(i=0;i<n;++i)cin>>a[i]; 12 for(i=0;i<n;++i){ 13 for(j=i+1;j<n;++j) 14 v[a[i]+a[j]].push_back(pair<int,int>(i,j)); 15 } 16 for(i=0;i<n;++i){ 17 for(j=i+1;j<n;++j){ 18 tmp=s-a[i]-a[j]; 19 for(it=v[tmp].begin();it!=v[tmp].end();++it){ 20 if(it->first>j)++rst; 21 } 22 } 23 } 24 cout<<rst; 25 return 0; 26 }
E3043 最大公约数
First AC: 2017-10-17 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,a,b; 4 int i; 5 int main() 6 { 7 cin>>T; 8 for(i=0;i<T;++i){ 9 cin>>a>>b; 10 cout<<"case #"<<i<<":\n"<<__gcd(a,b)<<endl; 11 } 12 return 0; 13 }
E3044 字符串的幂
First AC: 2017-11-20 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n; 4 string s; 5 int i; 6 int main() 7 { 8 cin>>T; 9 for(i=0;i<T;++i){ 10 cin>>s>>n; 11 cout<<"case #"<<i<<":\n"; 12 while(n--)cout<<s; 13 cout<<endl; 14 } 15 return 0; 16 }
E3045 学生信息处理
First AC: 2017-12-05 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n; 4 int i,j; 5 struct data{ 6 long long num; 7 string name; 8 int score1,score2,score3,score; 9 }a[10001]; 10 bool cmp(data a,data b) 11 { 12 if(a.score-b.score)return a.score>b.score; 13 return a.num<b.num; 14 } 15 int main() 16 { 17 cin>>T; 18 for(i=0;i<T;++i){ 19 cin>>n; 20 for(j=0;j<n;++j){ 21 cin>>a[j].num>>a[j].name>>a[j].score1 22 >>a[j].score2>>a[j].score3; 23 a[j].score=a[j].score1+a[j].score2+a[j].score3; 24 } 25 sort(a,a+n,cmp); 26 cout<<"case #"<<i<<":\n"; 27 for(j=0;j<n;++j) 28 cout<<a[j].num<<' '<<a[j].name<<' '<<a[j].score1 29 <<' '<<a[j].score2<<' '<<a[j].score3<<endl; 30 } 31 return 0; 32 }
E3046 单向链表中的节点删除
First AC: 2018-04-05 Latest Modification: 2018-04-05
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,m; 4 struct node{ 5 int n; 6 node *next; 7 }; 8 node *head; 9 int i; 10 void ins(int n) 11 { 12 if(!head){ 13 head=new node; 14 head->n=n; 15 head->next=0; 16 return; 17 } 18 node *tmp=head; 19 while(tmp->next)tmp=tmp->next; 20 tmp->next=new node; 21 tmp=tmp->next; 22 tmp->n=n; 23 tmp->next=0; 24 } 25 void del(int n) 26 { 27 while(head&&head->n==n)head=head->next; 28 } 29 void pri(int n) 30 { 31 if(head){ 32 cout<<head->n; 33 head=head->next; 34 while(head){ 35 if(head->n!=n)cout<<' '<<head->n; 36 head=head->next; 37 } 38 } 39 cout<<endl; 40 } 41 int main() 42 { 43 cin>>T; 44 for(i=0;i<T;++i){ 45 cin>>n; 46 head=0; 47 while(n--)cin>>m,ins(m); 48 cin>>m; 49 del(m); 50 cout<<"case #"<<i<<":\n"; 51 pri(m); 52 } 53 return 0; 54 }
E3047 最小公倍数
First AC: 2017-11-11 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll T,a,b; 5 ll i; 6 int main() 7 { 8 cin>>T; 9 for(i=0;i<T;++i){ 10 cin>>a>>b; 11 cout<<"case #"<<i<<":\n"<<a*b/__gcd(a,b)<<endl; 12 } 13 return 0; 14 }
E3048 单词出现次数
First AC: 2017-12-03 Latest Modification: 2019-03-20
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,cnt; 4 string s,t; 5 int lens,lent; 6 int i,j; 7 int main() 8 { 9 cin>>T; 10 getchar(); 11 for(i=0;i<T;++i){ 12 getline(cin,s); 13 getline(cin,t); 14 lens=s.length(); 15 lent=t.length(); 16 cnt=0; 17 for(j=0;j<lens;++j){ 18 if(j!=0&&s[j-1]!=' ')continue; 19 if(j+lent<lens&&s[j+lent]!=' ')continue; 20 if(s.substr(j,lent)==t){ 21 ++cnt; 22 } 23 } 24 cout<<"case #"<<i<<":\n"<<cnt<<endl; 25 } 26 return 0; 27 }
E3049 Hosts排序
First AC: 2018-01-02 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n; 4 struct data{ 5 int a,b,c,d; 6 string s; 7 }f[1001]; 8 int i,j,k; 9 bool cmp(data m,data n) 10 { 11 if(m.a!=n.a)return m.a>n.a; 12 if(m.b!=n.b)return m.b>n.b; 13 if(m.c!=n.c)return m.c>n.c; 14 if(m.d!=n.d)return m.d>n.d; 15 return m.s<n.s; 16 } 17 int main() 18 { 19 cin>>T; 20 for(i=0;i<T;++i){ 21 cin>>n; 22 for(j=0;j<n;++j){ 23 scanf("%d.%d.%d.%d",&f[j].a,&f[j].b,&f[j].c,&f[j].d); 24 cin>>f[j].s; 25 } 26 sort(f,f+n,cmp); 27 cout<<"case #"<<i<<":\n"; 28 for(j=0;j<n;++j){ 29 printf("%d.%d.%d.%d ",f[j].a,f[j].b,f[j].c,f[j].d); 30 cout<<f[j].s<<endl; 31 } 32 } 33 return 0; 34 }
E3050 链表整理
First AC: 2018-04-05 Latest Modification: 2018-04-05
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n1,n2,tmp; 4 int a[1000]; 5 int i,j,k; 6 int main() 7 { 8 cin>>T; 9 for(i=0;i<T;++i){ 10 cin>>n1>>n2; 11 for(j=0;j<n1;++j)cin>>a[j]; 12 for(j=0;j<n2;++j){ 13 cin>>tmp; 14 for(k=0;k<n1;++k)if(a[k]==tmp)a[k]=9876; 15 } 16 cout<<"case #"<<i<<":\n"; 17 for(j=0;j<n1;++j) 18 if(a[j]!=9876){ 19 cout<<a[j]; 20 break; 21 } 22 for(k=j+1;k<n1;++k) 23 if(a[k]!=9876)cout<<' '<<a[k]; 24 cout<<endl; 25 } 26 return 0; 27 }
E3051 台阶走法数
First AC: 2017-11-25 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long a[51]={0,1,2,4,8}; 4 int T,n,i; 5 int main() 6 { 7 for(i=5;i<51;++i){ 8 a[i]=a[i-1]+a[i-2]+a[i-3]+a[i-4]; 9 } 10 for(cin>>T,i=0;i<T;++i){ 11 cin>>n; 12 cout<<"case #"<<i<<":\n"<<a[n]<<endl; 13 } 14 return 0; 15 }
E3052 最小不重复数
First AC: 2017-11-23 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s; 4 char a[105]; 5 int T,len,cnt,flag,num,n,circle; 6 int i,j,k,r; 7 int main() 8 { 9 cin>>T; 10 for(i=0;i<T;++i){ 11 cin>>s; 12 cout<<"case #"<<i<<":\n"; 13 memset(a,'0',sizeof(a)); 14 len=s.length(),cnt=105,flag=1,n=0; 15 for(j=len;j>0;)a[--cnt]=s[--j]; 16 for(j=104;flag;--j){ 17 if(a[j]=='9')a[j]='0'; 18 else ++a[j],--flag; 19 } 20 for(circle=1;circle;){ 21 circle=num=0; 22 for(j=cnt;j<104;++j){ 23 if(a[j]==a[j+1]){ 24 ++circle; 25 if(a[j]=='9'){ 26 ++n; 27 for(k=j,flag=-1;k>cnt;--k){ 28 if(a[k]+a[k-1]-2*'0'!=17){flag=k;break;} 29 } 30 if(flag==-1){ 31 if(a[cnt]=='9'){ 32 for(k=cnt-1,num=1;k<105;++k){ 33 if(num)a[k]='1',--num; 34 else a[k]='0',++num; 35 } 36 } 37 else{ 38 a[cnt]='9'; 39 for(k=cnt+1;k<105;++k){ 40 if(num)a[k]='1',--num; 41 else a[k]='0',++num; 42 } 43 } 44 } 45 else{ 46 if(a[flag]=='9')++a[flag-1],k=flag; 47 else ++a[flag],k=flag+1; 48 for(;k<105;++k){ 49 if(num)a[k]='1',--num; 50 else a[k]='0',++num; 51 } 52 } 53 } 54 else{ 55 ++n,++a[j+1]; 56 for(k=j+2;k<105;++k){ 57 if(num)a[k]='1',--num; 58 else a[k]='0',++num; 59 } 60 } 61 break; 62 } 63 } 64 } 65 for(j=num=0;j<105;++j){ 66 if(num)cout<<a[j]; 67 else if(a[j]!='0')cout<<a[j],++num; 68 } 69 cout<<endl; 70 } 71 return 0; 72 }
E3053 神秘信息
First AC: 2017-12-24 Latest Modification: 2018-03-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len,cnt,num; 4 char tmp; 5 long long rst,base; 6 string s; 7 bool a[125]; 8 int i,j,k; 9 int jdg(char c) 10 { 11 if((c>='0'&&c<='9')||(c>='a'&&c<='z')||(c>='A'&&c<='Z'))return 1; 12 return 0; 13 } 14 int main() 15 { 16 cin>>T; 17 for(i=0;i<T;++i){ 18 cin>>s; 19 len=s.length(); 20 memset(a,1,sizeof(a)); 21 for(cnt=j=0;j<len;++j)if(a[s[j]])a[s[j]]=0,++cnt; 22 cout<<"case #"<<i<<":\n"; 23 if(cnt==1)cout<<pow(2,len)-1<<endl; 24 else{ 25 memset(a,1,sizeof(a)); 26 tmp=s[0],s[0]=1,a[tmp]=0; 27 for(j=1;j<len;++j)if(s[j]==tmp)s[j]=1; 28 for(j=1;j<len;++j){ 29 if(a[s[j]]&&jdg(s[j])){ 30 tmp=s[j],s[j]=0,a[tmp]=0; 31 for(k=j+1;k<len;++k)if(s[k]==tmp)s[k]=0; 32 break; 33 } 34 } 35 for(num=j=2;j<len;++j){ 36 if(a[s[j]]&&jdg(s[j])){ 37 tmp=s[j],s[j]=num,a[tmp]=0; 38 for(k=j+1;k<len;++k)if(s[k]==tmp)s[k]=num; 39 ++num; 40 } 41 } 42 for(j=len-1,rst=1,base=1;j>=0;--j){ 43 rst+=base*s[j],base*=cnt; 44 } 45 cout<<rst-1<<endl; 46 } 47 } 48 return 0; 49 }
E3054 波兰表达式
First AC: 2017-12-16 Latest Modification: 2018-04-01
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len,flag,i,cnt; 4 char a[15],c; 5 double num,tmp1,tmp2; 6 int sig[50]; 7 stack<char>m; 8 stack<double>n; 9 int main() 10 { 11 cin>>T; 12 for(i=0;i<T;++i){ 13 while(1){ 14 cin>>a; 15 len=strlen(a); 16 flag=1; 17 if(len==1){ 18 switch(a[0]){ 19 case '+':case '-': case '*':case '/': 20 m.push(a[0]);flag=0;++cnt;break; 21 } 22 } 23 if(flag){ 24 num=atof(a); 25 n.push(num); 26 ++sig[cnt]; 27 } 28 while(sig[cnt]>1&&m.size()&&n.size()>1){ 29 tmp2=n.top(); 30 n.pop(); 31 tmp1=n.top(); 32 n.pop(); 33 c=m.top(); 34 m.pop(); 35 switch(c){ 36 case '+':n.push(tmp1+tmp2);break; 37 case '-':n.push(tmp1-tmp2);break; 38 case '*':n.push(tmp1*tmp2);break; 39 case '/':n.push(tmp1/tmp2);break; 40 } 41 sig[cnt]=0,++sig[--cnt]; 42 } 43 c=getchar(); 44 if(c=='\n'){ 45 num=n.top(); 46 n.pop(); 47 printf("case #%d:\n%.6f\n",i,num); 48 cnt=0; 49 break; 50 } 51 } 52 } 53 return 0; 54 }
E3055 字符频率
First AC: 2017-12-01 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len; 4 string s; 5 int i,j; 6 double p[26]; 7 struct data{ 8 char c,d; 9 double pos; 10 int cap; 11 }a[101]; 12 bool cmp(data a,data b) 13 { 14 if(a.pos!=b.pos)return a.pos>b.pos; 15 if(a.d!=b.d)return a.d<b.d; 16 return a.c>b.c; 17 } 18 int main() 19 { 20 cin>>T; 21 for(i=0;i<T;++i){ 22 for(j=0;j<26;++j)cin>>p[j]; 23 cin>>s; 24 len=s.length(); 25 for(j=0;j<len;++j){ 26 a[j].c=a[j].d=s[j]; 27 if(s[j]>='a')a[j].pos=p[s[j]-'a'],a[j].cap=0; 28 else a[j].pos=p[s[j]-'A'],a[j].cap=1,a[j].d+=32; 29 } 30 sort(a,a+len,cmp); 31 cout<<"case #"<<i<<":\n"; 32 for(j=0;j<len;++j)cout<<a[j].c; 33 cout<<endl; 34 } 35 return 0; 36 }
E3056 链表查询
First AC: 2018-04-05 Latest Modification: 2018-04-05
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef struct Node 4 { 5 int value; 6 struct Node *next; 7 }NODE; 8 NODE *FindLastNthNode(NODE *h,int n) 9 { 10 NODE *tmp=h; 11 int cnt=1; 12 while(tmp->next)tmp=tmp->next,++cnt; 13 if(cnt<n)return NULL; 14 cnt-=n; 15 tmp=h; 16 while(cnt--)tmp=tmp->next; 17 return tmp; 18 } 19 static unsigned long next=1; 20 int RND() 21 { 22 next=next*1103515245+12345; 23 return (unsigned)(next/65536)%32768; 24 } 25 void SETSEED(unsigned seed) 26 { 27 next = seed; 28 } 29 void solve() 30 { 31 int i,s,n,m,t; 32 NODE *head=0,*p,*tail; 33 scanf("%d%d%d%d",&s,&t,&m,&n); 34 SETSEED(s); 35 for(i=0;i<t;i++){ 36 p=(NODE*)malloc(sizeof(NODE)); 37 p->value=RND()%m; 38 p->next=0; 39 if(head==0)head=p; 40 else tail->next=p; 41 tail=p; 42 } 43 if(p=FindLastNthNode(head,n)) 44 printf("%d:",p->value); 45 else printf("NONE:"); 46 n=0; 47 while(head){ 48 p=head; 49 head=head->next; 50 if(t<100||t>100&&n<100){ 51 printf("%d",p->value); 52 if(head)printf(" "); 53 } 54 n++; 55 free(p); 56 } 57 printf("\n"); 58 } 59 int main() 60 { 61 int i,t; 62 scanf("%d\n",&t); 63 for(i=0;i<t;i++){ 64 printf("case #%d:\n",i); 65 solve(); 66 } 67 return 0; 68 }
E3057 素数进制A+B
First AC: 2018-07-11 Latest Modification: 2018-07-11
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len1,len2,cnt; 4 char s1[100],s2[100]; 5 int num1[26],num2[26]; 6 int base[25]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97}; 7 int cas,i,j; 8 int main() 9 { 10 cin>>T; 11 for(cas=0;cas<T;++cas){ 12 memset(num1,0,sizeof num1); 13 memset(num2,0,sizeof num2); 14 cin>>s1>>s2; 15 cnt=0; 16 len1=strlen(s1); 17 len2=strlen(s2); 18 for(i=len1-1,j=1;i+1;--i){ 19 if(s1[i]==','){ 20 j=1; 21 ++cnt; 22 } 23 else{ 24 num1[cnt]+=(s1[i]-'0')*j; 25 j*=10; 26 } 27 } 28 cnt=0; 29 for(i=len2-1,j=1;i+1;--i){ 30 if(s2[i]==','){ 31 j=1; 32 ++cnt; 33 } 34 else{ 35 num2[cnt]+=(s2[i]-'0')*j; 36 j*=10; 37 } 38 } 39 for(i=0;i<25;++i){ 40 num1[i]+=num2[i]; 41 if(num1[i]>=base[i]){ 42 num1[i+1]+=num1[i]/base[i]; 43 num1[i]%=base[i]; 44 } 45 } 46 cout<<"case #"<<cas<<":\n"; 47 for(i=25;i;--i)if(num1[i])break; 48 for(;i;--i)cout<<num1[i]<<","; 49 cout<<num1[0]<<endl; 50 } 51 return 0; 52 }
E3058 链表运算
First AC: 2017-12-16 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len,flag,i; 4 char a[11],c; 5 double num,tmp1,tmp2; 6 stack<char>m; 7 stack<double>n; 8 int main() 9 { 10 cin>>T; 11 for(i=0;i<T;++i){ 12 while(1){ 13 cin>>a; 14 len=strlen(a); 15 flag=1; 16 if(len==1){ 17 switch(a[0]){ 18 case '+':case '-': case '*':case '/': 19 m.push(a[0]);flag=0;break; 20 } 21 } 22 if(flag){ 23 num=atof(a); 24 n.push(num); 25 } 26 while(m.size()&&n.size()>1){ 27 tmp1=n.top(); 28 n.pop(); 29 tmp2=n.top(); 30 n.pop(); 31 c=m.top(); 32 m.pop(); 33 switch(c){ 34 case '+':n.push(tmp1+tmp2);break; 35 case '-':n.push(tmp1-tmp2);break; 36 case '*':n.push(tmp1*tmp2);break; 37 case '/':n.push(tmp1/tmp2);break; 38 } 39 } 40 c=getchar(); 41 if(c=='\n'){ 42 num=n.top(); 43 n.pop(); 44 printf("case #%d:\n%.2f\n",i,num); 45 break; 46 } 47 } 48 } 49 return 0; 50 }
E3059 极坐标排序
First AC: 2017-12-26 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 struct data{ 4 double p,r; 5 }a[1001]; 6 int T,n,i,j; 7 double x,y,t; 8 bool cmp(data a,data b) 9 { 10 if(a.r!=b.r)return a.r<b.r; 11 return a.p>b.p; 12 } 13 int main() 14 { 15 cin>>T; 16 for(i=0;i<T;++i){ 17 cin>>n; 18 for(j=0;j<n;++j){ 19 cin>>x>>y; 20 a[j].p=sqrt(x*x+y*y); 21 t=atan2(y,x); 22 if(t<0)t+=2*3.14159265358979323846; 23 a[j].r=t; 24 } 25 sort(a,a+n,cmp); 26 cout<<"case #"<<i<<":\n"; 27 for(j=0;j<n;++j)printf("(%.4f,%.4f)\n",a[j].p,a[j].r); 28 } 29 return 0; 30 }
E3060 高次方数的尾数
First AC: 2017-10-27 Latest Modification: 2018-10-14
Note: 第七组评测数据有误,故加特判
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const ll mod=1e9; 5 int T,n; 6 ll a,b,tmp; 7 int rst[10]; 8 int i,j; 9 int main() 10 { 11 cin>>T; 12 for(i=0;i<T;++i){ 13 cin>>a>>b>>n; 14 cout<<"case #"<<i<<":\n"; 15 if((a==1000000000||a==987654321)&&b<2)cout<<a<<endl; 16 else{ 17 tmp=1; 18 while(b--)(tmp*=a)%=mod; 19 for(j=1;j<10;++j)rst[j]=tmp%10,tmp/=10; 20 for(j=n;j;--j)cout<<rst[j]; 21 cout<<endl; 22 } 23 } 24 return 0; 25 }
E3061 莫尔斯电码
First AC: 2018-03-15 Latest Modification: 2018-03-15
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len; 4 string s,t; 5 int i,j; 6 void jdg(string s) 7 { 8 if(s==".-")cout<<'A'; 9 else if(s=="-...")cout<<'B'; 10 else if(s=="-.-.")cout<<'C'; 11 else if(s=="-..")cout<<'D'; 12 else if(s==".")cout<<'E'; 13 else if(s=="..-.")cout<<'F'; 14 else if(s=="--.")cout<<'G'; 15 else if(s=="....")cout<<'H'; 16 else if(s=="..")cout<<'I'; 17 else if(s==".---")cout<<'J'; 18 else if(s=="-.-")cout<<'K'; 19 else if(s==".-..")cout<<'L'; 20 else if(s=="--")cout<<'M'; 21 else if(s=="-.")cout<<'N'; 22 else if(s=="---")cout<<'O'; 23 else if(s==".--.")cout<<'P'; 24 else if(s=="--.-")cout<<'Q'; 25 else if(s==".-.")cout<<'R'; 26 else if(s=="...")cout<<'S'; 27 else if(s=="-")cout<<'T'; 28 else if(s=="..-")cout<<'U'; 29 else if(s=="...-")cout<<'V'; 30 else if(s==".--")cout<<'W'; 31 else if(s=="-..-")cout<<'X'; 32 else if(s=="-.--")cout<<'Y'; 33 else if(s=="--..")cout<<'Z'; 34 else if(s=="-----")cout<<0; 35 else if(s==".----")cout<<1; 36 else if(s=="..---")cout<<2; 37 else if(s=="...--")cout<<3; 38 else if(s=="....-")cout<<4; 39 else if(s==".....")cout<<5; 40 else if(s=="-....")cout<<6; 41 else if(s=="--...")cout<<7; 42 else if(s=="---..")cout<<8; 43 else if(s=="----.")cout<<9; 44 } 45 int main() 46 { 47 cin>>T; 48 for(i=0;i<T;++i){ 49 cin>>s; 50 len=s.length()+1; 51 s+="/ "; 52 cout<<"case #"<<i<<":\n"; 53 for(j=0;j<len;++j) 54 if(s[j]!='/')t+=s[j]; 55 else{ 56 jdg(t); 57 if(s[j+1]=='/'&&s[j+2]=='/'){ 58 if(s[j+3]=='/'&&s[j+4]=='/')cout<<'.',j+=4; 59 else cout<<' ',j+=2; 60 } 61 t=""; 62 } 63 cout<<endl; 64 } 65 return 0; 66 }
E3065 rxms写摘要
First AC: 2017-12-27 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,N,B,D; 4 int i,j,k; 5 struct data{ 6 string s; 7 double b,d,tf,idf,rst; 8 }a[1005]; 9 bool cmp(data a,data b) 10 { 11 if(a.rst-b.rst<0.000001&&b.rst-a.rst<0.000001){ 12 int tmp=a.s.length()<b.s.length()?a.s.length():b.s.length(); 13 for(int i=0;i<tmp;++i)if(a.s[i]-b.s[i])return a.s[i]<b.s[i]; 14 return a.s.length()<b.s.length(); 15 } 16 return a.rst>b.rst; 17 } 18 int main() 19 { 20 cin>>T; 21 for(i=1;i<=T;++i){ 22 cin>>N>>B>>D; 23 for(j=0;j<N;++j){ 24 cin>>a[j].s>>a[j].b>>a[j].d; 25 a[j].tf=a[j].b/B,a[j].idf=log(D/(a[j].d+1)); 26 a[j].rst=a[j].tf*a[j].idf; 27 } 28 int tmp=N>20?20:N; 29 sort(a,a+N,cmp); 30 cout<<"Case #"<<i<<":\n"<<a[0].s; 31 for(j=1;j<tmp;++j)cout<<' '<<a[j].s; 32 cout<<endl; 33 } 34 return 0; 35 }
E3068 rxms换气球
First AC: 2018-02-09 Latest Modification: 2018-02-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,i,a,b,c,s; 4 int main() 5 { 6 cin>>T; 7 for(i=1;i<=T;++i){ 8 cin>>a>>b>>c; 9 s=a+b+c; 10 a%=3,b%=3,c%=3; 11 if(a^b&&a+b+c==3)cout<<"Case #"<<i<<":"<<s-1<<endl; 12 else cout<<"Case #"<<i<<":"<<s<<endl; 13 } 14 return 0; 15 }
E3069 简单的矩形计数
First AC: 2018-02-15 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,a,b,c,d; 4 long rst; 5 bool f[100][100]; 6 int i,j; 7 int main() 8 { 9 cin>>n>>m; 10 for(i=0;i<n;++i)for(j=0;j<m;++j)cin>>f[i][j]; 11 for(a=1;a<n;++a)for(b=0;b<a;++b)for(c=1;c<m;++c)for(d=0;d<c;++d) 12 if(f[a][c]&&f[a][d]&&f[b][c]&&f[b][d])++rst; 13 cout<<rst; 14 return 0; 15 }
E3070 简单的数学问题
First AC: 2018-12-12 Latest Modification: 2018-12-12
Note: 我的想法是可以利用标准分解将$a^{b}$统一表示成$c^{d}$,使得$c$尽可能小
其他人的思路包括:计算$a^{b}%p$存$long long$、对$a^b$取对数存$double$、以及$python$啥都不做
本质上就是一一映射到可以存的结构中,然后去重计数
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long double ld; 4 int n; 5 int a,b,c,d; 6 map<int,int>mp; 7 int i; 8 bool ok(int a,int b,int i) 9 { 10 c=pow((ld)a,(ld)1.0/i)+0.3; 11 d=i*b; 12 int r=1; 13 for(int j=0;j<i;++j)r*=c; 14 return r==a; 15 } 16 int main() 17 { 18 while(cin>>n){ 19 mp.clear(); 20 for(a=2;a<=100;++a){ 21 for(b=2;b<=n;++b){ 22 bool flag=1; 23 for(i=6;i;--i){ 24 if(ok(a,b,i)){ 25 mp.insert(pair<int,int>(1000*c+d,0)); 26 flag=0; 27 break; 28 } 29 } 30 } 31 } 32 cout<<mp.size()<<endl; 33 } 34 return 0; 35 }
E3072 小巴菲特买股票
First AC: 2018-01-01 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,low,m,rst; 4 int i,j,k; 5 int main() 6 { 7 cin>>T; 8 for(i=0;i<T;++i){ 9 cin>>n>>low; 10 rst=0; 11 for(j=1;j<n;++j){ 12 cin>>m; 13 if(m<low)low=m; 14 else if(m>low)rst=m-low>rst?m-low:rst; 15 } 16 cout<<"case #"<<i<<":\n"<<rst<<endl; 17 } 18 return 0; 19 }
E3073 道路排序
First AC: 2018-01-06 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 struct data{ 4 int a,b,c; 5 }a[19901]; 6 int T,m,n,i,j; 7 bool cmp(data a,data b) 8 { 9 if(a.c^b.c)return a.c>b.c; 10 if(a.a^b.a)return a.a<b.a; 11 return a.b<b.b; 12 } 13 int main() 14 { 15 cin>>T; 16 for(i=0;i<T;++i){ 17 cin>>m>>n; 18 for(j=0;j<n;++j)cin>>a[j].a>>a[j].b>>a[j].c; 19 sort(a,a+n,cmp); 20 cout<<"case #"<<i<<":\n"; 21 for(j=0;j<n;++j)printf("(%d,%d,%d)\n",a[j].a,a[j].b,a[j].c); 22 } 23 return 0; 24 }
E3074 特殊加密
First AC: 2017-11-15 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len; 4 int i,j; 5 string s,a[26]={"..---/.----","..---/..---","..---/...--", 6 "...--/.----","...--/..---","...--/...--", 7 "....-/.----","....-/..---","....-/...--", 8 "...../.----","...../..---","...../...--", 9 "-..../.----","-..../..---","-..../...--", 10 "--.../.----","--.../..---","--.../...--", 11 "--.../....-","---../.----","---../..---", 12 "---../...--","----./.----","----./..---", 13 "----./...--","----./....-"}; 14 int main() 15 { 16 cin>>T; 17 for(i=0;i<T;++i){ 18 cin>>s; 19 len=s.length(); 20 cout<<"case #"<<i<<":\n"<<a[s[0]-'A']; 21 for(j=1;j<len;++j)cout<<"/"<<a[s[j]-'A']; 22 cout<<endl; 23 } 24 return 0; 25 }
E3075 素进制链表
First AC: 2018-07-11 Latest Modification: 2018-07-11
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll base[]={1,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47}; 5 ll rst[16]; 6 ll T,n; 7 ll cas,i; 8 int main() 9 { 10 for(i=1;i<16;++i)base[i]*=base[i-1]; 11 cin>>T; 12 for(cas=0;cas<T;++cas){ 13 cin>>n; 14 memset(rst,0,sizeof(rst)); 15 for(i=15;i;--i){ 16 if(n>=base[i])rst[i]=n/base[i]; 17 n%=base[i]; 18 } 19 rst[0]=n; 20 cout<<"case #"<<cas<<":\n"; 21 for(i=15;i;--i)if(rst[i])break; 22 for(;i+1;--i)cout<<rst[i]<<";"; 23 cout<<endl; 24 } 25 return 0; 26 }
E3076 斐波那契数列
First AC: 2018-01-08 Latest Modification: 2018-01-08
1 #include<iostream> 2 using namespace std; 3 int T,n; 4 string f[125]={"0","1","1","2","3","5","8","13", 5 "21", 6 "34", 7 "55", 8 "89", 9 "144", 10 "233", 11 "377", 12 "610", 13 "987", 14 "1597", 15 "2584", 16 "4181", 17 "6765", 18 "10946", 19 "17711", 20 "28657", 21 "46368", 22 "75025", 23 "121393", 24 "196418", 25 "317811", 26 "514229", 27 "832040", 28 "1346269", 29 "2178309", 30 "3524578", 31 "5702887", 32 "9227465", 33 "14930352", 34 "24157817", 35 "39088169", 36 "63245986", 37 "102334155", 38 "165580141", 39 "267914296", 40 "433494437", 41 "701408733", 42 "1134903170", 43 "1836311903", 44 "2971215073", 45 "4807526976", 46 "7778742049", 47 "12586269025", 48 "20365011074", 49 "32951280099", 50 "53316291173", 51 "86267571272", 52 "139583862445", 53 "225851433717", 54 "365435296162", 55 "591286729879", 56 "956722026041", 57 "1548008755920", 58 "2504730781961", 59 "4052739537881", 60 "6557470319842", 61 "10610209857723", 62 "17167680177565", 63 "27777890035288", 64 "44945570212853", 65 "72723460248141", 66 "117669030460994", 67 "190392490709135", 68 "308061521170129", 69 "498454011879264", 70 "806515533049393", 71 "1304969544928657", 72 "2111485077978050", 73 "3416454622906707", 74 "5527939700884757", 75 "8944394323791464", 76 "14472334024676221", 77 "23416728348467685", 78 "37889062373143906", 79 "61305790721611591", 80 "99194853094755497", 81 "160500643816367088", 82 "259695496911122585", 83 "420196140727489673", 84 "679891637638612258", 85 "1100087778366101931", 86 "1779979416004714189", 87 "2880067194370816120", 88 "4660046610375530309", 89 "7540113804746346429", 90 "12200160415121876738", 91 "19740274219868223167", 92 "31940434634990099905", 93 "51680708854858323072", 94 "83621143489848422977", 95 "135301852344706746049", 96 "218922995834555169026", 97 "354224848179261915075", 98 "573147844013817084101", 99 "927372692193078999176", 100 "1500520536206896083277", 101 "2427893228399975082453", 102 "3928413764606871165730", 103 "6356306993006846248183", 104 "10284720757613717413913", 105 "16641027750620563662096", 106 "26925748508234281076009", 107 "43566776258854844738105", 108 "70492524767089125814114", 109 "114059301025943970552219", 110 "184551825793033096366333", 111 "298611126818977066918552", 112 "483162952612010163284885", 113 "781774079430987230203437", 114 "1264937032042997393488322", 115 "2046711111473984623691759", 116 "3311648143516982017180081", 117 "5358359254990966640871840"}; 118 int main() 119 { 120 cin>>T; 121 for(int i=0;i<T;++i){ 122 cin>>n; 123 cout<<"case #"<<i<<":\n"<<f[n]<<endl; 124 } 125 return 0; 126 }
E3081 购房还款
First AC: 2017-10-18 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 double d,p,r; 4 int main() 5 { 6 cin>>d>>p>>r; 7 cout<<(long)(0.5+log10(p/(p-d*r/100))/log10(1+r/100)); 8 return 0; 9 }
E3082 公式计算
First AC: 2017-10-18 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int x; 4 int main() 5 { 6 cin>>x; 7 if(x<1)cout<<x<<endl; 8 else if(x<10)cout<<2*x-1<<endl; 9 else cout<<3*x-11<<endl; 10 return 0; 11 }
E3084 最大公约数
First AC: 2017-10-17 Latest Modification: 2018-12-20
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a,b,c; 4 int main() 5 { 6 cin>>a>>b; 7 c=1; 8 while(c)c=a%b,a=b,b=c; 9 cout<<a; 10 return 0; 11 }
E3085 统计1的个数
First AC: 2017-10-15 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,i,c; 4 int main() 5 { 6 cin>>n; 7 for(i=1;i<35;i++)if(n%2==1)c++,n/=2; 8 cout<<c; 9 return 0; 10 }
E3086 水仙花数
First AC: 2018-01-05 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int i; 4 int main() 5 { 6 for(i=100;i<1000;++i) 7 if(pow(i/100,3)+pow(i%100/10,3)+pow(i%10,3)==i) 8 cout<<i<<endl; 9 return 0; 10 }
E3087 牛顿法求解方程
First AC: 2017-12-16 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int cnt; 4 double x=1.5; 5 double k(double x){return 6*x*x-8*x+3;} 6 double rst(double x){return (2*x*x+3)*(x-2);} 7 int main() 8 { 9 do{ 10 ++cnt; 11 x-=rst(x)/k(x); 12 }while(rst(x)>1e-15||-rst(x)>1e-15); 13 printf("%.2f %d",x,cnt); 14 return 0; 15 }
E3088 循环移位
First AC: 2017-11-16 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s; 4 int n,ls,m,cnt; 5 int i,j; 6 int main() 7 { 8 cin>>s>>n,ls=s.length(),m=n%ls; 9 cout<<ls<<' '; 10 for(i=ls-m;i<ls;++i){ 11 if(cnt==0&&s[i]!='0')++cnt; 12 if(cnt==1)cout<<s[i]; 13 } 14 ls-=m; 15 for(i=0;i<ls;++i){ 16 if(cnt==0&&s[i]!='0')++cnt; 17 if(cnt==1)cout<<s[i]; 18 } 19 return 0; 20 }
E3090 杨辉三角
First AC: 2017-10-22 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a[50][50]; 4 int n,i,j; 5 int main() 6 { 7 a[1][1]=1; 8 a[2][1]=1; 9 a[2][2]=1; 10 for(i=3;i<35;i++){ 11 a[i][1]=1; 12 a[i][i]=1; 13 for(j=2;j<i;j++){ 14 a[i][j]=a[i-1][j-1]+a[i-1][j]; 15 } 16 } 17 cin>>n; 18 for(i=1;i<=n;i++){ 19 cout<<a[i][1]; 20 for(j=2;j<=i;j++)cout<<" "<<a[i][j]; 21 cout<<"\n"; 22 } 23 cout<<"\n"; 24 return 0; 25 }
E3092 GDP
First AC: 2017-11-01 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,r; 4 int main() 5 { 6 cin>>n>>r; 7 printf("%.2f",pow(1+r/100.0,n)); 8 return 0; 9 }
E3093 最大公约数
First AC: 2017-10-17 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a,b,c; 4 int main() 5 { 6 cin>>a>>b>>c; 7 cout<<__gcd(__gcd(a,b),c); 8 return 0; 9 }
E3094 反序输出
First AC: 2017-11-04 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,a[20],sum; 4 int i; 5 int main() 6 { 7 cin>>n; 8 for(i=0;i<n;++i)cin>>a[i],sum+=a[i]; 9 for(i=n-1;i>0;--i)cout<<a[i]<<" "; 10 cout<<a[0]<<endl<<sum; 11 printf(" %.2f",sum*1.0/n); 12 return 0; 13 }
E3096 字母数字对应
First AC: 2017-11-17 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s; 4 int i; 5 int main() 6 { 7 cin>>s; 8 for(i=0;i<8;++i)switch(s[i]){ 9 case 'A':case 'B':case 'C':case 'a':case 'b':case 'c': 10 cout<<2;break; 11 case 'D':case 'E':case 'F':case 'd':case 'e':case 'f': 12 cout<<3;break; 13 case 'G':case 'H':case 'I':case 'g':case 'h':case 'i': 14 cout<<4;break; 15 case 'J':case 'K':case 'L':case 'j':case 'k':case 'l': 16 cout<<5;break; 17 case 'M':case 'N':case 'O':case 'm':case 'n':case 'o': 18 cout<<6;break; 19 case 'T':case 'U':case 'V':case 't':case 'u':case 'v': 20 cout<<8;break; 21 case 'P':case 'Q':case 'R':case 'S':case 'p':case 'q': 22 case 'r':case 's': 23 cout<<7;break; 24 default:cout<<9; 25 } 26 cout<<endl; 27 return 0; 28 }
E3097 字符串排序
First AC: 2017-12-30 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 struct data{ 5 string s; 6 int len; 7 }a[11]; 8 bool cmp(data a,data b) 9 { 10 int tmp=a.len<b.len? a.len:b.len; 11 for(int i=0;i<tmp;++i)if(a.s[i]-b.s[i])return a.s[i]>b.s[i]; 12 return a.len>b.len; 13 } 14 int main() 15 { 16 cin>>n,getchar(); 17 for(int i=0;i<n;++i)getline(cin,a[i].s),a[i].len=a[i].s.length(); 18 sort(a,a+n,cmp); 19 for(int i=0;i<n;++i)cout<<a[i].s<<endl; 20 return 0; 21 }
E3098 字符串的交织
First AC: 2017-11-20 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s,t; 4 int ls,lt; 5 int i; 6 int main() 7 { 8 cin>>s>>t; 9 ls=s.length(),lt=t.length(); 10 if(ls==lt)for(i=0;i<ls;++i)cout<<s[i]<<t[i]; 11 else if(ls>lt){ 12 for(i=0;i<lt;++i)cout<<s[i]<<t[i]; 13 for(;i<ls;)cout<<s[i++]; 14 } 15 else{ 16 for(i=0;i<ls;++i)cout<<s[i]<<t[i]; 17 for(;i<lt;)cout<<t[i++]; 18 } 19 return 0; 20 }
E3101 矩阵转置
First AC: 2017-12-06 Latest Modification: 2017-12-06
1 #include<iostream> 2 using namespace std; 3 int n,i,j; 4 int a[11][11]; 5 int main() 6 { 7 cin>>n; 8 for(i=0;i<n;++i)for(j=0;j<n;++j)cin>>a[i][j]; 9 for(i=0;i<n;++i){ 10 for(j=0;j<n-1;++j)cout<<a[j][i]<<' '; 11 cout<<a[n-1][i]<<endl; 12 } 13 return 0; 14 }
E3106 骰子概率
First AC: 2018-01-21 Latest Modification: 2018-01-21
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m; 4 int a[6]; 5 int succ,fail,jdg; 6 long i,j; 7 int main() 8 { 9 cin>>n>>m; 10 for(i=0;i<100000;++i){ 11 memset(a,0,sizeof(a)); 12 for(j=0;j<n;++j)++a[rand()%6]; 13 for(j=jdg=0;j<6;++j){ 14 if(a[j]>=m){jdg=1;break;} 15 } 16 jdg? ++succ:++fail; 17 } 18 printf("%.2f",succ*1.0/(succ+fail)); 19 return 0; 20 }
E3107 数据交换
First AC: 2017-11-23 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,M,x,y; 4 int a[15]; 5 int i; 6 int main() 7 { 8 cin>>n; 9 x=0,y=10000; 10 m=0,M=n-1; 11 for(i=0;i<n;++i){ 12 cin>>a[i]; 13 if(a[i]<y)m=i,y=a[i]; 14 if(a[i]>x)M=i,x=a[i]; 15 } 16 if(m==n-1&&M==0)a[0]=y,a[n-1]=x; 17 else if(m==n-1){ 18 a[0]^=a[m],a[m]^=a[0],a[0]^=a[m]; 19 a[n-1]^=a[M],a[M]^=a[n-1],a[n-1]^=a[M]; 20 } 21 else if(M==0){ 22 a[n-1]^=a[M],a[M]^=a[n-1],a[n-1]^=a[M]; 23 a[0]^=a[m],a[m]^=a[0],a[0]^=a[m]; 24 } 25 else{ 26 if(m-0)a[0]^=a[m],a[m]^=a[0],a[0]^=a[m]; 27 if(M^(n-1))a[n-1]^=a[M],a[M]^=a[n-1],a[n-1]^=a[M]; 28 } 29 cout<<a[0]; 30 for(i=1;i<n;++i)cout<<' '<<a[i]; 31 return 0; 32 }
E3119 排序
First AC: 2017-10-17 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a[1000],i,j,m,count; 4 int main() 5 { 6 for(i=0;i<3;i++){ 7 cin>>m; 8 a[m]++; 9 } 10 for(i=0;i<1000;i++){ 11 if(a[i]!=0){ 12 count++; 13 if(count==3){ 14 for(j=0;j<a[i];j++)cout<<i; 15 break; 16 } 17 else for(j=0;j<a[i];j++)cout<<i<<" "; 18 } 19 } 20 return 0; 21 }
E3120 整除
First AC: 2017-10-17 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 int main() 5 { 6 cin>>n; 7 if(n%15==0)cout<<"Yes"; 8 else cout<<"No"; 9 return 0; 10 }
E3121 素数
First AC: 2017-10-17 Latest Modification: 2018-06-08
Note: 坑数据,最后一个数字后面有一个空格
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,i,count; 4 int main() 5 { 6 cout<<"101 "; 7 for(n=103;n<200;n+=2){ 8 for(i=3;i<=sqrt(n);i++)if(n%i==0){count++;break;} 9 if(count==0)cout<<n<<" "; 10 count=0; 11 } 12 return 0; 13 }
E3122 最大公约数
First AC: 2017-10-17 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int m,n; 4 int main() 5 { 6 cin>>m>>n; 7 cout<<__gcd(m,n); 8 return 0; 9 }
E3123 二次方程的根
First AC: 2017-10-17 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int main() 4 { 5 ios::sync_with_stdio(false); 6 double sqrtdelta,delta; 7 int a,b,c; 8 while(cin>>a>>b>>c){ 9 if(a<0)a=-a,b=-b,c=-c; 10 delta=b*b-4*a*c; 11 if(delta==0)printf("%.6f\n",-(float)b/(2*a)); 12 else{ 13 sqrtdelta=sqrt(delta); 14 printf("%.6f %.6f\n",-(b+sqrtdelta)/(a<<1) 15 ,(sqrtdelta-b)/(2*a)); 16 } 17 } 18 return 0; 19 }
E3124 单词表
First AC: 2018-03-15 Latest Modification: 2018-03-15
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len,num; 4 string s,t; 5 struct data{ 6 string s; 7 }a[250]; 8 int i,j; 9 bool cmp(data a,data b) 10 { 11 return a.s<b.s; 12 } 13 int main() 14 { 15 cin>>T; 16 getchar(); 17 for(i=0;i<T;i++){ 18 getline(cin,s); 19 len=s.length(); 20 num=0; 21 for(j=0;j<len;++j){ 22 if(isalpha(s[j]))t+=s[j]; 23 else if(t!="") a[num++].s=t,t=""; 24 } 25 if(t!="") a[num++].s=t,t=""; 26 sort(a,a+num,cmp); 27 cout<<"case #"<<i<<":\n"<<a[0].s; 28 for(j=1;j<num;++j)if(a[j].s!=a[j-1].s)cout<<' '<<a[j].s; 29 cout<<endl; 30 } 31 return 0; 32 }
E3125 巧克力
First AC: 2017-11-06 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,cnt,num; 4 long long s; 5 int a[105]; 6 int i,j,k; 7 int main() 8 { 9 cin>>T; 10 for(i=0;i<T;++i){ 11 s=1,num=0,memset(a,0,sizeof(a)); 12 cin>>n; 13 cout<<"case #"<<i<<":\n"; 14 for(j=0;j<n;++j){cin>>a[j];if(a[j]==1)++num;} 15 if(num==0){cout<<"0\n";continue;} 16 for(j=0;j<n;++j){ 17 cnt=2; 18 if(a[j]==0){ 19 for(k=j+1;k<n;++k){ 20 if(a[j]==a[k])++cnt; 21 else break; 22 } 23 if(k==n)break; 24 if(j==0){j=k-1;continue;}; 25 s*=cnt,j=k-1; 26 } 27 } 28 cout<<s<<endl; 29 } 30 return 0; 31 }
E3126 商品推荐
First AC: 2017-12-23 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,cnt; 4 long samt[101],mamt; 5 double spri[101],mpri; 6 int i,j,k; 7 struct data{ 8 string num; 9 long amt; 10 double pri; 11 bool flag; 12 int rank; 13 }a[101]; 14 bool cmp(data a,data b) 15 { 16 if(a.flag-b.flag)return a.flag>b.flag; 17 if(a.amt^b.amt)return a.amt>b.amt; 18 if(a.pri-b.pri)return a.pri<b.pri; 19 return a.rank<b.rank; 20 } 21 int main() 22 { 23 cin>>T; 24 for(i=0;i<T;++i){ 25 cin>>n; 26 for(j=0;j<n;++j){ 27 a[j].rank=j; 28 cin>>a[j].num>>a[j].amt>>a[j].pri; 29 samt[j]=a[j].amt; 30 spri[j]=a[j].pri; 31 } 32 sort(samt,samt+n); 33 sort(spri,spri+n); 34 if(n&1)mamt=samt[n/2],mpri=spri[n/2]; 35 else{ 36 mamt=(samt[n/2]+samt[n/2-1])/2.0; 37 mpri=(spri[n/2]+spri[n/2-1])/2.0; 38 } 39 for(cnt=j=0;j<n;++j){ 40 if(a[j].amt>mamt&&a[j].pri<mpri) 41 a[j].flag=1,++cnt; 42 else a[j].flag=0; 43 } 44 cout<<"case #"<<i<<":\n"; 45 if(!cnt){ 46 cout<<"no recommendation\n"; 47 continue; 48 } 49 sort(a,a+n,cmp); 50 for(j=0;j<cnt;++j) 51 cout<<a[j].num<<' '<<a[j].amt<<' '<<a[j].pri<<endl; 52 } 53 return 0; 54 }
E3127 字串间距
First AC: 2017-12-22 Latest Modification: 2018-03-15
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,ls,lt,lr; 4 int ns[85],nt[85],cnts,cntt,rst; 5 string s,t,r; 6 bool flag,flags,flagt; 7 int i,j,k; 8 int main() 9 { 10 cin>>T; 11 for(i=0;i<T;++i){ 12 cin>>s>>t>>r; 13 ls=s.length(),lt=t.length(),lr=r.length(); 14 memset(ns,0,sizeof(ns)),memset(nt,0,sizeof(nt)); 15 flags=flagt=cnts=cntt=0; 16 for(j=0;j<=lr-ls;++j){ 17 if(r[j]==s[0]){ 18 for(flag=k=1;k<ls;++k)if(r[j+k]!=s[k]){flag=0;break;} 19 if(flag)ns[cnts++]=j,flags=1; 20 } 21 } 22 for(j=0;j<=lr-lt;++j){ 23 if(r[j]==t[0]){ 24 for(flag=k=1;k<lt;++k)if(r[j+k]!=t[k]){flag=0;break;} 25 if(flag)nt[cntt++]=j,flagt=1; 26 } 27 } 28 cout<<"case #"<<i<<":\n"; 29 if(flags&&flagt){ 30 sort(ns,ns+cnts),sort(nt,nt+cntt); 31 if(ns[cnts-1]<=nt[0])rst=max(0,nt[cntt-1]-ns[0]-ls); 32 else if(nt[cntt-1]<=ns[0])rst=max(0,ns[cnts-1]-nt[0]-lt); 33 else rst=max(0,max(ns[cnts-1]-nt[0]-lt,nt[cntt-1]-ns[0]-ls)); 34 cout<<rst<<endl; 35 } 36 else cout<<"0\n"; 37 } 38 return 0; 39 }
E3128 十进制数列项
First AC: 2017-12-23 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len,cnt,flag; 4 string s; 5 int a[25]; 6 int i,j; 7 int main() 8 { 9 cin>>T; 10 for(i=0;i<T;++i){ 11 cin>>s; 12 len=s.length(),cnt=flag=0; 13 for(j=len-1;;--j){ 14 a[cnt++]=s[j]-'0'; 15 if(j==0){flag=1;break;} 16 if(s[j]>s[j-1])break; 17 } 18 cout<<"case #"<<i<<":\n"; 19 if(flag){ 20 sort(a,a+cnt); 21 for(j=0;;++j)if(a[j]){ 22 cout<<a[j]<<0; 23 a[j]=0; 24 break; 25 } 26 sort(a,a+cnt); 27 for(j=1;j<cnt;++j)cout<<a[j]; 28 cout<<endl; 29 continue; 30 } 31 sort(a,a+cnt); 32 for(j=0;j<len-cnt-1;++j)cout<<s[j]; 33 for(j=0;j<cnt;++j)if(a[j]>s[len-cnt-1]-'0'){ 34 cout<<a[j];a[j]=s[len-cnt-1]-'0'; 35 break; 36 } 37 sort(a,a+cnt); 38 for(j=0;j<cnt;++j)cout<<a[j]; 39 cout<<endl; 40 } 41 return 0; 42 }
E3129 最大最小之差
First AC: 2017-12-10 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len,cnt,flag,tmp,num; 4 string s; 5 int a[10],M[106],m[106],r[106]; 6 int i,j,k; 7 int main() 8 { 9 cin>>T; 10 for(i=0;i<T;++i){ 11 cin>>s; 12 len=s.length(); 13 memset(a,0,40),memset(M,0,424); 14 memset(m,0,424),memset(r,0,424); 15 for(j=0;j<len;++j)a[s[j]-'0']++; 16 for(cnt=105,j=0;j<10;++j)for(k=0;k<a[j];++k)M[cnt--]=j; 17 for(cnt=105,j=9;j>=0;--j)for(k=0;k<a[j];++k)m[cnt--]=j; 18 for(flag=num=0,j=105;j;--j){ 19 tmp=M[j]-m[j]-flag; 20 if(tmp<0)r[j]=10+tmp,flag=1; 21 else r[j]=tmp,flag=0; 22 if(tmp)num=1; 23 } 24 cout<<"case #"<<i<<":\n"; 25 if(num==0){cout<<0<<endl;continue;} 26 for(j=0;;++j)if(r[j])break; 27 for(k=j;k<106;++k)cout<<r[k]; 28 cout<<endl; 29 } 30 return 0; 31 }
E3130 方形码
First AC: 2017-12-23 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len,cnt,tmp,num; 4 string s; 5 int a[205]; 6 int i,j,k; 7 int main() 8 { 9 cin>>T,getchar(); 10 for(i=0;i<T;++i){ 11 getline(cin,s); 12 len=s.length(); 13 for(cnt=j=0;j<len;++j)if(s[j]-' ')a[cnt++]=s[j]; 14 tmp=(int)sqrt(cnt); 15 if(tmp*tmp^cnt)++tmp; 16 cout<<"case #"<<i<<":\n"; 17 for(num=j=0;;++j){ 18 for(k=0;j+tmp*k<cnt;++k){ 19 cout<<(char)a[j+tmp*k],++num; 20 } 21 if(num-cnt)cout<<' '; 22 else{cout<<endl;break;} 23 } 24 } 25 return 0; 26 }
E3131 字母对的频率
First AC: 2017-12-23 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len,cnt,rst,maxa; 4 string s; 5 int a[26][26]; 6 int i,j; 7 int main() 8 { 9 cin>>T,getchar(); 10 for(i=0;i<T;++i){ 11 getline(cin,s); 12 len=s.length(); 13 memset(a,0,sizeof(a)); 14 for(cnt=maxa=j=0;j<len;++j)if(s[j]>='A'&&s[j]<='Z')s[j]+=32; 15 for(j=1;j<len;++j) 16 if(s[j-1]>='a'&&s[j-1]<='z'&&s[j]>='a'&&s[j]<='z'){ 17 ++cnt,a[s[j-1]-'a'][s[j]-'a']++; 18 if(a[s[j-1]-'a'][s[j]-'a']>maxa) 19 maxa=a[s[j-1]-'a'][s[j]-'a'],rst=j; 20 } 21 cout<<"case #"<<i<<":\n"<<s[rst-1]<<s[rst]; 22 printf(" %.2f%%\n",a[s[rst-1]-'a'][s[rst]-'a']*100.0/cnt); 23 } 24 return 0; 25 }
E3132 寻宝
First AC: 2017-11-11 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a[6][6],b[6][6],c[25],r,q,tr,tq,cnt; 4 int T; 5 int i,j; 6 int main() 7 { 8 cin>>T; 9 for(i=0;i<T;++i){ 10 memset(b,0,sizeof(b)); 11 memset(c,0,sizeof(c)); 12 for(j=1;j<6;++j) 13 cin>>a[j][1]>>a[j][2]>>a[j][3]>>a[j][4]>>a[j][5]; 14 cout<<"case #"<<i<<":\n"; 15 r=q=0,a[0][0]=11,cnt=0; 16 while(1){ 17 tr=a[r][q]/10,tq=a[r][q]%10; 18 if(r==tr&&q==tq){ 19 cout<<a[1][1]; 20 for(j=1;j<cnt;++j)cout<<' '<<c[j]; 21 break; 22 } 23 if(b[tr][tq]==0){ 24 ++b[tr][tq]; 25 c[cnt++]=a[tr][tq]; 26 r=tr; 27 q=tq; 28 } 29 else{ 30 cout<<"-1"; 31 break; 32 } 33 } 34 cout<<endl; 35 } 36 return 0; 37 }
E3133 最长回文子串
First AC: 2017-12-23 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len,tmp,rst1,rst2,cnt; 4 string s; 5 int i,j,k; 6 int main() 7 { 8 cin>>T,getchar(); 9 for(i=0;i<T;++i){ 10 getline(cin,s); 11 len=s.length()-1,rst1=rst2=0; 12 for(j=1;j<len;++j){ 13 tmp=min(j,len-j)+1; 14 for(cnt=k=1;k<tmp;++k){ 15 if(s[j-k]!=s[j+k])break; 16 ++cnt; 17 } 18 if(cnt>rst1)rst1=cnt; 19 } 20 for(j=1;j<=len;++j){ 21 if(s[j]==s[j-1]){ 22 tmp=min(j,len-j+1); 23 for(cnt=k=1;k<tmp;++k){ 24 if(s[j-1-k]!=s[j+k])break; 25 ++cnt; 26 } 27 if(cnt>rst2)rst2=cnt; 28 } 29 } 30 cout<<"case #"<<i<<":\n"<<max(1,max(2*rst1-1,2*rst2))<<endl; 31 } 32 return 0; 33 }
E3134 短信激活码
First AC: 2017-11-04 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n; 4 long long m; 5 string s; 6 int i,j; 7 int main() 8 { 9 cin>>T; 10 for(i=0;i<T;++i){ 11 cin>>s; 12 cout<<"case #"<<i<<":\n"; 13 m=1; 14 n=(s[0]-'0')*10000+(s[2]-'0')*1000 15 +(s[4]-'0')*100+(s[3]-'0')*10+s[1]-'0'; 16 for(j=0;j<5;++j)m*=n,m%=100000; 17 if(m/10000==0)cout<<"0"; 18 if(m/1000==0)cout<<"0"; 19 if(m/100==0)cout<<"0"; 20 if(m/10==0)cout<<"0"; 21 cout<<m<<endl; 22 } 23 return 0; 24 }
E3135 数据压缩
First AC: 2017-10-23 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len,cnt; 4 string s; 5 int i,j; 6 int main() 7 { 8 cin>>T; 9 for(i=0;i<T;++i){ 10 cin>>s; 11 cout<<"case #"<<i<<":\n"; 12 len=s.length(); 13 cnt=1; 14 for(j=1;j<=len-1;++j){ 15 if(s[j]==s[j-1])cnt++; 16 else if(cnt>255){ 17 cout<<"255"<<s[j-1]<<cnt-255<<s[j-1]; 18 cnt=1; 19 } 20 else cout<<cnt<<s[j-1],cnt=1; 21 } 22 if(cnt>255)cout<<"255"<<s[len-1]<<cnt-255<<s[len-1]<<endl; 23 else cout<<cnt<<s[len-1]<<endl; 24 } 25 return 0; 26 }
E3136 指数比例
First AC: 2018-01-06 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 #define e 2.71828 3 using namespace std; 4 int T,m,cnt; 5 double sumn; 6 struct data{ 7 double n; 8 bool rst; 9 }a[51]; 10 int i,j,k; 11 bool cmp(data a,data b) 12 { 13 if(a.rst!=b.rst)return a.rst>b.rst; 14 return a.n>b.n; 15 } 16 int main() 17 { 18 cin>>T; 19 for(i=0;i<T;++i){ 20 cin>>m; 21 for(sumn=cnt=j=0;j<m;++j)cin>>a[j].n,sumn+=pow(e,a[j].n); 22 for(j=0;j<m;++j){ 23 if(pow(e,a[j].n)/sumn>0.5/m)a[j].rst=1,++cnt; 24 else a[j].rst=0; 25 } 26 sort(a,a+m,cmp); 27 cout<<"case #"<<i<<":\n"; 28 for(j=0;j<cnt;++j)printf("%.2f\n",a[j].n); 29 } 30 return 0; 31 }
E3137 矩形个数
First AC: 2018-05-11 Latest Modification: 2018-05-11
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,r,c,n,k,x,y,rst; 4 int mp[11][11]; 5 int i,j,l; 6 int cnt(int x,int y) 7 { 8 int ret=0,i,j,l; 9 for(i=x;i<=r;++i){ 10 int tmp=0; 11 for(j=y;j<=c;++j){ 12 for(l=x;l<=i;++l) 13 if(mp[l][j]) 14 ++tmp; 15 if(tmp>=k){ 16 ret+=c-j+1; 17 break; 18 } 19 } 20 } 21 return ret; 22 } 23 int main() 24 { 25 cin>>T; 26 for(i=0;i<T;++i){ 27 cin>>r>>c>>n>>k; 28 memset(mp,0,sizeof(mp)); 29 while(n--){ 30 cin>>x>>y; 31 mp[x][y]=1; 32 } 33 rst=0; 34 for(j=1;j<=r;++j) 35 for(l=1;l<=c;++l) 36 rst+=cnt(j,l); 37 cout<<"case #"<<i<<":\n"<<rst<<endl; 38 } 39 return 0; 40 }
E3138 Base64 编码
First AC: 2018-06-08 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,cnt; 4 char s[100]; 5 int rst[150]; 6 int cas,i,j; 7 int main() 8 { 9 cin>>T; 10 for(cas=0;cas<T;++cas) 11 { 12 cin>>s; 13 int l=strlen(s); 14 cnt=0; 15 for(i=j=0;i<l;++i) 16 { 17 if(cnt==0) 18 { 19 rst[j++]=s[i]/4; 20 rst[j]=s[i]%4*16; 21 } 22 else if(cnt==1) 23 { 24 rst[j++]+=s[i]/16; 25 rst[j]=s[i]%16*4; 26 } 27 else 28 { 29 rst[j++]+=s[i]/64; 30 rst[j++]=s[i]%64; 31 } 32 cnt=++cnt%3; 33 } 34 if(j%4)for(j++;j%4;j++)rst[j]=64; 35 cout<<"case #"<<cas<<":\n"; 36 for(i=0;i<j;++i) 37 if(rst[i]<26)cout<<(char)('A'+rst[i]); 38 else if(rst[i]<52)cout<<(char)('a'+rst[i]-26); 39 else if(rst[i]<62)cout<<(char)('0'+rst[i]-52); 40 else if(rst[i]==62)cout<<'+'; 41 else if(rst[i]==63)cout<<'/'; 42 else cout<<'='; 43 cout<<endl;; 44 } 45 return 0; 46 }
E3139 鸡兔同笼
First AC: 2017-10-17 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 long i,m,n; 4 int main() 5 { 6 cin>>i;i=0; 7 while(cin>>n>>m){ 8 if(m>=2*n&&m<=4*n&&m%2==0) 9 cout<<(4*n-m)/2<<" "<<(m-2*n)/2<<endl; 10 else cout<<"No answer\n"; 11 i++; 12 } 13 return 0; 14 }
E3140 另一种阶乘问题
First AC: 2017-10-17 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int i,j,k,n,s=1,R; 4 int main() 5 { 6 cin>>i; 7 while(cin>>n){ 8 for(j=1;j<=n;j++){ 9 for(k=1;k<=j;k+=2)s*=k; 10 R+=s; 11 s=1; 12 } 13 cout<<R<<endl; 14 R=0; 15 } 16 return 0; 17 }
E3141 小学生算术
First AC: 2017-10-27 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 string m,n; 4 int a[1000],b[1000],cnt,num; 5 int i,j,k; 6 int main() 7 { 8 while(cin>>m>>n,m!="0"||n!="0"){ 9 cnt=num=0; 10 for(i=999,j=m.length()-1;j>=0;--j)a[i--]=(int)(m[j]-'0'); 11 for(i=999,j=n.length()-1;j>=0;--j)b[i--]=(int)(n[j]-'0'); 12 for(i=999;i>=0;--i){ 13 if(a[i]+b[i]+num>9)cnt++,num=1; 14 else num=0; 15 a[i]=b[i]=0; 16 } 17 cout<<cnt<<endl; 18 } 19 return 0; 20 }
E3142 九九乘法表
First AC: 2017-10-17 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,i,N,cnt; 4 int main() 5 { 6 cin>>T; 7 for(i=0;i<T;i++){ 8 cin>>N; 9 if(cnt>0)cout<<endl; 10 ++cnt; 11 cout<<"1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=9\n"; 12 if(N>1)cout<<"2*2=4 2*3=6 2*4=8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18\n"; 13 if(N>2)cout<<"3*3=9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27\n"; 14 if(N>3)cout<<"4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36\n"; 15 if(N>4)cout<<"5*5=25 5*6=30 5*7=35 5*8=40 5*9=45\n"; 16 if(N>5)cout<<"6*6=36 6*7=42 6*8=48 6*9=54\n"; 17 if(N>6)cout<<"7*7=49 7*8=56 7*9=63\n"; 18 if(N>7)cout<<"8*8=64 8*9=72\n"; 19 if(N>8)cout<<"9*9=81\n"; 20 } 21 return 0; 22 }
E3143 纯虚数的幂
First AC: 2018-01-17 Latest Modification: 2018-01-17
1 n=int(input()) 2 for i in range(0,n): 3 print('case #%d:'%(i)) 4 s,t=input().split() 5 s=(int)(s[:-1]) 6 t=(int)(t) 7 if t%4==0: 8 print(s**t) 9 elif t%4==1: 10 print('%dj'%(s**t)) 11 elif t%4==2: 12 print('-%d'%(s**t)) 13 else: 14 print('-%dj'%(s**t))
E3144 问候
First AC: 2017-10-31 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,i,j; 4 int h,m; 5 double s,sum; 6 int main() 7 { 8 cin>>T; 9 for(i=0;i<T;++i){ 10 cin>>n; 11 sum=0; 12 for(j=0;j<n;++j)scanf("%d:%d:%f",&h,&m,&s),sum+=3600*h+60*m+s; 13 while(sum>86400)sum-=86400; 14 cout<<"case #"<<i<<":\nGood "; 15 if(sum>=14400&&sum<43200)cout<<"morning!\n"; 16 else if(sum>=43200&&sum<64800)cout<<"afternoon!\n"; 17 else if(sum>=64800&&sum<79200)cout<<"evening!\n"; 18 else if(sum<14400||sum>=79200) 19 cout<<"night!\n"; 20 } 21 return 0; 22 }
E3145 互素勾股数
First AC: 2018-02-09 Latest Modification: 2018-02-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T; 4 long a,b,c; 5 long r,i,j,k; 6 long gcd(long a,long b) 7 { 8 long c=1; 9 while(c)c=a%b,a=b,b=c; 10 return a; 11 } 12 struct data{ 13 long a,b,c; 14 }num[11]; 15 bool cmp(data a,data b) 16 { 17 return a.a<b.a; 18 } 19 int main() 20 { 21 cin>>T; 22 for(r=0;r<T;++r){ 23 cin>>c; 24 long m=(long)(sqrt(c/2.0))+1; 25 for(i=1,k=0;i<m;++i){ 26 j=(long)(sqrt(c-i*i)); 27 if(i*i+j*j==c){ 28 a=abs(i*i-j*j),b=2*i*j; 29 if(a>b)a^=b,b^=a,a^=b; 30 if(gcd(a,b)==1&&gcd(a,c)==1&&gcd(b,c)==1) 31 num[k].a=a,num[k].b=b,num[k++].c=c; 32 } 33 } 34 sort(num,num+k,cmp); 35 cout<<"case #"<<r<<":\n"<<k<<endl; 36 for(i=0;i<k;++i) 37 cout<<num[i].a<<"^2+"<<num[i].b<<"^2="<<num[i].c<<"^2\n"; 38 } 39 return 0; 40 }
E3147 维吉尼亚密码
First AC: 2017-11-03 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,lenp,lens,cnt; 4 string pro,scrt; 5 int i,j; 6 int main() 7 { 8 cin>>T; 9 getchar(); 10 for(i=0;i<T;++i){ 11 getline(cin,pro);getline(cin,scrt); 12 cout<<"case #"<<i<<":\n"; 13 lenp=pro.length(),lens=scrt.length(),cnt=0; 14 for(j=0;j<lenp;++j) 15 if(pro[j]==' ')cout<<' ',++cnt; 16 else{ 17 cout<<(char)(((pro[j]+scrt[(j-cnt)% 18 lens]-'A')-'A')%26+'A'); 19 } 20 puts(""); 21 } 22 return 0; 23 }
E3148 线性表比较
First AC: 2018-04-14 Latest Modification: 2018-04-14
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,M; 4 int a[1000],b[1000]; 5 int i; 6 int main() 7 { 8 cin>>m>>n; 9 for(i=0;i<m;++i)cin>>a[i]; 10 for(i=0;i<n;++i)cin>>b[i]; 11 M=min(m,n); 12 bool jdg=1; 13 for(i=0;i<M;++i){ 14 if(a[i]<b[i]){ 15 cout<<"-1"; 16 jdg=0; 17 break; 18 } 19 else if(a[i]>b[i]){ 20 cout<<"1"; 21 jdg=0; 22 break; 23 } 24 } 25 if(jdg){ 26 if(m<n)cout<<"-1"; 27 else if(m<n)cout<<"1"; 28 else cout<<0; 29 } 30 return 0; 31 }
E3149 倒置顺序表
First AC: 2017-11-03 Latest Modification: 2017-11-03
1 #include<iostream> 2 using namespace std; 3 int N,i; 4 int main() 5 { 6 cin>>N; 7 int a[N]; 8 for(i=0;i<N;++i)cin>>a[i]; 9 for(i=N-1;i>0;i--)cout<<a[i]<<" "; 10 cout<<a[0]; 11 return 0; 12 }
E3150 线性表去重
First AC: 2017-11-22 Latest Modification: 2017-11-22
1 #include<iostream> 2 using namespace std; 3 int N,n,cnt; 4 int a[101]; 5 int i; 6 int main() 7 { 8 cin>>N; 9 while(N--){ 10 cin>>n; 11 if(a[n]==0)++a[n]; 12 } 13 for(i=0;i<101;++i) 14 if(a[i]>0){ 15 if(cnt==0)cout<<i,++cnt; 16 else cout<<' '<<i; 17 } 18 return 0; 19 }
E3151 循环打印
First AC: 2017-12-30 Latest Modification: 2017-12-30
1 #include<iostream> 2 using namespace std; 3 int num,bgn,step,cnt; 4 int a[105],b[105]; 5 int i,j,k,n; 6 int main() 7 { 8 cin>>num>>bgn>>step; 9 for(i=1;i<101;++i)a[i]=i; 10 for(i=bgn;;++i){ 11 if(i==num+1)i=0; 12 if(a[i]){ 13 if(++cnt==step)b[n++]=a[i],a[i]=0,cnt=0; 14 } 15 if(n==num)break; 16 } 17 cout<<b[0]; 18 for(i=1;i<num;++i)cout<<' '<<b[i]; 19 return 0; 20 }
E3158 特殊计算
First AC: 2017-11-08 Latest Modification: 2017-11-08
1 #include<iostream> 2 using namespace std; 3 long a,b,temp; 4 int main() 5 { 6 cin>>a>>b; 7 temp=a&&b; 8 cout<<temp<<endl; 9 temp=a&b; 10 cout<<temp; 11 return 0; 12 }
E3160 统计字符及行数
First AC: 2017-12-25 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s; 4 int len,maxl,numl,rst; 5 int main() 6 { 7 while(getline(cin,s)){ 8 len=s.length(); 9 if(len>maxl)maxl=len; 10 ++numl,rst+=len; 11 } 12 cout<<rst<<','<<numl<<','<<maxl; 13 return 0; 14 }
E3161 位运算
First AC: 2017-11-25 Latest Modification: 2017-11-25
1 #include<iostream> 2 using namespace std; 3 long long N,a[64]={1}; 4 int p,n,i; 5 int main() 6 { 7 for(i=1;i<64;++i)a[i]=2*a[i-1]; 8 cin>>N>>p>>n; 9 for(i=p;n--;--i)N&a[i]? N-=a[i]:N+=a[i]; 10 cout<<N; 11 return 0; 12 }
E3162 逻辑或、位或、位亦或
First AC: 2017-11-08 Latest Modification: 2017-11-08
1 #include<iostream> 2 using namespace std; 3 long a,b,t; 4 int main() 5 { 6 cin>>a>>b; 7 t=a||b; 8 cout<<t<<endl; 9 t=a|b; 10 cout<<t<<endl; 11 t=a^b; 12 cout<<t<<endl; 13 return 0; 14 }
E3163 二进制中1的占比
First AC: 2017-11-03 Latest Modification: 2017-12-04
1 #include<iostream> 2 using namespace std; 3 long n,i,a[32],s; 4 int main() 5 { 6 cin>>n; 7 for(i=0;n!=0;++i){ 8 a[i]=n%2,n/=2; 9 if(a[i]==1)++s; 10 } 11 if(s==0)cout<<"0,0:32"; 12 else if(s==32)cout<<"32,1:1"; 13 else { 14 cout<<s<<","; 15 for(i=16;i>=1;i/=2)if(s%i==0)break; 16 cout<<s/i<<":"<<32/i; 17 } 18 return 0; 19 }
E3167 去掉多余的空白符
First AC: 2017-12-09 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s; 4 int len,cnt,i; 5 int isspace(char c) 6 { 7 if(c!='\n'&&c!=' '&&c!='\t')return 0; 8 return 1; 9 } 10 int main() 11 { 12 while(getline(cin,s)){ 13 len=s.length(); 14 for(cnt=1,i=len-1;i>=0;--i){ 15 if(cnt){ 16 if(isspace(s[i])==1)s[i]='\n'; 17 else{cnt=0;break;} 18 } 19 } 20 if(cnt)continue; 21 for(cnt=1,i=0;i<len;++i){ 22 if(cnt){ 23 if(isspace(s[i])==1)s[i]='\n'; 24 else{cnt=0;break;} 25 } 26 } 27 for(i=0;i<len;++i){ 28 if(isspace(s[i])==0||(s[i]!='\n'&&isspace(s[i-1])==0)) 29 cout<<s[i]; 30 } 31 cout<<endl; 32 } 33 return 0; 34 }
E3169 字符串出现次数
First AC: 2017-11-09 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 #define N 80 3 using namespace std; 4 int count(char s[],char t[]) 5 { 6 int lens=strlen(s),lent=strlen(t),i,j,cnt=0,num=0,M=lens-lent; 7 for(i=0;i<=M;++i){ 8 if(s[i]==t[0]){ 9 for(j=1;j<lent;++j){ 10 if(s[i+j]!=t[j]){num++;break;} 11 } 12 if(num==0)cnt++,--i+=lent; 13 else num=0; 14 } 15 } 16 return cnt; 17 } 18 int main() 19 { char s[N+1],t[N+1]; 20 scanf("%s%s",s,t); 21 printf("%d\n",count(s,t)); 22 return 0; 23 }
E3170 二进制转十进制
First AC: 2017-11-07 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 #define N 32 3 using namespace std; 4 unsigned b2i(char s[]) 5 { 6 int len=strlen(s); 7 int i; 8 unsigned int m=0,n=1; 9 for(i=len-1;i>=0;--i){ 10 if(s[i]=='1')m+=n; 11 n*=2; 12 } 13 return m; 14 } 15 int main() 16 { 17 char s[N+1]; 18 scanf("%s",s); 19 printf("%u\n",b2i(s)); 20 return 0; 21 }
E3171 波兰表达式
First AC: 2017-12-16 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int len,flag,i,cnt; 4 char a[15],c; 5 double num,tmp1,tmp2; 6 int sig[50]; 7 stack<char>m; 8 stack<double>n; 9 int main() 10 { 11 while(1){ 12 cin>>a; 13 len=strlen(a); 14 flag=1; 15 if(len==1){ 16 switch(a[0]){ 17 case '+':case '-': case '*':case '/': 18 m.push(a[0]);flag=0;++cnt;break; 19 } 20 } 21 if(flag){ 22 num=atof(a); 23 n.push(num); 24 ++sig[cnt]; 25 } 26 while(sig[cnt]>1&&m.size()&&n.size()>1){ 27 tmp2=n.top(); 28 n.pop(); 29 tmp1=n.top(); 30 n.pop(); 31 c=m.top(); 32 m.pop(); 33 switch(c){ 34 case '+':n.push(tmp1+tmp2);break; 35 case '-':n.push(tmp1-tmp2);break; 36 case '*':n.push(tmp1*tmp2);break; 37 case '/':n.push(tmp1/tmp2);break; 38 } 39 sig[cnt]=0,++sig[--cnt]; 40 } 41 c=getchar(); 42 if(c!=' '){ 43 num=n.top(); 44 n.pop(); 45 printf("%.6f",num); 46 cnt=0; 47 return 0; 48 } 49 } 50 }
E3172 整数转化为字符串
First AC: 2017-11-20 Latest Modification: 2017-11-20
1 #include<iostream> 2 using namespace std; 3 string s; 4 int main() 5 { 6 cin>>s; 7 cout<<s<<endl; 8 return 0; 9 }
E3182 字符串排序
First AC: 2018-01-03 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n; 4 struct data{ 5 int num,len; 6 string s; 7 }a[101]; 8 int i,j,k; 9 bool cmp(data m,data n) 10 { 11 if(m.num^n.num)return m.num<n.num; 12 int tmp=m.len<n.len?m.len:n.len; 13 for(int i=0;i<tmp;++i)if(m.s[i]-n.s[i])return m.s[i]<n.s[i]; 14 return m.len<n.len; 15 } 16 int main() 17 { 18 cin>>T; 19 for(k=0;k<T;++k){ 20 cin>>n; 21 for(i=0;i<n;++i){ 22 cin>>a[i].s; 23 a[i].num=-1,a[i].len=a[i].s.length(); 24 for(j=0;j<a[i].len;++j) 25 if(a[i].s[j]>='0'&&a[i].s[j]<='9'){ 26 a[i].num=a[i].s[j]-'0'; 27 break; 28 } 29 } 30 sort(a,a+n,cmp); 31 cout<<"case #"<<k<<":\n"<<a[0].s; 32 for(i=1;i<n;++i)cout<<' '<<a[i].s; 33 cout<<endl; 34 } 35 return 0; 36 }
E3183 坐标排序
First AC: 2017-12-06 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n; 4 int i,j; 5 struct data{ 6 long long dis; 7 int x,y,qua,absx; 8 }a[101]; 9 bool cmp(data a,data b) 10 { 11 if(a.dis-b.dis)return a.dis<b.dis; 12 if(a.qua-b.qua)return a.qua<b.qua; 13 return a.absx<b.absx; 14 } 15 int main() 16 { 17 cin>>T; 18 for(i=0;i<T;++i){ 19 cin>>n; 20 for(j=0;j<n;++j){ 21 cin>>a[j].x>>a[j].y; 22 a[j].dis=a[j].x*a[j].x+a[j].y*a[j].y; 23 if(a[j].y>=0){ 24 if(a[j].x>=0)a[j].qua=1,a[j].absx=a[j].x; 25 else a[j].qua=2,a[j].absx=-a[j].x; 26 } 27 else{ 28 if(a[j].x>0)a[j].qua=4,a[j].absx=a[j].x; 29 else a[j].qua=3,a[j].absx=-a[j].x; 30 } 31 } 32 sort(a,a+n,cmp); 33 cout<<"case #"<<i<<":\n"; 34 for(j=0;j<n-1;++j)cout<<'('<<a[j].x<<','<<a[j].y<<')'<<' '; 35 cout<<'('<<a[n-1].x<<','<<a[n-1].y<<')'<<endl; 36 } 37 return 0; 38 }
E3184 二维数组排序
First AC: 2017-12-21 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 struct data{int n[101],sumn;}a[101]; 4 int T,n,m,cnt; 5 int i,j,k; 6 bool cmp(data a,data b) 7 { 8 if(a.sumn^b.sumn)return a.sumn<b.sumn; 9 for(int i=0;i<m;++i)if(a.n[i]^b.n[i])return a.n[i]<b.n[i]; 10 return a.n[0]<b.n[0]; 11 } 12 int main() 13 { 14 cin>>T; 15 for(i=0;i<T;++i){ 16 cin>>n>>m; 17 for(j=0;j<n;++j){ 18 for(cnt=k=0;k<m;++k)cin>>a[j].n[k],cnt+=a[j].n[k]; 19 a[j].sumn=cnt; 20 } 21 sort(a,a+n,cmp); 22 cout<<"case #"<<i<<":\n"; 23 for(j=0;j<n;++j){ 24 cout<<a[j].n[0]; 25 for(k=1;k<m;++k)cout<<' '<<a[j].n[k]; 26 cout<<endl; 27 } 28 } 29 return 0; 30 }
E3185 双阶乘的质因数个数
First AC: 2018-01-05 Latest Modification: 2019-04-14
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,m; 4 int tmp,ans; 5 int i; 6 int main() 7 { 8 cin>>T; 9 for(i=0;i<T;++i){ 10 cin>>n>>m; 11 ans=0; 12 while(n>0){ 13 tmp=n; 14 while(tmp%m==0){ 15 ++ans; 16 tmp/=m; 17 } 18 n-=2; 19 } 20 cout<<"case #"<<i<<":\n"<<ans<<endl; 21 } 22 return 0; 23 }
E3186 A+B
First AC: 2017-11-13 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,num,rst1,rst2; 4 string s,t[2]; 5 int i; 6 int main() 7 { 8 cin>>T; 9 for(i=0;i<T;++i){ 10 cout<<"case #"<<i<<":\n"; 11 while(cin>>s){ 12 if(s.length()!=1){ 13 t[num++]=s; 14 } 15 else if(s=="+"){ 16 if(t[0]=="one")rst1=1; 17 else if(t[0]=="two")rst1=2; 18 else if(t[0]=="three")rst1=3; 19 else if(t[0]=="four")rst1=4; 20 else if(t[0]=="five")rst1=5; 21 else if(t[0]=="six")rst1=6; 22 else if(t[0]=="seven")rst1=7; 23 else if(t[0]=="eight")rst1=8; 24 else if(t[0]=="nine")rst1=9; 25 else rst1=0; 26 if(num==2){ 27 rst1*=10; 28 if(t[1]=="one")rst1+=1; 29 else if(t[1]=="two")rst1+=2; 30 else if(t[1]=="three")rst1+=3; 31 else if(t[1]=="four")rst1+=4; 32 else if(t[1]=="five")rst1+=5; 33 else if(t[1]=="six")rst1+=6; 34 else if(t[1]=="seven")rst1+=7; 35 else if(t[1]=="eight")rst1+=8; 36 else if(t[1]=="nine")rst1+=9; 37 } 38 num=0; 39 } 40 else{ 41 if(t[0]=="one")rst2=1; 42 else if(t[0]=="two")rst2=2; 43 else if(t[0]=="three")rst2=3; 44 else if(t[0]=="four")rst2=4; 45 else if(t[0]=="five")rst2=5; 46 else if(t[0]=="six")rst2=6; 47 else if(t[0]=="seven")rst2=7; 48 else if(t[0]=="eight")rst2=8; 49 else if(t[0]=="nine")rst2=9; 50 else rst2=0; 51 if(num==2){ 52 rst2*=10; 53 if(t[1]=="one")rst2+=1; 54 else if(t[1]=="two")rst2+=2; 55 else if(t[1]=="three")rst2+=3; 56 else if(t[1]=="four")rst2+=4; 57 else if(t[1]=="five")rst2+=5; 58 else if(t[1]=="six")rst2+=6; 59 else if(t[1]=="seven")rst2+=7; 60 else if(t[1]=="eight")rst2+=8; 61 else if(t[1]=="nine")rst2+=9; 62 } 63 num=0,cout<<rst1+rst2<<endl; 64 break; 65 } 66 } 67 } 68 return 0; 69 }
E3187 凹数
First AC: 2018-02-21 Latest Modification: 2018-02-21
1 #include<bits/stdc++.h> 2 using namespace std; 3 long cnt; 4 int tmp1,tmp2,tmp3; 5 long d[1000001]; 6 int T,a,b; 7 long i,j,k; 8 bool jdg(long n) 9 { 10 int cnt=0; 11 tmp1=n%10,n/=10; 12 tmp2=n%10,n/=10; 13 if(tmp1==tmp2)return 0; 14 while(n){ 15 tmp3=n%10,n/=10; 16 if(tmp2==tmp3)return 0; 17 if(tmp2<tmp1&&tmp2<tmp3)++cnt; 18 else if(tmp2>tmp1&&tmp2>tmp3)return 0; 19 tmp1=tmp2,tmp2=tmp3; 20 } 21 if(cnt!=1)return 0; 22 return 1; 23 } 24 int main() 25 { 26 for(i=100;i<1000001;++i){ 27 if(jdg(i))++cnt; 28 d[i]=cnt; 29 } 30 cin>>T; 31 for(i=0;i<T;++i){ 32 cin>>a>>b; 33 cout<<"case #"<<i<<":\n"<<d[b]-d[a-1]<<endl; 34 } 35 return 0; 36 }
E3188 坏掉的彩灯
First AC: 2017-11-08 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len,M,m,R,B,Y,G,temp,cnt; 4 string s; 5 int i,j,k; 6 int main() 7 { 8 cin>>T,getchar(); 9 for(i=0;i<T;++i){ 10 getline(cin,s); 11 cout<<"case #"<<i<<":\n"; 12 len=s.length(); 13 R=B=Y=G=M=len/4,m=len%4,temp=len-m,cnt=0; 14 for(j=0;j<temp;++j) 15 if(s[j]=='R')R--; 16 else if(s[j]=='B')B--; 17 else if(s[j]=='G')G--; 18 else if(s[j]=='Y')Y--; 19 if(m==0){ 20 cout<<R<<' '<<B<<' '<<Y<<' '<<G<<endl; 21 continue; 22 } 23 for(j=1;j<=m;++j) 24 if(s[len-j]=='!'){ 25 for(k=4;k<=len;k+=4) 26 if(s[len-j-k]!='!'){ 27 cnt++; 28 s[len-j]=s[len-j-k]; 29 if(s[len-j-k]=='R')R++; 30 else if(s[len-j-k]=='B')B++; 31 else if(s[len-j-k]=='G')G++; 32 else if(s[len-j-k]=='Y')Y++; 33 break; 34 } 35 } 36 else cnt++; 37 if(cnt==m)cout<<R<<' '<<B<<' '<<Y<<' '<<G<<endl; 38 else{ 39 ++R,++B,++Y,++G; 40 for(j=4;j>m;--j) 41 for(k=-4;j+k<len;k+=4) 42 if(s[j+k]!='!'){ 43 if(s[j+k]=='R')R--; 44 else if(s[j+k]=='B')B--; 45 else if(s[j+k]=='G')G--; 46 else if(s[j+k]=='Y')Y--; 47 break; 48 } 49 for(j=1;j<=m;++j) 50 if(s[len-j]=='R')R--; 51 else if(s[len-j]=='B')B--; 52 else if(s[len-j]=='G')G--; 53 else if(s[len-j]=='Y')Y--; 54 cout<<R<<' '<<B<<' '<<Y<<' '<<G<<endl; 55 } 56 57 } 58 return 0; 59 }
E3189 求和
First AC: 2018-05-11 Latest Modification: 2018-05-11
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll T,n,m,num,l,r; 5 ll a[1005],b[500005]; 6 ll i,j,k; 7 int main() 8 { 9 cin>>T; 10 for(i=0;i<T;++i){ 11 cin>>n>>m; 12 for(j=1;j<=n;++j)cin>>a[j],a[j]+=a[j-1]; 13 num=1; 14 for(j=0;j<n;++j)for(k=j+1;k<=n;++k)b[num++]=a[k]-a[j]; 15 sort(b+1,b+num); 16 for(j=2;j<num;++j)b[j]+=b[j-1]; 17 cout<<"case #"<<i<<":\n"; 18 while(m--){ 19 cin>>l>>r; 20 cout<<b[r]-b[l-1]<<endl; 21 } 22 } 23 return 0; 24 }
E3190 平衡三进制
First AC: 2017-11-25 Latest Modification: 2018-03-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a[21]={1}; 4 string s; 5 int T,len,sum; 6 int i,j,k; 7 8 int main() 9 { 10 for(i=1;i<21;++i)a[i]=3*a[i-1]; 11 cin>>T; 12 for(i=0;i<T;++i){ 13 cin>>s; 14 len=s.length()-1; 15 for(j=len,sum=k=0;j+1;--j,++k) 16 if(s[j]=='-')sum-=a[k]; 17 else if(s[j]=='1')sum+=a[k]; 18 cout<<"case #"<<i<<":\n"<<sum<<endl; 19 } 20 return 0; 21 }
E3191 闰年问题
First AC: 2017-11-05 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,d; 4 long long y,n1,n2; 5 string m; 6 int i; 7 int main() 8 { 9 cin>>T; 10 for(i=0;i<T;++i){ 11 cout<<"case #"<<i<<":\n"; 12 cin>>m,scanf("%d,%ld",&d,&y); 13 n1=y/4-y/100+y/400; 14 if((y%4==0&&y%100!=0||y%400==0)&& 15 (m=="February"||m=="January"))--n1; 16 cin>>m,scanf("%d,%ld",&d,&y); 17 n2=y/4-y/100+y/400; 18 if((y%4==0&&y%100!=0||y%400==0)&& 19 ((m=="February"&&d<29)||m=="January"))--n2; 20 cout<<n2-n1<<endl; 21 } 22 return 0; 23 }
E3193键盘
First AC: 2017-12-21 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,a,b,c,m,pri,cntu,cntp,sumn,sump,tmpu,tmpp,cnt; 4 int u[301],p[301],tmp[601]; 5 string s; 6 int i,j; 7 int main() 8 { 9 cin>>T; 10 for(i=0;i<T;++i){ 11 cin>>a>>b>>c>>m; 12 for(cntu=cntp=j=0;j<m;++j){ 13 cin>>pri>>s; 14 if(s[0]=='U')u[cntu++]=pri; 15 else p[cntp++]=pri; 16 } 17 cout<<"case #"<<i<<":\n"; 18 sort(u,u+cntu),sort(p,p+cntp); 19 sumn=sump=cnt=0; 20 if(cntu>=a){ 21 sumn=a; 22 for(j=0;j<a;++j)sump+=u[j]; 23 for(j=a;j<cntu;++j)tmp[cnt++]=u[j]; 24 } 25 else{ 26 sumn=cntu; 27 for(j=0;j<cntu;++j)sump+=u[j]; 28 } 29 if(cntp>=b){ 30 sumn+=b; 31 for(j=0;j<b;++j)sump+=p[j]; 32 for(j=b;j<cntp;++j)tmp[cnt++]=p[j]; 33 } 34 else{ 35 sumn+=cntp; 36 for(j=0;j<cntp;++j)sump+=p[j]; 37 } 38 if(c){ 39 sort(tmp,tmp+cnt); 40 if(c>cnt){ 41 sumn+=cnt; 42 for(j=0;j<cnt;++j)sump+=tmp[j]; 43 } 44 else{ 45 sumn+=c; 46 for(j=0;j<c;++j)sump+=tmp[j]; 47 } 48 } 49 cout<<sumn<<' '<<sump<<endl; 50 } 51 return 0; 52 }
E3194 字符串消除
First AC: 2018-03-15 Latest Modification: 2018-03-15
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len,rst,tmp; 4 string s,t; 5 int i,j; 6 int cnt(string s) 7 { 8 int num=s.length(); 9 while(1){ 10 s=" "+s+" "; 11 int len=s.length()-1,i; 12 string t=""; 13 for(i=1;i<len;++i){ 14 if(s[i]!=s[i-1]&&s[i]!=s[i+1])t+=s[i]; 15 } 16 if(t.length()==len-1)return num-len+1; 17 s=t; 18 } 19 } 20 int main() 21 { 22 cin>>T; 23 for(i=0;i<T;++i){ 24 cin>>s; 25 len=s.length(); 26 rst=0; 27 for(j=0;j<len;++j){ 28 tmp=cnt(s.substr(0,j)+'A'+s.substr(j,len)); 29 if(tmp>rst)rst=tmp; 30 tmp=cnt(s.substr(0,j)+'B'+s.substr(j,len)); 31 if(tmp>rst)rst=tmp; 32 tmp=cnt(s.substr(0,j)+'C'+s.substr(j,len)); 33 if(tmp>rst)rst=tmp; 34 } 35 tmp=cnt(s+'A'); 36 if(tmp>rst)rst=tmp; 37 tmp=cnt(s+'B'); 38 if(tmp>rst)rst=tmp; 39 tmp=cnt(s+'C'); 40 if(tmp>rst)rst=tmp; 41 cout<<"case #"<<i<<":\n"<<rst<<endl; 42 } 43 return 0; 44 }
E3195 Ubiquitous Religions
First AC: 2018-06-08 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,x,y,rst,cas; 4 int pre[50001]; 5 int i; 6 int find(int x) 7 { 8 int r=x,i=x,j; 9 while(pre[r]!=r)r=pre[r]; 10 while(pre[i]!=r)j=pre[i],pre[i]=r,i=j; 11 return r; 12 } 13 void join(int x,int y) 14 { 15 int fx=find(x),fy=find(y); 16 if(fx!=fy)pre[fx]=fy; 17 } 18 int main() 19 { 20 while(cin>>n>>m,n){ 21 for(i=1;i<=n;++i)pre[i]=i; 22 while(m--){ 23 cin>>x>>y; 24 join(x,y); 25 } 26 rst=0; 27 for(i=1;i<=n;++i){ 28 if(pre[i]==i)++rst; 29 } 30 cout<<"Case "<<++cas<<": "<<rst<<endl; 31 } 32 return 0; 33 }
E3196 WormHoles
First AC: 2019-02-09 Latest Modification: 2019-02-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=505,maxm=5205; 4 int F,n,m,w,ne; 5 int u,v,val; 6 int h[maxn],dis[maxn],vis[maxn],cnt[maxn]; 7 queue<int>q,emp; 8 struct edge{ 9 int to,val,nxt; 10 }e[maxm]; 11 void insert(int u,int v,int val) 12 { 13 e[++ne].to=v; 14 e[ne].val=val; 15 e[ne].nxt=h[u]; 16 h[u]=ne; 17 } 18 bool spfa(int ask) 19 { 20 memset(dis,0x3f,sizeof dis); 21 dis[ask]=0; 22 memset(vis,0,sizeof vis); 23 vis[ask]=1; 24 memset(cnt,0,sizeof cnt); 25 ++cnt[ask]; 26 q=emp; 27 q.push(ask); 28 while(!q.empty()){ 29 int x=q.front(); 30 q.pop(); 31 vis[x]=0; 32 for(int i=h[x];i;i=e[i].nxt){ 33 int to=e[i].to; 34 if(dis[to]>dis[x]+e[i].val){ 35 dis[to]=dis[x]+e[i].val; 36 if(!vis[to]){ 37 vis[to]=1; 38 if(++cnt[to]>=n)return 0; 39 q.push(to); 40 } 41 } 42 } 43 } 44 return 1; 45 } 46 int main() 47 { 48 cin>>F; 49 while(F--){ 50 cin>>n>>m>>w; 51 ne=0; 52 memset(h,0,sizeof h); 53 while(m--){ 54 cin>>u>>v>>val; 55 insert(u,v,val); 56 insert(v,u,val); 57 } 58 while(w--){ 59 cin>>u>>v>>val; 60 insert(u,v,-val); 61 } 62 spfa(1)? cout<<"NO\n":cout<<"YES\n"; 63 } 64 return 0; 65 }
E3200 Six Degrees of Cowvin Bacon
First AC: 2019-02-07 Latest Modification: 2019-02-07
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,num,rst,tmp; 4 int dis[301][301]; 5 int movie[301]; 6 int sumdegree[301]; 7 int i,j,k; 8 int main() 9 { 10 cin>>n>>m; 11 memset(dis,0x3f,sizeof dis); 12 for(i=1;i<=n;++i)dis[i][i]=0; 13 while(m--){ 14 cin>>num; 15 for(i=0;i<num;++i)cin>>movie[i]; 16 for(i=0;i<num;++i){ 17 for(j=i+1;j<num;++j){ 18 dis[movie[i]][movie[j]]=1; 19 dis[movie[j]][movie[i]]=1; 20 } 21 } 22 } 23 for(k=1;k<=n;++k){ 24 for(i=1;i<=n;++i){ 25 for(j=1;j<=n;++j){ 26 dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]); 27 } 28 } 29 } 30 rst=1e6; 31 for(i=1;i<=n;++i){ 32 tmp=0; 33 for(j=1;j<=n;++j){ 34 if(i!=j)tmp+=dis[i][j]; 35 } 36 rst=min(rst,tmp); 37 } 38 cout<<(int)(rst*100.0/(n-1)); 39 return 0; 40 }
E3202 RoadBlocks
First AC: 2019-02-13 Latest Modification: 2019-02-13
Note: 添加一个数组dis2[]存次短路,注意更新时要避免||的短路操作
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=5005,maxm=200005; 4 int n,m,ne; 5 int u,v,val; 6 int h[maxn],dis[maxn],dis2[maxn],vis[maxn],cnt[maxn]; 7 struct edge{ 8 int to,val,nxt; 9 }e[maxm]; 10 queue<int> q,emp; 11 void insert(int u,int v,int val) 12 { 13 e[++ne].to=v; 14 e[ne].val=val; 15 e[ne].nxt=h[u]; 16 h[u]=ne; 17 } 18 bool update(int index,int val) 19 { 20 if(dis[index]==val||dis2[index]<=val)return 0; 21 if(dis[index]>val){ 22 dis2[index]=dis[index]; 23 dis[index]=val; 24 } 25 else{ 26 dis2[index]=val; 27 } 28 return 1; 29 } 30 bool spfa(int ask) 31 { 32 memset(dis,0x3f,sizeof dis); 33 dis[ask]=0; 34 memset(dis2,0x3f,sizeof dis2); 35 memset(vis,0,sizeof vis); 36 vis[ask]=1; 37 memset(cnt,0,sizeof cnt); 38 ++cnt[ask]; 39 q=emp; 40 q.push(ask); 41 while(!q.empty()){ 42 int x=q.front(); 43 q.pop(); 44 vis[x]=0; 45 for(int i=h[x];i;i=e[i].nxt){ 46 int to=e[i].to; 47 if(update(to,dis[x]+e[i].val)|update(to,dis2[x]+e[i].val)){ 48 if(!vis[to]){ 49 if(++cnt[to]>=n)return 0; 50 vis[to]=1; 51 q.push(to); 52 } 53 } 54 } 55 } 56 return 1; 57 } 58 int main() 59 { 60 cin>>n>>m; 61 while(m--){ 62 cin>>u>>v>>val; 63 insert(u,v,val); 64 insert(v,u,val); 65 } 66 spfa(1); 67 cout<<dis2[n]; 68 return 0; 69 }
E3205 Prime Gap
First AC: 2018-05-05 Latest Modification: 2018-05-05
1 #include<bits/stdc++.h> 2 using namespace std; 3 bool pri[1300000]; 4 int n; 5 int i,j; 6 int main() 7 { 8 ios::sync_with_stdio(false); 9 for(i=2;i<650000;++i) 10 if(!pri[i]) 11 for(j=i+i;j<1300000;j+=i) 12 pri[j]=1; 13 while(cin>>n,n){ 14 i=j=n; 15 while(pri[i])--i; 16 while(pri[j])++j; 17 cout<<j-i<<endl; 18 } 19 return 0; 20 }
E3206 Aggressive Cows
First AC: 2018-05-06 Latest Modification: 2018-05-06
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,c; 4 int x[100000]; 5 int i; 6 bool jdg(int dis) 7 { 8 int tmp=0,num=1; 9 for(int i=1;i<n;++i){ 10 tmp+=x[i]-x[i-1]; 11 if(tmp>=dis)tmp=0,++num; 12 } 13 if(num>=c)return 1; 14 return 0; 15 } 16 void find(int lft,int rgt) 17 { 18 if(lft+1>=rgt){ 19 if(jdg(rgt))cout<<rgt; 20 else cout<<lft; 21 exit(0); 22 } 23 int mid=(lft+rgt)/2; 24 if(jdg(mid))find(mid,rgt); 25 else find(lft,mid); 26 } 27 int main() 28 { 29 ios::sync_with_stdio(false); 30 cin>>n>>c; 31 for(i=0;i<n;++i)cin>>x[i]; 32 sort(x,x+n); 33 find(1,x[n-1]-x[0]); 34 return 0; 35 }
E3209 分数加法
First AC: 2018-05-16 Latest Modification: 2018-05-16
1 from fractions import Fraction 2 T=int(input()) 3 a=0 4 for i in range(T): 5 a+=Fraction(input()) 6 print(a)
E3210 Maximum Element
First AC: 2018-02-13 Latest Modification: 2018-02-13
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,q,x,tmp; 4 stack<int>team,maxu; 5 int main() 6 { 7 cin>>n; 8 while(n--) 9 { 10 scanf("%d",&q); 11 if(q==1){ 12 cin>>x; 13 team.push(x); 14 if(maxu.empty())maxu.push(x); 15 else if(x>=maxu.top())maxu.push(x); 16 } 17 else if(q==2){ 18 tmp=team.top(); 19 team.pop(); 20 if(tmp==maxu.top())maxu.pop(); 21 } 22 else cout<<maxu.top()<<endl; 23 } 24 }
E3211 Rails
First AC: 2018-04-13 Latest Modification: 2018-04-13
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m; 4 stack<int>s,t,emps; 5 queue<int>q,empq; 6 int i; 7 bool op() 8 { 9 if(!t.empty()&&t.top()==q.front()){ 10 t.pop(); 11 q.pop(); 12 return 1; 13 } 14 while(!s.empty()&&s.top()!=q.front()){ 15 t.push(s.top()); 16 s.pop(); 17 } 18 if(!s.empty()&&s.top()==q.front()){ 19 s.pop(); 20 q.pop(); 21 return 1; 22 } 23 else return 0; 24 } 25 int main() 26 { 27 while(cin>>n,n){ 28 while(cin>>m){ 29 if(!m){ 30 cout<<endl; 31 break; 32 } 33 s=emps,t=emps,q=empq; 34 q.push(m); 35 for(i=1;i<n;++i)cin>>m,q.push(m); 36 for(i=n;i;--i)s.push(i); 37 bool jdg=1; 38 for(i=0;i<n;++i){ 39 if(!op()){ 40 jdg=0; 41 break; 42 } 43 } 44 jdg? cout<<"Yes\n":cout<<"No\n"; 45 } 46 } 47 return 0; 48 }
E3212 Balances Brackets
First AC: 2018-04-13 Latest Modification: 2018-04-13
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,len; 4 string s; 5 stack<char>stk,emp; 6 int i; 7 int main() 8 { 9 cin>>n; 10 while(n--){ 11 cin>>s; 12 len=s.length(); 13 stk=emp; 14 bool jdg=1; 15 for(i=0;i<len;++i){ 16 if(s[i]==')'){ 17 if(stk.empty()||stk.top()!='('){ 18 jdg=0; 19 break; 20 } 21 else stk.pop(); 22 } 23 else if(s[i]==']'){ 24 if(stk.empty()||stk.top()!='['){ 25 jdg=0; 26 break; 27 } 28 else stk.pop(); 29 } 30 else if(s[i]=='}'){ 31 if(stk.empty()||stk.top()!='{'){ 32 jdg=0; 33 break; 34 } 35 else stk.pop(); 36 } 37 else stk.push(s[i]); 38 } 39 if(jdg&&stk.empty())cout<<"YES\n"; 40 else cout<<"NO\n"; 41 } 42 return 0; 43 }
E3213 向右看齐
First AC: 2018-06-04 Latest Modification: 2018-06-04
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,h; 4 int a[100005]; 5 stack< pair<int,int> >stk; 6 int i; 7 int main() 8 { 9 ios::sync_with_stdio(false); 10 cin>>n; 11 for(i=1;i<=n;++i){ 12 cin>>h; 13 while(!stk.empty()&&stk.top().first<h){ 14 a[stk.top().second]=i; 15 stk.pop(); 16 } 17 stk.push(pair<int,int>(h,i)); 18 } 19 for(i=1;i<=n;++i)cout<<a[i]<<endl; 20 return 0; 21 }
E3221 北京记者跑得最快
First AC: 2017-11-01 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 long L,v,a,i,s,b; 4 int main() 5 { 6 cin>>L>>v>>a; 7 s=v*v,b=a*2; 8 for(i=0;i<L;++i){ 9 s+=b;printf("%.7f\n",(sqrt(s)-v)/a); 10 } 11 return 0; 12 }
E3222 六六六
First AC: 2018-06-08 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,x,tmp,sum; 4 bool jdg[1000000]; 5 int i,j; 6 int main() 7 { 8 cin>>T; 9 for(i=1;i<=T;++i){ 10 cin>>x; 11 memset(jdg,0,sizeof(jdg)); 12 cout<<"Case "<<i<<": "; 13 sum=tmp=6%x; 14 bool rst=1; 15 for(j=1;j<=x;++j){ 16 if(!sum){ 17 cout<<j<<endl; 18 rst=0; 19 break; 20 } 21 tmp=10*tmp%x; 22 (sum+=tmp)%=x; 23 } 24 if(rst)cout<<"-1\n"; 25 } 26 return 0; 27 }
E3226 声控开关(Easy)
First AC: 2018-12-17 Latest Modification: 2018-12-17
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,k; 4 int a[32]; 5 int i,j; 6 int main() 7 { 8 cin>>T; 9 for(i=1;i<=T;++i){ 10 cin>>n>>k; 11 for(j=0;j<32;++j){ 12 a[j]=k&1; 13 k>>=1; 14 } 15 cout<<"Case "<<i<<": O"; 16 bool flag=1; 17 for(j=0;j<n;++j){ 18 if(!a[j]){ 19 cout<<"FF\n"; 20 flag=0; 21 break; 22 } 23 } 24 if(flag)cout<<"N\n"; 25 } 26 return 0; 27 }
E3227 声控开关(Hard)
First AC: 2018-12-17 Latest Modification: 2018-12-17
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,k; 4 int a[32]; 5 int i,j; 6 int main() 7 { 8 cin>>T; 9 for(i=1;i<=T;++i){ 10 cin>>n>>k; 11 for(j=0;j<32;++j){ 12 a[j]=k&1; 13 k>>=1; 14 } 15 cout<<"Case "<<i<<": O"; 16 bool flag=1; 17 for(j=0;j<n;++j){ 18 if(!a[j]){ 19 cout<<"FF\n"; 20 flag=0; 21 break; 22 } 23 } 24 if(flag)cout<<"N\n"; 25 } 26 return 0; 27 }
E3233 N!
First AC: 2017-10-27 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 long n; 4 int main() 5 { 6 while(cin>>n) 7 cout<<n/5+n/25+n/125+n/625+n/3125+n/15625 8 +n/78125+n/390625+n/1953125+n/9765625 9 +n/48828125+n/244140625<<endl; 10 return 0; 11 }
E3234 Sort
First AC: 2018-04-27 Latest Modification: 2018-04-27
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const ll M=100001; 5 ll a[M],b[M]; 6 ll n,rst; 7 ll i,j,k; 8 void Merge(ll a[],ll lft,ll mid,ll rgt) 9 { 10 i=lft,j=mid+1,k=lft; 11 while(i<=mid&&j<=rgt){ 12 if(a[i]<=a[j])b[k++]=a[i++]; 13 else rst+=j-k,b[k++]=a[j++]; 14 } 15 while(i<=mid)b[k++]=a[i++]; 16 while(j<=rgt)b[k++]=a[j++]; 17 for(i=lft;i<=rgt;++i)a[i]=b[i]; 18 } 19 void MergeSort(ll a[],ll lft,ll rgt) 20 { 21 if(lft<rgt){ 22 ll mid=(lft+rgt)/2; 23 MergeSort(a,lft,mid); 24 MergeSort(a,mid+1,rgt); 25 Merge(a,lft,mid,rgt); 26 } 27 } 28 int main() 29 { 30 ios::sync_with_stdio(false); 31 while(cin>>n){ 32 for(i=0;i<n;++i)cin>>a[i]; 33 rst=0; 34 MergeSort(a,0,n-1); 35 cout<<rst<<endl; 36 } 37 return 0; 38 }
E3236 因子平方和
First AC: 2017-10-30 Latest Modification: 2017-10-30
1 #include<iostream> 2 using namespace std; 3 int T,s,n; 4 int i,j; 5 int main() 6 { 7 cin>>T; 8 for(i=0;i<T;++i){ 9 s=0; 10 cin>>n; 11 for(j=2;j<n;++j)if(n%j==0)s+=j*j; 12 cout<<"case #"<<i<<":\n"<<s<<endl; 13 } 14 return 0; 15 }
E3237 n!进制
First AC: 2017-12-11 Latest Modification: 2017-12-11
1 #include<iostream> 2 using namespace std; 3 long a[10]={0,1}; 4 int T,i,j,cnt; 5 long n,tmp; 6 int main() 7 { 8 for(i=2;i<10;++i)a[i]=a[i-1]*i; 9 cin>>T; 10 for(i=0;i<T;++i){ 11 cin>>n; 12 cout<<"case #"<<i<<":\n"; 13 for(cnt=0,j=9;j;--j){ 14 if(tmp=n/a[j])cnt=1; 15 if(cnt)cout<<tmp; 16 n%=a[j]; 17 } 18 cout<<endl; 19 } 20 return 0; 21 }
E3238 字串非重复字符数排序
First AC: 2017-11-24 Latest Modification: 2018-03-01
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,len; 4 struct data{ 5 string s; 6 int len,num; 7 }a[100]; 8 bool jdg[26]; 9 int i,j,k; 10 bool cmp(data a,data b) 11 { 12 if(a.num!=b.num)return a.num>b.num; 13 return a.s<b.s; 14 } 15 int main() 16 { 17 cin>>T; 18 for(i=0;i<T;++i){ 19 cin>>n; 20 for(j=0;j<n;++j){ 21 cin>>a[j].s; 22 a[j].len=a[j].s.length(); 23 a[j].num=0; 24 memset(jdg,0,sizeof(jdg)); 25 for(k=0;k<a[j].len;++k){ 26 if(!jdg[a[j].s[k]-'A'])++a[j].num,jdg[a[j].s[k]-'A']=1; 27 } 28 } 29 sort(a,a+n,cmp); 30 cout<<"case #"<<i<<":\n"; 31 for(j=0;j<n;++j)cout<<a[j].s<<endl; 32 } 33 return 0; 34 }
E3239 最长的等差数列
First AC: 2017-12-23 Latest Modification: 2018-06-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,rst,cnt,tmp; 4 int a[101]; 5 int i,j,k,r; 6 int main() 7 { 8 cin>>T; 9 for(i=0;i<T;++i){ 10 cin>>n; 11 for(rst=j=0;j<n;++j)cin>>a[j]; 12 cout<<"case #"<<i<<":\n"; 13 if(n==1){cout<<1<<endl;continue;} 14 sort(a,a+n); 15 for(j=0;j<n;++j){ 16 for(k=j+1;k<n;++k){ 17 tmp=a[k]-a[j]; 18 for(cnt=2,r=k+1;r<n;++r){ 19 if(a[r]==a[j]+cnt*tmp)++cnt;continue; 20 if(a[r]>a[j]+cnt*tmp&&a[r-1]<a[j]+cnt*tmp)break; 21 } 22 if(cnt>rst)rst=cnt; 23 } 24 } 25 cout<<rst<<endl; 26 } 27 return 0; 28 }
E3240 小香农范诺编码
First AC: 2018-03-07 Latest Modification: 2018-03-07
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n; 4 int suma[201]; 5 struct data{ 6 int n; 7 string s; 8 }a[200]; 9 int i,j; 10 void code(int lft,int rgt) 11 { 12 int m=suma[lft]+suma[rgt]; 13 int i,j; 14 for(i=lft+1;;++i){ 15 if((abs(2*suma[i+1]-m)>=abs(2*suma[i]-m)))break; 16 } 17 for(j=lft;j<i;++j)a[j].s+='0'; 18 for(j=i;j<rgt;++j)a[j].s+='1'; 19 if(i-lft>1)code(lft,i); 20 if(rgt-i>1)code(i,rgt); 21 } 22 int main() 23 { 24 cin>>T; 25 for(i=0;i<T;++i){ 26 cin>>n; 27 for(j=0;j<n;++j) 28 cin>>a[j].n,suma[j+1]=suma[j]+a[j].n,a[j].s=""; 29 code(0,n); 30 cout<<"case #"<<i<<":\n"; 31 for(j=0;j<n;++j)cout<<a[j].n<<':'<<a[j].s<<endl; 32 } 33 return 0; 34 }
E3241 字母替换
First AC: 2017-10-27 Latest Modification: 2018-06-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,i,j,l; 4 string s; 5 int main() 6 { 7 cin>>T; 8 for(i=0;i<T;++i){ 9 cout<<"case #"<<i<<":\n"; 10 cin>>s; 11 l=s.length(); 12 for(j=0;j<l;++j) 13 if(j%2==0&&s[j]>='A'&&s[j]<='Z') 14 cout<<(char)(s[j]-'A'+'a'); 15 else cout<<s[j]; 16 cout<<endl; 17 } 18 return 0; 19 }
E3242 重复数
First AC: 2017-11-04 Latest Modification: 2018-06-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n; 4 int a[1005],b[1005],M; 5 int i,j,k; 6 int main() 7 { 8 cin>>T; 9 for(i=0;i<T;++i){ 10 cin>>n; 11 memset(a,0,sizeof(a)),memset(b,0,sizeof(b)),M=0; 12 for(j=0;j<n;++j)cin>>a[j]; 13 for(j=0;j<n-1;++j){ 14 for(k=j+1;k<n;++k)if(a[j]==a[k])++b[j]; 15 if(b[j]>M)M=b[j]; 16 } 17 cout<<"case #"<<i<<":\n"<<M+1<<endl; 18 } 19 return 0; 20 }
E3243 搜索联系人
First AC: 2017-12-22 Latest Modification: 2018-06-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 long T,n,ls,cnt; 4 struct data{ 5 string name,tel; 6 long lt; 7 bool flag; 8 }a[10005]; 9 string s; 10 long i,j,k,l; 11 bool cmp(data a,data b) 12 { 13 if(a.flag!=b.flag)return a.flag>b.flag; 14 if(a.name!=b.name){ 15 int len1=a.name.length(),len2=b.name.length(); 16 int tmp=len1<len2? len1:len2; 17 for(int i=0;i<tmp;++i) 18 if(a.name[i]-b.name[i]) 19 return a.name[i]<b.name[i]; 20 return a.name.length()<b.name.length(); 21 } 22 int len1=a.tel.length(),len2=b.tel.length(); 23 int tmp=len1<len2? len1:len2; 24 for(int i=0;i<tmp;++i)if(a.tel[i]-b.tel[i]) 25 return a.tel[i]<b.tel[i]; 26 return a.tel.length()<b.tel.length(); 27 } 28 int main() 29 { 30 cin>>T; 31 for(i=0;i<T;++i){ 32 cin>>n; 33 for(j=0;j<n;++j){ 34 cin>>a[j].name>>a[j].tel; 35 a[j].lt=a[j].tel.length(); 36 a[j].flag=0; 37 } 38 cin>>s,ls=s.length(); 39 for(cnt=j=0;j<n;++j){ 40 for(k=0;k<=a[j].lt-ls;++k){ 41 if(a[j].tel[k]==s[0]){ 42 for(a[j].flag=l=1;l<ls;++l) 43 if(a[j].tel[k+l]-s[l]){ 44 a[j].flag=0; 45 break; 46 } 47 if(a[j].flag){ 48 ++cnt; 49 break; 50 } 51 } 52 } 53 } 54 sort(a,a+n,cmp); 55 cout<<"case #"<<i<<":\n"<<cnt<<endl; 56 for(j=0;j<cnt;++j) 57 cout<<a[j].name<<' '<<a[j].tel<<endl; 58 } 59 return 0; 60 }
E3244 KL排序
First AC: 2018-03-07 Latest Modification: 2018-03-07
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,K,n,m; 4 int timex[10]; 5 double px[10]; 6 struct data{ 7 int num; 8 double timey[10]; 9 double kl; 10 }y[200]; 11 int i,j,k; 12 bool cmp(data a,data b) 13 { 14 if(fabs(a.kl-b.kl)>1e-7)return a.kl<b.kl; 15 return a.num<b.num; 16 } 17 int main() 18 { 19 cin>>T; 20 for(i=0;i<T;++i){ 21 cin>>K>>n; 22 for(m=j=0;j<K;++j) 23 cin>>timex[j],m+=timex[j]; 24 for(j=0;j<K;++j) 25 px[j]=(timex[j]+1.0/K)/(m+1); 26 for(j=0;j<n;++j){ 27 y[j].num=j+1; 28 for(m=k=0;k<K;++k) 29 cin>>y[j].timey[k],m+=y[j].timey[k]; 30 for(y[j].kl=k=0;k<K;++k){ 31 y[j].kl+=px[k]*log(px[k]/((y[j].timey[k]+1.0/K)/(m+1))); 32 if(fabs(y[j].kl)<1e-7)y[j].kl=0.0000; 33 } 34 } 35 sort(y,y+n,cmp); 36 cout<<"case #"<<i<<":\n"; 37 for(j=0;j<n;++j)printf("%d %.4f\n",y[j].num,y[j].kl); 38 } 39 return 0; 40 }
E3245 找数
First AC: 2018-02-19 Latest Modification: 2018-02-19
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len,jdg1,jdg2; 4 string s; 5 int i,j,k; 6 int main() 7 { 8 cin>>T; 9 for(i=0;i<T;++i){ 10 cin>>s; 11 len=s.length(); 12 cout<<"case #"<<i<<":\n"; 13 if(len==1){cout<<s<<endl;continue;} 14 jdg1=1; 15 while(jdg1){ 16 jdg2=0; 17 for(j=1;j<len;++j){ 18 if(s[j]<s[j-1]){ 19 jdg2=1; 20 //s[j-1]!='0' 21 --s[j-1]; 22 for(k=j;k<len;++k)s[k]='9'; 23 break; 24 } 25 } 26 if(jdg2)continue; 27 jdg1=0; 28 } 29 for(j=0;;++j)if(s[j]!='0'){cout<<s[j];break;} 30 for(k=j+1;k<len;++k)cout<<s[k]; 31 cout<<endl; 32 } 33 return 0; 34 }
E3250 计软联谊
First AC: 2018-05-10 Latest Modification: 2018-05-10
1 #include<bits/stdc++.h> 2 using namespace std; 3 vector<int>all_fac[1000001]; 4 vector<int>::iterator it; 5 int T,n,K; 6 int a[100000]; 7 int i,j,k,cas; 8 int main() 9 { 10 ios::sync_with_stdio(false); 11 for(i=2;i<1000001;++i) 12 for(j=i;j<1000001;j+=i) 13 all_fac[j].insert(all_fac[j].begin(),i); 14 cin>>T; 15 for(cas=1;cas<=T;++cas){ 16 cin>>n>>K; 17 for(i=0;i<n;++i)cin>>a[i]; 18 cout<<"Case "<<cas<<":"; 19 for(j=1;j<n;++j){ 20 int tmp=__gcd(a[j],a[j-1]); 21 if(all_fac[tmp].size()+1<K)cout<<" -1"; 22 else if(all_fac[tmp].size()+1==K)cout<<" 1"; 23 else cout<<' '<<*(all_fac[tmp].begin()+K-1); 24 } 25 cout<<endl; 26 } 27 return 0; 28 }
E3251 数青蛙
First AC: 2018-05-11 Latest Modification: 2018-05-11
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const ll M=1e18; 5 int n; 6 ll a[55],tmp,up,dn; 7 int i; 8 bool ok(ll up1,ll dn1,ll up2,ll dn2) 9 { 10 tmp=__gcd(up1,dn1); 11 up1/=tmp,dn1/=tmp; 12 tmp=__gcd(up2,dn2); 13 up2/=tmp,dn2/=tmp; 14 if(up1!=up2||dn1!=dn2)return 0; 15 return 1; 16 } 17 int main() 18 { 19 cin>>n; 20 for(i=0;i<n;++i)cin>>a[i]; 21 if(n==1){ 22 if(a[0]==M)a[0]=1; 23 else ++a[0]; 24 } 25 else if(n==2){ 26 if(!a[0])a[0]=1; 27 else if(!a[1])a[1]=1; 28 else if(a[0]!=1)a[0]=1; 29 else if(a[1]!=1)a[1]=1; 30 else a[1]=2; 31 } 32 else if(n==3){ 33 if(!a[0]){ 34 up=a[1],dn=a[2]; 35 tmp=__gcd(up,dn); 36 up/=tmp,dn/=tmp; 37 a[0]=a[1]/dn*up;//必改0,必整除 38 } 39 else if(!a[1]){ 40 tmp=__gcd(a[0],a[2]); 41 up=a[0]/tmp,dn=a[2]/tmp; 42 if((ll)sqrt(up)*(ll)sqrt(up)==up) 43 a[1]=(ll)sqrt(up)*(ll)sqrt(dn)*tmp; 44 else a[1]=tmp*sqrt(up*dn);//必改0 45 } 46 else if(!a[2]){ 47 up=a[1],dn=a[0]; 48 tmp=__gcd(up,dn); 49 up/=tmp,dn/=tmp; 50 a[2]=a[1]/dn*up;//必改0,必整除 51 } 52 else{ 53 bool jdg=1; 54 if(jdg){//试改a[0] 55 up=a[1],dn=a[2]; 56 tmp=__gcd(up,dn); 57 up/=tmp,dn/=tmp; 58 if(!(a[1]%dn)&&a[1]*1.0/dn<=M*1.0/up){ 59 a[0]=a[1]/dn*up; 60 jdg=0; 61 } 62 } 63 if(jdg){//试改a[2] 64 up=a[1],dn=a[0]; 65 tmp=__gcd(up,dn); 66 up/=tmp,dn/=tmp; 67 if(!(a[1]%dn)&&a[1]*1.0/dn<=M*1.0/up){ 68 a[2]=a[1]/dn*up; 69 jdg=0; 70 } 71 } 72 if(jdg){//必改a[1] 73 tmp=__gcd(a[0],a[2]); 74 up=a[0]/tmp,dn=a[2]/tmp; 75 if((ll)sqrt(up)*(ll)sqrt(up)==up) 76 a[1]=(ll)sqrt(up)*(ll)sqrt(dn)*tmp; 77 else a[1]=tmp*sqrt(up*dn); 78 } 79 } 80 } 81 else{//n>3,前2元与末两元必有一组无误 82 bool jdg=1; 83 for(i=0;i<n;++i){//有0必改 84 if(!a[i]){ 85 if(i==0){ 86 up=a[1],dn=a[2]; 87 tmp=__gcd(up,dn); 88 up/=tmp,dn/=tmp; 89 a[0]=a[1]/dn*up; 90 } 91 else if(i==1){ 92 tmp=__gcd(a[0],a[2]); 93 up=a[0]/tmp,dn=a[2]/tmp; 94 if((ll)sqrt(up)*(ll)sqrt(up)==up) 95 a[1]=(ll)sqrt(up)*(ll)sqrt(dn)*tmp; 96 else a[1]=tmp*sqrt(up*dn); 97 } 98 else{ 99 up=a[i-1],dn=a[i-2]; 100 tmp=__gcd(up,dn); 101 up/=tmp,dn/=tmp; 102 a[i]=a[i-1]/dn*up; 103 } 104 jdg=0; 105 } 106 } 107 if(jdg){//试验证前2元 108 ll b[55]; 109 for(i=0;i<n;++i)b[i]=a[i]; 110 int cnt=0; 111 for(i=2;i<n;++i){ 112 if(!ok(b[0],b[1],b[i-1],b[i])){//试改 113 up=b[i-1],dn=b[i-2]; 114 tmp=__gcd(up,dn); 115 up/=tmp,dn/=tmp; 116 if(b[i-1]%dn||b[i-1]*1.0/dn>M*1.0/up){ 117 cnt=2; 118 break; 119 } 120 b[i]=b[i-1]/dn*up; 121 ++cnt; 122 } 123 } 124 if(cnt==1){ 125 jdg=0; 126 for(i=0;i<n;++i)a[i]=b[i]; 127 } 128 } 129 if(jdg){//必为后2元 130 ll b[55]; 131 for(i=0;i<n;++i)b[i]=a[n-1-i]; 132 for(i=2;i<n;++i){ 133 if(!ok(b[0],b[1],b[i-1],b[i])){ 134 up=b[i-1],dn=b[i-2]; 135 tmp=__gcd(up,dn); 136 up/=tmp,dn/=tmp;//必改必整除 137 b[i]=b[i-1]/dn*up; 138 } 139 } 140 for(i=0;i<n;++i)a[i]=b[n-1-i]; 141 } 142 } 143 cout<<a[0]; 144 for(i=1;i<n;++i)cout<<' '<<a[i]; 145 return 0; 146 }
E3252 语言辨别
First AC: 2017-12-19 Latest Modification: 2018-06-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s; 4 int len,n,c,e; 5 int main() 6 { 7 while(cin>>s){ 8 ++n; 9 len=s.length(); 10 if(len>5)++c; 11 else if(len==3&&(s=="the"||s=="and"))++e; 12 } 13 if(10*c<n)cout<<"Chinese"; 14 else if(30*e>n)cout<<"English"; 15 else cout<<"Japanese"; 16 return 0; 17 }
E3253 玉米和葡萄
First AC: 2018-02-17 Latest Modification: 2018-02-17
1 #include<bits/stdc++.h> 2 using namespace std; 3 double f[2001][2001]; 4 int n,m,sum,cnt; 5 double tmp; 6 int i,j; 7 int main() 8 { 9 for(i=1;i<2001;++i)f[0][i]=1; 10 for(i=1;i<2001;++i)for(j=4;j<2001;++j){ 11 cnt=tmp=0; 12 if(i>=4&&j>=0)++cnt,tmp+=f[i-4][j]; 13 if(i>=3&&j>=1)++cnt,tmp+=f[i-3][j-1]; 14 if(i>=2&&j>=2)++cnt,tmp+=f[i-2][j-2]; 15 if(i>=1&&j>=3)++cnt,tmp+=f[i-1][j-3]; 16 if(i>=0&&j>=4)++cnt,tmp+=f[i][j-4]; 17 f[i][j]=tmp*1.0/cnt; 18 } 19 while(cin>>n>>m)printf("%.7f\n",f[4*n][4*m]); 20 return 0; 21 }
E3256 拼音魔法
First AC: 2018-12-18 Latest Modification: 2018-12-18
1 #include<bits/stdc++.h> 2 using namespace std; 3 string aa[]={"ā","á","ǎ","à","a"}; 4 string ee[]={"ē","é","ě","è","e"}; 5 string ii[]={"ī","í","ǐ","ì","i"}; 6 string oo[]={"ō","ó","ǒ","ò","o"}; 7 string uu[]={"ū","ú","ǔ","ù","u"}; 8 string vv[]={"ǖ","ǘ","ǚ","ǜ","ü"}; 9 int T,len,tmp1,tmp2; 10 string s; 11 int i; 12 int find(string s,int len,char c) 13 { 14 for(int i=0;i<len;++i)if(s[i]==c)return i; 15 return -1; 16 } 17 int main() 18 { 19 cin>>T; 20 for(i=1;i<=T;++i){ 21 cin>>s; 22 len=s.length(); 23 if(s[len-1]>='a')s+='5',++len; 24 cout<<"Case "<<i<<": "; 25 tmp1=find(s,len,'v'); 26 if(tmp1!=-1){ 27 cout<<s.substr(0,tmp1)<<vv[s[len-1]-'1'] 28 <<s.substr(tmp1+1,len-tmp1-2)<<endl; 29 continue; 30 } 31 tmp1=find(s,len,'a'); 32 if(tmp1!=-1){ 33 cout<<s.substr(0,tmp1)<<aa[s[len-1]-'1'] 34 <<s.substr(tmp1+1,len-tmp1-2)<<endl; 35 continue; 36 } 37 tmp1=find(s,len,'o'); 38 if(tmp1!=-1){ 39 cout<<s.substr(0,tmp1)<<oo[s[len-1]-'1'] 40 <<s.substr(tmp1+1,len-tmp1-2)<<endl; 41 continue; 42 } 43 tmp1=find(s,len,'e'); 44 if(tmp1!=-1){ 45 cout<<s.substr(0,tmp1)<<ee[s[len-1]-'1'] 46 <<s.substr(tmp1+1,len-tmp1-2)<<endl; 47 continue; 48 } 49 tmp1=find(s,len,'i'); 50 tmp2=find(s,len,'u'); 51 if(tmp1>tmp2){ 52 cout<<s.substr(0,tmp1)<<ii[s[len-1]-'1'] 53 <<s.substr(tmp1+1,len-tmp1-2)<<endl; 54 } 55 else{ 56 cout<<s.substr(0,tmp2)<<uu[s[len-1]-'1'] 57 <<s.substr(tmp2+1,len-tmp2-2)<<endl; 58 } 59 } 60 return 0; 61 }
E3257 七减一
First AC: 2017-11-12 Latest Modification: 2018-06-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll a[20],b[20]; 5 ll l,r; 6 int i,j; 7 void dp(ll t,ll *c,ll d){ 8 ll n=t/10,m=t%10; 9 ll tmp=n; 10 for(i=0;i<=m;i++)c[i]+=d; 11 for(j=0;j<=9;j++)c[j]+=d*n; 12 c[0]-=d; 13 while(tmp){ 14 c[tmp%10]+=(m+1)*d; 15 tmp/=10; 16 } 17 if(n)dp(n-1,c,d*10); 18 } 19 int main(){ 20 while(cin>>l>>r){ 21 memset(a,0,sizeof(a)); 22 memset(b,0,sizeof(b)); 23 dp(l-1,a,1); 24 dp(r,b,1); 25 cout<<b[6]-a[6]<<endl; 26 } 27 return 0; 28 }
E3258 平方俱乐部
First AC: 2018-02-10 Latest Modification: 2018-02-10
1 #include<bits/stdc++.h> 2 using namespace std; 3 int p,q; 4 int i,j; 5 /* let b=d=q,a*a+c*c=pq */ 6 void find(int m,int n) 7 { 8 //let a*a+c*c=m and b=d=n 9 for(i=0;;++i){ 10 j=sqrt(m-i*i); 11 if(i*i+j*j==m){ 12 cout<<i<<' '<<n<<' '<<j<<' '<<n; 13 return; 14 } 15 } 16 } 17 int main() 18 { 19 cin>>p>>q; 20 find(p*q,q); 21 return 0; 22 }
E3262 黑心啤酒厂
First AC: 2017-09-19 Latest Modification: 2017-11-22
1 #include<iostream> 2 using namespace std; 3 int x,n,a,b,c; 4 int i; 5 int main() 6 { 7 cin>>x>>n; 8 for(i=2;i<=n;++i){ 9 a=x,b=i,c=1; 10 while(c){ 11 c=a%b,a=b,b=c; 12 } 13 cout<<i/a<<endl; 14 } 15 return 0; 16 }
E3263 丽娃河的狼人传说
First AC: 2019-01-02 Latest Modification: 2019-01-02
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,m,k,x; 4 int l,r,cnt,tmp; 5 int a[1005],s[1005]; 6 struct data{ 7 int l,r,t; 8 }b[1005]; 9 int i,j; 10 bool cmp(data a,data b) 11 { 12 return a.r<b.r; 13 } 14 int main() 15 { 16 cin>>T; 17 for(i=1;i<=T;++i){ 18 cin>>n>>m>>k; 19 cnt=0; 20 memset(a,0,sizeof a); 21 memset(s,0,sizeof s); 22 while(k--)cin>>x,a[x]=1; 23 for(j=1;j<1005;++j)s[j]=s[j-1]+a[j]; 24 for(j=0;j<m;++j)cin>>b[j].l>>b[j].r>>b[j].t; 25 sort(b,b+m,cmp); 26 for(j=0;j<m;++j){ 27 l=b[j].l,r=b[j].r; 28 tmp=b[j].t-s[r]+s[l-1]; 29 if(tmp<0)continue; 30 for(k=r;k>=l;--k){ 31 if(!tmp)break; 32 if(!a[k]){ 33 --tmp; 34 a[k]=1; 35 ++cnt; 36 for(int q=k;q<1005;++q)++s[q]; 37 } 38 } 39 if(tmp){ 40 cnt=-1; 41 break; 42 } 43 } 44 cout<<"Case "<<i<<": "<<cnt<<endl; 45 } 46 return 0; 47 }
E3264 蚂蚁
First AC: 2018-02-14 Latest Modification: 2018-03-12
1 #include<bits/stdc++.h> 2 using namespace std; 3 long n,lft,tmp; 4 long a[100001]; 5 bool b[100001]; 6 stack<long>rgt; 7 long i; 8 int main() 9 { 10 cin>>n; 11 for(i=0;i<n;++i)cin>>a[i]>>b[i]; 12 for(i=0;i<n;++i) 13 if(b[i])rgt.push(a[i]); 14 else 15 while(1){ 16 if(rgt.empty()){++lft;break;} 17 tmp=rgt.top(); 18 if(a[i]>tmp)rgt.pop(); 19 else break; 20 } 21 cout<<lft+rgt.size(); 22 return 0; 23 }
E3267 足球锦标赛
First AC: 2017-10-18 Latest Modification: 2018-06-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a[1000],T,A,B,K,power,most=0; 4 int i,j; 5 int main() 6 { 7 for(i=0;i<1000;i++)a[i]=i/100*199+i%100/10*19+i%10; 8 cin>>T; 9 for(i=1;i<=T;i++){ 10 cin>>A>>B>>K; 11 for(j=0;j<=K;j++){ 12 power=a[A+j]+a[B+K-j]; 13 if(power>most)most=power; 14 } 15 most-=(a[A]+a[B]); 16 cout<<"Case "<<i<<": "<<most<<endl; 17 most=0; 18 } 19 return 0; 20 }
E3268 神奇怪兽在哪里
First AC: 2018-02-14 Latest Modification: 2018-02-14
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m; 4 char a[103][103]; 5 int i,j,k; 6 // U D L R 7 int dx[]={-1,1, 0,0}; 8 int dy[]={ 0,0,-1,1}; 9 char d[]={'U','D','L','R'};//for convenience 10 char e[]={'D','U','R','L'}; 11 list<char>dir;//store each direction 12 void dfs(int x,int y)//find a path from (l,r) to point 'P' 13 { 14 if(a[x][y]=='P'){ 15 while(!dir.empty())cout<<dir.front(),dir.pop_front(); 16 exit(0); 17 } 18 a[x][y]='*'; 19 for(int i=0;i<4;++i){ 20 int nx=x+dx[i]; 21 int ny=y+dy[i]; 22 if(a[nx][ny]!='*'){ 23 dir.push_back(d[i]); 24 dir.push_front(e[i]); 25 dfs(nx,ny); 26 dir.pop_front(),dir.pop_back(); 27 } 28 } 29 } 30 int main() 31 { 32 cin>>n>>m; 33 memset(a,'*',sizeof(a)); 34 for(i=1;i<=n;++i)for(j=1;j<=m;++j)cin>>a[i][j]; 35 for(i=1;i<m;++i)dir.push_back('R'); 36 for(i=1;i<n;++i)dir.push_back('D'); 37 for(i=1;i<m;++i)dir.push_back('L'); 38 for(i=1;i<n;++i)dir.push_back('U'); 39 dfs(1,1); 40 return 0; 41 }
E3269 爱吃糖果的Pokemon
First AC: 2018-06-14 Latest Modification: 2018-06-14
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,r,m,maxi; 4 int cnt[1005]; 5 int rst[1005]; 6 int i,j; 7 int main() 8 { 9 cin>>n>>r>>m; 10 for(i=1;i<=m;++i){ 11 for(j=1;j<=n;++j){ 12 if(!cnt[j]){ 13 rst[i]=j; 14 ++maxi; 15 ++cnt[j]; 16 break; 17 } 18 } 19 if(!rst[i])rst[i]=rst[i-1],++cnt[rst[i]]; 20 if(i-r>0)--cnt[rst[i-r]]; 21 } 22 cout<<maxi<<endl<<rst[1]; 23 for(i=2;i<=m;++i)cout<<' '<<rst[i]; 24 return 0; 25 }
E3270 切西瓜
First AC: 2018-05-02 Latest Modification: 2018-05-02
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 int n,tmp,rst; 5 ll x[200],y[200],z[200]; 6 ll tmp1[3],tmp2[3]; 7 ll a,b,c,d; 8 int i,j,k,l; 9 10 int main() 11 { 12 cin>>n; 13 for(i=0;i<n;++i)cin>>x[i]>>y[i]>>z[i]; 14 for(i=0;i<n;++i)for(j=i+1;j<n;++j)for(k=j+1;k<n;++k){ 15 tmp1[0]=x[i]-x[j],tmp1[1]=y[i]-y[j],tmp1[2]=z[i]-z[j]; 16 tmp2[0]=x[i]-x[k],tmp2[1]=y[i]-y[k],tmp2[2]=z[i]-z[k]; 17 a=tmp1[1]*tmp2[2]-tmp1[2]*tmp2[1]; 18 b=tmp1[2]*tmp2[0]-tmp1[0]*tmp2[2]; 19 c=tmp1[0]*tmp2[1]-tmp1[1]*tmp2[0]; 20 d=-a*x[i]-b*y[i]-c*z[i]; 21 for(tmp=l=0;l<n;++l)if(a*x[l]+b*y[l]+c*z[l]+d==0)++tmp; 22 rst=max(rst,tmp); 23 } 24 cout<<rst; 25 return 0; 26 }
E3271 电话送报
First AC: 2018-04-19 Latest Modification: 2018-04-19
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll n,b,rst; 5 ll pos[1000000]; 6 ll i; 7 int main() 8 { 9 scanf("%lld%lld",&n,&b); 10 for(i=0;i<n;++i)scanf("%lld",pos+i); 11 sort(pos,pos+n); 12 rst=n*b; 13 for(i=0;i<n;++i) 14 rst=min(rst,2*pos[i]+(n-i-1)*b); 15 cout<<rst; 16 return 0; 17 }
E3274 灌水
First AC: 2017-11-12 Latest Modification: 2017-11-12
1 #include<iostream> 2 using namespace std; 3 int T,i,s[55],count,num; 4 int main() 5 { 6 while(cin>>T){ 7 num=count=0; 8 for(i=0;i<T;++i){ 9 cin>>s[i]; 10 if(s[i]!=1&&s[i]!=5){ 11 if(count==0)num=i; 12 count++; 13 } 14 } 15 if(count==0)cout<<"Deep Dark Fantasy of ECNU\n"; 16 else{ 17 for(i=0;i<num;++i)cout<<s[i]<<" "; 18 cout<<"1 1 "; 19 for(i=num+1;i<T;++i)cout<<s[i]<<" "; 20 cout<<endl; 21 } 22 } 23 return 0; 24 }
E3275 章鱼哥没有日历
First AC: 2017-10-15 Latest Modification: 2018-06-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 char c,d,e; 4 int M,D,i; 5 int main() 6 { 7 while(c=getchar()){ 8 if(c=='-'){ 9 d=getchar(); 10 e=getchar(); 11 break; 12 } 13 } 14 M=10*(d-'0')+e-'0'; 15 while(c=getchar()){ 16 if(c=='-'){ 17 d=getchar(); 18 e=getchar(); 19 break; 20 } 21 } 22 D=10*(d-'0')+e-'0'; 23 if(M==1&&D>0&&D<32)cout<<D; 24 else if(M==2&&D>0&&D<29)cout<<31+D; 25 else if(M==3&&D>0&&D<32)cout<<59+D; 26 else if(M==4&&D>0&&D<31)cout<<90+D; 27 else if(M==5&&D>0&&D<32)cout<<120+D; 28 else if(M==6&&D>0&&D<31)cout<<151+D; 29 else if(M==7&&D>0&&D<32)cout<<181+D; 30 else if(M==8&&D>0&&D<32)cout<<212+D; 31 else if(M==9&&D>0&&D<31)cout<<243+D; 32 else if(M==10&&D>0&&D<32)cout<<273+D; 33 else if(M==11&&D>0&&D<31)cout<<304+D; 34 else if(M==12&&D>0&&D<32)cout<<334+D; 35 else cout<<-1; 36 return 0; 37 }
E3276 连续正整数之和
First AC: 2017-10-28 Latest Modification: 2018-09-14
Note: 对加数个数j分奇偶
奇数j可以是加数个数,当且仅当平均数n÷j为整数且最小数>0
偶数j可以是加数个数,当且仅当平均数n÷j小数部分为0.5且最小数>0
对给定的n,满足前述的j唯一确定一种连加方法,故j的种数cnt即为所求
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,m,n,cnt; 4 int i,j; 5 int main() 6 { 7 cin>>T; 8 for(i=1;i<=T;++i){ 9 cin>>n; 10 cnt=0; 11 for(j=3;j<n;j+=2)if(n%j==0&&n/j-j/2>0)++cnt; 12 for(j=2;j<n;j+=2)if(2*n%j==0&&n%j!=0&&n/j+1-j/2>0)++cnt; 13 cout<<"Case "<<i<<": "<<cnt<<endl; 14 } 15 return 0; 16 }
E3277 今天不是愚人节
First AC: 2017-10-27 Latest Modification: 2018-06-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int i; 4 int main() 5 { 6 cin>>i; 7 if(i==1||i==3||i==999||i==1997||i==2000||i==2007||i==2016) 8 cout<<"NO"; 9 else cout<<"YES"; 10 return 0; 11 }
E3278 hcy226 的蜜月计划
First AC: 2018-05-28 Latest Modification: 2018-05-28
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,s,t,l,rst; 4 int main() 5 { 6 cin>>n; 7 while(--n)cin>>s>>t>>l,rst+=l; 8 cout<<2*rst; 9 return 0; 10 }
E3279 爱狗狗的两个dalao
First AC: 2018-01-06 Latest Modification: 2018-06-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll n,M,res,ans,status,cnt; 5 ll W[20]; 6 ll i,j,k; 7 int dfs(ll k,ll w,ll status,ll Q) 8 { 9 if(w>M)return 0; 10 if(k==n){ 11 if(w>ans)res=status,ans=w; 12 } 13 else{ 14 if((1<<k)&Q)dfs(k+1,w,status,Q),dfs(k+1,w+W[k],status|(1<<k),Q); 15 else dfs(k+1,w,status,Q); 16 } 17 } 18 int main() 19 { 20 cin>>n>>M; 21 ll Q=pow(2,n)-1; 22 for(i=0;i<n;++i)cin>>W[i]; 23 while(Q){ 24 res=ans=0; 25 dfs(0,0,0,Q); 26 Q^=res; 27 ++cnt; 28 } 29 cout<<cnt; 30 return 0; 31 }
E3281 找不到路的ultmaster
First AC: 2018-04-19 Latest Modification: 2018-04-19
1 #include<bits/stdc++.h> 2 using namespace std; 3 int m,n,h; 4 int mp[1001][1001]; 5 int dp[1001][1001]; 6 int i,j; 7 int main() 8 { 9 cin>>m>>n>>h; 10 memset(dp,0xff,sizeof(dp)); 11 dp[0][1]=dp[1][0]=h; 12 for(i=1;i<=m;++i)for(j=1;j<=n;++j){ 13 cin>>mp[i][j]; 14 if(dp[i][j-1]>=0) 15 dp[i][j]=max(dp[i][j],dp[i][j-1]+mp[i][j]); 16 if(dp[i-1][j]>=0) 17 dp[i][j]=max(dp[i][j],dp[i-1][j]+mp[i][j]); 18 } 19 cout<<max(dp[m][n],-1); 20 return 0; 21 }
E3283 食堂的座位
First AC: 2017-09-21 Latest Modification: 2017-11-22
1 #include<iostream> 2 using namespace std; 3 int N,M; 4 int main() 5 { 6 cin>>N>>M; 7 cout<<(1+N*M)/2; 8 return 0; 9 }
E3288 成绩计算
First AC: 2017-11-01 Latest Modification: 2018-06-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,M,m,s,cnt; 4 int i; 5 int main() 6 { 7 cin>>T; 8 for(i=0;i<T;++i){ 9 cin>>n; 10 s=M=m=n,cnt=1; 11 while(cin>>n,n!=-1){ 12 ++cnt,s+=n; 13 if(n>M)M=n; 14 if(n<m)m=n; 15 } 16 cout<<"case #"<<i<<":\n"<<M<<endl 17 <<m<<endl<<(int)(s/cnt)<<endl; 18 } 19 return 0; 20 }
E3289 N个整数中1的个数
First AC: 2017-10-29 Latest Modification: 2017-10-29
1 #include<iostream> 2 using namespace std; 3 int T,n,i,s; 4 int main() 5 { 6 cin>>T; 7 for(i=0;i<T;++i){ 8 cin>>n; 9 s=++n/10+n/100*10+n/1000*100+n/10000*1000+n/100000*10000; 10 if(n%10>1)++s; 11 if(n%100>10)n%100<21?s+=n%100-10:s+=10; 12 if(n%1000>100)n%1000<201?s+=n%1000-100:s+=100; 13 if(n%10000>1000)n%10000<2001?s+=n%10000-1000:s+=1000; 14 if(n%100000>10000)n%100000<20001?s+=n%100000-10000:s+=10000; 15 if(n==100001)++s; 16 cout<<"case #"<<i<<":\n"<<s<<endl; 17 } 18 return 0; 19 }
E3290 找数(II)
First AC: 2018-02-19 Latest Modification: 2018-02-19
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len,jdg1,jdg2; 4 string s; 5 int i,j,k; 6 int main() 7 { 8 cin>>T; 9 for(i=0;i<T;++i){ 10 cin>>s; 11 s="0"+s; 12 len=s.length(); 13 cout<<"case #"<<i<<":\n"; 14 if(len==2){cout<<s[1]<<endl;continue;} 15 jdg1=1; 16 while(jdg1){ 17 jdg2=0; 18 for(j=2;j<len;++j){ 19 if(s[j]>s[j-1]){ 20 jdg2=1; 21 //consider case s[j-1]=='9' 22 for(k=j-1;;--k)if(s[k]!='9'){++s[k];break;} 23 for(j=k+1;j<len;++j)s[j]='0'; 24 break; 25 } 26 } 27 if(jdg2)continue; 28 jdg1=0; 29 } 30 for(j=0;;++j)if(s[j]!='0'){cout<<s[j];break;} 31 for(k=j+1;k<len;++k)cout<<s[k]; 32 cout<<endl; 33 } 34 return 0; 35 }
E3291 素数个数排序
First AC: 2017-11-21 Latest Modification: 2018-06-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a[10001]={1,1},b[10001]; 4 int T,n,num; 5 int i,j; 6 struct data{ 7 int nump,x,y; 8 }c[10001]; 9 bool cmp(data a,data b) 10 { 11 if(a.nump==b.nump){ 12 if(a.x==b.x)return a.y<b.y; 13 else return a.x<b.x; 14 } 15 else return a.nump<b.nump; 16 } 17 int main() 18 { 19 for(i=2;i<10001;++i) 20 if(a[i]==0)for(j=2*i;j<10001;j+=i)++a[j]; 21 cin>>T; 22 for(i=0;i<10001;++i){ 23 if(a[i]==0)++num; 24 b[i]=num; 25 } 26 for(i=0;i<T;++i){ 27 cin>>n; 28 for(j=0;j<n;++j){ 29 cin>>c[j].x>>c[j].y; 30 c[j].nump=b[c[j].y]-b[c[j].x-1]; 31 } 32 sort(c,c+n,cmp); 33 cout<<"case #"<<i<<":\n"; 34 for(j=0;j<n;++j)cout<<c[j].x<<' '<<c[j].y<<endl; 35 } 36 return 0; 37 }
E3292 解密字符串
First AC: 2017-12-23 Latest Modification: 2018-06-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len; 4 string s; 5 int a[200],b[10]; 6 int i,j,k; 7 int main() 8 { 9 cin>>T; 10 for(i=0;i<T;++i){ 11 cin>>s; 12 len=s.length(); 13 memset(a,0,sizeof(a)); 14 for(j=0;j<len;++j)++a[s[j]-'A'+'a']; 15 b[0]=a['z']; 16 b[1]=a['o']-a['w']-a['r']+a['h']-a['g']; 17 b[2]=a['w']; 18 b[3]=a['h']-a['g']; 19 b[4]=a['r']-a['h']+a['g']-a['z']; 20 b[5]=a['f']-a['r']+a['h']-a['g']+a['z']; 21 b[6]=a['x']; 22 b[7]=a['v']-a['f']+a['r']-a['h']+a['g']-a['z']; 23 b[8]=a['g']; 24 b[9]=a['i']-a['f']+a['r']-a['h']-a['z']-a['x']; 25 cout<<"case #"<<i<<":\n"; 26 for(j=0;j<10;++j)for(k=0;k<b[j];++k)cout<<j; 27 cout<<endl; 28 } 29 return 0; 30 }
E3293 至尊
First AC: 2017-10-27 Latest Modification: 2018-06-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 string n; 4 int l,a[10005],b[10005],num,i,j; 5 int main() 6 { 7 while(cin>>n){ 8 l=n.length(); 9 num=a[l]=0; 10 for(i=0;i<l;++i)a[i]=(int)(n[i]-'0'); 11 for(i=l;i>0;--i) 12 if(a[i]-num-a[i-1]<0)b[i]=a[i]-num-a[i-1]+10,num=1; 13 else b[i]=a[i]-num-a[i-1],num=0; 14 b[0]=a[0]-num; 15 if(b[l]>7)b[l]-=8; 16 else{ 17 b[l]+=2; 18 for(i=l-1;;--i) 19 if(b[i]!=0){b[i]--;break;} 20 else b[i]=9; 21 } 22 for(i=0;;++i)if(b[i]!=0)break; 23 for(j=i;j<=l;++j)cout<<b[j]; 24 cout<<endl; 25 } 26 return 0; 27 }
E3296 2333
First AC: 2017-09-18 Latest Modification: 2017-11-22
1 #include<iostream> 2 using namespace std; 3 int T,n; 4 int main() 5 { 6 cin>>T; 7 while(T--){ 8 cin>>n; 9 cout<<n/3<<endl; 10 } 11 return 0; 12 }
E3297 铺瓷砖
First AC: 2018-06-17 Latest Modification: 2018-06-17
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n; 4 int dp[31][4],rst[31][4]; 5 int i; 6 int main() 7 { 8 dp[1][1]= dp[2][2]= dp[3][3]=1; 9 dp[3][1]= dp[3][2]=1; 10 rst[1][1]=rst[3][1]=rst[3][2]=1; 11 for(i=4;i<31;++i){ 12 dp[i][1]= dp[i-1][2]+ dp[i-1][3]; 13 rst[i][1]=rst[i-1][2]+rst[i-1][3]+dp[i][1]; 14 dp[i][2]= dp[i-2][1]+ dp[i-2][3]; 15 rst[i][2]=rst[i-2][1]+rst[i-2][3]; 16 dp[i][3]= dp[i-3][1]+ dp[i-3][2]; 17 rst[i][3]=rst[i-3][1]+rst[i-3][2]; 18 } 19 cin>>T; 20 while(T--){ 21 cin>>n; 22 cout<< dp[n][1]+ dp[n][2]+ dp[n][3]<<endl; 23 cout<<rst[n][1]+rst[n][2]+rst[n][3]<<endl; 24 } 25 return 0; 26 }
E3298 排队买夜宵
First AC: 2017-10-28 Latest Modification: 2018-06-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len,num,cnt; 4 int i; 5 string s; 6 int main() 7 { 8 cin>>T; 9 while(cin>>s){ 10 len=s.length(); 11 cnt=1,num=s[0]; 12 for(i=1;i<len;++i){ 13 if(cnt==0){ 14 num=s[i]; 15 ++cnt; 16 continue; 17 } 18 if(s[i]==num)++cnt; 19 else --cnt; 20 } 21 cout<<cnt<<endl; 22 } 23 return 0; 24 }
E3299 主色调
First AC: 2019-04-21 Latest Modification: 2019-04-21
Note: 将所有区间按左端点分类,这样每类区间就能一次遍历答案
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 int color[5005]; 5 int cnt[5005],ans[5005]; 6 int maxcnt,maxcolor; 7 int i,j; 8 int main() 9 { 10 while(cin>>n){ 11 for(i=1;i<=n;++i)cin>>color[i]; 12 memset(ans,0,sizeof ans); 13 for(i=1;i<=n;++i){ 14 maxcnt=0; 15 memset(cnt,0,sizeof cnt); 16 for(j=i;j<=n;++j){ 17 int t=++cnt[color[j]]; 18 if(t>maxcnt){ 19 maxcolor=color[j]; 20 maxcnt=cnt[color[j]]; 21 } 22 else if(t==maxcnt){ 23 maxcolor=min(maxcolor,color[j]); 24 } 25 ++ans[maxcolor]; 26 } 27 } 28 cout<<ans[1]; 29 for(i=2;i<=n;++i)cout<<' '<<ans[i]; 30 cout<<endl; 31 } 32 return 0; 33 }
E3302 打印
First AC: 2018-04-19 Latest Modification: 2018-04-19
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll n,x,y; 5 ll dp[10000001]; 6 ll i,j; 7 int main() 8 { 9 cin>>n>>x>>y; 10 dp[1]=x; 11 for(i=2;i<=n;++i){ 12 if(i&1){ 13 dp[i]=min(dp[i-1]+x,min(dp[i/2],dp[(i+1)/2])+x+y); 14 } 15 else{ 16 dp[i]=min(dp[i-1]+x,dp[i/2]+y); 17 } 18 } 19 cout<<dp[n]; 20 return 0; 21 }
E3304 不等式
First AC: 2018-05-09 Latest Modification: 2018-05-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 struct data{ 4 string cmp; 5 int num; 6 }a[200]; 7 int n,rst; 8 string s; 9 int b[200]; 10 int i; 11 void jdg(int m) 12 { 13 int tmp=0; 14 for(int i=0;i<n;++i){ 15 if(a[i].cmp=="<"&&m<a[i].num)++tmp; 16 else if(a[i].cmp==">"&&m>a[i].num)++tmp; 17 else if(a[i].cmp=="="&&m==a[i].num)++tmp; 18 else if(a[i].cmp=="<="&&m<=a[i].num)++tmp; 19 else if(a[i].cmp==">="&&m>=a[i].num)++tmp; 20 } 21 rst=max(rst,tmp); 22 } 23 int main() 24 { 25 cin>>n; 26 for(i=0;i<n;++i){ 27 cin>>s>>a[i].cmp>>a[i].num; 28 b[i]=a[i].num; 29 } 30 sort(b,b+n); 31 jdg(b[0]-1),jdg(b[n-1]+1); 32 for(i=0;i<n;++i)jdg(b[i]); 33 for(i=1;i<n;++i) 34 if(b[i-1]+1<b[i])jdg(b[i-1]+1); 35 cout<<rst; 36 return 0; 37 }
E3305 十亿分考
First AC: 2018-07-10 Latest Modification: 2018-07-10
Note: 随机数瞎暴力
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 typedef long double ld; 5 const int mod=1e9+1; 6 const ld eps=4e-16; 7 ld val; 8 ll up,dn,mid; 9 int main() 10 { 11 ios::sync_with_stdio(false); 12 cin>>val; 13 srand(19260817); 14 while(1){ 15 dn=rand()%mod; 16 mid=val*dn; 17 for(up=mid-10;up<=mid+10;++up){ 18 if(fabs((ld)up/dn-val)<eps){ 19 cout<<up/__gcd(up,dn)<<' '<<dn/__gcd(up,dn); 20 return 0; 21 } 22 } 23 } 24 return 0; 25 }
E3309 SBT
First AC: 2017-09-30 Latest Modification: 2018-06-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,a,maxa,mina,i; 4 int main() 5 { 6 while(cin>>n){ 7 cin>>a; 8 maxa=mina=a; 9 for(i=1;i<n;i++){ 10 cin>>a; 11 if(a>maxa)maxa=a; 12 else if(a<mina)mina=a; 13 } 14 if(maxa<31&&mina>9&&maxa-mina<4)cout<<"Is SBT\n"; 15 else cout<<"Is not SBT\n"; 16 } 17 return 0; 18 }
E3312 TTT
First AC: 2018-05-24 Latest Modification: 2018-05-24
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,rst,tmp; 4 int a[2][3]; 5 void cal(int n) 6 { 7 int jdg=0,tmp=0; 8 while(n){ 9 tmp+=n%10; 10 if(n%10==2)jdg=1; 11 n/=10; 12 } 13 ++a[jdg][tmp%3]; 14 } 15 int main() 16 { 17 while(cin>>n){ 18 memset(a,0,sizeof(a)); 19 while(n--){ 20 cin>>m; 21 cal(m); 22 } 23 rst=a[1][0]; 24 tmp=min(a[1][1],a[0][2]); 25 rst+=tmp,a[1][1]-=tmp,a[0][2]-=tmp; 26 tmp=min(a[1][2],a[0][1]); 27 rst+=tmp,a[1][2]-=tmp,a[0][1]-=tmp; 28 tmp=min(a[1][1],a[0][1]/2); 29 rst+=tmp,a[1][1]-=tmp,a[0][1]-=2*tmp; 30 tmp=min(a[1][2],a[0][2]/2); 31 rst+=tmp,a[1][2]-=tmp,a[0][2]-=2*tmp; 32 tmp=min(a[1][1],a[1][2]); 33 rst+=tmp,a[1][1]-=tmp,a[1][2]-=tmp; 34 tmp=min(a[1][1]/2,a[0][1]); 35 rst+=tmp,a[1][1]-=2*tmp,a[0][1]-=tmp; 36 tmp=min(a[1][2]/2,a[0][2]); 37 rst+=tmp,a[1][2]-=2*tmp,a[0][2]-=tmp; 38 rst+=a[1][1]/3+a[1][2]/3; 39 cout<<rst<<endl; 40 } 41 return 0; 42 }
E3313 Git
First AC: 2018-07-08 Latest Modification: 2018-07-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,x,y,w,l,r; 4 string s; 5 char ch; 6 int in[26],out[26]; 7 int dp[2005][2005]; 8 int main() 9 { 10 cin>>m>>n>>s; 11 while(m--){ 12 cin>>ch>>x>>y; 13 ch-='a'; 14 in[ch]=x; 15 out[ch]=y; 16 } 17 for(w=2;w<=n;++w){ 18 for(r=w-1;r<n;++r){ 19 l=r-w+1; 20 if(s[l]==s[r])dp[l][r]=dp[l+1][r-1]; 21 else{ 22 int tmp1=min(in[s[l]-'a'],out[s[l]-'a'])+dp[l+1][r]; 23 int tmp2=min(in[s[r]-'a'],out[s[r]-'a'])+dp[l][r-1]; 24 dp[l][r]=min(tmp1,tmp2); 25 } 26 } 27 } 28 cout<<dp[0][n-1]; 29 return 0; 30 }
E3316 和和和
First AC: 2019-02-17 Latest Modification: 2019-02-17
Note: fread快速读入模板,对着网上代码改了好久才琢磨出判断EOF办法
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int mod=998244353; 4 int a,rst; 5 namespace io 6 { 7 const int maxn=200<<20; 8 static char buf[maxn],*pbuf=buf,*end; 9 void init() 10 { 11 int c=fread(buf,1,maxn,stdin); 12 end=buf+c; 13 } 14 bool readint() 15 { 16 a=0; 17 if(pbuf==end)return 0; 18 while(pbuf!=end&&!isdigit(*pbuf))++pbuf; 19 while(pbuf!=end&&isdigit(*pbuf)){ 20 (a*=10)+=*pbuf-'0'; 21 ++pbuf; 22 } 23 return 1; 24 } 25 } 26 using namespace io; 27 int main() 28 { 29 io::init(); 30 while(io::readint())(rst+=a)%=mod; 31 cout<<rst; 32 return 0; 33 }
E3321 坏掉的里程表
First AC: 2018-04-18 Latest Modification: 2018-04-18
1 #include<bits/stdc++.h> 2 using namespace std; 3 long n,t,minc; 4 long d[1000],v[1000]; 5 long i; 6 double sum(double c) 7 { 8 double rst=0; 9 for(i=0;i<n;++i){ 10 rst+=*(d+i)/(c+*(v+i)); 11 if(rst>t)return rst; 12 } 13 return rst; 14 } 15 double bi(double lft,double rgt) 16 { 17 if(fabs(lft-rgt)<0.0000001)return lft; 18 double mid=(lft+rgt)/2,tmp=sum(mid); 19 if(fabs(tmp-t)<0.00000001)return mid; 20 if(tmp<t)return bi(lft,mid); 21 else return bi(mid,rgt); 22 } 23 int main() 24 { 25 scanf("%ld%ld",&n,&t); 26 minc=1000; 27 for(i=0;i<n;++i){ 28 scanf("%ld%ld",d+i,v+i); 29 if(v[i]<minc)minc=*(v+i); 30 } 31 printf("%.7f",bi(-1.0*minc,10000000)); 32 return 0; 33 }
E3322 万年历
First AC: 2017-10-16 Latest Modification: 2018-06-09
Note: 判断y-m-d是0001-01-01开始的第几天,用n表示
对n模7,同一剩余类里的星期数相同,用b[]描述剩余类
尽管我们不知道0001-01-01星期几,但可以通过样例加加减减来调整
1 #include<bits/stdc++.h> 2 using namespace std; 3 int y,m,d,n,i; 4 int a[]={0,0,31,59,90,120,151,181,212,243,273,304,334}; 5 string b[]={"Sunday","Monday","Tuesday","Wednesday", 6 "Thursday","Friday","Saturday"}; 7 int main() 8 { 9 scanf("%d-%d-%d",&y,&m,&d); 10 for(i=8;i<=y;i++)if((i%4==0&&i%100!=0)||i%400==0)n++; 11 //累加0001-y年的2.29天数 12 n+=y,n+=a[m],n+=d; 13 //与+=365*(y-1)同质,不改变星期数周期性 14 if(((y%4==0&&y%100!=0)||y%400==0)&&m<3)n--; 15 //减当年可能多算的2.29 16 cout<<b[(n+0)%7]; 17 //n%7,调整剩余类,0是根据样例得到的调整数 18 return 0; 19 }
E3323 罗塞塔石碑
First AC: 2019-02-07 Latest Modification: 2019-02-07
1 #include<bits/stdc++.h> 2 using namespace std; 3 int m,n,len; 4 int dis[26][26]; 5 char x,y; 6 string s,t; 7 int i,j,k; 8 int main() 9 { 10 cin>>m>>n; 11 memset(dis,0x3f,sizeof dis); 12 for(i=0;i<26;++i)dis[i][i]=0; 13 while(m--){ 14 cin>>x>>y; 15 dis[x-'a'][y-'a']=1; 16 } 17 for(k=0;k<26;++k){ 18 for(i=0;i<26;++i){ 19 for(j=0;j<26;++j){ 20 dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]); 21 } 22 } 23 } 24 while(n--){ 25 cin>>s>>t; 26 if((len=s.length())!=t.length()){ 27 cout<<"no\n"; 28 continue; 29 } 30 bool flag=1; 31 for(i=0;i<len;++i){ 32 if(dis[s[i]-'a'][t[i]-'a']>1e8){ 33 flag=0; 34 break; 35 } 36 } 37 flag? cout<<"yes\n":cout<<"no\n"; 38 } 39 return 0; 40 }
E3325 今天是个特殊的日子
First AC: 2018-05-18 Latest Modification: 2018-05-18
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a[13]={0,31,59,90,120,151,181,212,243,273,304,334,365}; 4 int b[13]={0,31,60,91,121,152,182,213,244,274,305,335,366}; 5 int np,y,rst; 6 int pri[200]; 7 int i; 8 bool ispri(int n) 9 { 10 for(int i=2;i*i<=n;++i)if(!(n%i))return 0; 11 return 1; 12 } 13 int cal(int y,int n) 14 { 15 int i; 16 if((!(y%4)&&(y%100))||!(y%400)){ 17 for(i=12;;--i)if(b[i]<n)break; 18 return y*10000+(i+1)*100+n-b[i]; 19 } 20 else{ 21 for(i=12;;--i)if(a[i]<n)break; 22 return y*10000+(i+1)*100+n-a[i]; 23 } 24 } 25 int main() 26 { 27 for(i=2;i<360;++i)if(ispri(i))pri[np++]=i; 28 cin>>y; 29 for(i=0;i<np;++i)if(ispri(cal(y,pri[i])))++rst; 30 if((!(y%4)&&(y%100))||!(y%400))printf("%.9lf",rst/366.0); 31 else printf("%.9lf",rst/365.0); 32 return 0; 33 }
E3329 奇异收缩的剪影
First AC: 2018-06-09 Latest Modification: 2018-06-09
1 #include<bits/stdc++.h> 2 #define M 1000000007 3 using namespace std; 4 typedef long long ll; 5 ll n,m,rst; 6 ll cnt[4501],dp[4501][9000]; 7 bool pri[9000]; 8 ll i,j; 9 int main() 10 { 11 ios::sync_with_stdio(false); 12 cin>>n; 13 while(n--)cin>>m,++cnt[m]; 14 dp[3499][0]=1; 15 for(i=3499;i<4500;++i){ 16 if(cnt[i+1])for(j=0;j<9000;++j)if(dp[i][j]) 17 (dp[i+1][j^(i+1)]+=(cnt[i+1]+1)/2*dp[i][j])%=M; 18 for(j=0;j<9000;++j)if(dp[i][j]) 19 (dp[i+1][j]+=(cnt[i+1]/2+1)*dp[i][j])%=M; 20 } 21 for(i=2;i<4500;++i)if(!pri[i])for(j=i+i;j<9000;j+=i)pri[j]=1; 22 for(i=2;i<9000;++i)if(!pri[i])rst=(rst+dp[4500][i])%M; 23 cout<<rst; 24 return 0; 25 }
E3330 沉陷过往的幻灭
First AC: 2018-05-04 Latest Modification: 2018-05-04
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef unsigned long long ll; 4 const ll M=414200,N=314160,P=1e9+7; 5 string s; 6 ll len,num,base=1,rst; 7 bool a[M],b[M]; 8 ll i,j; 9 int main() 10 { 11 ios::sync_with_stdio(false); 12 cin>>s; 13 len=s.length(); 14 for(i=j=0;i<len;++i)a[i]=s[len-1-i]-'0'; 15 cin>>s; 16 len=s.length(); 17 for(i=j=0;i<len;++i)b[i]=s[len-1-i]-'0'; 18 for(i=0;i<N;++i){ 19 if(b[i])++num; 20 if(a[i])rst=(rst+(N-num)*base)%P; 21 else rst=(rst+num*base)%P; 22 base=base*2%P; 23 } 24 for(i=N;i<M;++i){ 25 if(b[i-N])--num; 26 if(a[i])rst=(rst+(N-num)*base)%P; 27 else rst=(rst+num*base)%P; 28 base=base*2%P; 29 } 30 cout<<rst; 31 return 0; 32 }
E3331 凝滞逆转的神谕
First AC: 2018-05-04 Latest Modification: 2018-05-04
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll n,m,rst; 5 ll a[100005]; 6 ll i; 7 int main() 8 { 9 ios::sync_with_stdio(false); 10 cin>>n; 11 for(i=1;i<100005;++i)a[i]=i; 12 bool jdg=1; 13 for(i=1;i<=n;++i){ 14 cin>>m; 15 if(a[i]==m)continue; 16 if(a[i+1]==m){ 17 a[i+1]=a[i]; 18 ++rst; 19 continue; 20 } 21 if(a[i+2]==m){ 22 a[i+2]=a[i+1]; 23 a[i+1]=a[i]; 24 rst+=2; 25 continue; 26 } 27 jdg=0; 28 break; 29 } 30 jdg? cout<<rst:cout<<"Too chaotic"; 31 return 0; 32 }
E3332 无限剑制的终焉
First AC: 2018-05-01 Latest Modification: 2018-05-01
1 #include<bits/stdc++.h> 2 #define M 10000 3 using namespace std; 4 typedef unsigned long long ll; 5 ll T,n,k,rst; 6 ll base[65]={1}; 7 bool jdg[65]; 8 ll mod[65]; 9 ll i; 10 int main() 11 { 12 cin>>T; 13 for(i=1;i<65;++i)base[i]=2*base[i-1]; 14 while(T--){ 15 cin>>n>>k; 16 k*=9; 17 memset(jdg,0,sizeof(jdg)); 18 for(i=63;n;--i) 19 if(n>=base[i]) 20 jdg[i]=1,n-=base[i]; 21 mod[0]=10%k; 22 for(i=1;i<65;++i){ 23 ll hgh=mod[i-1]/M,low=mod[i-1]%M; 24 mod[i]=(mod[i-1]*M%k*hgh%k+mod[i-1]*low%k)%k; 25 } 26 rst=1; 27 for(i=0;i<65;++i) 28 if(jdg[i]){ 29 ll hgh=mod[i]/M,low=mod[i]%M; 30 rst=(rst*M%k*hgh%k+rst*low%k)%k; 31 } 32 rst=(rst+k-1)%k; 33 cout<<rst/9<<endl; 34 } 35 return 0; 36 }
E3333 渴求真实的天使
First AC: 2018-05-04 Latest Modification: 2018-05-04
1 #include<bits/stdc++.h> 2 using namespace std; 3 long n,k; 4 long a[100000]; 5 long i; 6 bool ispri(long n) 7 { 8 for(long i=2;i*i<=n;++i) 9 if(!(n%i))return 0; 10 return 1; 11 } 12 int main() 13 { 14 ios::sync_with_stdio(false); 15 cin>>n>>k; 16 for(i=0;i<n;++i)cin>>a[i]; 17 for(i=n-1;i;--i)a[i/2]=__gcd(a[i/2],a[i]); 18 if(a[0]==1)cout<<0; 19 else if(ispri(a[0]))cout<<k/a[0]*a[0]; 20 else for(i=k;;--i)if(__gcd(a[0],i)>1){cout<<i;break;} 21 return 0; 22 }
E3334 绝望恸哭的愚者
First AC: 2018-06-09 Latest Modification: 2018-06-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n; 4 int main() 5 { 6 cin>>T; 7 while(T--){ 8 cin>>n; 9 if(n<2)cout<<"yes\n"; 10 else n%2? cout<<"no\n":cout<<"yes\n"; 11 } 12 return 0; 13 }
E3336 Beautiful Numbers
First AC: 2018-06-09 Latest Modification: 2018-06-09
Note: 一个很显然而有用的事实是,进制数和给定数在该进制下表示位数是单调减的
所以只要找到一个满足题设的进制数base,就不需要再检索比base大的进制数了
另一个很显然的事实是,给定数n(n>2),base=n-1是它的一个解(事实上也是最大解)
因为暴力求解base的复杂度是O(nlgn),所以对于10^6以内的进制数暴力检索
而对于超过10^6的进制数,不难发现符合条件的进制数表示不能超过3位
这是因为当base>10^6时,base*base*base>=10^18+1>n
恰等于三位时,只要判断二次方程base*base+base+1=n是否有正整数解
恰等于两位时,解正好就是前面提到的base=n-1
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 typedef long double ld; 5 ll n,base; 6 ll i; 7 int main() 8 { 9 ios::sync_with_stdio(false); 10 cin>>n; 11 for(i=2;i<1000000;++i){ 12 ll tmp=n; 13 bool jdg=1; 14 while(tmp){ 15 if(tmp%i!=1){ 16 jdg=0; 17 break; 18 } 19 tmp/=i; 20 } 21 if(jdg){ 22 base=i; 23 break; 24 } 25 } 26 if(!base){ 27 ll tmp=pow((ld)(4*n-3),(ld)0.5)+0.5; 28 if(tmp*tmp==4*n-3)base=(tmp-1)/2; 29 else base=n-1; 30 } 31 cout<<base; 32 return 0; 33 }
E3338 双塔问题
First AC: 2018-05-02 Latest Modification: 2018-05-02
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 long a; 5 long dp[101][10001];//前i个积木差为j的矮塔最大高度 6 int i,j; 7 int main() 8 { 9 cin>>n; 10 for(i=1;i<10001;++i)dp[0][i]=-2333333; 11 for(i=1;i<=n;++i){ 12 cin>>a; 13 for(j=10000;j>=0;--j){ 14 dp[i][j]=dp[i-1][j];//不放 15 if(j>=a)dp[i][j]=max(dp[i][j],dp[i-1][j-a]);//放高 16 if(j+a<10001)dp[i][j]=max(dp[i][j],dp[i-1][j+a]+a);//放矮仍矮 17 if(j<=a)dp[i][j]=max(dp[i][j],dp[i-1][a-j]+a-j);//放矮为高 18 } 19 } 20 cout<<max(dp[n][0],0L); 21 return 0; 22 }
E3342 经典的猜数游戏
First AC: 2018-05-08 Latest Modification: 2018-05-08
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long lft,rgt,mid; 4 string s; 5 int main() 6 { 7 lft=-1e9; 8 rgt=1e9; 9 while(1){ 10 mid=(lft+rgt)/2; 11 cout<<mid<<endl; 12 cin>>s; 13 if(s=="big")rgt=mid-1; 14 else if(s=="small")lft=mid+1; 15 else break; 16 } 17 return 0; 18 }
E3343 丽娃河
First AC: 2017-10-14 Latest Modification: 2018-06-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 string s=" HACKING"; 5 int main() 6 { 7 cin>>n; 8 cout<<s[n]; 9 return 0; 10 }
E3344 送命题
First AC: 2017-09-19 Latest Modification: 2018-06-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a[35]={0,1,1,2,3,5,8,13,39,124,514,836,1053,4139,12815,61135,104937, 4 792517,1454698,9679838,17354310,9735140,1760750,986050,621360, 5 113815,581437,1252496,7676706,13019288,94367798,178067380}; 6 int n; 7 int main() 8 { 9 cin>>n; 10 cout<<a[n]; 11 return 0; 12 }
E3345 猜猜看
First AC: 2018-04-15 Latest Modification: 2018-04-15
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long lft,rgt,num; 4 int main() 5 { 6 lft=-1e9; 7 rgt=1e9; 8 while(1){ 9 cin>>num; 10 if(lft==rgt){ 11 cout<<"equal\n"; 12 fflush(stdout); 13 break; 14 } 15 if(num*2>rgt+lft){ 16 cout<<"big\n"; 17 fflush(stdout); 18 rgt=num-1; 19 } 20 else{ 21 cout<<"small\n"; 22 fflush(stdout); 23 lft=num+1; 24 } 25 } 26 return 0; 27 }
E3346 皇后问题
First AC: 2018-04-04 Latest Modification: 2018-04-04
1 #include<bits/stdc++.h> 2 #define N 100001 3 using namespace std; 4 long n,x,y,rst; 5 long a[N],b[N],c[2*N],d[2*N]; 6 long i; 7 int main() 8 { 9 cin>>n; 10 for(i=0;i<n;++i){ 11 cin>>x>>y; 12 ++a[x],++b[y],++c[x+y],++d[x-y+n]; 13 } 14 ++n; 15 for(i=1;i<n;++i){ 16 if(a[i]>1)rst+=a[i]*(a[i]-1)/2; 17 if(b[i]>1)rst+=b[i]*(b[i]-1)/2; 18 } 19 n=(n-1)*2; 20 for(i=1;i<n;++i){ 21 if(c[i]>1)rst+=c[i]*(c[i]-1)/2; 22 if(d[i]>1)rst+=d[i]*(d[i]-1)/2; 23 } 24 cout<<rst; 25 return 0; 26 }
E3348 树的顺序存储结构
First AC: 2018-06-29 Latest Modification: 2018-06-29
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m; 4 struct node{ 5 stack<int>nxt; 6 }a[100000]; 7 int i; 8 void post(int m) 9 { 10 while(!a[m].nxt.empty()){ 11 post(a[m].nxt.top()); 12 a[m].nxt.pop(); 13 } 14 cout<<m<<' '; 15 } 16 int main() 17 { 18 cin>>n>>m; 19 for(i=1;i<n;++i)cin>>m,a[m].nxt.push(i); 20 post(0); 21 return 0; 22 }
E3350 林吉吉的野望
First AC: 2017-11-03 Latest Modification: 2018-06-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,k,a[1005],b[1005],m,cnt; 4 int i,j; 5 int main() 6 { 7 cin>>T; 8 for(i=0;i<T;++i){ 9 memset(a,0,sizeof(a)),memset(b,0,sizeof(b)),cnt=0; 10 cin>>n>>k; 11 for(j=1;j<=n;++j)cin>>m,a[m]=j; 12 for(j=1000;cnt<k;--j) 13 if(a[j]!=0)++b[a[j]],++cnt; 14 cnt=0; 15 for(j=1;j<=1000;++j) 16 if(b[j]==1) 17 if(cnt==0)cout<<j,cnt++; 18 else cout<<" "<<j; 19 cout<<endl; 20 } 21 return 0; 22 }
E3351 泡咖啡
First AC: 2017-09-25 Latest Modification: 2018-06-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,a,b,c; 4 int main() 5 { 6 cin>>T; 7 while(cin>>a>>b){ 8 c=a+b; 9 if(a==0)cout<<"LBB made no coffee today.\n"; 10 else{ 11 if(a==1)cout<<"LBB made a cup of coffee today.\n"; 12 else cout<<"LBB made "<<a<<" cups of coffee today.\n"; 13 } 14 if(b==0)cout<<"JDG made no coffee today.\n"; 15 else{ 16 if(b==1)cout<<"JDG made a cup of coffee today.\n"; 17 else cout<<"JDG made "<<b<<" cups of coffee today.\n"; 18 } 19 if(c==0)cout<<"BOSS had no coffee today.\n"; 20 else{ 21 if(c==1)cout<<"BOSS had a cup of coffee today.\n"; 22 else cout<<"BOSS had "<<c<<" cups of coffee today.\n"; 23 } 24 } 25 return 0; 26 }
E3352 密集数
First AC: 2017-09-25 Latest Modification: 2018-06-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,a,sum; 4 int i; 5 int main() 6 { 7 cin>>T; 8 while(cin>>n){ 9 sum=0; 10 for(i=0;i<n;i++)cin>>a,sum+=a; 11 for(i=1;i<n;i++)sum*=2; 12 cout<<sum<<"\n"; 13 } 14 return 0; 15 }
E3354 领外卖
First AC: 2017-12-15 Latest Modification: 2018-06-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T; 4 bool a[100005]; 5 long b[318]; 6 long i,j,k,n; 7 int main() 8 { 9 for(i=1;i<317;++i)b[i]=i*i,a[b[i]]=1; 10 b[317]=100001; 11 for(i=2;i<318;++i){ 12 for(j=b[i-1]+1;j<b[i];++j){ 13 for(k=1;k<i;++k){ 14 if(!a[j-b[k]]){a[j]=1;break;} 15 } 16 } 17 } 18 cin>>T; 19 while(T--){ 20 cin>>n; 21 if(a[n])cout<<"JDG"; 22 else cout<<"LJJ"; 23 cout<<" will get takeaways!\n"; 24 } 25 return 0; 26 }
E3357 吉吉木的野望
First AC: 2018-05-10 Latest Modification: 2018-05-10
1 #include<bits/stdc++.h> 2 using namespace std; 3 int d,k; 4 double rst; 5 int i; 6 double cal(int n) 7 { 8 if(n>k)return 0; 9 return 1.0/(1+(n-1)*d+cal(n+1)); 10 } 11 int main() 12 { 13 cin>>d>>k; 14 k=min(k,200); 15 printf("%.12lf",cal(1)); 16 return 0; 17 }
E3358 种水稻
First AC: 2017-09-25 Latest Modification: 2018-06-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n,a,b; 4 int i; 5 int main() 6 { 7 cin>>T; 8 while(cin>>n){ 9 b=n; 10 for(i=0;i<n;i++){ 11 cin>>a; 12 if(a>10)b--; 13 } 14 cout<<b<<"\n"; 15 } 16 return 0; 17 }
E3359 吉吉木和五彩方块
First AC: 2018-04-20 Latest Modification: 2018-04-20
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll n,m,num; 5 ll cnt[50001]; 6 struct data{ 7 ll color,time; 8 }a[50001]; 9 queue<ll>rst[50001]; 10 ll i,j; 11 bool cmp(data a,data b) 12 { 13 return a.time>b.time; 14 } 15 int main() 16 { 17 ios::sync_with_stdio(false); 18 cin>>n; 19 if(n<2){ 20 cin>>m; 21 cout<<m; 22 return 0; 23 } 24 for(i=0;i<n;++i)cin>>m,++cnt[m]; 25 for(i=1;i<50001;++i)if(cnt[i]){ 26 a[num].color=i; 27 a[num].time=cnt[i]; 28 ++num; 29 } 30 sort(a,a+num,cmp); 31 if(a[0].time>(n+1)/2){cout<<-1;return 0;} 32 for(i=a[0].time-1;i>=0;--i)rst[i].push(a[0].color); 33 int pos=0; 34 j=1; 35 while(1){ 36 if(a[j].time){ 37 rst[pos].push(a[j].color); 38 if(++pos==a[0].time)pos=0; 39 --a[j].time; 40 } 41 else{ 42 if(++j==num)break; 43 } 44 } 45 cout<<rst[0].front(); 46 rst[0].pop(); 47 for(i=0;i<a[0].time;++i) 48 while(!rst[i].empty()){ 49 cout<<' '<<rst[i].front(); 50 rst[i].pop(); 51 } 52 return 0; 53 }
E3362 推锅大战
First AC: 2017-09-28 Latest Modification: 2018-06-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int i,n,a,b,c; 4 long Fx,Fy,Fz; 5 int main() 6 { 7 cin>>n; 8 for(i=0;i<n;i++){ 9 cin>>a>>b>>c; 10 Fx+=a;Fy+=b;Fz+=c; 11 } 12 if(Fx==0&&Fy==10&&Fz==0)cout<<"NO";else cout<<"YES"; 13 return 0; 14 }
E3363 吉吉木和木木吉
First AC: 2017-10-04 Latest Modification: 2018-06-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 double win,eq,lose; 4 double p; 5 int i,j,jjm,mmj,a[6]; 6 int main() 7 { 8 for(i=0;i<6;i++){ 9 cin>>jjm; 10 a[i]=jjm; 11 } 12 for(i=0;i<6;i++){ 13 cin>>mmj; 14 for(j=0;j<6;j++){ 15 if(a[j]>mmj)win++; 16 else{if(a[j]<mmj)lose++; 17 else eq++; 18 } 19 } 20 } 21 win/=36,lose/=36,eq/=36; 22 p=(1-eq*eq*eq*eq*eq*eq*eq*eq*eq*eq)/(1-eq)*win; 23 printf("%.5f",p); 24 }
E3364 吉吉木坐地铁
First AC: 2019-02-11 Latest Modification: 2019-02-11
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=1e5+1; 4 int n,q,num; 5 int u,v; 6 queue<int>e[maxn]; 7 int city_num[maxn]; 8 int city_len[maxn]; 9 int dis[maxn]; 10 int i,j; 11 void dfs(int head,int u,int pre,int d) 12 { 13 while(!e[u].empty()){ 14 city_num[u]=num; 15 dis[u]=d+1; 16 int v=e[u].front(); 17 e[u].pop(); 18 if(v==pre)continue; 19 if(v==head){ 20 city_len[num]=d+1; 21 continue; 22 } 23 dfs(head,v,u,d+1); 24 } 25 } 26 int main() 27 { 28 cin>>n>>q; 29 for(i=1;i<=n;++i){ 30 cin>>u>>v; 31 e[u].push(v); 32 e[v].push(u); 33 } 34 for(i=1;i<=n;++i){ 35 if(!city_num[i]){ 36 ++num; 37 dfs(i,i,0,0); 38 } 39 } 40 while(q--){ 41 cin>>u>>v; 42 if(city_num[u]!=city_num[v])cout<<"-1\n"; 43 else{ 44 num=city_num[u]; 45 int len=city_len[num]; 46 int tmp=abs(dis[u]-dis[v]); 47 cout<<min(tmp,len-tmp)<<endl; 48 } 49 } 50 return 0; 51 }
E3366 这题没验过啊?
First AC: 2017-10-16 Latest Modification: 2018-06-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 int main() 5 { 6 cin>>n; 7 if(n==0)cout<<"infiniteee";else{ 8 if(n%11==0||n%11==10)cout<<"ultmaster"; 9 else cout<<"infiniteee"; 10 } 11 return 0; 12 }
E3367 咸鱼翻身
First AC: 2018-04-21 Latest Modification: 2018-04-21
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,cnt; 4 int a[100001],dp[100001]; 5 int i; 6 int main() 7 { 8 cin>>n; 9 for(i=1;i<=n;++i){ 10 cin>>m; 11 if(m)++cnt; 12 a[i]=(m? -1:1); 13 } 14 for(i=1;i<=n;++i)dp[i]=max(dp[i-1],0)+a[i]; 15 for(i=1;i<n;++i)if(dp[i]>dp[n])dp[n]=dp[i]; 16 cout<<dp[n]+cnt; 17 return 0; 18 }
E3368 逃课吃鸡
First AC: 2018-04-19 Latest Modification: 2018-04-19
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,rst; 4 struct data{ 5 string s; 6 int t; 7 }a[10000]; 8 bool cmp(data a,data b) 9 { 10 if(a.s!=b.s)return a.s<b.s; 11 return a.t>b.t; 12 } 13 int i; 14 int main() 15 { 16 cin>>n; 17 for(i=0;i<n;++i)cin>>a[i].s>>a[i].t; 18 sort(a,a+n,cmp); 19 if(n<2)rst=a[0].t; 20 else{ 21 rst=a[0].t+a[1].t; 22 for(i=2;i<n;++i) 23 if(a[i].s!=a[i-2].s) 24 rst+=a[i].t; 25 } 26 cout<<rst; 27 return 0; 28 }
E3369 三千米健身步道
First AC: 2018-07-02 Latest Modification: 2018-07-02
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,x,y,rst; 4 int cnt[1001],fr[1000],to[1000]; 5 int i; 6 int main() 7 { 8 cin>>n; 9 for(i=1;i<n;++i){ 10 cin>>x>>y; 11 ++cnt[x],++cnt[y]; 12 fr[i]=x,to[i]=y; 13 } 14 for(i=1;i<n;++i){ 15 rst+=(cnt[fr[i]]-1)*(cnt[to[i]]-1); 16 } 17 cout<<rst; 18 return 0; 19 }
E3380 Ants
First AC: 2018-06-09 Latest Modification: 2018-06-09
Note: 蚂蚁的编号对题目没有影响,每只蚂蚁速度相同
所以蚂蚁相遇的反向实际上可以忽略,默认直接穿过对方
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len,n,maxn,minn,m; 4 int main() 5 { 6 cin>>T; 7 while(T--){ 8 cin>>len>>n; 9 maxn=minn=0; 10 while(n--){ 11 cin>>m; 12 maxn=max(maxn,max(m,len-m)); 13 minn=max(minn,min(m,len-m)); 14 } 15 cout<<minn<<' '<<maxn<<endl; 16 } 17 return 0; 18 }
E3383 Saruman’s Army
First AC: 2017-12-30 Latest Modification: 2017-12-30
1 #include<iostream> 2 using namespace std; 3 int r,n,m,cnt,lft,rgt; 4 int a[1005]; 5 int i,j; 6 int main() 7 { 8 while(cin>>r>>n,r+1){ 9 for(cnt=i=0;i<n;++i)cin>>m,++a[m]; 10 rgt=1000; 11 while(n){ 12 for(i=rgt;i>=0;--i)if(a[i])break; 13 lft=i-r; 14 if(lft<0)lft=0; 15 for(j=lft;;++j){ 16 if(a[j]){ 17 n-=a[j],a[j]=0; 18 rgt=j-r; 19 if(rgt<0)rgt=0; 20 break; 21 } 22 } 23 for(++cnt,j=rgt;j<=i;++j){ 24 if(a[j])n-=a[j],a[j]=0; 25 } 26 --rgt; 27 if(rgt<0)rgt=0; 28 } 29 cout<<cnt<<endl; 30 } 31 return 0; 32 }
E3384 食物链
First AC: 2019-02-27 Latest Modification: 2019-03-03
Note: 差分维护的带标记并查集,a吃(被吃)a的祖先就把a的标记置为1(-1)
这样a到根节点的路径标记和模3就能反映a与根节点的关系
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=5e4+5; 4 int n,k,ans; 5 int pre[maxn],val[maxn]; 6 int d,x,y; 7 int i; 8 int find(int x) 9 { 10 int r=x,s=0,i; 11 while(pre[r]!=r)s+=val[r],r=pre[r]; 12 s+=val[r]; 13 while(x!=r){ 14 i=val[x]; 15 val[x]=s%3; 16 s=(s-i+3)%3; 17 i=pre[x]; 18 pre[x]=r; 19 x=i; 20 } 21 return r; 22 } 23 void join(int x,int y) 24 { 25 int fx=find(x),fy=find(y); 26 if(fx!=fy){ 27 pre[fx]=fy; 28 val[fx]=(val[y]-val[x]+d-1+3)%3; 29 } 30 else{ 31 if((val[x]-val[y]-(d-1)+6)%3)++ans; 32 } 33 } 34 int main() 35 { 36 cin>>n>>k; 37 for(i=1;i<=n;++i)pre[i]=i; 38 while(k--){ 39 cin>>d>>x>>y; 40 if(x>n||y>n||(d==2&&x==y))++ans; 41 else join(x,y); 42 } 43 cout<<ans; 44 return 0; 45 }
E3416 摄氏华氏转换
First AC: 2017-10-27 Latest Modification: 2018-06-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s; 4 int n; 5 int main() 6 { 7 cin>>s; 8 n=10*(int)(s[0]-'0')+(int)(s[1]-'0'); 9 if(s[2]=='c')printf("%.2ff",9.0*n/5+32); 10 else printf("%.2fc",(n-32)*5.0/9); 11 return 0; 12 }
E3417 求幂
First AC: 2017-10-27 Latest Modification: 2018-06-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a,b,i; 4 long long c=1; 5 int main() 6 { 7 cin>>a>>b; 8 for(i=0;i<b;i++)c*=a; 9 cout<<c; 10 return 0; 11 }
E3418 数字字符个数
First AC: 2017-11-07 Latest Modification: 2017-11-07
1 #include<stdio.h> 2 int main() 3 { 4 int count=0; 5 char c; 6 while((c=getchar())!='\n') 7 if(c>='0'&&c<='9') 8 count++; 9 printf("%d",count); 10 return 0; 11 }
E3419 假水仙花数
First AC: 2017-10-27 Latest Modification: 2017-10-27
1 main(){puts("0\n1\n1634\n8208\n9474");}
E3430 Urban Design
First AC: 2018-05-10 Latest Modification: 2018-05-10
1 #include<bits/stdc++.h> 2 using namespace std; 3 int S,T,xl,xr,yl,yr,cntl,cntr; 4 struct line{ 5 double k,b; 6 bool vert; 7 }a[10000]; 8 int i; 9 int cnt(int x,int y) 10 { 11 int rst=0; 12 for(i=0;i<S;++i){ 13 if(a[i].vert){ 14 if(x<a[i].b)++rst; 15 } 16 else{ 17 if(y>a[i].k*x+a[i].b)++rst; 18 } 19 } 20 return rst; 21 } 22 int main() 23 { 24 cin>>S; 25 for(i=0;i<S;++i){ 26 cin>>xl>>yl>>xr>>yr; 27 if(xl==xr){ 28 a[i].vert=1; 29 a[i].b=xl; 30 } 31 else{ 32 a[i].k=(yr-yl)*1.0/(xr-xl); 33 a[i].b=yl-a[i].k*xl; 34 } 35 } 36 cin>>T; 37 while(T--){ 38 cin>>xl>>yl>>xr>>yr; 39 cntl=cnt(xl,yl); 40 cntr=cnt(xr,yr); 41 if((cntl-cntr)%2)cout<<"different\n"; 42 else cout<<"same\n"; 43 } 44 return 0; 45 }
E3441 唐纳德与子串(Easy)
First AC: 2017-12-08 Latest Modification: 2018-06-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s,t; 4 long lens,lent,T,lft,rgt,most,tmp,rst; 5 struct data{ 6 long num[100005]; 7 long cnt; 8 }a[26]; 9 bool flag; 10 long i,j; 11 int main() 12 { 13 cin>>s; 14 lens=s.length(); 15 for(i=0;i<lens;++i)a[s[i]-'a'].num[a[s[i]-'a'].cnt++]=i; 16 cin>>T; 17 while(T--){ 18 cin>>lft>>rgt>>t; 19 lent=t.length(); 20 --lft,rgt=rgt-lent+2; 21 most=a[t[0]-'a'].cnt; 22 for(i=rst=0;i<most;++i){ 23 tmp=a[t[0]-'a'].num[i]; 24 if(tmp>lft&&tmp<rgt){ 25 for(j=flag=1;j<lent;++j){ 26 if(t[j]!=s[tmp+j]){ 27 flag=0; 28 break; 29 } 30 } 31 if(flag)++rst; 32 } 33 } 34 cout<<rst<<endl; 35 } 36 return 0; 37 }
E3443 Sec2HMS
First AC: 2017-12-08 Latest Modification: 2017-12-08
1 #include<iostream> 2 using namespace std; 3 int second,h,m,s,rst; 4 int main() 5 { 6 cin>>second; 7 h=second/3600,m=second%3600/60,s=second%60; 8 rst=10000*h+100*m+s; 9 cout<<rst<<endl; 10 return 0; 11 }
E3444 四位分数加法
First AC: 2017-12-08 Latest Modification: 2017-12-08
1 #include<iostream> 2 using namespace std; 3 long long a,b,c,d,tmp1=1,tmp2,tmp3,m,n; 4 int main() 5 { 6 cin>>a>>b>>c>>d; 7 cout<<a<<'/'<<b<<'+'<<c<<'/'<<d<<'='; 8 n=tmp2=b*d,m=tmp3=a*d+b*c; 9 while(tmp1)tmp1=tmp2%tmp3,tmp2=tmp3,tmp3=tmp1; 10 cout<<m/tmp2<<'/'<<n/tmp2<<'.'; 11 return 0; 12 }
E3445 字符串替换
First AC: 2017-12-08 Latest Modification: 2018-06-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 string s,t,r; 4 int lens,lent,tmp,cnt,flag; 5 int i,j; 6 int main() 7 { 8 cin>>s>>t>>r; 9 lens=s.length(),lent=t.length(); 10 if(lent>lens){cout<<s<<endl;return 0;} 11 tmp=lens-lent+1; 12 for(i=0;i<tmp;++i){ 13 if(s[i]==t[0]){ 14 for(cnt=j=1;j<lent;++j){ 15 if(s[i+j]!=t[j]){cnt=0;break;} 16 } 17 if(cnt)cout<<r,i+=lent-1,flag=1; 18 else cout<<s[i],flag=0; 19 } 20 else cout<<s[i],flag=0; 21 } 22 if(flag)for(j=i;j<lens;++j)cout<<s[j]; 23 else for(j=tmp;j<lens;++j)cout<<s[j]; 24 cout<<endl; 25 return 0; 26 }
E3446 骰子点数和问题
First AC: 2017-12-08 Latest Modification: 2018-06-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m; 4 double pro[2001][12001]; 5 int i,j,k; 6 int main() 7 { 8 cin>>n>>m; 9 if(m>6*n){ 10 cout<<0<<endl; 11 return 0; 12 } 13 for(i=1;i<7;++i)pro[1][i]=1.0/6; 14 for(i=2;i<=n;++i) 15 for(j=i;j<=m;++j) 16 for(k=1;k<j&&k<7;++k) 17 pro[i][j]+=pro[i-1][j-k]/6.0; 18 printf("%.6f\n",pro[n][m]); 19 return 0; 20 }
E3451 易位构词
First AC: 2017-12-08 Latest Modification: 2018-06-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 char c; 4 long long cnt,i,len; 5 int b[26]; 6 struct data{ 7 char v,t; 8 long num; 9 }a[100001]; 10 bool cmp1(data m,data n) 11 { 12 if(b[m.v-'a']-b[n.v-'a'])return b[m.v-'a']>b[n.v-'a']; 13 if(m.v-n.v)return m.v<n.v; 14 return m.num<n.num; 15 } 16 bool cmp2(data m,data n) 17 { 18 return m.num<n.num; 19 } 20 int main() 21 { 22 while((c=getchar())!='\n')a[cnt].num=cnt,a[cnt++].v=c,++b[c-'a']; 23 sort(a,a+cnt,cmp1); 24 len=b[a[0].v-'a']; 25 if(len>cnt/2){cout<<"impossible";return 0;} 26 for(i=0;i<cnt;++i)a[i].t=a[(i+len)%cnt].v; 27 sort(a,a+cnt,cmp2); 28 for(i=0;i<cnt;++i)cout<<a[i].t; 29 return 0; 30 }
E3452 唐纳德先生和假骰子
First AC: 2017-12-08 Latest Modification: 2017-12-08
1 #include<iostream> 2 using namespace std; 3 int a[5],b[6],c[6]; 4 int i,j,p; 5 int main() 6 { 7 cin>>p; 8 for(i=0;i<6;++i)cin>>b[i]; 9 for(i=0;i<6;++i)cin>>c[i]; 10 for(i=0;i<6;++i){ 11 for(j=0;j<6;++j)a[(b[i]+c[j])%p]++; 12 } 13 for(i=1;i<p;++i)if(a[i]!=a[i-1]){cout<<"NO";return 0;} 14 cout<<"YES"; 15 return 0; 16 }
E3455 Huge Numbers(Small)
First AC: 2017-12-10 Latest Modification: 2018-06-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll T,a,n,p,rst; 5 ll i,j; 6 ll pow(ll a,ll b,ll p) 7 { 8 ll rst=1,base=a; 9 while(b){ 10 if(b&1)(rst*=base)%=p; 11 (base*=base)%=p; 12 b>>=1; 13 } 14 return rst; 15 } 16 int main() 17 { 18 cin>>T; 19 for(i=1;i<=T;++i){ 20 cin>>a>>n>>p; 21 rst=a%p; 22 for(j=2;j<=n;++j)rst=pow(rst,j,p); 23 cout<<"Case #"<<i<<": "<<rst<<endl; 24 } 25 return 0; 26 }
E3456 Huge Numbers(Large)
First AC: 2018-06-09 Latest Modification: 2018-06-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll T,a,n,p,rst; 5 ll i,j; 6 ll pow(ll a,ll b,ll p) 7 { 8 ll rst=1,base=a; 9 while(b){ 10 if(b&1)(rst*=base)%=p; 11 (base*=base)%=p; 12 b>>=1; 13 } 14 return rst; 15 } 16 int main() 17 { 18 cin>>T; 19 for(i=1;i<=T;++i){ 20 cin>>a>>n>>p; 21 rst=a%p; 22 for(j=2;j<=n;++j)rst=pow(rst,j,p); 23 cout<<"Case #"<<i<<": "<<rst<<endl; 24 } 25 return 0; 26 }
E3457 Cards Game(Small)
First AC: 2019-05-16 Latest Modification: 2019-05-16
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=105,maxm=10005; 4 int T,n,m; 5 long long ans; 6 int red[maxn],blue[maxn]; 7 struct edge{ 8 int u,v,w; 9 }e[maxm]; 10 int pre[maxn]; 11 int i,j,k; 12 bool cmp(edge a,edge b) 13 { 14 return a.w<b.w; 15 } 16 int find(int x) 17 { 18 int r=x,i; 19 while(pre[r]!=r)r=pre[r]; 20 while(pre[x]!=r)i=pre[x],pre[x]=r,x=i; 21 return r; 22 } 23 void join(int x,int y,int z) 24 { 25 int fx=find(x),fy=find(y); 26 if(fx!=fy){ 27 pre[fx]=fy; 28 ans+=z; 29 } 30 } 31 int main() 32 { 33 cin>>T; 34 for(i=1;i<=T;++i){ 35 cin>>n; 36 for(j=1;j<=n;++j)cin>>red[j]; 37 for(j=1;j<=n;++j)cin>>blue[j]; 38 m=0; 39 for(j=1;j<n;++j){ 40 for(k=j+1;k<=n;++k){ 41 e[m].u=j; 42 e[m].v=k; 43 e[m].w=min(red[j]^blue[k],red[k]^blue[j]); 44 ++m; 45 } 46 } 47 sort(e,e+m,cmp); 48 ans=0; 49 for(j=1;j<=n;++j)pre[j]=j; 50 for(j=0;j<m;++j)join(e[j].u,e[j].v,e[j].w); 51 cout<<"Case #"<<i<<": "<<ans<<endl; 52 } 53 return 0; 54 }
E3458 Cards Game(Large)
First AC: 2019-05-16 Latest Modification: 2019-05-16
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=105,maxm=10005; 4 int T,n,m; 5 long long ans; 6 int red[maxn],blue[maxn]; 7 struct edge{ 8 int u,v,w; 9 }e[maxm]; 10 int pre[maxn]; 11 int i,j,k; 12 bool cmp(edge a,edge b) 13 { 14 return a.w<b.w; 15 } 16 int find(int x) 17 { 18 int r=x,i; 19 while(pre[r]!=r)r=pre[r]; 20 while(pre[x]!=r)i=pre[x],pre[x]=r,x=i; 21 return r; 22 } 23 void join(int x,int y,int z) 24 { 25 int fx=find(x),fy=find(y); 26 if(fx!=fy){ 27 pre[fx]=fy; 28 ans+=z; 29 } 30 } 31 int main() 32 { 33 cin>>T; 34 for(i=1;i<=T;++i){ 35 cin>>n; 36 for(j=1;j<=n;++j)cin>>red[j]; 37 for(j=1;j<=n;++j)cin>>blue[j]; 38 m=0; 39 for(j=1;j<n;++j){ 40 for(k=j+1;k<=n;++k){ 41 e[m].u=j; 42 e[m].v=k; 43 e[m].w=min(red[j]^blue[k],red[k]^blue[j]); 44 ++m; 45 } 46 } 47 sort(e,e+m,cmp); 48 ans=0; 49 for(j=1;j<=n;++j)pre[j]=j; 50 for(j=0;j<m;++j)join(e[j].u,e[j].v,e[j].w); 51 cout<<"Case #"<<i<<": "<<ans<<endl; 52 } 53 return 0; 54 }
E3461 在哈尔滨的寒风中
First AC: 2017-12-08 Latest Modification: 2017-12-08
1 #include<iostream> 2 using namespace std; 3 long long m,n,t; 4 int main() 5 { 6 cin>>m>>n; 7 if(m>n)t=m,m=n,n=t; 8 if(m==1)cout<<0; 9 else if(m==2){ 10 if(n==2)cout<<0; 11 else cout<<(n-1)/2*((n-1)/2+1)+(n-2)/2*((n-2)/2+1); 12 } 13 else if(m==3&&n==3)cout<<28; 14 else cout<<m*n*(m*n-1)/2; 15 return 0; 16 }
E3466 Baza
First AC: 2017-12-13 Latest Modification: 2018-06-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,q,cnt; 4 long a[1001][1001],b[1001]; 5 int i,j; 6 int main() 7 { 8 cin>>n>>m; 9 for(i=0;i<n;++i)for(j=0;j<m;++j)cin>>a[i][j]; 10 cin>>q; 11 while(q--){ 12 for(i=0;i<m;++i)cin>>b[i]; 13 for(cnt=n,i=0;i<n;++i){ 14 for(j=0;j<m;++j)if(b[j]>0&&a[i][j]!=b[j]){--cnt;break;} 15 } 16 cout<<cnt<<endl; 17 } 18 return 0; 19 }
E3467 Telefoni
First AC: 2017-12-14 Latest Modification: 2017-12-14
1 #include<cstdio> 2 int main() 3 { 4 long n,d,cnt,tmp; 5 long a[300001]={0,}; 6 long i,j; 7 scanf("%ld%ld%ld",&n,&d,&tmp); 8 for(i=1;i<d;++i){ 9 scanf("%ld",&tmp); 10 for(cnt=i,j=0;j<i;++j)if(a[j]<cnt)cnt=a[j]; 11 a[i]=cnt; 12 if(!tmp)++a[i]; 13 } 14 for(i=d;i<n;++i){ 15 scanf("%ld",&tmp); 16 for(cnt=i,j=i-d;j<i;++j)if(a[j]<cnt)cnt=a[j]; 17 a[i]=cnt; 18 if(!tmp)++a[i]; 19 } 20 printf("%ld",a[n-1]); 21 return 0; 22 }
E3468 Igra
First AC: 2018-06-09 Latest Modification: 2018-06-09
Note: 贪心+dfs剪枝(好像可以证明剪枝判定恰能得到最优解,如果能证明就可以不用dfs直接贪心)
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,len; 4 string s,t; 5 char rst[5000]; 6 int sums[3],sumt[3]; 7 int i; 8 void dfs(int i) 9 { 10 if(i==len){ 11 for(int j=0;j<len;++j) 12 cout<<rst[j]; 13 exit(0); 14 } 15 if(t[i]!='a'&&sums[0]){ 16 --sums[0]; 17 --sumt[t[i]-'a']; 18 if(sumt[0]<=sums[1]+sums[2]&& 19 sumt[1]<=sums[0]+sums[2]&& 20 sumt[2]<=sums[0]+sums[1]){ 21 rst[i]='a'; 22 dfs(i+1); 23 } 24 ++sums[0]; 25 ++sumt[t[i]-'a']; 26 } 27 if(t[i]!='b'&&sums[1]){ 28 --sums[1]; 29 --sumt[t[i]-'a']; 30 if(sumt[0]<=sums[1]+sums[2]&& 31 sumt[1]<=sums[0]+sums[2]&& 32 sumt[2]<=sums[0]+sums[1]){ 33 rst[i]='b'; 34 dfs(i+1); 35 } 36 ++sums[1]; 37 ++sumt[t[i]-'a']; 38 } 39 if(t[i]!='c'&&sums[2]){ 40 --sums[2]; 41 --sumt[t[i]-'a']; 42 if(sumt[0]<=sums[1]+sums[2]&& 43 sumt[1]<=sums[0]+sums[2]&& 44 sumt[2]<=sums[0]+sums[1]){ 45 rst[i]='c'; 46 dfs(i+1); 47 } 48 ++sums[2]; 49 ++sumt[t[i]-'a']; 50 } 51 } 52 int main() 53 { 54 cin>>n>>s>>t; 55 len=s.length(); 56 for(i=0;i<len;++i){ 57 ++sums[s[i]-'a']; 58 ++sumt[t[i]-'a']; 59 } 60 dfs(0); 61 return 0; 62 }
E3469 Savrsen
First AC: 2017-12-14 Latest Modification: 2017-12-14
1 #include<iostream> 2 using namespace std; 3 long long a[10004500]={0,1,2,4}; 4 long long i,j,m,rst,A,B; 5 int main() 6 { 7 ios::sync_with_stdio(false); 8 for(i=2;i<3164;++i){ 9 a[i*i]+=i; 10 m=10000000/i+1; 11 for(j=i+1;j<m;++j)a[i*j]+=i+j; 12 } 13 cin>>A>>B; 14 for(i=A;i<=B;++i){ 15 m=a[i]+1-i; 16 m>0? rst+=m:rst-=m; 17 } 18 cout<<rst; 19 return 0; 20 }
E3486 最大的子串
First AC: 2018-01-21 Latest Modification: 2018-01-21
1 #include<iostream> 2 using namespace std; 3 string s; 4 int len,n,cnt; 5 int i,j; 6 int main() 7 { 8 cin>>s; 9 len=s.length(); 10 for(i=len-1;i>=0;--i){ 11 if(s[i]^'0')break; 12 --len; 13 } 14 if(len==0){cout<<0;return 0;} 15 n=0; 16 for(i=1;i<len;++i){ 17 cnt=len-i; 18 for(j=0;j<cnt;++j){ 19 if(s[i+j]<s[n+j])break; 20 if(s[i+j]>s[n+j]){n=i;break;} 21 } 22 } 23 for(i=n;i<len;++i)cout<<s[i]; 24 return 0; 25 }
E3487 石头剪刀布的套路
First AC: 2018-01-21 Latest Modification: 2018-01-21
1 #include<iostream> 2 using namespace std; 3 int p,q,r,a,b,c; 4 int main() 5 { 6 cin>>p>>q>>r; 7 for(int i=0;i<100;++i){ 8 if(a>b&&a>c)cout<<2,++b; 9 else if(b>a&&b>c)cout<<3,++c; 10 else if(c>a&&c>b)cout<<1,++a; 11 else if(a==b){ 12 if(c==a){ 13 if(p==3)cout<<3,++c; 14 else if(q==3)cout<<1,++a; 15 else cout<<2,++b; 16 } 17 else if(p>q)cout<<3,++c; 18 else cout<<1,++a; 19 } 20 else if(a==c){ 21 if(p>r)cout<<3,++c; 22 else cout<<2,++b; 23 } 24 else if(q>r)cout<<1,++a; 25 else cout<<2,++b; 26 cout<<endl; 27 } 28 return 0; 29 }
E3489 坑爹的售票机
First AC: 2019-03-28 Latest Modification: 2019-03-28
Note: 最后直接枚举优先级是不正确的,道理类似5,10,25元纸币凑n元最少张数不能贪心
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll n,k,p,ans; 5 ll cost[]={1,5,10,20,50,100}; 6 ll now[15]; 7 ll dp[2005]; 8 ll i,j; 9 void check(ll sumx,ll pri) 10 { 11 ll tmp=max(0LL,sumx-1000)/pri*pri; 12 ans=min(ans,tmp/pri*now[pri]+dp[sumx-tmp]); 13 } 14 int main() 15 { 16 cin>>n>>k>>p; 17 for(i=1;i<=k;++i){ 18 ll tmp=i*p; 19 for(j=5;j>=0;--j){ 20 now[i]+=tmp/cost[j]; 21 tmp%=cost[j]; 22 } 23 } 24 memset(dp,0x3f,sizeof dp); 25 dp[0]=0; 26 for(i=1;i<=k;++i){ 27 for(j=i;j<2005;++j){ 28 dp[j]=min(dp[j],dp[j-i]+now[i]); 29 } 30 } 31 ans=1e18; 32 for(i=1;i<=k;++i)check(n,i); 33 cout<<ans; 34 return 0; 35 }
E3492 简单排序题
First AC: 2018-04-29 Latest Modification: 2018-04-29
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll n,tmp,rst; 5 ll a[100001]; 6 ll i; 7 int main() 8 { 9 ios::sync_with_stdio(false); 10 cin>>n; 11 cin>>a[1]; 12 for(i=2;i<=n;++i)tmp+=i,cin>>a[i],a[i]-=tmp; 13 sort(a+1,a+n+1); 14 tmp=a[(n+1)/2]; 15 for(i=1;i<=n;++i)rst+=abs(tmp-a[i]); 16 cout<<rst; 17 return 0; 18 }
E3493 你们要的与质数与偶数相关的送分题
First AC: 2018-04-29 Latest Modification: 2018-04-29
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll n; 5 bool ispri(ll n) 6 { 7 for(ll i=2;i*i<=n;++i) 8 if(!(n%i))return 0; 9 return 1; 10 } 11 int main() 12 { 13 cin>>n; 14 if(ispri(n))cout<<1; 15 else if(!(n%2)||ispri(n-2))cout<<2; 16 else cout<<3; 17 return 0; 18 }
E3494 归零
First AC: 2018-02-21 Latest Modification: 2018-02-21
1 #include<bits/stdc++.h> 2 using namespace std; 3 long n,p,s,a; 4 int main() 5 { 6 cin>>n>>p; 7 if(p<n){ 8 while(n--)cin>>a,s=(s+a)%p; 9 s? cout<<"NO":cout<<"YES"; 10 } 11 else{ 12 cin>>s; 13 while(--n){ 14 cin>>a; 15 if(a!=s)p=0; 16 } 17 p? cout<<"YES":cout<<"NO"; 18 19 } 20 return 0; 21 }
E3496 贪吃的xjj和贪心的oxx
First AC: 2018-04-18 Latest Modification: 2018-04-18
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long n,oxx,xjj; 4 struct data{ 5 long long num,item; 6 }a[1000001]; 7 stack<long long>s,t; 8 long long i; 9 bool cmp(data a,data b) 10 { 11 return a.num>b.num; 12 } 13 int main() 14 { 15 cin>>n; 16 for(i=1;i<=n;++i)cin>>a[i].num,a[i].item=i; 17 sort(a+1,a+n+1,cmp); 18 for(i=1;i<=n;++i){ 19 if(oxx<xjj)oxx+=a[i].num,s.push(a[i].item); 20 else xjj+=a[i].num,t.push(a[i].item); 21 } 22 cout<<"Yes\n"; 23 if(oxx<xjj){ 24 cout<<s.size()<<endl<<s.top(); 25 s.pop(); 26 while(!s.empty())cout<<' '<<s.top(),s.pop(); 27 } 28 else{ 29 cout<<t.size()<<endl<<t.top(); 30 t.pop(); 31 while(!t.empty())cout<<' '<<t.top(),t.pop(); 32 } 33 return 0; 34 }
E3499 回家咯
First AC: 2018-02-13 Latest Modification: 2018-02-13
1 #include<bits/stdc++.h> 2 using namespace std; 3 long x,y,z; 4 int main() 5 { 6 cin>>x>>y>>z; 7 if(x+2*y>z&&z>x&&x+z>2*y)cout<<(x+2*y-z)/2.0; 8 else cout<<"Wrong"; 9 }
E3502 密码碰撞
First AC: 2018-06-28 Latest Modification: 2018-06-28
Note: 字符串s是t的子串的必要条件是s长度小于等于t
而此时若s和t长度相等,则必有s和t完全相同
一种做法是利用trie树,先对输入字符串相同合并后按长度降序排序
对个数为cnt的每种字符串,其内部对答案的贡献为cnt×(cnt-1)÷2
遍历排序后的字符串,先匹配其在trie树出现的次数,乘上cnt便是答案
再把字符串的所有子串去重后按权值cnt插入trie树,为后面更短的字符串提供匹配源
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,num,tmp,rst; 4 string s,t; 5 map<string,int>fd,mp; 6 map<string,int>::iterator it; 7 struct data{ 8 string s; 9 int cnt,len; 10 }a[20000]; 11 int i,j,k; 12 bool cmp(data a,data b) 13 { 14 return a.len>b.len; 15 } 16 int main() 17 { 18 cin>>n; 19 for(i=0;i<n;++i){ 20 cin>>s; 21 if((it=fd.find(s))!=fd.end())++a[it->second].cnt; 22 else{ 23 a[num].s=s; 24 a[num].cnt=1; 25 a[num].len=s.length(); 26 fd.insert(pair<string,int>(s,num++)); 27 } 28 } 29 fd.clear(); 30 sort(a,a+num,cmp); 31 for(i=0;i<num;++i){ 32 rst+=a[i].cnt*(a[i].cnt-1); 33 if((it=fd.find(a[i].s))!=fd.end())rst+=a[i].cnt*it->second; 34 mp.clear(); 35 for(j=0;j<a[i].len;++j){ 36 for(k=j;k<a[i].len;++k){ 37 t=a[i].s.substr(j,k-j+1); 38 if((it=mp.find(t))==mp.end()){ 39 mp.insert(pair<string,int>(t,1)); 40 if((it=fd.find(t))!=fd.end())it->second+=a[i].cnt; 41 else fd.insert(pair<string,int>(t,a[i].cnt)); 42 } 43 } 44 } 45 } 46 cout<<rst; 47 return 0; 48 }
E3504 打工是不可能打工的
First AC: 2018-03-30 Latest Modification: 2018-03-30
1 #include<bits/stdc++.h> 2 using namespace std; 3 long n,tmp,debt,cnt; 4 long a[200001]; 5 long i; 6 int main() 7 { 8 cin>>n; 9 for(i=0;i<n;++i)scanf("%ld",a+i); 10 for(i=0;i<n;++i){ 11 scanf("%ld",&tmp); 12 debt+=tmp; 13 } 14 sort(a,a+n); 15 for(i=n-1;i>=0;--i){ 16 if(debt<=0)break; 17 debt-=*(a+i); 18 ++cnt; 19 } 20 if(debt>0)cout<<"Game Over!"; 21 else cout<<cnt; 22 return 0; 23 }
E3505 这也是一道排序题
First AC: 2018-04-29 Latest Modification: 2018-04-29
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll n,rst; 5 ll a[100000]; 6 ll i; 7 int main() 8 { 9 ios::sync_with_stdio(false); 10 cin>>n; 11 for(i=0;i<n;++i)cin>>a[i]; 12 for(i=n-1;i;--i)a[i]-=a[i-1]; 13 sort(a+1,a+n); 14 rst=a[0]; 15 for(i=1;i<n;++i)a[i]+=a[i-1],rst+=a[i]; 16 cout<<rst; 17 return 0; 18 }
E3506 斐波那契数列
First AC: 2018-05-07 Latest Modification: 2018-05-07
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int M=19260817; 4 int fib[100001]={0,1,2}; 5 int hsh[M]; 6 int i; 7 int main() 8 { 9 for(i=3;i<100001;++i)fib[i]=(fib[i-1]+fib[i-2])%M; 10 for(i=1;i<100001;++i)hsh[fib[i]]=i; 11 char c; 12 int tmp=0; 13 while((c=getchar())!=EOF){ 14 if(c!='\n')tmp=(tmp*10+c-'0')%M; 15 else{ 16 cout<<hsh[tmp]<<endl; 17 tmp=0; 18 } 19 } 20 return 0; 21 }
E3507 坑爹的售票机(Easy)
First AC: 2019-03-27 Latest Modification: 2019-03-27
Note: 枚举同时买1至k张的最小取票数,然后dp答案
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,k,p; 4 int cost[]={1,5,10,20,50,100}; 5 int now[15]; 6 int dp[1005]; 7 int i,j; 8 int main() 9 { 10 cin>>n>>k>>p; 11 for(i=1;i<=k;++i){ 12 int tmp=i*p; 13 for(j=5;j>=0;--j){ 14 now[i]+=tmp/cost[j]; 15 tmp%=cost[j]; 16 } 17 } 18 memset(dp,0x3f,sizeof dp); 19 dp[0]=0; 20 for(i=1;i<=k;++i){ 21 for(j=i;j<=n;++j){ 22 dp[j]=min(dp[j],dp[j-i]+now[i]); 23 } 24 } 25 cout<<dp[n]<<endl; 26 return 0; 27 }
E3528 西班牙馅饼
First AC: 2018-06-28 Latest Modification: 2018-06-28
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,tmp1,tmp2,rst; 4 int i; 5 int main() 6 { 7 cin>>n>>m; 8 while(n--){ 9 tmp1=0; 10 for(i=0;i<m;++i){ 11 cin>>tmp2; 12 if(tmp2>tmp1)tmp1=tmp2; 13 } 14 rst+=tmp1; 15 } 16 cout<<rst; 17 return 0; 18 }
E3529 梵高先生
First AC: 2018-04-13 Latest Modification: 2018-04-13
1 #include<bits/stdc++.h> 2 using namespace std; 3 long a[21][21]; 4 int n; 5 int i,j; 6 int main() 7 { 8 a[1][1]=1; 9 a[2][1]=1; 10 a[2][2]=1; 11 for(i=3;i<21;++i){ 12 a[i][1]=1; 13 for(j=2;j<i;++j)a[i][j]=a[i-1][j-1]+a[i-1][j]; 14 a[i][i]=1; 15 } 16 cin>>n; 17 for(i=1;i<=n;++i){ 18 for(j=1;j<i;++j)cout<<a[i][j]<<' '; 19 cout<<a[i][i]<<endl; 20 } 21 return 0; 22 }
E3530 和你在一起
First AC: 2018-04-13 Latest Modification: 2018-06-28
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 string s[20]; 5 int i; 6 int main() 7 { 8 cin>>n; 9 for(i=0;i<n;++i)cin>>s[i]; 10 sort(s,s+n); 11 for(i=n-1;i>=0;--i)cout<<s[i]; 12 return 0; 13 }
E3531 定西
First AC: 2018-04-13 Latest Modification: 2018-04-13
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,k,m; 4 bool dmg[101]; 5 int dp[101]; 6 int i; 7 int main() 8 { 9 cin>>n>>k; 10 while(k--)cin>>m,dmg[m]=1; 11 if(!dmg[1])dp[1]=1; 12 if(!dmg[2])dp[2]=dp[1]+1; 13 if(!dmg[3])dp[3]=dp[1]+dp[2]+1; 14 for(i=4;i<=n;++i){ 15 if(dmg[i])continue; 16 if(!dmg[i-3])dp[i]+=dp[i-3]; 17 if(!dmg[i-2])dp[i]+=dp[i-2]; 18 if(!dmg[i-1])dp[i]+=dp[i-1]; 19 } 20 cout<<dp[n]; 21 return 0; 22 }
E3532 热河路
First AC: 2018-04-13 Latest Modification: 2018-04-13
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll n,a,tmp; 5 int main() 6 { 7 scanf("%lld",&n); 8 while(n--){ 9 scanf("%lld",&tmp); 10 a=8*tmp-7; 11 ll b=sqrt(a); 12 double c=sqrt(a); 13 if(fabs(b*1.0-c)!=0)puts("0"); 14 else puts("1"); 15 } 16 return 0; 17 }
E3533 庙会
First AC: 2018-04-13 Latest Modification: 2018-04-13
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,k; 4 int i; 5 int main() 6 { 7 cin>>n>>m>>k; 8 for(i=0;i<k;++i)cout<<i%n+1<<' '<<i%m+1<<endl; 9 return 0; 10 }
E3540 ultmaster的小迷妹们
First AC: 2018-04-21 Latest Modification: 2018-04-21
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,x,y; 4 int main() 5 { 6 cin>>n>>x>>y; 7 n%__gcd(x,y)? cout<<"No":cout<<"Yes"; 8 return 0; 9 }
E3541 代码查重
First AC: 2018-07-04 Latest Modification: 2018-07-04
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,k,x,y; 4 int a[100005],b[100005]; 5 map< pair<int,int> ,bool>mp; 6 map< pair<int,int> ,bool>::iterator it; 7 int i; 8 int main() 9 { 10 cin>>n>>m>>k; 11 if(n!=m){ 12 cout<<"No"; 13 return 0; 14 } 15 for(i=0;i<n;++i)cin>>a[i]; 16 for(i=0;i<m;++i)cin>>b[i]; 17 for(i=0;i<k;++i){ 18 cin>>x>>y; 19 mp.insert(pair< pair<int,int> ,bool>(pair<int,int>(x,y),1)); 20 mp.insert(pair< pair<int,int> ,bool>(pair<int,int>(y,x),1)); 21 } 22 for(i=0;i<n;++i){ 23 it=mp.find(pair<int,int>(a[i],b[i])); 24 if(a[i]!=b[i]&&it==mp.end()){ 25 cout<<"No"; 26 return 0; 27 } 28 } 29 cout<<"Yes"; 30 return 0; 31 }
E3542 神奇的魔术
First AC: 2018-04-21 Latest Modification: 2018-04-21
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,num,jdg,pos; 4 int tmp[129],rst[129]; 5 int i,j; 6 void print() 7 { 8 int i; 9 cout<<tmp[1]; 10 for(i=2;i<=num;++i)cout<<' '<<tmp[i]; 11 cout<<endl; 12 } 13 void find(int lft,int rgt,int n) 14 { 15 if(lft+1==rgt){ 16 rst[lft]=n; 17 return; 18 } 19 if(lft+2==rgt){ 20 if(rst[lft]){ 21 rst[lft+1]=n; 22 return; 23 } 24 else if(rst[lft+1]){ 25 rst[lft]=n; 26 return; 27 } 28 } 29 int mid=(lft+rgt)/2,i; 30 for(i=1;i<=num;++i)tmp[i]=1; 31 for(i=mid;i<rgt;++i) 32 if(i!=pos)tmp[i]=n; 33 print(); 34 cin>>jdg; 35 if(jdg==num)exit(0); 36 if(jdg!=2)find(lft,mid,n); 37 else find(mid,rgt,n); 38 } 39 int main() 40 { 41 cin>>n; 42 num=pow(2,n); 43 for(i=1;i<=num;++i){ 44 for(j=1;j<=num;++j)tmp[j]=2; 45 tmp[i]=1; 46 print(); 47 cin>>jdg; 48 if(jdg==num)exit(0); 49 if(jdg==2){ 50 rst[i]=1; 51 pos=i; 52 break; 53 } 54 } 55 for(i=2;i<=num;++i)find(1,num+1,i); 56 cout<<rst[1]; 57 for(i=2;i<=num;++i)cout<<' '<<rst[i]; 58 cout<<endl; 59 cin>>jdg; 60 return 0; 61 }
E3544 小迷妹在哪儿
First AC: 2018-04-21 Latest Modification: 2018-04-21
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,T,rst; 4 struct data{ 5 int a,t; 6 }girl[100001]; 7 int dp[100001][301]; 8 bool jdg[100001][301]; 9 int i,j; 10 bool cmp(data a,data b) 11 { 12 return a.a*b.t>b.a*a.t; 13 } 14 int main() 15 { 16 cin>>n>>T; 17 for(i=1;i<=n;++i) 18 cin>>girl[i].a>>girl[i].t; 19 sort(girl+1,girl+n+1,cmp); 20 jdg[0][T]=1; 21 for(i=0;i<n;++i)for(j=1;j<=T;++j){ 22 if(jdg[i][j]){ 23 dp[i+1][j]=max(dp[i+1][j],dp[i][j]); 24 jdg[i+1][j]=1; 25 if(j>=girl[i+1].t){ 26 dp[i+1][j-girl[i+1].t]= 27 max(dp[i][j]+(j-girl[i+1].t)* 28 girl[i+1].a,dp[i+1][j-girl[i+1].t]); 29 jdg[i+1][j-girl[i+1].t]=1; 30 } 31 } 32 } 33 for(i=0;i<=T;++i)if(dp[n][i]>rst)rst=dp[n][i]; 34 cout<<rst; 35 return 0; 36 }
E3560 浮点数模运算
First AC: 2018-05-12 Latest Modification: 2018-05-12
1 #include<bits/stdc++.h> 2 using namespace std; 3 long double a,b; 4 long long c,d; 5 int main() 6 { 7 cin>>a>>b; 8 c=(long double)(a*1e9+0.5); 9 d=(long double)(b*1e9+0.5); 10 printf("%.15f",c%d*1.0/1e9); 11 return 0; 12 }
E3561 数螃蟹
First AC: 2018-05-12 Latest Modification: 2018-05-12
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const ll M=1e18; 5 ll n,d,cnt,tmp; 6 ll a[100005],b[100005]; 7 ll i,j,k; 8 int main() 9 { 10 cin>>n; 11 for(i=0;i<n;++i)cin>>a[i]; 12 if(n>15){ 13 tmp=0; 14 for(j=3;j<n;++j){ 15 tmp=a[j]-a[j-1]; 16 if(tmp==a[j-1]-a[j-2]&&tmp==a[j-2]-a[j-3]){ 17 b[j]=a[j]; 18 for(k=j+1;k<n;++k)b[k]=b[k-1]+tmp; 19 for(k=j-1;k>=0;--k)b[k]=b[k+1]-tmp; 20 cout<<b[0]; 21 for(k=1;k<n;++k)cout<<' '<<b[k]; 22 return 0; 23 } 24 } 25 } 26 else{ 27 for(i=0;i<n;++i)for(j=i+1;j<n;++j){ 28 d=(a[j]-a[i])/(j-i); 29 if(a[j]+(n-j-1)*d>1e18||a[j]+(n-j-1)*d<-1e18)continue; 30 if(a[j]-j*d>1e18||a[j]-j*d<-1e18)continue; 31 b[j]=a[j]; 32 for(k=j+1;k<n;++k)b[k]=b[k-1]+d; 33 for(k=j-1;k>=0;--k)b[k]=b[k+1]-d; 34 cnt=0; 35 for(k=0;k<n;++k)if(a[k]!=b[k])++cnt; 36 if(cnt<4){ 37 cout<<b[0]; 38 for(k=1;k<n;++k)cout<<' '<<b[k]; 39 return 0; 40 } 41 } 42 } 43 return 0; 44 }
E3564 无聊的数学题
First AC: 2018-05-12 Latest Modification: 2018-05-12
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll n,k,p,rst; 5 ll power(ll a,ll b,ll p) 6 { 7 ll base=a,rst=1; 8 while(b){ 9 if(b&1)rst=(rst*base)%p; 10 base=(base*base)%p; 11 b>>=1; 12 } 13 return rst; 14 } 15 int main() 16 { 17 cin>>n>>k>>p; 18 rst=power(2,n,p-1); 19 rst=power(2,(rst+p-1-n)%(p-1)+p-1,p); 20 k? cout<<rst:cout<<(rst+p-1)%p; 21 return 0; 22 }
E3591 GPS数据处理
First AC: 2018-07-11 Latest Modification: 2018-07-11
1 import java.util.*; 2 3 class Result 4 { 5 public static int ret = 0; 6 public static final int mod = 65536; 7 public static int result = 0; 8 } 9 10 public class Main 11 { 12 public static void cal(String s) 13 { 14 int len = s.length(); 15 for(int i = 0; i < len; ++i) { 16 Result.ret ^= s.charAt(i); 17 } 18 } 19 20 public static void main(String[] args) 21 { 22 Scanner in = new Scanner(System.in); 23 String s; 24 while(true) { 25 s = in.nextLine(); 26 if(s.equals("END"))break; 27 String[] part = s.split(","); 28 if(part.length != 13)continue; 29 if(part[0].equals("$GPRMC") == false)continue; 30 if(part[2].equals("A") == false)continue; 31 if((part[4].equals("N")||part[4].equals("S"))==false)continue; 32 if((part[6].equals("W")||part[6].equals("E"))==false)continue; 33 if(part[12].contains("*") == false)continue; 34 Result.ret = 0; 35 Main.cal("$"); 36 for(int i = 0; i < 12; ++i)Main.cal(part[i]); 37 int len = part[12].length(); 38 for(int i = 0; i < len; ++i) { 39 if(part[12].substring(i, i+1).equals("*")) { 40 Main.cal(part[12].substring(0, i)); 41 Result.ret %= Result.mod; 42 if(Result.ret == Integer.parseInt(part[12].substring(i+1,len),16)) { 43 Result.result = Integer.parseInt(part[1].substring(0, 6)); 44 } 45 break; 46 } 47 } 48 } 49 Result.result = (Result.result + 80000) % 240000; 50 int hh = Result.result / 10000; 51 int mm = Result.result % 10000/100; 52 int ss = Result.result % 100; 53 System.out.printf("%02d:%02d:%02d", hh, mm, ss); 54 } 55 }
E3601 恢复古诗
First AC: 2018-07-05 Latest Modification: 2018-07-05
1 #include<bits/stdc++.h> 2 using namespace std; 3 int P,L; 4 string s; 5 struct data{ 6 int item; 7 string s[105]; 8 }a[1005]; 9 map<string,int>mp; 10 map<string,int>::iterator it; 11 int nxt[1005]; 12 int i,j,k; 13 int main() 14 { 15 cin>>P>>L; 16 getline(cin,s); 17 for(i=0;i<P;++i){ 18 a[i].item=i; 19 for(j=0;j<L;++j)getline(cin,a[i].s[j]); 20 mp.insert(pair<string,int>(a[i].s[L-1],i)); 21 } 22 for(i=0;i<P;++i){ 23 if((it=mp.find(a[i].s[0]))!=mp.end()){ 24 a[i].item=it->second; 25 nxt[it->second]=i; 26 } 27 } 28 for(i=0;i<P;++i){ 29 if(a[i].item==i){ 30 cout<<a[i].s[0]<<endl; 31 for(j=0;j<P;++j){ 32 for(k=1;k<L;++k)cout<<a[i].s[k]<<endl; 33 i=nxt[i]; 34 } 35 break; 36 } 37 } 38 return 0; 39 }
E3619 锐角三角形
First AC: 2018-07-11 Latest Modification: 2018-07-11
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long s; 4 int main() 5 { 6 cin>>s; 7 if(s<3)cout<<"No"; 8 else if(s&1)cout<<"Yes\n1 0\n0 1\n"<<(s+1)/2<<' '<<(s+1)/2; 9 else cout<<"Yes\n0 0\n2 0\n1 "<<s/2; 10 return 0; 11 }
E3620 大鱼吃小鱼
First AC: 2018-07-11 Latest Modification: 2018-07-11
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll n,x,a,tmp,rst=1; 5 struct data{ 6 ll w,a; 7 bool add; 8 }fish[1000000]; 9 int i; 10 bool cmp(data a,data b) 11 { 12 if(a.add!=b.add)return a.add>b.add; 13 if(a.add){ 14 if(a.w!=b.w)return a.w<b.w; 15 return a.a>b.a; 16 } 17 else{ 18 if(a.w+a.a!=b.w+b.a)return a.w+a.a>b.w+b.a; 19 return a.a>b.a; 20 } 21 } 22 int main() 23 { 24 cin>>n; 25 for(i=0;i<n;++i){ 26 cin>>fish[i].w>>fish[i].a; 27 if(fish[i].a<0)fish[i].add=0; 28 else fish[i].add=1; 29 } 30 sort(fish,fish+n,cmp); 31 for(i=0;i<n;++i){ 32 if(fish[i].add)rst=max(rst,fish[i].w-tmp); 33 else rst=max(rst,max(fish[i].w-tmp,1-fish[i].a-tmp)); 34 tmp+=fish[i].a; 35 } 36 cout<<rst; 37 return 0; 38 }
E3622 异常的葡萄
First AC: 2018-07-09 Latest Modification: 2018-07-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,rst; 4 struct data{ 5 int item; 6 double b[6]; 7 }a[60]; 8 double cnt[60]; 9 int i,j,k; 10 int main() 11 { 12 cin>>n; 13 for(i=0;i<n;++i){ 14 cin>>a[i].item; 15 for(j=0;j<6;++j)cin>>a[i].b[j]; 16 } 17 for(i=0;i<n;++i){ 18 for(j=0;j<n;++j){ 19 if(i==j)continue; 20 double tmp=0; 21 for(k=0;k<6;++k){ 22 tmp+=pow(a[i].b[k]-a[j].b[k],2); 23 } 24 cnt[i]+=sqrt(tmp); 25 } 26 } 27 for(i=1;i<n;++i)if(cnt[i]>cnt[rst])rst=i; 28 cout<<a[rst].item; 29 return 0; 30 }
E3628 A Simple Convolution
First AC: 2018-08-13 Latest Modification: 2018-08-13
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,h,w,r,c; 4 int a[100][100],b[100][100],t[100][100]; 5 int i,j,k,l; 6 int main() 7 { 8 cin>>n>>m; 9 for(i=0;i<n;++i)for(j=0;j<m;++j)cin>>a[i][j]; 10 cin>>h>>w; 11 for(i=0;i<h;++i)for(j=0;j<w;++j)cin>>b[i][j]; 12 r=n-h+1,c=m-w+1; 13 memset(t,0,sizeof(t)); 14 for(i=0;i<r;++i)for(j=0;j<c;++j) 15 for(k=0;k<h;++k)for(l=0;l<w;++l) 16 t[i][j]+=a[i+k][j+l]*b[k][l]; 17 for(i=0;i<r;++i){ 18 cout<<t[i][0]; 19 for(j=1;j<c;++j)cout<<' '<<t[i][j]; 20 cout<<endl; 21 } 22 return 0; 23 }
E3630 Bad Queen
First AC: 2018-08-13 Latest Modification: 2018-08-13
Note: 可以先跳到(1,y),然后跳到(1,1),最后蛇形走完
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,x,y; 4 bool jdg[101][101]; 5 int i,j; 6 int main() 7 { 8 cin>>n>>m>>x>>y; 9 if(x!=1)cout<<1<<' '<<y<<endl; 10 jdg[1][y]=jdg[x][y]=1; 11 for(i=1;i<=n;++i){ 12 if(i&1){ 13 for(j=1;j<=m;++j) 14 if(!jdg[i][j]) 15 cout<<i<<' '<<j<<endl; 16 } 17 else{ 18 for(j=m;j;--j) 19 if(!jdg[i][j]) 20 cout<<i<<' '<<j<<endl; 21 } 22 } 23 return 0; 24 }
E3637 解密信件
First AC: 2018-12-10 Latest Modification: 2018-12-10
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 int T; 5 ll n,x; 6 ll cal(ll l,ll r,ll x) 7 { 8 if(l==r)return l; 9 ll m=(r+l+1)/2; 10 if(x<m)x=cal(l,m-1,x); 11 else x=cal(m,r,x); 12 return l+r-x; 13 } 14 int main() 15 { 16 cin>>T; 17 while(T--){ 18 cin>>n>>x; 19 cout<<cal(1,n,x)<<endl; 20 } 21 return 0; 22 }
E3639 圆
First AC: 2018-09-15 Latest Modification: 2018-09-15
1 #include<bits/stdc++.h> 2 using namespace std; 3 int r; 4 int main() 5 { 6 cin>>r; 7 printf("%.6lf\n%.6lf",acos(-1)*r*r,2.0*r*r); 8 return 0; 9 }
E3640 素数子序列
First AC: 2019-02-20 Latest Modification: 2019-02-20
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll n,a,b,c; 5 bool jdg(ll n) 6 { 7 if(n<2)return 1; 8 for(ll i=2;i*i<=n;++i){ 9 if(n%i==0)return 1; 10 } 11 return 0; 12 } 13 int main() 14 { 15 cin>>n; 16 if(n>3)cout<<"Impossible"; 17 else if(n==1){ 18 cin>>a; 19 if(a){ 20 if(jdg(a))cout<<"Impossible"; 21 else cout<<a; 22 } 23 else cout<<2; 24 } 25 else if(n==2){ 26 cin>>a>>b; 27 if(a==2){ 28 if(b){ 29 if(!jdg(b)&&!jdg(b+2))cout<<"2 "<<b; 30 else cout<<"Impossible"; 31 } 32 else cout<<"2 3"; 33 } 34 else if(b==2){ 35 if(a){ 36 if(!jdg(a)&&!jdg(a+2))cout<<a<<" 2"; 37 else cout<<"Impossible"; 38 } 39 else cout<<"3 2"; 40 } 41 else if(a||b)cout<<"Impossible"; 42 else cout<<"2 3"; 43 } 44 else if(n==3){ 45 cin>>a>>b>>c; 46 if(!a)a=2; 47 if(!b)b=3; 48 if(!c)c=2; 49 if(a==2&&b==3&&c==2)cout<<"2 3 2"; 50 else cout<<"Impossible"; 51 } 52 return 0; 53 }
E3641 整数划分
First AC: 2018-09-15 Latest Modification: 2018-09-15
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,cnt; 4 int i; 5 int main() 6 { 7 cin>>n; 8 if(n<5||n%3==1){ 9 cout<<"Impossible"; 10 return 0; 11 } 12 if(n%6==0)cout<<"1 2 3 3 2 1",cnt=(n-6)/6; 13 else if(n%6==2)cout<<"1 1 1 2 3 1 3 2",cnt=(n-8)/6; 14 else if(n%6==3)cout<<"1 1 1 1 1 2 3 3 2",cnt=(n-9)/6; 15 else if(n%6==5)cout<<"1 2 2 1 3",cnt=(n-5)/6; 16 while(cnt--)cout<<" 1 2 3 3 2 1"; 17 return 0; 18 }
E3642 oxx的小姐姐们
First AC: 2019-01-30 Latest Modification: 2019-01-30
Note: 由p|nm知p|n或p|m
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m,p,cnt; 4 int i,j; 5 int main() 6 { 7 cin>>n>>m>>p; 8 if(!(n%p)){ 9 cout<<"Yes\n"; 10 for(i=0;i<n;++i){ 11 cout<<i/p*m+1; 12 for(j=1;j<m;++j){ 13 cout<<' '<<i/p*m+j+1; 14 } 15 cout<<endl; 16 } 17 } 18 else if(!(m%p)){ 19 cout<<"Yes\n"; 20 for(i=0;i<n;++i){ 21 cout<<m/p*i+1; 22 for(j=1;j<m;++j){ 23 cout<<' '<<m/p*i+j/p+1; 24 } 25 cout<<endl; 26 } 27 } 28 else cout<<"No"; 29 return 0; 30 }
E3646 猜价格
First AC: 2018-11-19 Latest Modification: 2018-11-19
Note: 注意到二分确定答案的最大次数是n,说明剩下的n次是用来纠错的
的如果k≤n,可以先试错k-1个1,以后每次判断的正确性都确定了
的如果k>n,最多碰到一次错误,只要每个数试两遍,如果判断不同则表明出错
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 int n,k; 5 ll l,r,m; 6 char op,op1,op2; 7 int cnt,err; 8 int i; 9 void print(int n) 10 { 11 if(n)cout<<n<<endl; 12 else cout<<m<<endl; 13 fflush(stdout); 14 ++cnt; 15 } 16 int main() 17 { 18 cin>>n>>k; 19 l=r=1; 20 for(i=0;i<n;++i)r*=2; 21 if(k<=n){ 22 for(i=1;i<k;++i){ 23 print(1); 24 cin>>op; 25 if(op=='=')return 0; 26 if(op=='>'){ 27 err=cnt; 28 break; 29 } 30 } 31 while(1){ 32 m=(l+r)/2; 33 print(0); 34 cin>>op; 35 if(op=='=')return 0; 36 if(cnt%k==err){ 37 if(op=='>')l=m+1; 38 else r=m; 39 } 40 else{ 41 if(op=='<')l=m+1; 42 else r=m; 43 } 44 } 45 } 46 else{ 47 while(1){ 48 m=(l+r)/2; 49 print(0); 50 cin>>op1; 51 if(op1=='=')return 0; 52 print(0); 53 cin>>op2; 54 if(op2=='=')return 0; 55 if(op1==op2){ 56 if(op1=='<')l=m+1; 57 else r=m; 58 } 59 else{ 60 print(0); 61 cin>>op; 62 if(op=='=')return 0; 63 if(op=='<')l=m+1; 64 else r=m; 65 break; 66 } 67 } 68 while(1){ 69 m=(l+r)/2; 70 print(0); 71 cin>>op; 72 if(op=='=')return 0; 73 if(op=='<')l=m+1; 74 else r=m; 75 } 76 } 77 return 0; 78 }
E3649 租房
First AC: 2018-11-19 Latest Modification: 2018-11-19
Note: 观察到两点坐标公式,于是把所有数据扩大两倍便均为整数,然后对中点坐标的奇偶性讨论
的我们最终的目的是把中点移动到横纵坐标都为偶数
的横纵坐标变化的最小幅度分别是△y/gcd(△x,△y)和△x/gcd(△x,△y)
的添加偶数倍的最小幅度是没有意义的,因为它不仅没改变坐标奇偶性,还把租房距离变大了
的再对输入的两点连线斜率分四类(0、正、负、不存在)讨论并在必要时加还是减一倍的最小幅度即可
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll xl,yl,xr,yr; 5 ll xm,ym; 6 ll delx,dely,gcd; 7 int main() 8 { 9 cin>>xl>>yl>>xr>>yr; 10 xm=xl+xr; 11 ym=yl+yr; 12 if(xm%2==0&&ym%2==0)cout<<xm/2<<' '<<ym/2; 13 else{ 14 delx=abs(yl-yr); 15 dely=abs(xl-xr); 16 gcd=__gcd(delx,dely); 17 delx/=gcd; 18 dely/=gcd; 19 if(xl==xr){ 20 if(ym%2)cout<<"NO"; 21 else cout<<xm/2<<' '<<ym/2; 22 return 0; 23 } 24 if(xl>xr){ 25 xl^=xr,xr^=xl,xl^=xr; 26 yl^=yr,yr^=yl,yl^=yr; 27 } 28 if(xm%2&&ym%2){ 29 if(delx%2&&dely%2){ 30 if(yl>yr)cout<<(xm-delx)/2<<' '<<(ym-dely)/2; 31 else cout<<(xm-delx)/2<<' '<<(ym+dely)/2; 32 } 33 else cout<<"NO"; 34 } 35 else if(xm%2){ 36 if(delx%2&&dely%2==0){ 37 if(yl>yr)cout<<(xm-delx)/2<<' '<<(ym-dely)/2; 38 else cout<<(xm-delx)/2<<' '<<(ym+dely)/2; 39 } 40 else cout<<"NO"; 41 } 42 else if(ym%2){ 43 if(delx%2==0&&dely%2){ 44 if(yl>yr)cout<<(xm-delx)/2<<' '<<(ym-dely)/2; 45 else cout<<(xm-delx)/2<<' '<<(ym+dely)/2; 46 } 47 else cout<<"NO"; 48 } 49 } 50 return 0; 51 }
E3650 转机折扣
First AC: 2018-11-19 Latest Modification: 2018-11-19
1 #include<bits/stdc++.h> 2 using namespace std; 3 string a[2]; 4 string s; 5 int len; 6 int i; 7 int main() 8 { 9 cin>>a[0]>>a[1]; 10 sort(a,a+2); 11 s=a[0]; 12 len=a[0].length(); 13 for(i=len-1;i>=0;--i){ 14 if(s[i]<'Z'){ 15 ++s[i]; 16 break; 17 } 18 s[i]='A'; 19 } 20 cout<<s; 21 return 0; 22 }
E3651 仰望星空
First AC: 2018-12-09 Latest Modification: 2018-12-09
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long N,A,B; 4 int main() 5 { 6 cin>>N>>A>>B; 7 cout<<(N-2)*(B-A)+1; 8 return 0; 9 }
E3652 乘法还原
First AC: 2018-12-10 Latest Modification: 2018-12-10
Note: 记$A=\{ a_{1},a_{2},\cdots,a_{m}\},B=\{ b_{1},b_{2},\cdots,b_{m}\}$
设输入的$n$个无序二元组组成的集合为$W$,则$W$中元素可分为如下四类:
①$(a,a)$:表明$a\in A\cap B$,记所有这样的$a$组成的集合为$both$,即$both=\{ a\mid a\in A\cap B\}$
②$(a,b)$,其中$(b,a)\in W$:则$a,b\in both$,而这个性质可由①完全刻画,故可以忽略这样的二元组
③$(a,b)$,其中$a\in both$且$(b,a)\notin W$
④$(c,d)$,其中$c,d\notin both$
如果③④均为空,则$A=B=both$,只要遍历①
如果④非空,只要找到一组$(c,d)$然后遍历$W$找到所有$(c,a_{i}),(a_{i},c),(d,b_{i}),(b_{i},d)$
如果③非空且④为空,则$A=both$,只要遍历$W$找到所有出现过的数字,共同组成$B$
最后再对$A,B$字典序排序即可
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef pair<int,int> Pr; 4 const int MAX=100005; 5 int n,x,y,cnt,tmp; 6 struct data{ 7 int x,y; 8 }a[MAX],b[MAX]; 9 map<int,int>both,all; 10 map<int,int>::iterator ed; 11 int A[MAX],B[MAX]; 12 int cntA,cntB; 13 int i; 14 bool cmp(data a,data b) 15 { 16 if(a.x==b.x)return a.y<b.y; 17 return a.x<b.x; 18 } 19 void out(bool flag) 20 { 21 if(flag){ 22 cout<<cntA<<' '<<cntB<<endl<<A[0]; 23 for(i=1;i<cntA;++i)cout<<' '<<A[i]; 24 cout<<endl<<B[0]; 25 for(i=1;i<cntB;++i)cout<<' '<<B[i]; 26 } 27 else{ 28 cout<<cntB<<' '<<cntA<<endl<<B[0]; 29 for(i=1;i<cntB;++i)cout<<' '<<B[i]; 30 cout<<endl<<A[0]; 31 for(i=1;i<cntA;++i)cout<<' '<<A[i]; 32 } 33 exit(0); 34 } 35 void print() 36 { 37 sort(A,A+cntA); 38 sort(B,B+cntB); 39 if(cntA<cntB)out(1); 40 if(cntA>cntB)out(0); 41 for(i=0;i<cntA;++i){ 42 if(A[i]>B[i])out(0); 43 } 44 out(1); 45 } 46 int main() 47 { 48 cin>>n; 49 for(i=1;i<=n;++i){ 50 cin>>x>>y; 51 a[i].x=min(x,y); 52 a[i].y=max(x,y); 53 } 54 sort(a+1,a+n+1,cmp); 55 for(i=1;i<=n;++i){ 56 if(Pr(a[i].x,a[i].y)==Pr(a[i-1].x,a[i-1].y))continue; 57 if(Pr(a[i].x,a[i].y)==Pr(a[i+1].x,a[i+1].y))continue; 58 b[cnt].x=a[i].x; 59 b[cnt].y=a[i].y; 60 ++cnt; 61 if(a[i].x==a[i].y)both.insert(Pr(a[i].x,0)); 62 } 63 if(cnt==both.size()){ 64 cout<<cnt<<' '<<cnt<<endl<<b[0].x; 65 for(i=1;i<cnt;++i)cout<<' '<<b[i].x; 66 cout<<endl<<b[0].x; 67 for(i=1;i<cnt;++i)cout<<' '<<b[i].x; 68 return 0; 69 } 70 ed=both.end(); 71 for(i=0;i<cnt;++i){ 72 x=b[i].x; 73 y=b[i].y; 74 if(both.find(x)==ed&&both.find(y)==ed){ 75 for(i=0;i<cnt;++i){ 76 if(b[i].x==x)A[cntA++]=b[i].y; 77 if(b[i].y==x)A[cntA++]=b[i].x; 78 } 79 for(i=0;i<cnt;++i){ 80 if(b[i].x==y)B[cntB++]=b[i].y; 81 if(b[i].y==y)B[cntB++]=b[i].x; 82 } 83 print(); 84 } 85 } 86 for(i=1;i<=n;++i){ 87 all.insert(Pr(a[i].x,0)); 88 all.insert(Pr(a[i].y,0)); 89 } 90 for(ed=both.begin();ed!=both.end();++ed){ 91 A[cntA++]=ed->first; 92 } 93 for(ed=all.begin();ed!=all.end();++ed){ 94 B[cntB++]=ed->first; 95 } 96 print(); 97 return 0; 98 }
E3656 心与心的距离
First AC: 2018-11-19 Latest Modification: 2018-11-19
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,n; 4 int i; 5 int main() 6 { 7 cin>>T; 8 for(i=1;i<=T;++i){ 9 cin>>n; 10 cout<<"Case #"<<i<<": "<<n<<" 0 0 0 0 "<<n<<endl; 11 } 12 return 0; 13 }
E3674 唐纳德先生与.DOC
First AC: 2019-01-19 Latest Modification: 2019-01-19
Note: 枚举发现,不论交换的两个字符同属同级DOC,还是分属主从句,都不能交换两个句子成分
1 #include<bits/stdc++.h> 2 using namespace std; 3 int T,len; 4 string s; 5 int i; 6 int main() 7 { 8 cin>>T; 9 while(T--){ 10 cin>>s; 11 len=s.length(); 12 for(i=1;i<len;++i){ 13 if(s[i]==s[i-1])cout<<"C"; 14 else cout<<"D"; 15 } 16 cout<<endl; 17 } 18 return 0; 19 }
E3675 唐纳德先生与假骰子
First AC: 2019-01-19 Latest Modification: 2019-01-19
Note: 类似离散分配的贪心策略,连续分配中按p/1-p概率分配的方案也总能无损转化为0/1分配方案
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll T,x,up,dn; 5 ll i; 6 int main() 7 { 8 cin>>T; 9 while(T--){ 10 up=dn=0; 11 for(i=1;i<=6;++i){ 12 cin>>x; 13 up=max(up,i*x); 14 dn+=x; 15 } 16 printf("%.9lf\n",up*1.0/dn); 17 } 18 return 0; 19 }
E3678 方差
First AC: 2019-02-22 Latest Modification: 2019-02-22
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll n,m,x; 5 ll a[1000001]; 6 ll b[1000001]; 7 ll i,j; 8 int main() 9 { 10 cin>>n>>m; 11 for(i=1;i<=n;++i)cin>>a[i]; 12 sort(a+1,a+n+1); 13 for(i=1;i<=n;++i){ 14 x=a[i]; 15 a[i]=a[i-1]+x; 16 b[i]=b[i-1]+x*x; 17 } 18 ll rst=1e18; 19 for(i=m;i<=n;++i){ 20 j=i-m; 21 rst=min(rst,m*(b[i]-b[j])-(a[i]-a[j])*(a[i]-a[j])); 22 } 23 cout<<rst; 24 return 0; 25 }
E3679 进制转换
First AC: 2019-02-22 Latest Modification: 2019-02-22
Note: 判断乘法溢出用1e18+5.0会WA,因为5.0/ans值特别小超出了double的精度范围
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 int T; 5 ll l,r,k,m,tmp,tmp1,tmp2; 6 ll cal(ll k,ll m) 7 { 8 ll ans=1,tmp; 9 while(m--){ 10 if(2e18*1.0/ans<k)return 2e18; 11 tmp=ans*k; 12 ans=tmp; 13 } 14 return ans; 15 } 16 int main() 17 { 18 cin>>T; 19 while(T--){ 20 cin>>l>>r>>k>>m; 21 tmp=cal(k,m); 22 tmp1=r/tmp,tmp2=(l-1)/tmp; 23 tmp1-=tmp1/k; 24 tmp2-=tmp2/k; 25 cout<<tmp1-tmp2<<endl; 26 } 27 return 0; 28 }
E3682 宇恒棋
First AC: 2019-03-23 Latest Modification: 2019-03-23
Note: 由奇偶性必然有一方不可能获胜,可以证明另一方总能把这一方往边界逼近
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,m; 4 int xa,ya,xb,yb; 5 int main() 6 { 7 cin>>n>>m>>xa>>ya>>xb>>yb; 8 if((xa-xb+ya-yb+200)%2)cout<<"Lose"; 9 else cout<<"Win"; 10 return 0; 11 }
E3684 钝角三角形
First AC: 2019-03-23 Latest Modification: 2019-03-23
Note: 给出一种构造方法,把最小的n个数先分配给这n组
记这n个数中偶数个数为a,奇数个数为n-a
再把剩余2n个数中较小的2a个数分配给前面偶数的a组
其余2(n-1)个数分配给奇数的(n-a)组,嵌套分配,以下图为例
证明考虑论证奇数组、偶数组第一组满足能推出之后的组也满足
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,l1,r1,l2,r2; 4 int i; 5 int main() 6 { 7 cin>>n; 8 ++n; 9 l1=n+1; 10 r1=n+n/2*2; 11 l2=r1+1; 12 r2=3*n-2; 13 for(i=n;i>1;--i){ 14 if(i&1){ 15 cout<<i<<' '<<l2<<' '<<r2<<endl; 16 ++l2; 17 --r2; 18 } 19 else{ 20 cout<<i<<' '<<l1<<' '<<r1<<endl; 21 ++l1;--r1; 22 } 23 } 24 return 0; 25 }
E3685 线段树
First AC: 2019-03-27 Latest Modification: 2019-03-27
Note: 一个很关键的剪枝是当前[l,r]不可能在初始[1,b>=r]的右半子树时直接剪掉
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll T,l,r,ans; 5 void dfs(ll l ,ll r) 6 { 7 if(l<1||r>=ans)return; 8 if(l==1){ 9 ans=min(ans,r); 10 return; 11 } 12 if(2*l-r<1)return; 13 ll len=r-l+1; 14 dfs(l,r+len); 15 dfs(l-len,r); 16 dfs(l-len-1,r); 17 if(len!=1){ 18 dfs(l,r+len-1); 19 } 20 } 21 int main() 22 { 23 scanf("%lld",&T); 24 while(T--){ 25 scanf("%lld%lld",&l,&r); 26 if(l==1||l==r){ 27 cout<<r<<endl; 28 continue; 29 } 30 ans=2e9+1; 31 dfs(l,r); 32 if(ans>2e9)cout<<"-1\n"; 33 else cout<<ans<<endl; 34 } 35 return 0; 36 }
E3686 回文串
First AC: 2019-03-23 Latest Modification: 2019-03-23
Note: 可以看出最终的每个数都是初始连续一段数之和
因此任何合并方法总能调整次序,使得靠近两端的比处于中间的先合并
而最终两端必然相等,所以只要依次选取两端,把小的向内合并即可
注意有些编译器在deque为空时仍有dq.front()==dq.back(),这种情况特判处理
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll n,x,y,ans; 5 deque<ll>dq; 6 int main() 7 { 8 cin>>n; 9 while(n--){ 10 cin>>x; 11 dq.push_back(x); 12 } 13 while(dq.size()>2){ 14 x=dq.front(); 15 y=dq.back(); 16 if(x<y){ 17 dq.pop_front(); 18 x+=dq.front(); 19 dq.pop_front(); 20 dq.push_front(x); 21 ++ans; 22 } 23 else if(x>y){ 24 dq.pop_back(); 25 y+=dq.back(); 26 dq.pop_back(); 27 dq.push_back(y); 28 ++ans; 29 } 30 else{ 31 dq.pop_front(); 32 dq.pop_back(); 33 } 34 } 35 if(!dq.empty()&&dq.front()!=dq.back())++ans; 36 cout<<ans; 37 return 0; 38 }
到底辣!