code forces 1173 C. Nauuo and Cards
本文链接: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的更多相关文章
- code forces 1173 B. Nauuo and Chess
本文链接:https://www.cnblogs.com/blowhail/p/10991237.html B. Nauuo and Chess 原题链接:http://codeforces.com ...
- 思维题--code forces round# 551 div.2
思维题--code forces round# 551 div.2 题目 D. Serval and Rooted Tree time limit per test 2 seconds memory ...
- Code Forces 796C Bank Hacking(贪心)
Code Forces 796C Bank Hacking 题目大意 给一棵树,有\(n\)个点,\(n-1\)条边,现在让你决策出一个点作为起点,去掉这个点,然后这个点连接的所有点权值+=1,然后再 ...
- Code Forces 833 A The Meaningless Game(思维,数学)
Code Forces 833 A The Meaningless Game 题目大意 有两个人玩游戏,每轮给出一个自然数k,赢得人乘k^2,输得人乘k,给出最后两个人的分数,问两个人能否达到这个分数 ...
- 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 ...
- 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 ...
- Code Forces 543A Writing Code
题目描述 Programmers working on a large project have just received a task to write exactly mm lines of c ...
- 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 ...
- code forces 382 D Taxes(数论--哥德巴赫猜想)
Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...
随机推荐
- Bootstrap Edit 使用方法
Getting Started <!-- rounded edit text --> <com.beardedhen.androidbootstrap.BootstrapEditTe ...
- JS判断android/IOS,并执行回调函数
判断类型: var u = navigator.userAgent; var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') ...
- C# 操作XML文档 使用XmlDocument类方法
W3C制定了XML DOM标准.很多编程语言中多提供了支持W3C XML DOM标准的API.我在之前的文章中介绍过如何使用Javascript对XML文档进行加载与查询.在本文中,我来介绍一下.Ne ...
- Emgu-WPF学习使用-识别二维码的位置
原文:Emgu-WPF学习使用-识别二维码的位置 参考链接:http://blog.csdn.net/gaobobo138968/article/details/47663607 我完全参 ...
- WPF 附加属性的使用
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- WPF中的图像处理简介
原文:WPF中的图像处理简介 和Winform中的GDI+相比,WPF提供了一组新的API用于显示和编辑图像.新API特点如下: 适用于新的或专用图像格式的扩展性模型. 对包括位图 (BMP).联合图 ...
- 配置 Visual Studio Tools for Apache Cordova
原文:配置 Visual Studio Tools for Apache Cordova 1.连接地址 https://msdn.microsoft.com/zh-cn/library/vs/alm/ ...
- ARTS 1.14 - 1.18
期望: 每周一个 Algorithm,Review 一篇英文文章,总结一个工作中的技术 Tip,以及 Share 一个传递价值观的东西! Algorithm: 学习算法 Two Sum IV - In ...
- 微信小程序把玩(三十九)navigation API
原文:微信小程序把玩(三十九)navigation API 演示效果也看到了小程序也就提供这几个处理导航控制.值得注意的是只能同时导航五个页面 主要属性: 导航条一些方法 wx.setNavigati ...
- 关于EF ORM 框架的使用问题
1.无法更新 EntitySet“System_UserInfo20140218001”,因为它有一个 DefiningQuery,而 <ModificationFunctionMapping& ...