Codeforces Beta Round #5
http://codeforces.com/contest/5
A
模拟题
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 typedef long long ll; 7 /*#ifndef ONLINE_JUDGE 8 freopen("1.txt","r",stdin); 9 #endif */ 10 11 12 int main(){ 13 std::ios::sync_with_stdio(false); 14 #ifndef ONLINE_JUDGE 15 freopen("1.txt","r",stdin); 16 #endif 17 set<string>se; 18 string str; 19 int i; 20 int ans=0; 21 while(getline(cin,str)){ 22 if(str.length()==0) break; 23 if(str[0]==‘+‘||str[0]==‘-‘){ 24 if(str[0]==‘+‘){ 25 str=str.substr(1); 26 se.insert(str); 27 } 28 else{ 29 str=str.substr(1); 30 se.erase(str); 31 } 32 } 33 else{ 34 for(i=0;i<str.length();i++){ 35 if(str[i]==‘:‘) break; 36 } 37 // cout<<i<<endl; 38 ans+=se.size()*(str.length()-1-i); 39 } 40 } 41 cout<<ans<<endl; 42 return 0; 43 }
B
画图题
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 typedef long long ll; 7 /*#ifndef ONLINE_JUDGE 8 freopen("1.txt","r",stdin); 9 #endif */ 10 string str[1005]; 11 12 int main(){ 13 std::ios::sync_with_stdio(false); 14 #ifndef ONLINE_JUDGE 15 freopen("1.txt","r",stdin); 16 #endif 17 int n=0; 18 while(getline(cin,str[n++])); 19 int Max=0; 20 n--; 21 for(int i=0;i<n;i++){ 22 int len=str[i].length(); 23 Max=max(Max,len); 24 } 25 for(int i=0;i<Max+2;i++){ 26 cout<<‘*‘; 27 } 28 cout<<endl; 29 int cc=1; 30 for(int i=0;i<n;i++){ 31 cout<<‘*‘; 32 int k=Max-str[i].length(); 33 int tou=k/2; 34 int wei=k/2; 35 k=k-wei-tou; 36 if(k){ 37 if(cc%2) wei++,cc++; 38 else tou++,cc++; 39 } 40 for(int j=0;j<tou;j++) cout<<‘ ‘; 41 cout<<str[i]; 42 for(int j=0;j<wei;j++) cout<<‘ ‘; 43 cout<<‘*‘<<endl; 44 } 45 for(int i=0;i<Max+2;i++){ 46 cout<<‘*‘; 47 } 48 cout<<endl; 49 return 0; 50 }
C
找到匹配最大字符串,直接暴力模拟
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 typedef long long ll; 7 /*#ifndef ONLINE_JUDGE 8 freopen("1.txt","r",stdin); 9 #endif */ 10 11 int a[1000005]; 12 int len[1000005]; 13 14 int main(){ 15 std::ios::sync_with_stdio(false); 16 #ifndef ONLINE_JUDGE 17 freopen("1.txt","r",stdin); 18 #endif 19 string str; 20 cin>>str; 21 stack<int>st; 22 /// () (()) () 23 memset(a,-1,sizeof(a)); 24 for(int i=0;i<str.length();i++){ 25 if(str[i]==‘(‘){ 26 st.push(i); 27 } 28 else{ 29 if(st.empty()){ 30 continue; 31 } 32 else{ 33 a[i]=st.top(); 34 st.pop(); 35 } 36 } 37 } 38 int Max=0,num=0; 39 for(int i=0;i<str.length();i++){ 40 if(a[i]!=-1){ 41 len[i]=(a[i]>0?len[a[i]-1]:0)+i-a[i]+1; 42 if(Max<len[i]){ 43 Max=len[i]; 44 num=1; 45 } 46 else if(Max==len[i]) num++; 47 } 48 } 49 50 51 if(!Max) cout<<"0 1"<<endl; 52 else{ 53 cout<<Max<<" "<<num<<endl; 54 } 55 return 0; 56 }
D
物理题
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 typedef long long ll; 7 /*#ifndef ONLINE_JUDGE 8 freopen("1.txt","r",stdin); 9 #endif */ 10 double a,v,l,d,w,ans,t1,t2,t3,x1,x2,x3; 11 int flag; 12 13 int main(){ 14 scanf("%lf%lf",&a,&v); 15 scanf("%lf%lf%lf",&l,&d,&w); 16 t1=v/a; 17 x1=a*t1*t1/2; 18 if(w<v) 19 flag=1; 20 if(flag==1){ 21 t2=w/a; 22 x2=a*t2*t2/2; 23 if(x2<d){ 24 t3=(v-w)/a; 25 x3=w*t3+a*t3*t3/2; 26 if(x1+x3<d) 27 ans=t1+t3+(d-x1-x3)/v; 28 else 29 ans=t2+2*(sqrt(a*(d-x2)+w*w)-w)/a; 30 if(x3<l-d) 31 ans+=t3+(l-d-x3)/v; 32 else 33 ans+=(sqrt(2*a*(l-d)+w*w)-w)/a; 34 } 35 else 36 flag=0; 37 } 38 if(flag==0){ 39 if(x1<l) 40 ans=t1+(l-x1)/v; 41 else 42 ans=sqrt(2*l/a); 43 } 44 printf("%.6f\n",ans); 45 return 0; 46 }
E
首先我们把环变成一个链,然后用一个并查集的思想,处理每一座山左边第一个比这个山高的山是哪个,右边第一个比这个山高的是哪个,这个区域内和这个山高度一样的山有多少
然后跑一发并查集就好了
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define maxn 1000010 7 typedef long long ll; 8 /*#ifndef ONLINE_JUDGE 9 freopen("1.txt","r",stdin); 10 #endif */ 11 12 int a[maxn]; 13 int b[maxn]; 14 int l[maxn]; 15 int r[maxn]; 16 int k[maxn]; 17 int main() 18 { 19 int n; 20 cin>>n; 21 int p=-1; 22 for(int i=0;i<n;i++) 23 { 24 cin>>a[i]; 25 if(p==-1) 26 p=i; 27 else if(a[i]>a[p]) 28 { 29 p=i; 30 } 31 } 32 for(int i=0;i<n;i++) 33 b[i]=a[(i+p+n)%n]; 34 b[n]=a[p]; 35 for(int i=1;i<n;i++) 36 { 37 l[i]=i-1; 38 while(l[i]>0&&b[i]>=b[l[i]]) 39 l[i]=l[l[i]]; 40 } 41 k[n]=0; 42 for(int i=n-1;i>=0;i--) 43 { 44 r[i]=i+1; 45 while(r[i]<n&&b[i]>b[r[i]]) 46 r[i]=r[r[i]]; 47 if(r[i]<n&&b[i]==b[r[i]]) 48 { 49 k[i]=k[r[i]]+1; 50 r[i]=r[r[i]]; 51 } 52 } 53 ll ans=0; 54 for(int i=1;i<n;i++) 55 { 56 ans+=k[i]; 57 ans+=1; 58 if(l[i]!=0||r[i]!=n) 59 ans++; 60 } 61 cout<<ans<<endl; 62 }
原文地址:https://www.cnblogs.com/Fighting-sh/p/10347269.html
时间: 2024-10-12 20:52:10