4

POJ 1804 - Brainman

 2 years ago
source link: https://exp-blog.com/algorithm/poj/poj1804-brainman/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

和 [POJ2299](./> 可以参看 [POJ2299](./> 把 S[i]s[i+1~n] 的元素逐个比较,如果 s[i] > s[k] (k∈[i+1,n]) 则逆序数 t++

解题方法一:归并排序

/*借助Mergesort求逆序数O(nlogn)*/

//Memory Time 
//228K   172MS 

#include<iostream>
using namespace std;

const int inf=1000001;
int t;  //数字序列s[]的逆序数

void compute_t(int* s,int top,int mid,int end)
{
    int len1=mid-top+1;
    int len2=end-mid;

    int* left=new int[len1+2];
    int* right=new int[len2+2];

    int i,j;
    for(i=1;i<=len1;i++)
        left[i]=s[top+i-1];
    left[len1+1]=inf;

    for(j=1;j<=len2;j++)
        right[j]=s[mid+j];
    right[len2+1]=inf;

    i=j=1;
    for(int k=top;k<=end;k++)
        if(left[i]<=right[j])
            s[k]=left[i++];
        else
        {
            s[k]=right[j++];
            t+=len1-i+1;
        }

    delete left;
    delete right;

    return;
}

void mergesort(int* s,int top,int end)
{
    if(top<end)
    {
        int mid=(top+end)/2;
        mergesort(s,top,mid);
        mergesort(s,mid+1,end);
        compute_t(s,top,mid,end);
    }
    return;
}
int main(void)
{
    int test;
    cin>>test;
    for(int p=1;p<=test;p++)
    {
        int n;  //数字序列s[]长度
        cin>>n;

        int* s=new int[n+1];

        for(int i=1;i<=n;i++)
            cin>>s[i];

        t=0;
        mergesort(s,1,n);

        cout<<"Scenario #"<<p<<':'<<endl<<t<<endl<<endl;

        delete s;
    }
    return 0;
}

解题方法二:直接求逆序数

/*直接求逆序数O(n^2)*/

//Memory Time 
//220K  188MS

#include <iostream>    
using namespace std;

int main(int i,int j)
{
    int test;
    cin>>test;
    for(int p=1;p<=test;p++)
    {
        int n;
        cin>>n;

        int* s=new int[n+1];
        for(i=1;i<=n;i++)
            cin>>s[i];

        int t=0;  //s[]的逆序数
        for(i=1;i<=n-1;i++)   //把S[i]和s[i+1~n]的元素逐个比较
            for(j=i+1;j<=n;j++)
                if(s[i]>s[j])  //如果s[i] > s[j] (j∈[i+1,n]) 
                    t++;   //则逆序数t++

        cout<<"Scenario #"<<p<<':'<<endl<<t<<endl<<endl;

        delete s;
    } 
    return 0;
}

Recommend

  • 13
    • blog.woshiluo.com 3 years ago
    • Cache

    POJ 3071 Football

    POJ 3071 Football 2020年9月24日2020年12月16日 by woshiluo 1 题目大意 给定 2n 个整数,从 1 到 2n 编号 给定 pi,...

  • 3

    Armin's BlogPOJ 1611 The Suspects(并查集)November 28, 2015题目链接 题意:非典时期,共有 n 个人(标号 0~n-1),分成 m 组。第一行输入 n(0...

  • 9

    Armin's BlogPOJ 2524 Ubiquitous Religions(并查集)November 29, 2015题目链接 题意:调查学校的学生宗教信仰情况,第一行输入 n(总人数)和 m,...

  • 8
    • zxs.io 3 years ago
    • Cache

    poj 1455 Crazy tea party

    poj 1455 Crazy tea party 2013-10-03 分类:ACM 阅读(4401) 评论(0)   这道题第一眼看去很难,其...

  • 18
    • zxs.io 3 years ago
    • Cache

    poj 1503 高精度加法

    poj 1503 高精度加法 2013-09-07 分类:未分类 阅读(4401) 评论(0) 把输入...

  • 8

    poj 1205 :Water Treatment Plants (DP+高精度) 题意:有n个城市,它们由一个污水处理系统连接着,每个城市可以选择1、将左边城市过来的污水和右边城市过来的污水连同本身的污水排到河里  >V<2、将左边来的污水连同自己的污水排...

  • 14

    poj 2155 Matrix (二维树状数组) 2013-08-31 分类:未分类 阅读(4293) 评论(0) ...

  • 6

    poj 1990 MooFest 树状数组 2013-08-24 分类:未分类 阅读(4320) 评论(0) ...

  • 1
    • codeforces.com 2 years ago
    • Cache

    poj.org is broken?

    poj.org is broken? poj.org is broken?

  • 2
    • exp-blog.com 2 years ago
    • Cache

    POJ 1013 - Counterfeit Dollar

    Counterfeit Dollar POJ 1013 - Counterfeit Dollar Time: 1000MS Memory: 10000K 难度: 初级 ...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK