题意:有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;
}

  

最新文章

  1. python核心编程第二版练习题答案
  2. React Native 之 Text的使用
  3. socket编程知识
  4. funny_python 01 import antigravity
  5. cnodejs社区论坛3--发表话题
  6. loj 1154(最大流+枚举汇点)
  7. UIAlertController——之Block回调
  8. Codeforces Educational Codeforces Round 5 A. Comparing Two Long Integers 高精度比大小,模拟
  9. 第 5 章 工厂方法模式【Factory Method Pattern】
  10. 403. Frog Jump
  11. Android开源--MenuDrawer
  12. json 百分比转化
  13. MySQL数据库导入错误:ERROR 1064 (42000) 和 ERROR at line xx:
  14. Hadoop数据类型
  15. 关于RNA-Seq数据去接头(Adapter)这事需要讲一讲
  16. ES7/8新特性学习随笔
  17. PBR Step by Step(二)辐射度
  18. 【BZOJ】1061: [Noi2008]志愿者招募(费用流+数学)
  19. 如何访问tomcat所在服务器的其他盘符的资源。
  20. spark插件入门完整版本

热门文章

  1. java 使用poi 导入Excel 数据到数据库
  2. TomcatJVM参数优化降低内存使用率(重点)!
  3. log4j2 异步多线程打印日志
  4. 读取npz,并显示图像
  5. Spring Boot 2.x教程-Thymeleaf 原理是什么
  6. ➡️➡️➡️leetcode 需要每天打卡,养成习惯
  7. Python学习第十五课——类的基本思想(实例化对象,类对象)
  8. python字符记录
  9. 【Python数据挖掘】第六篇--特征工程
  10. SpringBoot之数据访问和事务-专题三