AtCoder Beginner Contest 072

来源:互联网 发布:程序员带什么眼镜护眼 编辑:程序博客网 时间:2024/05/19 03:44

A - Sandglass2


Time limit : 2sec / Memory limit : 256MB

Score : 100 points

Problem Statement

We have a sandglass that runs for X seconds. The sand drops from the upper bulb at a rate of 1 gram per second. That is, the upper bulb initially contains X grams of sand.

How many grams of sand will the upper bulb contains after t seconds?

Constraints

  • 1X109
  • 1t109
  • X and t are integers.

Input

The input is given from Standard Input in the following format:

X t

Output

Print the number of sand in the upper bulb after t second.


Sample Input 1

100 17

Sample Output 1

83

17 out of the initial 100 grams of sand will be consumed, resulting in 83 grams.


Sample Input 2

48 58

Sample Output 2

0

All 48 grams of sand will be gone, resulting in 0 grams.


Sample Input 3

1000000000 1000000000

Sample Output 3

0

#include<iostream>using namespace std;int main(){long long int x,t;cin>>x>>t;if(x-t==0){cout<<0<<endl;}if(x-t>0){cout<<x-t<<endl;}if(x-t<0){cout<<0<<endl;}return 0;}

B - OddString


Time limit : 2sec / Memory limit : 256MB

Score : 200 points

Problem Statement

You are given a string s consisting of lowercase English letters. Extract all the characters in the odd-indexed positions and print the string obtained by concatenating them. Here, the leftmost character is assigned the index 1.

Constraints

  • Each character in s is a lowercase English letter.
  • 1|s|105

Input

The input is given from Standard Input in the following format:

s

Output

Print the string obtained by concatenating all the characters in the odd-numbered positions.


Sample Input 1

atcoder

Sample Output 1

acdr

Extract the first character a, the third character c, the fifth character d and the seventh character r to obtain acdr.


Sample Input 2

aaaa

Sample Output 2

aa

Sample Input 3

z

Sample Output 3

z

Sample Input 4

fukuokayamaguchi

Sample Output 4

fkoaaauh

#include<iostream>using namespace std;char a[100010];int main(){long long int n=0;while(~scanf("%c",&a[n])){n++;}for(int i=0;i<n;i=i+2){cout<<a[i];}return 0;}

C - Together


Time limit : 2sec / Memory limit : 256MB

Score : 300 points

Problem Statement

You are given an integer sequence of length Na1,a2,…,aN.

For each 1iN, you have three choices: add 1 to ai, subtract 1 from ai or do nothing.

After these operations, you select an integer X and count the number of i such that ai=X.

Maximize this count by making optimal choices.

Constraints

  • 1N105
  • 0ai<105(1iN)
  • ai is an integer.

Input

The input is given from Standard Input in the following format:

Na1 a2 .. aN

Output

Print the maximum possible number of i such that ai=X.


Sample Input 1

73 1 4 1 5 9 2

Sample Output 1

4

For example, turn the sequence into 2,2,3,2,6,9,2 and select X=2 to obtain 4, the maximum possible count.


Sample Input 2

100 1 2 3 4 5 6 7 8 9

Sample Output 2

3

Sample Input 3

199999

Sample Output 3

1
#include <iostream>#include<cstring>#include <algorithm>using namespace std;int a[100001];int main(){    int n;    while(cin>>n)    {        int t;        memset(a,0,sizeof(a));        for(int i=1;i<=n;i++)        {            scanf("%d",&t);            a[t+1]++;            a[t-1]++;            a[t]++;        }        sort(a,a+100001);        cout<<a[100000]<<endl;    }    return 0;}

D - Derangement


Time limit : 2sec / Memory limit : 256MB

Score : 400 points

Problem Statement

You are given a permutation p1,p2,…,pN consisting of 1,2,..,N. You can perform the following operation any number of times (possibly zero):

Operation: Swap two adjacent elements in the permutation.

You want to have pii for all 1iN. Find the minimum required number of operations to achieve this.

Constraints

  • 2N105
  • p1,p2,..,pN is a permutation of 1,2,..,N.

Input

The input is given from Standard Input in the following format:

Np1 p2 .. pN

Output

Print the minimum required number of operations


Sample Input 1

51 4 3 5 2

Sample Output 1

2

Swap 1 and 4, then swap 1 and 3p is now 4,3,1,5,2 and satisfies the condition. This is the minimum possible number, so the answer is 2.


Sample Input 2

21 2

Sample Output 2

1

Swapping 1 and 2 satisfies the condition.


Sample Input 3

22 1

Sample Output 3

0

The condition is already satisfied initially.


Sample Input 4

91 2 4 9 5 8 7 3 6

Sample Output 4

3
#include<iostream>using namespace std;int a[100010];int main(){int n,k=0,temp,c=0;cin>>n;for(int i=1;i<=n;i++){cin>>a[i];if(a[i]!=i)k++;}for(int i=1;i<=n;i++){if(a[i]==i){temp=a[i];a[i]=a[i+1];a[i+1]=temp;c++;}}if(k==n)cout<<0<<endl;elsecout<<c<<endl;}