CodeForces 222A Shooshuns and Sequence

本文介绍了一个涉及序列操作的问题,目标是最少操作次数使序列中所有元素相同。通过分析,确定了关键步骤在于寻找序列中首个不匹配元素的位置,进而得出最小操作数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

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

注意点:


以下为AC代码:

#AuthorProblemLangVerdictTimeMemorySentJudged
9709088Practice:
luminous11
222A - 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 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;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值