题意:有n个数的序列,n个数都为0,每次指定某个数变为1,当序列中第i个数为1,第i+1个数为0时,这两个数可交换,将序列从头到尾进行一次交换记为1次,直到某一次从头到尾的交换中没有任何两个数交换。序列的hard值为进行前面叙述过程中共进行的从头到尾交换的次数。每修改某个数为1,都输出对应的hard值。

分析:

1、如果第一次将最后一个数变为1,显然验证交换的次数为1,这一趟验证了没有任何两个数需要交换,因此结束。

2、如果第一次将中间的某个数变为1,显然我们在第一次从头到尾的交换中可以把这个1挪到最后,第二次从头到尾的交换验证了没有数需要交换,因此次数为2。

3、综上所述,只要是中间的数,那么交换次数必加1,一开始指针指向最后一个数,如果变了指针指向的数,那交换次数自然减1,指针相应前移,复杂度O(n)。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 300000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int a[MAXN];
vector<int> v;
int main(){
int n, x;
scanf("%d", &n);
int ans = 1;
int pos = n;
v.push_back(1);
for(int i = 0; i < n; ++i){
scanf("%d", &x);
++ans;
a[x] = 1;
while(pos >= 1 && a[pos]){
--pos;
--ans;
}
v.push_back(ans);
}
int len = v.size();
for(int i = 0; i < len; ++i){
if(i) printf(" ");
printf("%d", v[i]);
}
printf("\n");
return 0;
}

  

CodeForces - 876D Sorting the Coins的更多相关文章

  1. Codeforces D. Sorting the Coins

    D. Sorting the Coins time limit per test 1 second memory limit per test 512 megabytes input standard ...

  2. codeforces 876 D. Sorting the Coins

    http://codeforces.com/contest/876/problem/D D. Sorting the Coins time limit per test 1 second memory ...

  3. Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) D. Sorting the Coins

    http://codeforces.com/contest/876/problem/D 题意: 最开始有一串全部由"O"组成的字符串,现在给出n个数字,指的是每次把位置n上的&qu ...

  4. Codeforces Round #441 D. Sorting the Coins(模拟)

    http://codeforces.com/contest/876/problem/D 题意:题意真是难懂,就是给一串序列,第i次操作会在p[x](1<=x<=i)这些位置放上硬币,然后从 ...

  5. codeforces 876 D. Sorting the Coins(线段树(不用线段树写也行线段树写比较装逼))

    题目链接:http://codeforces.com/contest/876/problem/D 题解:一道简单的类似模拟的题目.其实就是看右边连出来有多少连续不需要换的假设位置为pos只要找pos- ...

  6. D. Sorting the Coins

    Recently, Dima met with Sasha in a philatelic store, and since then they are collecting coins togeth ...

  7. [set]Codeforces 830B-Cards Sorting

    Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  8. Codeforces 335C Sorting Railway Cars

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

  9. CodeForces 605A Sorting Railway Cars 思维

    早起一水…… 题意看着和蓝桥杯B组的大题第二道貌似一个意思…… 不过还是有亮瞎双眼的超短代码…… 总的意思呢…… 就是最长增长子序列且增长差距为1的的…… 然后n-最大长度…… 这都怎么想的…… 希望 ...

随机推荐

  1. The Last Puzzle ZOJ - 3541

    题目链接 本题也是区间dp,贪心可证,每一次出发必定是从端点,否则必然有重复,不会是最小值,那我们可以设dpi,j,0/1,0代表从左端点出发,1代表从右端点,因为每次都是从端点出发,状态方程为 dp ...

  2. Linux - kali Linux重置密码

    1. recovery mode -> E 2. ro -> rw 3. plus init=/bin/bash 4. passwd root

  3. 如鹏网仿QQ侧滑菜单:ResideMenu组件的使用笔记整理+Demo

    ResideMenu菜单 课堂笔记: https://github.com/SpecialCyCi/AndroidResideMenu Github:如何使用开源组件1. 下载 下载方式: 1. 项目 ...

  4. UICollectionViewCell点击高亮效果(附带效果GIF)

    首先效果如下: 背景是这样的:UI上使用的是UICollectionView,所以后面会使用它的协议方法完成. 实现思路是这样的:高亮状态+点击后短时间内保持颜色变化 实现的代码参考如下: // Ce ...

  5. urllib 库的代替品 requests 的用法

    Requuests 官方的介绍时多么的霸气,之所以那么霸气,是因为 Requestts 相比于 urllib 在使用方面上会让开发者感到更加的人性化.更加简洁.更加舒适,并且国外的一些公司也在使用re ...

  6. CentOS7配置python3教程

    环境准备: 1.python3.7.1 下载地址:https://www.python.org/    注意:CentOS7默认就有python2的 2.虚拟机CentOS 7 64位,下载地址:ht ...

  7. day4-1深入理解对象之创建对象

    深入理解对象 之创建对象: 工厂模式: 工厂模式虽然解决了创建\多个相似对象的问题,但却没有解决对象识别的问题(即怎样知道一个对象的类型) 工厂模式问题:那就是识别问题,因为根本无法 搞清楚他们到底是 ...

  8. ANSYS-MFC生成APDL

    目录 1. 简介 2. APDL生成 3. 调用ANSYS批处理 1. 简介 对于ANSYS-MFC二次开发,两者之间的关系非常明确,从界面中读取参数并转换成APDL语言,然后调用批处理操作. 对于简 ...

  9. MyBatis6——一级缓存、二级缓存、逆向工程

    查询缓存 一级缓存:同一个sqlSession对象 MyBatis默认开启一级缓存,如果用同样的sqlSession对象查询相同的数据,则会在第一次查询时向数据库发送SQL语句,并将查询的结果放入到S ...

  10. 133、Java获取main主函数参数

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...