codeforces 250/C 逆向思维

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.

Sample test(s)
input
10 3
1 1 2 3 2 3 3 1 1 3
output
3
input
7 3
3 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.



有一行数字从开头遍历到结尾,如果一个数字和前面的数字不同则改变次数加一,题目的要求是去掉其中一种数,能使得变化的次数最小。求去掉的是哪一类数。

刚开始想的是逐一去掉,然后求各种改变次数,然后比较所有的改变次数,取出最小值。显然在数据10^5之下,这种时间复杂度是O(n^2)的算法是绝对不被允许的。

可以换一种想法,首先有一点可以明确就是,在不去掉任何一类数之前可以发现总的改变次数一定,我们只要求出去掉一个数后能使改变次数减少最多的哪一中数就可以了。

另外有一点补充,像序列112233完全可以写成123,连着的两个相同的数是一种数,他们个数的增加或者减少对总的改变数是完全没有影响的。

代码如下:

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
int a[100005],p[100005];
int main()
{
    int m,n,l;
    while(~scanf("%d%d",&n,&m))
    {
        l=1;
        for(int i=1;i<=n;i++)
        {
             cin >> a[i];
             if(a[i]==a[i-1])
                  i--,n--;
        }
        for(int i=1;i<=n;i++)
        {
            if(a[i-1]==a[i+1])
                 p[a[i]]+=2;
            else
                 p[a[i]]++;
        }
        for(int i=1;i<=m;i++)
        {
            if(p[i]>p[l])
                 l=i;
        }
        cout <<l<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值