Codeforces Round #496 (Div. 3) ABCDE1

 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 }

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

时间: 2024-08-30 01:32:46

Codeforces Round #496 (Div. 3) ABCDE1的相关文章

Codeforces Round #496 (Div. 3)

1.(1005E2)http://codeforces.com/contest/1005/problem/E2 题意:给定一个长度为n的排列,先求有多少个区间[l,r]满足该区间的中位数为m 分析:设置函数count(m):表示对于给定长度为n的序列,有多少个区间的中位数是>=m的(当[区间>=m数的个数]>[区间<m的个数]时满足).最终的答案为count(m)-count(m+1). 那么如何求解count(m)?设置变量cnt表示当前累计的值,初始值为n,当a[i]>=

Codeforces Round #496 (Div. 3)A~F

(寒假训练赛,也是lj难得补完的一场 https://codeforces.com/contest/1005 A.题意是  每一个楼梯有x个台阶,小女孩爬楼的时候会从1开始数每层楼有几个台阶,现在给给出n个数字a1~an,代表着小女孩爬楼时数数的序列,求有多少层楼梯,并且输出每层楼梯台阶数. 解法:for循环模拟小女孩从1开始数,数到下一个1的话楼层++,然后把他前一个数存进去. int now = 0; for(int i = 0;i < n;++i){ cin>>a; if(a ==

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿

Codeforces Round #424 (Div. 2) C. Jury Marks(乱搞)

题目链接:Codeforces Round #424 (Div. 2) C. Jury Marks 题意: 给你一个有n个数序列,现在让你确定一个x,使得x通过挨着加这个序列的每一个数能出现所有给出的k个数. 问合法的x有多少个.题目保证这k个数完全不同. 题解: 显然,要将这n个数求一下前缀和,并且排一下序,这样,能出现的数就可以表示为x+a,x+b,x+c了. 这里 x+a,x+b,x+c是递增的.这里我把这个序列叫做A序列 然后对于给出的k个数,我们也排一下序,这里我把它叫做B序列,如果我

[Codeforces] Round #352 (Div. 2)

人生不止眼前的狗血,还有远方的狗带 A题B题一如既往的丝帛题 A题题意:询问按照12345678910111213...的顺序排列下去第n(n<=10^3)个数是多少 题解:打表,输出 1 #include<bits/stdc++.h> 2 using namespace std; 3 int dig[10],A[1005]; 4 int main(){ 5 int aa=0; 6 for(int i=1;;i++){ 7 int x=i,dd=0; 8 while(x)dig[++dd

Codeforces Round #273 (Div. 2)

Codeforces Round #273 (Div. 2) 题目链接 A:签到,仅仅要推断总和是不是5的倍数就可以,注意推断0的情况 B:最大值的情况是每一个集合先放1个,剩下都丢到一个集合去,最小值是尽量平均去分 C:假如3种球从小到大是a, b, c,那么假设(a + b) 2 <= c这个比較明显答案就是a + b了.由于c肯定要剩余了,假设(a + b)2 > c的话,就肯定能构造出最优的(a + b + c) / 3,由于肯定能够先拿a和b去消除c,而且控制a和b成2倍关系或者消除

Codeforces Round #339 (Div. 2) B. Gena&#39;s Code

B. Gena's Code It's the year 4527 and the tanks game that we all know and love still exists. There also exists Great Gena's code, written in 2016. The problem this code solves is: given the number of tanks that go into the battle from each country, f

Codeforces Round #315 (Div. 1)

A. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Rikhail Mubinchik believes that the current definition of prime numbers is obsolete as they are too complex and un