码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #496 (Div. 3) ABCDE1

时间:2018-07-12 01:03:48      阅读:343      评论:0      收藏:0      [点我收藏+]

标签:区间   定位   abc   恢复   power   ring   type   ems   code   

 1 //B. Delete from the Left
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <set>
 7 #include <map>
 8 #include <vector>
 9 using namespace std;
10 const int inf=0x3f3f3f3f;
11 const int N=2e5+9;
12 char a[N],b[N];
13 int main()
14 {
15     scanf("%s%s",a,b);
16     int lena=strlen(a);
17     int  lenb=strlen(b);
18     int i=lena-1,j=lenb-1;
19     int sum=0;
20     while(a[i]==b[j]&&i>=0&&j>=0)//s[-1]与p[-1]是不确定的
21     {   //因此加上i>=0&&j>=0  和下面的D题相似
22         sum++;
23         i--,j--;
24     }    
25     printf("%d\n",lena+lenb-2*sum);
26     return  0;
27 }

 

  1 //C. Summarize to the Power of Two
 /*

     2^(n-1)   x   2^n  2^(n+1)

   因为x<2^n,所以2*x<2^(n+1),那么x+y(0<x<y)一定位于

    2^(n-1)和2^(n+1)之间。若x+y为2的指数幂,那么必定是2^n.

*/
2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 #include <set> 7 #include <map> 8 #include <vector> 9 #include <cmath> 10 using namespace std; 11 typedef long long ll; 12 const int inf=0x3f3f3f3f; 13 const int N=130000; 14 ll n; 15 ll a[N]; 16 map<ll,ll>mp; 17 set<ll>s; 18 int main() 19 { 20 scanf("%lld",&n); 21 for(int i=0;i<n;i++) 22 { 23 scanf("%lld",&a[i]); 24 mp[a[i]]++; 25 } 26 sort(a,a+n);//为了二分 27 s.clear(); 28 for(int i=0;i<n;i++) 29 { 30 ll ans=log2(a[i]); 31 ll ret=pow(2,ans+1)-a[i]; 32 if(binary_search(a,a+i,ret)) 33 { //二分查找[) ,不会取到自己 34 s.insert(a[i]); 35 s.insert(ret);//set可以去重 36 } 37 } 38 ll cnt=0; 39 for(auto it=s.begin();it!=s.end();it++){ 40 cnt+=mp[*it]; 41 } 42 printf("%lld\n",n-cnt); 43 return 0; 44 } 45 /* 46 下面是简单的二分查找函数 47 scanf("%d",&n); 48 for(int i=0;i<n;i++) 49 { 50 scanf("%lld",&a[i]); 51 } 52 ll m; 53 while(~scanf("%lld",&m)){ 54 if(binary_search(a+1,a+4,m)){//[) 55 cout<<"yes\n"; 56 } 57 else{ 58 cout<<"no\n"; 59 } 60 } 61 return 0; 62 } 63 64 5 65 1 2 3 4 5 66 1 67 no 68 2 69 yes 70 3 71 yes 72 4 73 yes 74 5 75 no 76 77 */ 78 79 80 81 //C. Summarize to the Power of Two 82 #include <iostream> 83 #include <cstdio> 84 #include <cstring> 85 #include <algorithm> 86 #include <set> 87 #include <map> 88 #include <vector> 89 #include <cmath> 90 using namespace std; 91 typedef long long ll; 92 const int inf=0x3f3f3f3f; 93 ll n; 94 map<ll,ll>mp; 95 const int M=1.3e5+9; 96 ll a[M]; 97 int main() 98 { 99 scanf("%lld",&n); 100 for(ll i=0;i<n;i++) 101 { 102 scanf("%lld",&a[i]); 103 mp[a[i]]++; 104 } 105 ll ans=0; 106 bool flag; 107 for(ll i=0;i<n;i++) 108 { 109 flag=0; 110 mp[a[i]]--;//将a[i]先取出 111 //为了避免a[i]与(1<<j)-a[i]是同位置 112 //当然也有其他的方法 113 //if(mp[(1<<j)-a[i]]==1&&a[i]!=(1<<j)-a[i]||mp[(1<<j)-a[i]]>1) 114 //出现多次,可以互相替换,1次只要两者不等,就一定不是同一位置 115 //不相等一定不是同位置 116 for(int j=1;j<=30;j++) 117 { 118 if(mp[(1<<j)-a[i]]) 119 { 120 flag=1; 121 break; 122 } 123 } 124 if(!flag) 125 ans++; 126 mp[a[i]]++;//再恢复 127 } 128 printf("%lld\n",ans); 129 return 0; 130 }

 

 

 1 ////D. Polycarp and Div 3
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include  <set>
 7 #include <map>
 8 #include <vector>
 9 #include <cmath>
10 using namespace std;
11 typedef long long ll;
12 const int inf=0x3f3f3f3f;
13 const int N=2e5+9;
14 ll dp[N];
15 char s[N];
16 int main()
17 {    
18     scanf("%s",s+1);
19     /*
20     if(s[1]==‘1‘&&s[2]==‘1‘){
21         if(strlen(s+1)==2){
22             printf("0\n");
23             return 0;
24         }
25     }
26     */
27     
28     int lens=strlen(s+1);
29     memset(dp,0,sizeof(dp));
30     if((s[1]-0)%3==0){
31         dp[1]=1;
32     }
33     //int i,j,k,sum;
34     for( int i=2;i<=lens;i++)
35     {
36          dp[i]=dp[i-1];
37         //cout<<"iii"<<i<<endl;
38         for( int j=i-1;j>=0;j--)//起初没加j>=0
39         { //如dp[-1]=dp[0]=dp[1]=0 。数组的负下标问题
40          //可能令最终的结果有问题,也会时间更长
41             if(dp[j]!=dp[i-1])
42                 break;
43             if(s[j+1]==0&&i-j>1)
44                 continue;
45             //cout<<dp[j]<<" "<<dp[i-1]<<endl;
46             int  sum=0;
47                for(int  k=i;k>=j+1;k--)
48                {
49                    sum=(sum+(s[k]-0))%3;
50                }
51                //cout<<"sum "<<sum<<endl;
52                    if(sum==0)
53                    {
54                        dp[i]=max(dp[i],dp[j]+1);
55                    }
56                
57         }    
58         //cout<<i<<" "<<dp[i]<<endl;
59     }
60     printf("%lld\n",dp[lens]);
61     return  0;
62 }
63 
64 
65 //D. Polycarp and Div 3
66 #include <iostream>
67 #include <cstdio>
68 #include <cstring>
69 #include <algorithm>
70 #include  <set>
71 #include <map>
72 #include <vector>
73 #include <cmath>
74 using namespace std;
75 typedef long long ll;
76 const int inf=0x3f3f3f3f;
77 const int N=2e5+9;
78 int main()
79 {
80     char s[N];
81     scanf("%s",s);
82     int sum=0,cnt=0,tmp;
83     int ans=0;
84     for(int i=0;s[i];i++)
85     {
86         tmp=(s[i]-0)%3;
87         sum+=tmp;
88         cnt++;
89         if(tmp==0||sum%3==0||cnt==3)
90         {
91             ans++;
92             sum=cnt=0;
93         }
94     }
95     printf("%d\n",ans);
96     return  0;
97 }

 

 

 1 // E1 - Median on Segments (Permutations Edition)
 2 //区间内大于m的数的个数-小于m的数的个数(sum)==0or1
 3 //在m出现之前依次记录sum,用map存储每个sum出现的个数
 4 //等到m出现后,每当遇到一个sum,ans+=(sum出现的次数)+(sum-1出现的次数)
 5 //sum sum 区间内大于m的数的个数-小于m的数的个数==0
 6 //sum(前面) sum-1(后面) 区间内大于m的数的个数-小于m的数的个数==1
 7 //m出现后不再增加sum出现的次数,因为那些区间不会包含m
 8 //符合条件的区间一定要包含m
 9 #include <iostream>
10 #include <cstdio>
11 #include <cstring>
12 #include <algorithm>
13 #include  <set>
14 #include <map>
15 #include <vector>
16 #include <cmath>
17 using namespace std;
18 typedef long long ll;
19 const int inf=0x3f3f3f3f;
20 const int N=2e5+9;
21 int n,m,a[N];
22 map<ll,ll>mp;
23 int main()
24 {
25     scanf("%d%d",&n,&m);
26     for(int i=0;i<n;i++)
27     {
28         scanf("%d",&a[i]);
29     }
30    ll sum=0;
31     mp[0]=1;
32     bool flag=0;
33     ll ans=0;
34     for(int i=0;i<n;i++)
35     {
36         if(a[i]<m)
37             sum--;
38         else if(a[i]>m)
39             sum++;
40         if(a[i]==m)
41             flag=1;
42          if(flag){
43              ans+=mp[sum]+mp[sum-1];
44          }
45          else{
46              mp[sum]++;
47          }
48     }
49     printf("%lld\n",ans);
50     return 0;
51 }

 

Codeforces Round #496 (Div. 3) ABCDE1

标签:区间   定位   abc   恢复   power   ring   type   ems   code   

原文地址:https://www.cnblogs.com/tingtin/p/9297408.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!