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 ...
随机推荐
- CF 455A(Boredom-dp)
A. Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- js 生成表格及其颜色
<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...
- Web应用程序和网站的区别
1项目就是一个应用程序.在VS中查看的时候,项目中建立的一般处理程序,有两个文件,网站只有一个.写个代码测试,发现在代码层次上没有2再有就是项目中的一般处理程序有命名空间,而网站中的没有.WEB网站每 ...
- 利用WIX制作安装包(3)
原文 利用WIX制作安装包(3) 利用WIX安装服务非常简单.只需要短短几句话就可以.当我们创建好一个Windows服务之后.我们在项目中创建一个Service.wxs 文件来安装服务,并且编辑代码如 ...
- Win10中解决Prolific PL2303出现错误代码10的问题
PL2303 是Prolific 公司生产的一种高度集成的RS232-USB接口转换器,在Win10中默认安装的驱动程序会出现错误代码10的问题,如下图所示: 下载Win10上可以用的PL2303驱动 ...
- 【shell】gerrit同步备份脚本的实现
本期分享下gerrit如何实现备份,由于之前的一台代码服务器直接down掉,所以为了以防万一,开始备份gerrit数据,有人说,gerrit不过是个审查代码的工具,备份这个做什么,git是分布式代码管 ...
- HTTP协议解析(格式和举例十分清楚)
掌握HTTP虽然不是必须的,但是如果你知道它的工作原理,那么在学习JSP开发中的某些知识就可以易如反掌了. 一,HTTP协议详解之URL篇 http(超文本传输协议)是一个基于请求与响应模式的.无状态 ...
- C#判断系统是32位还是64位
bool type; type = Environment.Is64BitOperatingSystem; Console.WriteLine(type);
- Linux 下 Redis 服务 Shell启动脚本
# chkconfig: 2345 10 90 # description: Start and Stop redis PATH=/usr/local/bin:/sbin:/usr/bin:/bin ...
- SQL Server 将某一列的值拼接成字符串
名称 海鲜水产 水果蔬菜 海参 肉禽蛋 牛排 腊味 生鲜食品 将以上一列变成: 生鲜食品,海鲜水产,水果蔬菜,海参,牛排,肉禽蛋,腊味 sql for xml path('')