CodeForces 222A Shooshuns and Sequence

来源:互联网 发布:电瓶车速度测试软件 编辑:程序博客网 时间:2024/05/02 04:44

题意:给出若干个数字形成一个序列,每次进行两项操作,(1).在序列末尾添加一个和第k个数字相同的数字。(2).删除第一个数字,求至少要多少次操作之后才能使得这个序列中所有数字一致。

链接:http://codeforces.com/problemset/problem/222/A

思路:规律题,考虑到第k个数字开始会进行复制,并且同时删除第一个数字(复制位置向后移动),所以,只有当第k个数字以后的所有数字相同时,才能使得这个序列中数字保持一致,并且前k-1个元素中,找到最后一个与第k个数字不同的位置n,就是需要的最少的操作次数。

注意点:


以下为AC代码:

#AuthorProblemLangVerdictTimeMemorySentJudged9709088Practice:
luminous11222A -32GNU C++11Accepted154 ms388 KB2015-02-04 08:54:232015-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 Cint 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;    }}


0 0
原创粉丝点击