AtCoder Beginner Contest 072

来源:互联网 发布:pslogo美工基础知识 编辑:程序博客网 时间:2024/05/17 16:43

学了这么久终于有了第一次AK,激动,虽然今晚题目简单了点,但还是忍不住写了博客,纪念一下,我的第一次AK,
题目好像真的很水,我就发一下300和400的题纪念一下;
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 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
这道题我打了个表,因为数据不是很大,吧输入它本身和加一减一都存入数组,每个进去就加一,然后用sort函数排序,找出最大的那个数,输出

#include <cstdio>#include <iostream>#include <string>#include <cstring>#include <algorithm>using namespace std;int a[100100],b[100100];bool cmp(int a,int b){ return a>b;}int main(){  int n;  cin>>n;  memset(b,0,sizeof(b));  for(int i=0;i<n;i++)    {        cin>>a[i];        b[a[i]+1]++;        b[a[i]-1]++;        b[a[i]]++;    }    sort(b,b+100100,cmp);        cout<<b[0]<<endl;}

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
这道题虽然是400分但我感觉是我做的最水的400分,开始被自己吓到了,从来没做出过这道400分的题,后来仔细一看,其实很简单,最简单的模拟就好啦。

#include <cstdio>#include <iostream>#include <string>#include <cstring>#include <algorithm>using namespace std;int a[100100],b[100100];bool cmp(int a,int b){ return a>b;}int main(){  int n;  cin>>n;  int k=0,c=0;  memset(b,0,sizeof(b));  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)           {               int t;               t=a[i];               a[i]=a[i+1];               a[i+1]=t;               c++;           }        }          if(k==n)       {            cout<<'0'<<endl;       }    else        cout<<c<<endl;}

感觉自己学的水题真的是越做越顺手了。

原创粉丝点击