题意:有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. [原]SVN代码管理

    1.SVN工程结构 branches zhangsan lisi wangwu tags project-release trunk project-trunk branches:下为trunk的分支 ...

  2. 关于PGSQL连接问题

    今天把运维管理服务移植在Linux下测试,但又不想在Linux中安装PGSQL的服务器,就想让运维管理服务在虚拟机中连接windows的PG服务,却发现PG服务器一直拒绝连接,检查了网络端口之后都没问 ...

  3. python中解方程

    from sympy import * import numpy as np from numpy import linalg # 方程中的符号 x = Symbol('x') # 计算 result ...

  4. 折腾前端条形码(Barcode)扫描识别, 笔记

    barcode @zxing/library 方案 本地勉强把 Demo 在 React 里面跑通, 但是不好控制开始结束, API 不明确.实际识别率很低. 我是用手机屏幕放的条形码, 大概也有影响 ...

  5. Servlet 学习(七)

     ServletConfig 1.定义 ServletConfig接口:servlet容器在初始化期间将信息传递给servlet的servlet配置对象 代表当前Servlet在web.xml中的配置 ...

  6. springMVC的 Converter转换器 和 Formatter

    Converter转换器 spring的Converter是可以将一种类型转换成另一种类型的一个对象, 自定义Converter需要实现Converter接口 日期转换器 import java.te ...

  7. Spring开发踩坑记录

    #1 @EnableEurekaServer无法正常import原因是spring-cloud-dependencies版本太低,改成高版本的Edgware.SR4即可.参考:https://www. ...

  8. Git远程分支代码强制回退&Tag添加

    Git指令大全:https://www.alexkras.com/getting-started-with-git/ Git提交错了,还是Master分支,哎呦喂咋整?请见下文.   [场景描述] 项 ...

  9. 远程登陆ubantu服务器 .bashrc文件每次打开终端都需要source的问题

    通过创建的用户登录ubantu服务器时,.bashrc文件每次都要重新配置,要不然里面的配置如命令的简写如 ll 等就无法识别,本方法用于实现登录时自动执行.bashrc文件. 1.ubantu启动时 ...

  10. js读取本地json/txt/xml存在跨越问题,可以用jsonp 读取本地json文件

    想自己用 js写一个原生的ajax请求,访问本地文件,json/txt.但是demo,写了一个后,发现 原来是跨域了. js 写的原生ajax 请求代码如下 html代码 <div id=&qu ...