国庆练习6

Meeting of Old Friends CF 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 l1r1l2r2 and k (1 ≤ l1, r1, l2, r2, k ≤ 1018l1 ≤ r1l2 ≤ 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

Input
1 10 9 20 1
Output
2
Input
1 100 50 200 75
Output
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.

题目意思:F将要去拜访S,可是S可能在睡觉,给出S睡觉的时间l1~r1,F计划拜访的时间l2~r2,并且在第k分钟F要去打扮,问他们待在一起最多多长时间。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<map>
 4 #include<algorithm>
 5 #define ll long long int
 6 using namespace std;
 7 int main()
 8 {
 9     ll l1,r1,l2,r2,k,ans;
10     ll l,r;
11     ans=0;
12     scanf("%lld%lld%lld%lld%lld",&l1,&r1,&l2,&r2,&k);
13     if(l2>r1||r2<l1)
14     {
15         ans=0;
16     }
17     else
18     {
19         if(l1>l2)
20         {
21             l=l1;
22         }
23         else
24         {
25             l=l2;
26         }
27         if(r1>r2)
28         {
29             r=r2;
30         }
31         else
32         {
33             r=r1;
34         }///这里的l和r作为重合时间段的起始
35         if(k>=l&&k<=r)
36         {
37             ans=r-l+1-1;
38         }
39         else
40         {
41             ans=r-l+1;
42         }
43     }
44     printf("%lld\n",ans);
45     return 0;
46 }

 

Filya and Homework CF 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

Input
5
1 3 3 2 1
Output
YES
Input
5
1 2 3 4 5
Output
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.

题目意思:给n个数,问这n个数能否通过加上或者减去某个数x变成一样大的数。

解题思路:我们知道要是这一组数能通过这一种变换,那么最多只会有3种,a,a+x,a-x。那么我们只要看看是不是这n个数能划分为三类,同时满足a*2=(a+x)+(a-x)。我开始用map来维护,后来想了想直接使用set比较好,这里没有讨论每一类的数量,用set去重就可以了,这里我给出map和set的代码,正好做一个对比。

map:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<map>
 4 #include<algorithm>
 5 #define ll long long int
 6 using namespace std;
 7 ll a[100010];
 8 ll b[100010];
 9 map<ll,int>mp;
10 int main()
11 {
12     int n,i,counts,flag,j;
13     scanf("%d",&n);
14     counts=0;
15     flag=1;
16     for(i=0;i<n;i++)
17     {
18         scanf("%lld",&a[i]);
19         mp[a[i]]++;
20     }
21     j=0;
22     map<ll,int>::iterator it;
23     for(it=mp.begin();it!=mp.end();it++)
24     {
25         //printf("%lld\n",it->first);
26         b[j]=it->first;
27         j++;
28     }
29     if(j>=4)
30     {
31         flag=0;
32     }
33     else
34     {
35         if(j==3)
36         {
37             if(b[0]+b[2]!=b[1]*2)
38             {
39                 flag=0;
40             }
41         }
42     }
43     if(flag)
44     {
45         printf("YES\n");
46     }
47     else
48     {
49         printf("NO\n");
50     }
51     return 0;
52 }

set:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<map>
 4 #include<set>
 5 #include<algorithm>
 6 #define ll long long int
 7 using namespace std;
 8 ll a[100010];
 9 ll b[100010];
10 set<ll>st;
11 int main()
12 {
13     int n,i,counts,flag,j;
14     set<ll>::iterator it;
15     scanf("%d",&n);
16     counts=0;
17     flag=1;
18     for(i=0;i<n;i++)
19     {
20         scanf("%lld",&a[i]);
21         st.insert(a[i]);
22     }
23     j=0;
24     for(it=st.begin();it!=st.end();it++)
25     {
26         b[j]=*it;
27         j++;
28     }
29     if(j>=4)
30     {
31         flag=0;
32     }
33     else
34     {
35         if(j==3)
36         {
37             if(b[0]+b[2]!=b[1]*2)
38             {
39                 flag=0;
40             }
41         }
42     }
43     if(flag)
44     {
45         printf("YES\n");
46     }
47     else
48     {
49         printf("NO\n");
50     }
51     return 0;
52 }

 

Sonya and Queries CF 714C

 

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:

  1.  + 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.
  2.  - 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.
  3. ?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

Input
12
+ 1
+ 241
? 1
+ 361
- 241
? 0101
+ 101
? 101
- 101
? 101
+ 4000
? 0
Output
2
1
2
1
1
Input
4
+ 200
+ 200
- 200
? 0
Output
1

Hint

Consider the integers matching the patterns from the queries of the third type. Queries are numbered in the order they appear in the input.

  1. 1 and 241.
  2. 361.
  3. 101 and 361.
  4. 361.
  5. 4000.

 

题目意思:对于t次操作,'+'代表向集合中添加一个数,'-'代表将集合中相应的数去掉,'?'则代表查询集合中有多少个符合要求的数,查询时给了一个01字符串,0代表对应位数是偶数,1代表对应位数是奇数,如果被查询的位数不相等,则可以给01字符串添加前导零,使其长度对应相等。

解题思路:把集合中的数可以逐位拿出来判断是奇数还是偶数,处理之后奇数为1,偶数变成0,得到的01字符串可以看出二进制数然后转为十进制数,而查询需要的01字符串则直接转为十进制数,看看有多少个与之相等的十进制数就可以了。用map来维护这个集合。

 

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<map>
 4 #include<algorithm>
 5 #define ll long long int
 6 using namespace std;
 7 map<ll,int>mp;
 8 ll change(ll x)
 9 {
10     ll t,k,ans;
11     ans=0;
12     k=1;
13     while(x!=0)
14     {
15         t=x%10;
16         if(t%2)///奇数
17         {
18             ans+=k*1;
19         }
20         k=k*10;
21         x=x/10;
22     }
23     return ans;
24 }
25 int main()
26 {
27     int t;
28     ll n,m;
29     char q;
30     scanf("%d",&t);
31     getchar();
32     while(t--)
33     {
34         scanf("%c",&q);
35         scanf("%lld",&n);
36         getchar();
37         m=change(n);
38         if(q=='+')
39         {
40             mp[m]++;
41         }
42         else if(q=='-')
43         {
44               mp[m]--;
45         }
46         else if(q=='?')
47         {
48             printf("%d\n",mp[m]);
49         }
50     }
51     return 0;
52 }

 

 

 

Forgery(伪造) CF 1059B

Description

Student Andrey has been skipping physical education lessons for the whole term, and now he must somehow get a passing grade on this subject. Obviously, it is impossible to do this by legal means, but Andrey doesn't give up. Having obtained an empty certificate from a local hospital, he is going to use his knowledge of local doctor's handwriting to make a counterfeit certificate(证明) of illness. However, after writing most of the certificate, Andrey suddenly discovered that doctor's signature is impossible to forge. Or is it?

For simplicity, the signature(签名) is represented as an n×mgrid, where every cell is either filled with ink or empty. Andrey's pen can fill a 3×3 square without its central cell if it is completely contained inside the grid, as shown below.


xxx
x.x
xxx

Determine whether is it possible to forge the signature on an empty n×mn×m grid.

Input

The first line of input contains two integers nn and mm (3n,m1000).

Then n lines follow, each contains mm characters. Each of the characters is either '.', representing an empty cell, or '#', representing an ink filled cell.

Output

If Andrey can forge the signature, output "YES". Otherwise output "NO".

You can print each letter in any case (upper or lower).

Sample Input

Input
3 3
###
#.#
###
Output
YES
Input
3 3
###
###
###
Output
NO
Input
4 3
###
###
###
###
Output
YES
Input
5 7
.......
.#####.
.#.#.#.
.#####.
.......
Output
YES

Hint

In the first sample Andrey can paint the border of the square with the center in (2,2).

In the second sample the signature is impossible to forge.

In the third sample Andrey can paint the borders of the squares with the centers in (2,2) and (3,2):

  1. we have a clear paper:

    ...
    ...
    ...
    ...
  2. use the pen with center at (2,2).

    ###
    #.#
    ###
    ...
  3. use the pen with center at (3,2).

    ###
    ###
    ###
    ###

In the fourth sample Andrey can paint the borders of the squares with the centers in (3,3) and (3,5).

 

 

题目意思:要想去伪造医生的签名。医生的签名被简化为中间一个点,周围八个点为#的图形,问所给的图形能否被这个签名填充。

解题思路:这道题当时做的时候一直理解不了意思。。。。在给定的图中寻找特定的点,该点的所有相邻点均被涂色,即为#,找到该点。将另一张空白纸上的该点相邻的点涂色,最后比较两张图是否相同。

 

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<map>
  4 #include<algorithm>
  5 #define ll long long int
  6 using namespace std;
  7 int n,m;
  8 char a[1010][1010];
  9 char b[1010][1010];
 10 void init()///初始化b纸
 11 {
 12     int i,j;
 13     for(i=0; i<n; i++)
 14     {
 15         for(j=0; j<m; j++)
 16         {
 17             b[i][j]='.';
 18         }
 19     }
 20 }
 21 int judge(int x,int y)///在a纸中寻找符合条件的点
 22 {
 23     int i,j;
 24     for(i=-1; i<=1; i++)
 25     {
 26         for(j=-1; j<=1; j++)
 27         {
 28             if(i==0&&j==0)
 29             {
 30                 continue;
 31             }
 32             if(a[x+i][y+j]=='.')///四周为#
 33             {
 34                 return 0;
 35             }
 36         }
 37     }
 38     return 1;
 39 }
 40 void draw(int x,int y)///将a中符合条件的点,画到b中
 41 {
 42     int i,j;
 43     for(i=-1; i<=1; i++)
 44     {
 45         for(j=-1; j<=1; j++)
 46         {
 47             if(i==0&&j==0)
 48             {
 49                 continue;
 50             }
 51             b[x+i][y+j]='#';
 52         }
 53     }
 54 }
 55 int compare_AB()///比较a、b两张纸
 56 {
 57     int i,j;
 58     for(i=0; i<n; i++)
 59     {
 60         for(j=0; j<m; j++)
 61         {
 62             if(a[i][j]!=b[i][j])
 63             {
 64                 return 0;
 65             }
 66         }
 67     }
 68     return 1;
 69 }
 70 int main()
 71 {
 72     int i,j;
 73     while(scanf("%d%d",&n,&m)!=EOF)
 74     {
 75         getchar();
 76         init()
 77         for(i=0; i<n; i++)
 78         {
 79             for(j=0; j<m; j++)
 80             {
 81                 scanf("%c",&a[i][j]);
 82             }
 83             getchar();
 84         }
 85         for(i=1; i<n-1; i++)
 86         {
 87             for(j=1; j<m-1; j++)
 88             {
 89                 if(judge(i,j))
 90                 {
 91                     draw(i,j);
 92                 }
 93             }
 94         }
 95         if(compare_AB())
 96         {
 97             printf("YES\n");
 98         }
 99         else
100         {
101             printf("NO\n");
102         }
103     }
104     return 0;
105 }

 

 

 

 

转载于:https://www.cnblogs.com/wkfvawl/p/9748356.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值