CodeForces

来源:互联网 发布:淘宝客佣金结算时间 编辑:程序博客网 时间:2024/06/06 01:25
C. Movie Critics
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A film festival is coming up in the city N. The festival will last for exactly n days and each day will have a premiere of exactly one film. Each film has a genre — an integer from 1 to k.

On the i-th day the festival will show a movie of genre ai. We know that a movie of each of k genres occurs in the festival programme at least once. In other words, each integer from 1 to k occurs in the sequence a1, a2, ..., an at least once.

Valentine is a movie critic. He wants to watch some movies of the festival and then describe his impressions on his site.

As any creative person, Valentine is very susceptive. After he watched the movie of a certain genre, Valentine forms the mood he preserves until he watches the next movie. If the genre of the next movie is the same, it does not change Valentine's mood. If the genres are different, Valentine's mood changes according to the new genre and Valentine has a stress.

Valentine can't watch all n movies, so he decided to exclude from his to-watch list movies of one of the genres. In other words, Valentine is going to choose exactly one of the k genres and will skip all the movies of this genre. He is sure to visit other movies.

Valentine wants to choose such genre x (1 ≤ x ≤ k), that the total number of after-movie stresses (after all movies of genre x are excluded) were minimum.

Input

The first line of the input contains two integers n and k (2 ≤ k ≤ n ≤ 105), where n is the number of movies and k is the number of genres.

The second line of the input contains a sequence of n positive integers a1a2, ..., an (1 ≤ ai ≤ k), where ai is the genre of the i-th movie. It is guaranteed that each number from 1 to k occurs at least once in this sequence.

Output

Print a single number — the number of the genre (from 1 to k) of the excluded films. If there are multiple answers, print the genre with the minimum number.

Examples
input
10 31 1 2 3 2 3 3 1 1 3
output
3
input
7 33 1 3 2 3 1 2
output
1
Note

In the first sample if we exclude the movies of the 1st genre, the genres 2, 3, 2, 3, 3, 3 remain, that is 3 stresses; if we exclude the movies of the 2nd genre, the genres 1, 1, 3, 3, 3, 1, 1, 3 remain, that is 3 stresses; if we exclude the movies of the 3rd genre the genres 1, 1, 2, 2, 1, 1 remain, that is 2 stresses.

In the second sample whatever genre Valentine excludes, he will have exactly 3 stresses.

题意:n场电影,每场有个代号,可以去掉相同的一些,去掉之后,如果每个相邻的电影值不同,他就会压力值+1,求去掉那个之后压力值最小

思路:优化一下,直接把相同的值去掉,后来计算会少麻烦

#include<iostream>#include<stdio.h>#include<string.h>using namespace std;#define mod 1000000007#define ll long longint s[100010],d[100010];int main(){    int n,k,x,t;    while(cin>>n>>k)    {        t=1;        memset(d,0,sizeof(d));        s[0]=0;        for(int i=1;i<=n;i++)        {            cin>>x;            if(x!=s[t-1])                s[t++]=x;        }        s[0]=s[t]=0;        for(int i=1;i<t;i++)        {            if(s[i-1]==s[i+1])                d[s[i]]+=2;            else                d[s[i]]+=1;        }        int p=1;        cout<<d[1]<<endl;        for(int i=2;i<=k;i++)        {            cout<<d[i]<<endl;            if(d[i]>d[p])                 p=i;            }        cout<<p<<endl;    }    return 0;}

E. Mad Joe
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Joe has been hurt on the Internet. Now he is storming around the house, destroying everything in his path.

Joe's house has n floors, each floor is a segment of m cells. Each cell either contains nothing (it is an empty cell), or has a brick or a concrete wall (always something one of three). It is believed that each floor is surrounded by a concrete wall on the left and on the right.

Now Joe is on the n-th floor and in the first cell, counting from left to right. At each moment of time, Joe has the direction of his gaze, to the right or to the left (always one direction of the two). Initially, Joe looks to the right.

Joe moves by a particular algorithm. Every second he makes one of the following actions:

  • If the cell directly under Joe is empty, then Joe falls down. That is, he moves to this cell, the gaze direction is preserved.
  • Otherwise consider the next cell in the current direction of the gaze.
    • If the cell is empty, then Joe moves into it, the gaze direction is preserved.
    • If this cell has bricks, then Joe breaks them with his forehead (the cell becomes empty), and changes the direction of his gaze to the opposite.
    • If this cell has a concrete wall, then Joe just changes the direction of his gaze to the opposite (concrete can withstand any number of forehead hits).

Joe calms down as soon as he reaches any cell of the first floor.

The figure below shows an example Joe's movements around the house.

Determine how many seconds Joe will need to calm down.

Input

The first line contains two integers n and m (2 ≤ n ≤ 1001 ≤ m ≤ 104).

Next n lines contain the description of Joe's house. The i-th of these lines contains the description of the (n - i + 1)-th floor of the house — a line that consists of m characters: "." means an empty cell, "+" means bricks and "#" means a concrete wall.

It is guaranteed that the first cell of the n-th floor is empty.

Output

Print a single number — the number of seconds Joe needs to reach the first floor; or else, print word "Never" (without the quotes), if it can never happen.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cincout streams or the %I64dspecifier.

Examples
input
3 5..+.##+..++.#+.
output
14
input
4 10...+.##+.++#++..+++#++.#++++...+##.++#.+
output
42
input
2 2..++
output
Never
题意:不知道什么游戏,给出地图,.可行,+是砖块,#混凝土,砖块可以撞破,然后转向,初始位置是右上角,只要向下可以走便向下,问你走到最下面一层需要多少秒或者到不了

调了近两个小时的这个题目,还是没调通,到最后发现错误的根源,一直以为是下降左右走的方式错了,后来才发现是输入的时候错了,Scanf(“%s”,map[i])想着是从1-m,但是这么输入成了0  -  m-1  

看了个比较好的思想,确定左右的限制区域,因为已经确定这中间全是'.',所以直接减去即可,剩下的就是模拟

注意   向下是绝对优先的

#include<iostream>#include<stdio.h>#include<string.h>#include<cmath>using namespace std;#define mod 1000000007#define ll long longchar map[110][10010];ll ans;int main(){    int m,n,now=1,l,r,t=1;    scanf("%d%d",&m,&n);    for(int i=0;i<m;i++)    {        scanf("%s",map[i]+1);        map[i][0]=map[i][n+1]='#';    }    int i,j;    for(i=0;i<m-1;i++)    {        if(map[i+1][now]=='.')        {            ++ans;            continue;        }        l=now-1;        r=now+1;        while(1)        {            if(map[i][l]=='#'&&map[i][r]=='#')            {                cout<<"Never"<<endl;                return 0;            }            ans+=r-l-2;            if(t==1)            {                while(map[i][r]=='.'&&map[i+1][r]!='.')                {                    r++;                    ans++;                }                if(map[i][r]=='.')                {                    ans+=2;                    now=r;                    break;                }                if(map[i][r]=='+')                {                    t=-t;                    ans++;                    map[i][r]='.';                }                else if(map[i][r]=='#')                {                    t=-t;                    ans++;                }            }            else            {                while(map[i][l]=='.'&&map[i+1][l]!='.')                {                    l--;                    ans++;                }                if(map[i][l]=='.')                {                    ans+=2;                    now=l;                    break;                }                if(map[i][l]=='+')                {                    t=-t;                    ans++;                    map[i][l]='.';                }                else if(map[i][l]=='#')                {                    t=-t;                    ans++;                }            }        }    }    cout<<ans<<endl;    return 0;}