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