题意:给出若干个数字形成一个序列,每次进行两项操作,(1).在序列末尾添加一个和第k个数字相同的数字。(2).删除第一个数字,求至少要多少次操作之后才能使得这个序列中所有数字一致。
链接:http://codeforces.com/problemset/problem/222/A
思路:规律题,考虑到第k个数字开始会进行复制,并且同时删除第一个数字(复制位置向后移动),所以,只有当第k个数字以后的所有数字相同时,才能使得这个序列中数字保持一致,并且前k-1个元素中,找到最后一个与第k个数字不同的位置n,就是需要的最少的操作次数。
注意点:
以下为AC代码:
# | Author | Problem | Lang | Verdict | Time | Memory | Sent | Judged |
---|---|---|---|---|---|---|---|---|
9709088 | Practice: luminous11 | 222A - 32 | GNU C++11 | Accepted | 154 ms | 388 KB | 2015-02-04 08:54:23 | 2015-02-04 08:54:23 |
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <deque>
#include <list>
#include <cctype>
#include <algorithm>
#include <climits>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#define ll long long
#define ull unsigned long long
#define all(x) (x).begin(), (x).end()
#define clr(a, v) memset( a , v , sizeof(a) )
#define pb push_back
#define mp make_pair
#define read(f) freopen(f, "r", stdin)
#define write(f) freopen(f, "w", stdout)
using namespace std;
const double pi = acos(-1);
//Problem C
int main()
{
ios::sync_with_stdio( false );
int num[100005];
int n;
int k;
while ( cin >> n >> k ){
for ( int i = 1; i <= n; i ++ ){
cin >> num[i];
}
bool flag = 0;
for ( int i = k + 1; i <= n; i ++ ){
if ( num[i] != num[i-1] ){
flag = 1;
break;
}
}
if ( flag ){
cout << -1 << endl;
continue;
}
for ( int i = k - 1; i > 0; i -- ){
if ( num[i] != num[k] ){
cout << i << endl;
flag = 1;
break;
}
}
if ( ! flag )
cout << 0 << endl;
}
}