• Codeforces Codeforces Round #316 (Div. 2) C. Replacement set


    C. Replacement

    Time Limit: 20 Sec

    Memory Limit: 256 MB

    题目连接

    http://codeforces.com/contest/570/problem/C

    Description

    Daniel has a string s, consisting of lowercase English letters and period signs (characters '.'). Let's define the operation of replacementas the following sequence of steps: find a substring ".." (two consecutive periods) in string s, of all occurrences of the substring let's choose the first one, and replace this substring with string ".". In other words, during the replacement operation, the first two consecutive periods are replaced by one. If string s contains no two consecutive periods, then nothing happens.

    Let's define f(s) as the minimum number of operations of replacement to perform, so that the string does not have any two consecutive periods left.

    You need to process m queries, the i-th results in that the character at position xi (1 ≤ xi ≤ n) of string s is assigned value ci. After each operation you have to calculate and output the value of f(s).

    Help Daniel to process all queries.

     
     

    Input

    The first line contains two integers n and m (1 ≤ n, m ≤ 300 000) the length of the string and the number of queries.

    The second line contains string s, consisting of n lowercase English letters and period signs.

    The following m lines contain the descriptions of queries. The i-th line contains integer xi and ci (1 ≤ xi ≤ nci — a lowercas English letter or a period sign), describing the query of assigning symbol ci to position xi.

    Output

    Print m numbers, one per line, the i-th of these numbers must be equal to the value of f(s) after performing the i-th assignment.

    Sample Input

    10 3
    .b..bz....
    1 h
    3 c
    9 f

    Sample Output

    4
    3
    1

    HINT

    题意

    给你一个字符串,然后每两个点可以变成一个点

    然后有m次修改操作,每次可以修改一个位置的字符

    然后问你修改之后,需要多少次操作,把所有的点,都变成连续的一个点

    题解

        出去玩了5天一回来,本不该做比赛的:(

        set乱搞

    代码:

    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <queue>
    #include <typeinfo>
    #include <map>
    #include<bits/stdc++.h>
    #include <stack>
    typedef long long ll;
    using namespace std;
    #define inf 10000000
    inline ll read()
    {
        ll x=0,f=1;
        char ch=getchar();
        while(ch<'0'||ch>'9')
        {
            if(ch=='-')f=-1;
            ch=getchar();
        }
        while(ch>='0'&&ch<='9')
        {
            x=x*10+ch-'0';
            ch=getchar();
        }
        return x*f;
    }
    //***************************************************************
    set<int >s;
    set<int >::iterator it,itt;
    int vis[4000000];   char a[3000000];
    int main()
    {
    
       int  n=read();
       int  m=read();
        gets(a+1);
        s.insert(0);
        s.insert(n+1);
       int  last=0;
        int r=0;
        int ans=0;
        for(int i=1;i<=n;i++){
            if(a[i]!='.'){
                s.insert(i);
                vis[i]=1;
                if(r>last)
                ans+=(r-last-1);
                last=i;
                r=i;
            }
            else {r++;
             if(i==n){
                 if(r>last)
                ans+=(r-last-1);
             }
            }
           // cout<<ans<<" "<<endl;
        }
    
        //cout<<ans<<endl;
       int x,l,mid;
       char ch;
       for(int i=1;i<=m;i++){
         scanf("%d %c",&x,&ch);
         if(ch!='.')
         {
             if(vis[x]){
                printf("%d
    ",ans);
                continue;
             }
             r=*s.lower_bound(x);
             l=*--s.lower_bound(x);
              // if(i==3){cout<<l<<" "<<r<<endl;}
             ans-=(max(r-l-2,0));
             //if(i==3)cout<<ans<<" ";
             ans+=max(x-l-2,0);
             // if(i==3)cout<<ans<<" ";
             ans+=max(r-x-1-1,0);
            //  if(i==3)cout<<ans<<" ";
             vis[x]=1;
             s.insert(x);
             cout<<ans<<endl;
         }
         else {
            if(s.count(x)==0){
                cout<<ans<<endl;
                continue;
            }
            it=s.lower_bound(x);
            mid=*it;
            l=*--it;
            it++;
            r=*++it;
            ans-=max(mid-l-2,0);
            ans-=max(r-mid-2,0);
            ans+=max(r-l-2,0);
            vis[x]=0;
            s.erase(x);
              cout<<ans<<endl;
         }
       }
        return 0;
    }

     好特么捉急

    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <queue>
    #include <typeinfo>
    #include <map>
    #include<bits/stdc++.h>
    #include <stack>
    typedef long long ll;
    using namespace std;
    #define inf 10000000
    inline ll read()
    {
        ll x=0,f=1;
        char ch=getchar();
        while(ch<'0'||ch>'9')
        {
            if(ch=='-')f=-1;
            ch=getchar();
        }
        while(ch>='0'&&ch<='9')
        {
            x=x*10+ch-'0';
            ch=getchar();
        }
        return x*f;
    }
    //***************************************************************
    int n,m,x;
    char c,ss[300005];
    int main()
    {
        while(~scanf("%d%d",&n,&m))
        {
            int ans=0;
            scanf("%s",ss+1);
            for(int i=1;i<=n;i++)
            {
                if(ss[i]=='.'&&ss[i+1]=='.')
                {
                    ans++;
                }
            }
            for(int i=1;i<=m;i++)
            {
                scanf("%d %c",&x,&c);
                if(c=='.'&&ss[x]!='.')
                {
                    if(ss[x-1]=='.') ans++;
                    if(ss[x+1]=='.') ans++;
                }
                if(c!='.'&&ss[x]=='.')
                {
                    if(ss[x-1]=='.') ans--;
                    if(ss[x+1]=='.') ans--;
                }
                ss[x]=c;
                printf("%d
    ",ans);
            }
        }
        return 0;
    }
  • 相关阅读:
    SP3946 MKTHNUM
    P1948 [USACO08JAN]电话线Telephone Lines(二分答案+最短路)
    CF375D Tree and Queries(dsu on tree)
    P2051 [AHOI2009]中国象棋(动态规划)
    P3810 【模板】三维偏序(陌上花开)(cdq分治)
    P4390 [BOI2007]Mokia 摩基亚(cdq分治)
    P2163 [SHOI2007]园丁的烦恼(cdq分治)
    UVA11270 Tiling Dominoes(轮廓线动态规划)
    P2475 [SCOI2008]斜堆(递归模拟)
    P2617 Dynamic Rankings(带修主席树)
  • 原文地址:https://www.cnblogs.com/zxhl/p/4729170.html
Copyright © 2020-2023  润新知