AC日记——Cards Sorting codeforces 830B

Cards Sorting

思路:

  线段树;

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define maxn 100005
#define INF 0x3f3f3f3f
#define maxtree maxn<<2
int n,ai[maxn],val[maxtree],L[maxtree],R[maxtree],Min[maxtree];
int P[maxtree],mid[maxtree];
inline void in(int &now)
{
    char Cget=getchar();now=0;
    while(Cget>‘9‘||Cget<‘0‘) Cget=getchar();
    while(Cget>=‘0‘&&Cget<=‘9‘)
    {
        now=now*10+Cget-‘0‘;
        Cget=getchar();
    }
}
void updata(int now)
{
    val[now]=val[now<<1]+val[now<<1|1];
    Min[now]=min(Min[now<<1],Min[now<<1|1]);
    if(Min[now]==Min[now<<1|1]) P[now]=P[now<<1|1];
    else P[now]=P[now<<1];
}
void build(int now,int l,int r)
{
    L[now]=l,R[now]=r;
    if(l==r)
    {
        Min[now]=ai[l],P[now]=l,val[now]=1;
        return;
    }
    mid[now]=l+r>>1;
    build(now<<1,l,mid[now]);
    build(now<<1|1,mid[now]+1,r);
    updata(now);
}
void change(int now,int to)
{
    if(L[now]==R[now])
    {
        val[now]=0,Min[now]=INF,P[now]=0;
        return;
    }
    if(to<=mid[now]) change(now<<1,to);
    else change(now<<1|1,to);
    updata(now);
}
int query(int now,int l,int r)
{
    if(L[now]>=l&&R[now]<=r) return val[now];
    int res=0;
    if(l<=mid[now]) res+=query(now<<1,l,r);
    if(r>mid[now]) res+=query(now<<1|1,l,r);
    return res;
}
int get(int now,int l,int r)
{
    if(L[now]>=l&&R[now]<=r) return P[now];
    int tmp1=0,tmp2=0;
    if(l<=mid[now]) tmp1=get(now<<1,l,r);
    if(r>mid[now]) tmp2=get(now<<1|1,l,r);
    if(tmp2&&tmp1)
    {
        if(ai[tmp1]<ai[tmp2]) return tmp1;
        else return tmp2;
    }
    if(tmp2) return tmp2;
    if(tmp1) return tmp1;
    return 0;
}
int main()
{
    in(n);
    for(int i=1;i<=n;i++) in(ai[i]);
    int l=1,r=n;while(l<r) swap(ai[l],ai[r]),l++,r--;
    build(1,1,n);
    int now=n,tmp1,tmp2,tmp;
    long long ans=0;
    for(int i=1;i<=n;i++)
    {
        tmp1=0,tmp2=0,tmp1=get(1,1,now);
        if(now<n) tmp2=get(1,now+1,n);
        if(tmp1&&tmp2)
        {
            if(ai[tmp2]<ai[tmp1]) tmp=tmp2;
            else tmp=tmp1;
        }
        else if(tmp1) tmp=tmp1;
        else tmp=tmp2;
        if(tmp<=now) ans+=query(1,tmp,now);
        else ans+=query(1,1,now)+query(1,tmp,n);
        change(1,tmp),now=tmp;
    }
    cout<<ans;
    return 0;
}
时间: 2024-10-26 05:18:30

AC日记——Cards Sorting codeforces 830B的相关文章

AC日记——Success Rate codeforces 807c

Success Rate 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define ll long long inline void in(ll &now) { char Cget=getchar();now=0; while(Cget>'9'||Cget<'0

AC日记——Broken BST codeforces 797d

D - Broken BST 思路: 二叉搜索树: 它时间很优是因为每次都能把区间缩减为原来的一半: 所以,我们每次都缩减权值区间. 然后判断dis[now]是否在区间中: 代码: #include <map> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn 100005

AC日记——T-Shirt Hunt codeforces 807b

T-Shirt Hunt 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; int p,x,y,ai[27],ans; bool check(int xx) { if(xx<y) return false; int i=(xx/50)%475; for(int j=1;j<=2

Codeforces Round #424 (Div. 2) E. Cards Sorting(线段树)

题目链接:Codeforces Round #424 (Div. 2) E. Cards Sorting 题意: 将n个数放进一个队列,每次检查队首,看看是不是队列中最小的数,如果是就扔掉,如果不是就放到队尾. 这样直到队列为空,为需要操作多少次. 题解: 考虑用两个指针模拟,最开始now指针指向第一个数,然后nxt指针指向下一个将要被删除的数. 然后我们要算出这里需要移动多少步,然后删掉这个数,一直重复操作,直到将全部的数删完. nxt指针可以用set来维护,now指针可以用并查集来维护. 计

cf 830B - Cards Sorting 树状数组

B. Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this integer is between 1 and 10

Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Cards Sorting(树状数组)

Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this integer is between 1 and 100?0

AC日记——Aragorn&#39;s Story HDU 3966

Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10510    Accepted Submission(s): 2766 Problem Description Our protagonist is the handsome human prince Aragorn comes from The Lor

CodeForces 830B - Cards Sorting

将每个数字的位置存进该数字的vector中 原数组排个序从小到大处理,每次在vector里二分找到距离当前位置“最远”的位置(相差最大),更新答案 树状数组维护每个数字现在的位置和原位置之差 1 #include <bits/stdc++.h> 2 using namespace std; 3 #define LL long long 4 const int N = 100005; 5 int n; 6 int a[N], b[N]; 7 vector<int> v[N]; 8 i

AC日记——Dynamic Problem Scoring codeforces 807d

Dynamic Problem Scoring 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn 130 int n,ai[maxn][6],ac[6],cnt,all,last1,last2; double map[3][6]; inline void in(i