AtCoder Beginner Contest 072

今天打得真的很爽,15分钟就AK了,第一次在13名。。。在过生日时打真的是我的幸运日,哈哈哈。

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

Copy
100 17

Sample Output 1

Copy
83

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

有x克,每秒减少一克,问t秒后还有多少克,水题。

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 
 5 int main() {
 6     int x, t;
 7     cin >> x >> t;
 8     printf("%d
",max(0,x-t));
 9     return 0;
10 }

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

Copy
atcoder

Sample Output 1

Copy
acdr

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

把位数为奇数的输入下,水题。

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int N = 1e5+10;
 5 char str[N];
 6 int main() {
 7     cin >> str;
 8     for(int i = 0; str[i];  i++) {
 9         if(i %2 == 0) printf("%c",str[i]);
10     }
11     printf("
");
12     return 0;
13 }

C - Together


Time limit : 2sec / Memory limit : 256MB

Score : 300 points

Problem Statement

You are given an integer sequence of length N, a1,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:

N
a1 a2 .. aN

Output

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


Sample Input 1

Copy
7
3 1 4 1 5 9 2

Sample Output 1

Copy
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.

每位可以加减一或不动,求相同数字的个数最大值。水题。

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int N = 1e5+10;
 5 // int a[N];
 6 map<int, int> mp;
 7 int main() {
 8     int n, x;
 9     scanf("%d",&n);
10     for(int i = 0; i < n; i ++) {
11         scanf("%d",&x);
12         mp[x]++;
13         mp[x+1]++;
14         mp[x-1]++;
15     }
16     map<int, int> ::iterator it;
17     int MAX = 1;
18     for(it = mp.begin(); it != mp.end(); ++it) {
19         if(MAX < (*it).second){
20             MAX = (*it).second;
21         }
22     }
23     printf("%d
",MAX);
24     return 0;
25 }

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:

N
p1 p2 .. pN

Output

Print the minimum required number of operations


Sample Input 1

Copy
5
1 4 3 5 2

Sample Output 1

Copy
2

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

每次可以和相邻的交换位置,求最少的交换次数使的a[i] != i,继续水题。

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int N = 1e5+10;
 5 int a[N];
 6 int main() {
 7     int n;
 8     scanf("%d",&n);
 9     for(int i = 1; i <= n; i ++) {
10         scanf("%d", &a[i]);
11     }
12     int ans = 0;
13     for(int i = 1; i < n; i ++) {
14         if(a[i] == i) {
15             swap(a[i], a[i+1]);
16             ans++;
17         }
18     }
19     if(a[n] == n) ans++;
20     printf("%d
",ans);
21     return 0;
22 }
原文地址:https://www.cnblogs.com/xingkongyihao/p/7467785.html