Thứ Hai, 26 tháng 11, 2012

Problem Movie Critics

Link: http://codeforces.com/problemset/problem/250/C

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), whereai 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.


Thuật toán:

  • Đọc dữ liệu, bỏ qua các số liền kề giống nhau bằng cách sử dụng một vector.
  • tính mảng c[i], i = 1...k.
  • Duyệt qua mảng c để tìm kết quả.
Code:

/*
Coder : Nguyen Duc Tam
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<set>
#include<vector>
#include<utility>
#include<map>
#include<list>
#include<queue>
#include<deque>
#include<stack>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<iomanip>
#include<ctime>

using namespace std;

#define REP(i, start, end, step) for(int i = start; i < end; i += step)
#define DOWN(i, start, end, step) for(int i = start; i > end; i -= step)
#define FOR(it,c) for(__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define ALL(c) (c).begin(), (c).end()
#define SZ(x) ((int)(x).size())
#define X first
#define Y second

#define L(x,i) ((x) << (i))
#define R(x,i) ((x) >> (i))
#define AND(a,b) ((a) & (b))
#define OR(a,b) ((a) | (b))
#define XOR(a,b) ((a) ^ (b))
#define NOT(a) (~(a))
#define SB(x,i) (OR((x), L(1, (i)))) // x | 1 << i
#define CB(x,i) (AND((x),NOT(L(1,(i))))) // x & ~(1 << i)
#define TB(x,i) (AND((x), L(1,(i)))) // x & (1 << i)

#define FILL(a,val) memset(a,val,sizeof(a));
#define INIT(a,l,r,val) REP(i,l,r,1) (a)[i] = val;
#define DIG(c) (int)((c) - '0')
#define CHR(c) (char)((c) + '0')
#define LOW(c) (char)((c) + 32)
#define UPP(c) (char)((c) - 32)

#define EPS 1e-7
#define OO 1000000005
#define N 100005

const int DAY[13] = {-1,31,29,31,30,31,30,31,31,30,31,30,31};
const int dx4[4] = {};
const int dy4[4] = {};
const int dx8[8] = {};
const int dy8[8] = {};


typedef pair<int,int> II;
typedef pair<II,int> D;
typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned char UC;

vector<int>::iterator it, low, up; // using FOR, lower_bound, upper_bound
pair<vector<int>,vector<int> > bound; // using function equal_range(first, last, x)

int n,x,res = (1<<30),k,c[N];
vector<int> a;

int main()
{
#define Off  true
if(Off)
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
}
cin >> n >> k;
REP(i,0,n,1)
{
scanf("%d",&x);
if(a.size() == 0 || a.back() != x)
a.push_back(x);
}

c[a[0]]++;
c[a[a.size()-1]]++;

REP(i,1,a.size()-1,1)
{
if(a[i-1] == a[i+1])
c[a[i]] += 2;
else
c[a[i]] += 1;
}
REP(i,1,k+1,1)
if(res > a.size() - 1 - c[i])
{
res = a.size() - 1 - c[i];
x = i;
}
//REP(i,0,k + 1,1) cout << "c["<<i<<"]=" <<c[i] << endl;
cout << x << endl;
return 0;
}





Không có nhận xét nào:

Đăng nhận xét