题意:有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. solve License Key is legacy format when use ACTIVATION_CODE activate jetbrains-product 2019.3.1

    1.the java-agent and ACTIVATION_CODE can get from this site:https://zhile.io/2018/08/25/jetbrains-li ...

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

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

  3. VS2019 发布单文件

    在项目.csproj文件下添加 <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework&g ...

  4. 循环读取寄存器(QSFP-DD)并且分别保存log

    #!/bin/bash ####################################################################### #Created by: Bin ...

  5. centos将uwsgi添加为系统服务

    如果退出ssh 链接, 都会导致uwsgi进程关闭 这时, 我们需要进行管理软件管理uwsgi进行的运行, centos系统中我们采用 systemd, 让我们的项目变为系统服务 第一步: 首先 vi ...

  6. [蓝桥杯2015初赛]生命之树(树状dp)

    在X森林里,上帝创建了生命之树.他给每棵树的每个节点(叶子也称为一个节点)上,都标了一个整数,代表这个点的和谐值.上帝要在这棵树内选出一个非空节点集S,使得对于S中的任意两个点a,b,都存在一个点列 ...

  7. 前端代码编译器Hbuilder下载地址和谷歌浏览器下载地址

    编译器:HbuilderX 浏览器:谷歌浏览器

  8. MAC97A6检测

  9. 工作脚本拆分xml文并重定向数据

    sed -n '/<N/p' CM-ENB-SRVIDENTIFYBASEBSRTDD-2C-ALLV2.9.0-20191209020003.xml.gz.xml|awk -F"&g ...

  10. Linux centosVMware 负载均衡集群介绍、LVS介绍、LVS调度算法、LVS NAT模式搭建

    一.负载均衡集群介绍 主流开源软件LVS.keepalived.haproxy.nginx等 其中LVS属于4层(网络OSI 7层模型),nginx属于7层,haproxy既可以认为是4层,也可以当做 ...