Codeforces#371 Div2

这是一场非常需要总结的比赛,交了3题,最后终测的时候3题全部没过,一下掉到了绿名,2333

Problem A

题意:给定区间[l1,r1],[l2,r2],然后给定一个整数k,求区间当中相交的元素,k这个点不可算

分析:画图即可得出答案,注意两个l1和r2,以及r1和l2刚好重合的情况,赛场上就是漏电这种情况

 1 //
 2 //  main.cpp
 3 //  Codeforces
 4 //
 5 //  Created by wanghan on 16/9/14.
 6 //  Copyright © 2016年 wanghan. All rights reserved.
 7 //
 8
 9 #include<iostream>
10 #include<cstdio>
11 #include<cstring>
12 #include<string>
13 #include<cmath>
14 #include<vector>
15 #include<algorithm>
16 #include<map>
17 #include<set>
18 #include<deque>
19 #include<queue>
20 #include<stack>
21 #include<cctype>
22 using namespace std;
23 long long l1,r1,l2,r2,k;
24 int main(int argc, const char * argv[]) {
25     // insert code here...
26     while(cin>>l1>>r1>>l2>>r2>>k)
27     {
28         if(l1<=l2){
29             if(l2>r1){
30                 cout<<"0"<<endl;
31             }else if(l2==r1){
32                 if(l2==k)
33                     cout<<"0"<<endl;
34                 else  cout<<"1"<<endl;
35             }
36             else{
37                 if(r1>=r2){
38                     if(k>=l2&&k<=r2)
39                         cout<<r2-l2<<endl;
40                     else
41                         cout<<r2-l2+1<<endl;
42                 }else{
43                     if(k>=l2&&k<=r1){
44                         cout<<r1-l2<<endl;
45                     }else{
46                         cout<<r1-l2+1<<endl;
47                     }
48                 }
49             }
50         }
51         else {
52             if(l1>r2){
53                 cout<<"0"<<endl;
54             }else if(l1==r2)
55             {
56                 if(k==l1)
57                     cout<<"0"<<endl;
58                 else
59                     cout<<"1"<<endl;
60             }
61             else{
62                 if(r2>=r1){
63                     if(k>=l1&&k<=r1)
64                         cout<<r1-l1<<endl;
65                     else
66                         cout<<r1-l1+1<<endl;
67                 }else{
68                     if(k>=l1&&k<=r2)
69                         cout<<r2-l1<<endl;
70                     else
71                         cout<<r2-l1+1<<endl;
72                 }
73             }
74         }
75     }
76     return 0;
77 }

Problem B

题意:给定一串数,问是否可以都加上或者减去同一个数1次或0次,让所有数最后都相等

分析:用set进行维护,统计其中不同元素的个数,若小于3个,肯定可以;若大于3个,肯定不行;若等于3个,判断一下是否中间一个是另外两个的平均数

 1 //
 2 //  main.cpp
 3 //  CodeforcesB
 4 //
 5 //  Created by wanghan on 16/9/14.
 6 //  Copyright © 2016年 wanghan. All rights reserved.
 7 //
 8
 9 #include<iostream>
10 #include<cstdio>
11 #include<cstring>
12 #include<string>
13 #include<cmath>
14 #include<vector>
15 #include<algorithm>
16 #include<map>
17 #include<set>
18 #include<deque>
19 #include<queue>
20 #include<stack>
21 #include<cctype>
22 using namespace std;
23 const int maxn=100010;
24 int a[maxn];
25 int n;
26 int main()
27 {
28     while(cin>>n)
29     {
30         set<int> s;
31         set<int>::iterator iter;
32         for(int i=1;i<=n;i++){
33             cin>>a[i];
34             int x=a[i];
35             s.insert(x);
36         }
37         if(s.size()<3){
38             cout<<"YES"<<endl;
39         }else if(s.size()>3){
40             cout<<"NO"<<endl;
41         }else{
42             int b[5];
43             int cnt=0;
44             for(iter=s.begin();iter!=s.end();iter++){
45                 b[cnt]=*iter;
46                     cnt++;
47             }
48             int num=b[0]+b[2];
49             if(b[1]*2==num)
50                 cout<<"YES"<<endl;
51             else  cout<<"NO"<<endl;
52         }
53     }
54 }

Problem C

题意:+代表往集合里面加一个元素,-代表删除集合里面的这个元素,每个元素按位%2得到一个,?表示统计集合当中有多少个数按位%2等于要求的值

分析:用map来进行维护,+时p[num]++,-时p[num]--,?求出p[num]的个数即可

 1 //
 2 //  main.cpp
 3 //  Codeforces
 4 //
 5 //  Created by wanghan on 16/9/17.
 6 //  Copyright © 2016年 wanghan. All rights reserved.
 7 //
 8
 9 #include<iostream>
10 #include<cstdio>
11 #include<cstring>
12 #include<string>
13 #include<cmath>
14 #include<stack>
15 #include<queue>
16 #include<vector>
17 #include<set>
18 #include<algorithm>
19 #include<map>
20 using namespace  std;
21 map<long long,int> p;
22 int t;
23 int main()
24 {
25     cin>>t;
26     while(t--)
27     {
28         char ch[10];
29         char s[20];
30         scanf("%s %s",ch,s);
31         int len;
32         if(ch[0]==‘+‘||ch[0]==‘-‘){
33             long long ans=0;
34             len=strlen(s);
35             for(int i=0;i<len-1;i++){
36                 long long t=(s[i]-‘0‘);
37                 ans=ans+t%2;
38                 ans*=10;
39             }
40             ans+=(s[len-1]-‘0‘)%2;
41             //cout<<ans<<endl;
42             if(ch[0]==‘+‘) p[ans]++;
43             else p[ans]--;
44         }else{
45             long long ans1=0;
46             len=strlen(s);
47             for(int i=0;i<len-1;i++){
48                 long long t1=(s[i]-‘0‘);
49                 ans1=ans1+t1;
50                 ans1*=10;
51             }
52             ans1+=(s[len-1]-‘0‘)%2;
53             cout<<p[ans1]<<endl;
54         }
55     }
56 }

时间: 2024-08-13 18:04:37

Codeforces#371 Div2的相关文章

Codeforces 583 DIV2 Robot&#39;s Task 贪心

原题链接:http://codeforces.com/problemset/problem/583/B 题意: 就..要打开一个电脑,必须至少先打开其他若干电脑,每次转向有个花费,让你设计一个序列,使得总花费最小. 题解: 就傻傻的走就好..从左走到右,再走回来,更新序列和答案就好. 代码: #include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #define MA

Codeforces #180 div2 C Parity Game

// Codeforces #180 div2 C Parity Game // // 这道题的题目意思就不解释了 // // 题目有那么一点难(对于我而言),不多说啦 // // 解题思路: // // 首先如果a串和b串相等,不多说直接YES // 如果b串全是0,直接YES // 注意到a串有一个性质,1的个数不会超过本身的加1. // a有个1的上限设为x,b有个1的个数设为y,则如果x < y // 那么直接NO. // // 现在一般情况下,就是模拟啦,找到a的后缀和b的前缀一样的

Codeforces #246(div2)

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <ti

Codeforces #245(div2)

A:A. Points and Segments (easy) 题目看了n久,开始觉得尼玛这是div2的题目么,题目还标明了easy.. 意思是给你一n个点,m个区间,在n个点上放蓝球或者红球,然后让你找一种选择方案使得m个区间内的蓝球和红球数量之差不超过1. 开始想过用dfs,不过这只是div2的A题而已.. 然后想了下,直接输出010101序列不就可以么. 交了一发,发现要先排个序,再输出就可以了. AC代码: #include<iostream> #include<cstdio&g

codeforces#327 div2

codeforces#327 div2 这场状态不好有点可惜,题目都不难,而且很好.. A题:水题. #include<bits/stdc++.h> #define REP(i,a,b) for(int i=a;i<=b;i++) #define MS0(a) memset(a,0,sizeof(a)) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 using namespace std; typedef lo

codeforces#FF(div2) DZY Loves Sequences

n个数,可以任意改变其中一个数,求最长的上升子区间长度 思路:记录一个from[i]表示从位置i的数开始最长的上升区间长度 记录一个to[i]表示到位置i的数所能达到的最长上升区间长度 枚举要改变的数的位置i,此时能达到的长度为to[i - 1] + from[i + 1] + 1,取最大值 //#pragma comment(linker, "/STACK:102400000,102400000") //HEAD #include <cstdio> #include &l

Codeforces 258 Div2

A题,n*m根木棍,相交放置,轮流取走相交的两根,最后谁不能行动,则输掉. min(n,m)&1 为1则先取者赢. B题,给定一个长度为n,且各不相同的数组,问能否通过交换连续一段L....R使得变成单调递增. 如果一开始就是递增的,那么直接输出L...R就是1 1,交换一个就行了:否则判断中间是否有且一段单调递减,且两端交换后使得数组递增. 代码: 1 //Template updates date: 20140718 2 #include <iostream> 3 #include

Codeforces #263 div2 解题报告

比赛链接:http://codeforces.com/contest/462 这次比赛的时候,刚刚注册的时候很想好好的做一下,但是网上喝了个小酒之后,也就迷迷糊糊地看了题目,做了几题,一觉醒来发现rating掉了很多,那个心痛啊! 不过,后来认真的读了题目,发现这次的div2并不是很难! 官方题解:http://codeforces.com/blog/entry/13568 A. Appleman and Easy Task 解析: 一个水题,判断每个细胞周围是否都是有偶数个相邻细胞.   代码

codeforces#333 div2 B. Approximating a Constant Range

http://codeforces.com/contest/602/problem/B 这道题的两种做法, 滑窗+线段树查询区间最值: #include<bits/stdc++.h> #define REP(i,a,b) for(int i=a;i<=b;i++) #define MS0(a) memset(a,0,sizeof(a)) using namespace std; typedef long long ll; const int maxn=1000100; const int