本文链接:https://www.cnblogs.com/blowhail/p/10990833.html

Nauuo and Cards

原题链接:http://codeforces.com/contest/1173/problem/C

题目大意 :有2n张卡片,其中n张编号为1-n,另外n张为0. 现在手中拿n张,桌子上放n张,每次操作可以在桌子上底部放入一张卡片,最上面拿走一张卡片,问最小操作次数。

思路: 先看看卡片1的位置,卡片1在桌子上时,看看是否满足从1递增到底部,例如 0 0 1 2 3 4 。如果满足就依次插入卡片,取出卡片。

   如果1在手中,就开始依次插入卡片,取出卡片,从1开始,插入1之后,如果2卡片此时仍然在桌子上的卡组里,操作次数+1,取出最前面的卡片,继续判断2卡片。(这里意思是如果2不在手里,就要等2拿到之后再插入,相当于在1卡片的前面插入了0卡片,即1卡片插入的次数延后)

代码如下:

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#define ll long long
using namespace std;
int a[],b[],c[];
int main ()
{
int i,t,m,n,u,sum=,maxs,mins,x,y=,z;
scanf("%d",&n);
for(i=;i<=n;++i){
scanf("%d",&t);
a[t]++;
c[t]++; //a,c数组记录手中的卡牌
}
for(i=;i<=n;++i)
scanf("%d",&b[i]);
int j,k;
if(a[]==) //判断1在不在手中
{
for(i=;i<=n;++i)
if(b[i]==)
{
k=;j=;
while(j) // 1在桌子上的卡组时,判断1后面是否依次递增到最后一张
{
if(i==n)
{
break;
}
else
{
i++;
k++;
if(b[i]!=k)
{
j=;
}
}
}
if(!j)
break;
}
}
k=;
if(j) //如果从卡片1到最后一张都是依次递增的,判断能否继续往后添加卡片
{
x=b[n]+;
if(x-==n) //如果正好桌上的牌满足1-n递增放好
k=;
for(i=;i<=n;++i){
if(c[x]==)
sum++;
else {
break;
}
c[ b[i] ]++; //取出最前面的卡片 if(x>=n) //顺利放完全部卡片
k=;
x++;
}
}
if(k){
printf("%d\n",sum);
return ;
}
  //如果卡片1在手中
sum=;
int p=;
for(i=; ;++i)
{
if(a[p]==) //将卡片p放入,如果此时卡片p不存在,操作数+1 ,取出最前面的卡片 (相当于在卡片1前面插入一张0卡片)
p++; a[ b[i] ]++; //取出最前面的卡片
sum++; //记录操作次数
if(p==n+)
break;
}
printf("%d\n",sum); return ;
}

code forces 1173 C. Nauuo and Cards的更多相关文章

  1. code forces 1173 B. Nauuo and Chess

    本文链接:https://www.cnblogs.com/blowhail/p/10991237.html B. Nauuo and Chess  原题链接:http://codeforces.com ...

  2. 思维题--code forces round# 551 div.2

    思维题--code forces round# 551 div.2 题目 D. Serval and Rooted Tree time limit per test 2 seconds memory ...

  3. Code Forces 796C Bank Hacking(贪心)

    Code Forces 796C Bank Hacking 题目大意 给一棵树,有\(n\)个点,\(n-1\)条边,现在让你决策出一个点作为起点,去掉这个点,然后这个点连接的所有点权值+=1,然后再 ...

  4. Code Forces 833 A The Meaningless Game(思维,数学)

    Code Forces 833 A The Meaningless Game 题目大意 有两个人玩游戏,每轮给出一个自然数k,赢得人乘k^2,输得人乘k,给出最后两个人的分数,问两个人能否达到这个分数 ...

  5. Codeforces Round #564 (Div. 2) C. Nauuo and Cards

    链接:https://codeforces.com/contest/1173/problem/C 题意: Nauuo is a girl who loves playing cards. One da ...

  6. code force 401B. Game of Credit Cards

    B. Game of Credit Cards time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  7. Code Forces 543A Writing Code

    题目描述 Programmers working on a large project have just received a task to write exactly mm lines of c ...

  8. code forces 383 Arpa's loud Owf and Mehrdad's evil plan(有向图最小环)

    Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 megab ...

  9. code forces 382 D Taxes(数论--哥德巴赫猜想)

    Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...

随机推荐

  1. 狄利克雷过程(Dirichlet Process)

    0. 引入 现观察得到两个样本 θ1,θ2,来推测它们可能来自的分布: 假设来自于连续型概率密度函数, θ1,θ2∼H(θ) 则 θ1,θ2 相等的概率为 0,p(θ1=θ2)=0 概率为 0,不代表 ...

  2. Lync 2013和Exchange 2013集成

    定位到下面Powershell 文件夹: C:\Program Files\Microsoft\Exchange Server\V15\Scripts\,运行例如以下命令: .\Configure-E ...

  3. WPF中Style文件的引用——使用xaml代码或者C#代码动态加载

    原文:WPF中Style文件的引用--使用xaml代码或者C#代码动态加载 WPF中控件拥有很多依赖属性(Dependency Property),我们可以通过编写自定义Style文件来控制控件的外观 ...

  4. Entity相互关系

    查看 1.图表(.edmx) Model First可以2.代码 内部包含对方(回溯) 1:1   1   2 1:N   3   4 N:M   5   6

  5. js错误界面

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  6. 同时使用SpringJUnit4ClassRunner和Parameterized进行参数化

    标题实际上是个不可能完成的任务,因为我们只能用一个Runwith注解,而且只能写一个类,但是我们可以曲线救国,插入下方的5到14行就可以注入了 @ContextConfiguration(classe ...

  7. Win8 Metro(C#)数字图像处理--3.2图像方差计算

    原文:Win8 Metro(C#)数字图像处理--3.2图像方差计算 /// <summary> /// /// </summary>Variance computing. / ...

  8. GIS基础软件及操作(十一)

    原文 GIS基础软件及操作(十一) 练习十一.3D 可视分析 1.对地理数据进行透视观察.三维浏览:2.制作飞行动画 GIS数据三维显示 (1) 运行程序:[开始菜单]>>[所有程序]&g ...

  9. WPF应用App.Config文件的保存路径

    App.Config文件有更改后,自动会保存到以下路径: C:\Users\你的系统用户名\AppData\Local\你的应用名\

  10. Ext5.1日期控件仅显示年月

    1.注册xtype类型 2.保存文件为xxxx.js 3.使用 xtype : monthfield return this.buildToolbar({ items: [ { xtype: 'mon ...