zoukankan      html  css  js  c++  java
  • HDU 5775 Bubble Sort

    对于一个数,可以记录3个位置:初始位置,终点位置,最右边的位置。

    初始位置和终点位置容易计算。最多边的位置即为初始状态下该数的位置+该数之后还有多少数比该数小。

    三个位置中的min即为leftpos,max即为rightpos

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<iostream>
    using namespace std;
    typedef long long LL;
    const double pi=acos(-1.0),eps=1e-8;
    void File()
    {
        freopen("D:\in.txt","r",stdin);
        freopen("D:\out.txt","w",stdout);
    }
    inline int read()
    {
        char c = getchar();  while(!isdigit(c)) c = getchar();
        int x = 0;
        while(isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); }
        return x;
    }
    
    const int maxn=100000+10;
    int n,a[maxn],c[maxn],p[maxn],L[maxn],R[maxn];
    
    int lowbit(int x) { return x&(-x); }
    void update(int x) { while(x<=n) c[x]+=1,x+=lowbit(x); }
    int get(int x) {int res=0; while(x) res=res+c[x],x-=lowbit(x); return res;}
    
    int main()
    {
        int T; scanf("%d",&T); int cas=1;
        while(T--)
        {
            scanf("%d",&n); memset(c,0,sizeof c);
            for(int i=1;i<=n;i++)
                scanf("%d",&a[i]),L[a[i]]=min(i,a[i]),R[a[i]]=max(i,a[i]);
            for(int i=1;i<=n;i++)
            {
                int x=get(a[i]-1); x=a[i]-1-x; update(a[i]);
                L[a[i]]=min(L[a[i]],i+x), R[a[i]]=max(R[a[i]],i+x);
            }
    
            printf("Case #%d:",cas++);
            for(int i=1;i<=n;i++) printf(" %d",abs(L[i]-R[i]));
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    模拟Session原理
    练手项目通讯录
    Win 8前台开发小体会
    WP自定义控件
    任务锁和自定义控件制作
    window文件复制到linux系统下
    Linux:Ubuntu配置jdk环境变量
    ubuntu 20 安装完成,配置ip信息
    Unity骨骼优化(转)
    利用栈实现简单计算器
  • 原文地址:https://www.cnblogs.com/zufezzt/p/5721016.html
Copyright © 2011-2022 走看看