zoukankan      html  css  js  c++  java
  • CodeForces

    题意:有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("
    ");
        return 0;
    }
    

      

  • 相关阅读:
    每日总结3.8
    Go中定时器实现原理及源码解析
    Go语言实现布谷鸟过滤器
    详解Go语言调度循环源码实现
    Go语言中时间轮的实现
    详解Go语言I/O多路复用netpoller模型
    详解Go中内存分配源码实现
    Go中由WaitGroup引发对内存对齐思考
    【分享】java精品实战教程
    nginx实战教程
  • 原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/7695756.html
Copyright © 2011-2022 走看看