Codeforces Round #137 (Div. 2) A

Python 真神奇。。。

传送门:Codeforces Round #137 (Div. 2) A

题意
你能对长度为n的序列执行一个操作
这个操作有两步,
第一步是找到第k个数字,然后把这个数字加到序列最后
第二步是删除第一个数字
你可以执行无数次操作,输出使得这个序列所有数字相同的最小操作数,如果不行,则输出-1
第一行为n和k,第二行为n个数字

思路
如果a[k]…a[n]有一个不同,显 然 输出-1 (自己写一下)
相同的话,a[k]往前遍历,看跟a[k]同不同,相同则可以减少操作的次数

代码

#include  <algorithm>//           ¨|¨|¨|        ¨|¨|¨|¨|
#include   <iostream>//         ¨}¨}¨}¨}      ¨~¨~¨~
#include    <cstring>//        ¨~¨~ ¨~¨~    ¨~¨~
#include    <stdio.h>//       ¨~¨~  ¨~¨~   ¨~¨~
#include     <vector>//      ¨~¨~   ¨~¨~  ¨~¨~
#include      <cmath>//     ¨~¨~    ¨~¨~  ¨~¨~
#include      <queue>//    ¨~¨~ ¨~¨~¨~¨~  ¨~¨~
#include        <map>//   ¨~¨~      ¨~¨~   ¨~¨~
using namespace std ;//  ¨~¨~       ¨~¨~    ¨~¨~¨~
typedef long long ll;// ¨~¨~        ¨~¨~      ¨~¨~¨~¨~¨~
#define pi pair<int,int>  ///
#define P(x,y) make_pair(x,y)
const int maxn = 1e5 + 5;
const ll mod = 998244353;
ll read()
{
	ll x=0;char ch=getchar(); bool flag = false;
	if(ch=='-') { flag = true; ch = getchar();}
	while(ch<'0'||ch>'9')ch=getchar();
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	if(flag) return -x;  else return x;
}



int main()
{   if(fopen("in.txt","r")) freopen("in.txt","r",stdin);
    int T,n,m,k,i,sum,j,t,tmp;
    scanf("%d%d",&n,&k);
    int a[maxn];
    int flag = 0;
    for(i=1;i<=n;i++){
        scanf("%d",&a[i]);
        if(i==k) t = a[i];
        else if(i>k){
            if(a[i]!=t) flag = 1;
        }
    }
    if(flag) printf("-1\n");
    else{
        sum = k-1;
        for(i=k-1;i>=1;i--){
            if(a[i]==t) sum--;
            else break;
        }
        printf("%d\n",sum);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值