A - A codeforces 714A
Description
Today an outstanding event is going to happen in the forest — hedgehog Filya will come to his old fried Sonya!
Sonya is an owl and she sleeps during the day and stay awake from minute l1 to minute r1 inclusive. Also, during the minute k she prinks and is unavailable for Filya.
Filya works a lot and he plans to visit Sonya from minute l2 to minute r2 inclusive.
Calculate the number of minutes they will be able to spend together.
Input
The only line of the input contains integers l1, r1, l2, r2 and k (1 ≤ l1, r1, l2, r2, k ≤ 1018, l1 ≤ r1, l2 ≤ r2), providing the segments of time for Sonya and Filya and the moment of time when Sonya prinks.
Output
Print one integer — the number of minutes Sonya and Filya will be able to spend together.
Sample Input
1 10 9 20 1
2
1 100 50 200 75
50
Hint
In the first sample, they will be together during minutes 9 and 10.
In the second sample, they will be together from minute 50 to minute 74 and from minute 76 to minute 100.
题意:小E要在白天去拜访S,S呢在白天只有(l1,r1)是醒着的,小E选择在(l2,r2)去拜访S,但是在第K分钟的时候,他们不能共处,问他们最多能一起相处多长时间
分析:这题其实就是找两个区间的交集啦,注意数据量要开long long,所以错了一发
分下面六种情况:
l2 r2 l1 r1
l1 r1 l2 r2
l1 l2 r1 r2
l2 l1 r2 r1
l1 l2 r2 r1
l2 l1 r1 r2
然后开写啦~是不是很简单
1 #include<cstdio> 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<cmath> 6 using namespace std; 7 int main() 8 { 9 long long l1,r1,l2,r2,k; 10 while(~scanf("%lld %lld %lld %lld %lld",&l1,&r1,&l2,&r2,&k)) 11 { 12 long long sum=0; 13 if(l1>=l2&&r2>=r1) 14 { 15 sum=r1-l1+1; 16 if(k>=l1&&k<=r1) 17 sum--; 18 } 19 else if(r2<l1||l2>r1) 20 { 21 sum=0; 22 } 23 else if(l2<=l1&&r2>=l1&&r2<=r1) 24 { 25 sum=r2-l1+1; 26 if(k<=r2&&k>=l1) 27 sum--; 28 } 29 else if(l2>=l1&&l2<=r1&&r2>=r1) 30 { 31 sum=r1-l2+1; 32 if(k<=r1&&k>=l2) 33 sum--; 34 } 35 else if(l2>=l1&&r2<=r1) 36 { 37 sum=r2-l2+1; 38 if(k>=l2&&k<=r2) 39 sum--; 40 } 41 printf("%lld\n",sum); 42 } 43 return 0; 44 }
B - B
codeforces 1059B
#include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #include<iostream> using namespace std; char a[1005][1005],b[1005][1005]; void judge(int i,int j) { int flag=0; for(int t=i-1;t<=i+1;t++) for(int tt=j-1;tt<=j+1;tt++) { if(t==i&&tt==j) continue; if(a[t][tt]!='#') { flag=1; break; } }//判断周围九个点 if(flag==0) { b[i-1][j-1]='#'; b[i-1][j+1]='#'; b[i][j-1]='#'; b[i][j+1]='#'; b[i+1][j-1]='#'; b[i+1][j+1]='#'; b[i-1][j]='#'; b[i+1][j]='#'; }//将b也染色 } int main() { int n,m; while(~scanf("%d %d",&n,&m)) { getchar(); memset(b,'.',sizeof(b));//将b初始化 for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { scanf("%c",&a[i][j]); } getchar(); } for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(i-1>=1&&i+1<=n&&j-1>=1&&j+1<=m) judge(i,j); } } int flag=0; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(a[i][j]!=b[i][j]) { flag=1; break; } } }//判断a数组与b数组是否相等 if(flag==1) printf("NO\n"); else printf("YES\n"); } return 0; }
D - D codeforces 714B
Description
Today, hedgehog Filya went to school for the very first time! Teacher gave him a homework which Filya was unable to complete without your help.
Filya is given an array of non-negative integers a1, a2, ..., an. First, he pick an integer x and then he adds x to some elements of the array (no more than once), subtract x from some other elements (also, no more than once) and do no change other elements. He wants all elements of the array to be equal.
Now he wonders if it's possible to pick such integer x and change some elements of the array using this x in order to make all elements equal.
Input
The first line of the input contains an integer n (1 ≤ n ≤ 100 000) — the number of integers in the Filya's array. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109) — elements of the array.
Output
If it's impossible to make all elements of the array equal using the process given in the problem statement, then print "NO" (without quotes) in the only line of the output. Otherwise print "YES" (without quotes).
Sample Input
5
1 3 3 2 1
YES
5
1 2 3 4 5
NO
Hint
In the first sample Filya should select x = 1, then add it to the first and the last elements of the array and subtract from the second and the third elements.
题意:给你一个数组,可以选择对这个数组的某些数进行加x操作(只能操作一次)或者对这个数组里的某些数进行减x操作(只能操作一次),问是否能使整个数组里的所有数都相等,如果可以,输出YES,否则输出NO
分析:如果数组里不同的数的个数大于3时,一定输出NO,
如果数组里的不同的数的个数小于等于2时,一定输出YES
如果数组里的不同的数的个数刚好等于3时,
当最大的数和最小的数到中间那个数的距离相等的时候,他们加减的都是x,一定输出YES,否则输出NO
苍天啊啊啊,一定很注意细节的问题了,交题之前还专门瞄了一眼YESNO的大小写,没想到竟然是栽在N0上,被自己蠢哭了)
1 #include<cstdio> 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<cmath> 6 using namespace std; 7 int main() 8 { 9 int n,a[100005],b[100005]; 10 while(~scanf("%d",&n)) 11 { 12 int k=0; 13 for(int i=1;i<=n;i++) 14 { 15 scanf("%d",&a[i]); 16 } 17 sort(a+1,a+1+n); 18 b[1]=a[1]; 19 int j=2; 20 for(int i=2;i<=n;i++) 21 { 22 if(a[i]!=a[i-1]) 23 { 24 k++; 25 b[j++]=a[i]; 26 }//k其实就是来统计不同的数的个数的 27 } 28 int t=0; 29 if(k==2) 30 { 31 if(b[2]-b[1]==b[3]-b[2]) 32 t=1; 33 }当最大的数和最小的数到中间那个数的距离相等的时候 34 if(k<2)//当不同的数的个数小于等于2时 35 t=1; 36 if(k>2||t==0) 37 { 38 printf("NO\n"); 39 }//当不同的数的个数大于3时 40 else 41 { 42 printf("YES\n"); 43 } 44 45 } 46 return 0; 47 }
E - E codeforces714c
Description
Today Sonya learned about long integers and invited all her friends to share the fun. Sonya has an initially empty multiset with integers. Friends give her t queries, each of one of the following type:
- + ai — add non-negative integer ai to the multiset. Note, that she has a multiset, thus there may be many occurrences of the same integer.
- - ai — delete a single occurrence of non-negative integer ai from the multiset. It's guaranteed, that there is at least one ai in the multiset.
- ?s — count the number of integers in the multiset (with repetitions) that match some pattern s consisting of 0 and 1. In the pattern, 0 stands for the even digits, while 1 stands for the odd. Integer x matches the pattern s, if the parity of the i-th from the right digit in decimal notation matches the i-th from the right digit of the pattern. If the pattern is shorter than this integer, it's supplemented with 0-s from the left. Similarly, if the integer is shorter than the pattern its decimal notation is supplemented with the 0-s from the left.
For example, if the pattern is s = 010, than integers 92, 2212, 50 and 414 match the pattern, while integers 3, 110, 25 and 1030 do not.
Input
The first line of the input contains an integer t (1 ≤ t ≤ 100 000) — the number of operation Sonya has to perform.
Next t lines provide the descriptions of the queries in order they appear in the input file. The i-th row starts with a character ci — the type of the corresponding operation. If ci is equal to '+' or '-' then it's followed by a space and an integer ai (0 ≤ ai < 1018) given without leading zeroes (unless it's 0). If ci equals '?' then it's followed by a space and a sequence of zeroes and onse, giving the pattern of length no more than 18.
It's guaranteed that there will be at least one query of type '?'.
It's guaranteed that any time some integer is removed from the multiset, there will be at least one occurrence of this integer in it.
Output
For each query of the third type print the number of integers matching the given pattern. Each integer is counted as many times, as it appears in the multiset at this moment of time.
Sample Input
12
+ 1
+ 241
? 1
+ 361
- 241
? 0101
+ 101
? 101
- 101
? 101
+ 4000
? 0
2
1
2
1
1
4
+ 200
+ 200
- 200
? 0
1
题意:你有一个空数组,t次查询,每次查询有一个字符 c和数字a,如果c是‘+’,则把数字a加到数组中去(数组里的数字可以重复),如果c是‘-’,则把数字a从数组中删去(保证数组里一定会有数字a),如果c是‘?’,则在数组里查询有多少个数字a的奇偶性相同(a中的0代表偶数,1代表奇数),如果数组里的数字长度小于a时,则在数字的左边添零,同样,如果a的长度小于数组里的数字时,在a的左边添0,输出和a奇偶性相同的数的个数.
分析:如果直接用数组来存的话,会TLE的,所以选择用map来存,存之前得预处理一下,把数字a转换成1和0的形式
1 #include<cstdio> 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<cmath> 6 #include<map> 7 using namespace std; 8 int main() 9 { 10 map<long long,int>m; 11 int t; 12 char c; 13 long long d; 14 scanf("%d",&t); 15 while(t--) 16 { 17 getchar(); 18 scanf("%c%lld",&c,&d); 19 long long dd=0,k=1; 20 while(d) 21 { 22 if((d%10)%2==1) 23 dd+=k*((d%10)%2); 24 k*=10; 25 d/=10; 26 }//预处理 27 // printf("%lld\n",dd); 28 if(c=='+') 29 { 30 m[dd]++; 31 } 32 else if(c=='-') 33 { 34 m[dd]--; 35 } 36 else 37 { 38 printf("%d\n",m[dd]); 39 } 40 } 41 return 0; 42 }