AtCoder Regular Contest 082 ABCD
A
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a[123456]; 4 int n,m; 5 int main(){ 6 cin>>n>>m; 7 cout<<max(0,n-m)<<endl; 8 return 0; 9 }
B
1 #include<bits/stdc++.h> 2 using namespace std; 3 int a[123456]; 4 int n,m; 5 int main(){ 6 string s; 7 cin>>s; 8 string ans = ""; 9 for (int i = 0; i < s.length(); i += 2) 10 { 11 ans += s[i]; 12 } 13 cout << ans << endl; 14 return 0; 15 }
Problem Statement
You are given an integer sequence of length N, a1,a2,…,aN.
For each 1≤i≤N, 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
- 1≤N≤105
- 0≤ai<105(1≤i≤N)
- 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
7 3 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
10 0 1 2 3 4 5 6 7 8 9
Sample Output 2
3
Sample Input 3
1 99999
Sample Output 3
1
解法:数字-1 +1 或者不变,最后留下数字最多的次数
解法:那就...讨论一下
本身的出现次数,i和i+1 i和i+2 i和i+1和i+2的出现次数
1 #include<bits/stdc++.h> 2 using namespace std; 3 long long a[123456]; 4 int n; 5 bool vis[1009]; 6 map<long long,long long>Mp; 7 int main(){ 8 scanf("%d",&n); 9 for(int i=1;i<=n;i++){ 10 scanf("%d",&a[i]); 11 Mp[a[i]]++; 12 } 13 long long Max=1; 14 for(int i=0;i<=100010;i++){ 15 Max=max(Max,Mp[i]); 16 if(Mp[i]&&Mp[i+1]&&Mp[i+2]){ 17 long long ans=Mp[i]+Mp[i+1]+Mp[i+2]; 18 Max=max(Max,ans); 19 } 20 if(Mp[i]&&Mp[i+1]){ 21 long long ans=Mp[i]+Mp[i+1]; 22 Max=max(Max,ans); 23 } 24 if(Mp[i]&&Mp[i+2]){ 25 long long ans=Mp[i]+Mp[i+2]; 26 Max=max(Max,ans); 27 } 28 } 29 cout<<Max<<endl; 30 return 0; 31 }
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 pi≠i for all 1≤i≤N. Find the minimum required number of operations to achieve this.
Constraints
- 2≤N≤105
- 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
5 1 4 3 5 2
Sample Output 1
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.
Sample Input 2
2 1 2
Sample Output 2
1
Swapping 1 and 2 satisfies the condition.
Sample Input 3
2 2 1
Sample Output 3
0
The condition is already satisfied initially.
Sample Input 4
9 1 2 4 9 5 8 7 3 6
Sample Output 4
3
题意:交换相邻的数字,使得ai!=i 问最少的次数
解法:贪心,反正如果是正确位置上的,我们去交换相邻的就可以了
#include<bits/stdc++.h> using namespace std; int a[123456]; int n; bool vis[1009]; map<int,int>Mp; int main(){ scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); if(a[i]==i){ Mp[i]=1; } } int sum=0; for(int i=1;i<=n;i++){ if(Mp[i]==1){ sum++; Mp[i]=0; Mp[i+1]=0; } } cout<<sum<<endl; return 0; }