2018/8/14

一:

D - Ultra-QuickSort

 POJ - 2299 

In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequence 

9 1 0 5 4 ,


Ultra-QuickSort produces the output 

0 1 4 5 9 .


Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.

Input

The input contains several test cases. Every test case begins with a line that contains a single integer n < 500,000 -- the length of the input sequence. Each of the the following n lines contains a single integer 0 ≤ a[i] ≤ 999,999,999, the i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must not be processed.

Output

For every input sequence, your program prints a single line containing an integer number op, the minimum number of swap operations necessary to sort the given input sequence.

Sample Input

5
9
1
0
5
4
3
1
2
3
0

Sample Output

6
0

//和昨天一个题一样,换了一种方法,归并排序做的

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
long long sum;
int n;
int a[500001];
int t[500001];
void he(int l,int m,int r)
{
    int p=0;
    int i=l,j=m+1;
    while(i<=m&&j<=r)
    {
        if(a[i]>a[j])
        {
            t[p++]=a[j++];
            sum+=m-i+1;
        }
        else
            t[p++]=a[i++];
    }
    while(i<=m)t[p++]=a[i++];
    while(j<=r)t[p++]=a[j++];
    for(int i=0;i<p;i++)
        a[i+l]=t[i];

}
void mergesort(int l,int r)
{
    int m;
    if(l<r)
    {
        m=(l+r)/2;
        mergesort(l,m);
        mergesort(m+1,r);
        he(l,m,r);
    }
}
int main()
{
    while(~scanf("%d",&n)&&n)
    {
        sum=0;
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        mergesort(0,n-1);
        printf("%lld\n",sum);
    }
    return 0;

}

二:

C - Assignment

 HDU - 5289 

Tom owns a company and he is the boss. There are n staffs which are numbered from 1 to n in this company, and every staff has a ability. Now, Tom is going to assign a special task to some staffs who were in the same group. In a group, the difference of the ability of any two staff is less than k, and their numbers are continuous. Tom want to know the number of groups like this.

Input

In the first line a number T indicates the number of test cases. Then for each case the first line contain 2 numbers n, k (1<=n<=100000, 0<k<=10^9),indicate the company has n persons, k means the maximum difference between abilities of staff in a group is less than k. The second line contains n integers:a[1],a[2],…,a[n](0<=a[i]<=10^9),indicate the i-th staff’s ability.

Output

For each test,output the number of groups.

Sample Input

2
4 2
3 1 2 4
10 5
0 3 4 5 2 1 6 7 8 9

Sample Output

5
28

        
  

Hint

First Sample, the satisfied groups include:[1,1]、[2,2]、[3,3]、[4,4] 、[2,3] 
      

//果rmq

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
int n,k;
int a[100005];
int dpmax[100005][30],dpmin[100005][30];
void RMQ()
{
    for(int i=1;i<=n;i++)
    {
        dpmax[i][0]=a[i];
        dpmin[i][0]=a[i];
    }
    for(int j=1;(1<<j)<=n;j++)
        for(int i=1;i+(1<<j)-1<=n;i++)
        {
            dpmax[i][j]=max(dpmax[i][j-1],dpmax[i+(1<<(j-1))][j-1]);
            dpmin[i][j]=min(dpmin[i][j-1],dpmin[i+(1<<(j-1))][j-1]);
        }
}
int query(int l,int r)
{
    int m=log2(r-l+1);
    int maxx=max(dpmax[l][m],dpmax[r-(1<<m)+1][m]);
    int minn=min(dpmin[l][m],dpmin[r-(1<<m)+1][m]);
    return maxx-minn;
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        scanf("%d%d",&n,&k);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        RMQ();
        ll ans=0;
        int l,r;
        for(int i=1;i<=n;i++)
        {
            l=i,r=n;
            while(l<=r)
            {
                int mid=(l+r)/2;
                int cha=query(i,mid);
                if(cha<k)l=mid+1;
                else r=mid-1;
            }
            ans+=l-i;
        }
        printf("%lld\n",ans);

    }
    return 0;

}

三:

F - Assignment

 HDU - 5289 

Tom owns a company and he is the boss. There are n staffs which are numbered from 1 to n in this company, and every staff has a ability. Now, Tom is going to assign a special task to some staffs who were in the same group. In a group, the difference of the ability of any two staff is less than k, and their numbers are continuous. Tom want to know the number of groups like this.

Input

In the first line a number T indicates the number of test cases. Then for each case the first line contain 2 numbers n, k (1<=n<=100000, 0<k<=10^9),indicate the company has n persons, k means the maximum difference between abilities of staff in a group is less than k. The second line contains n integers:a[1],a[2],…,a[n](0<=a[i]<=10^9),indicate the i-th staff’s ability.

Output

For each test,output the number of groups.

Sample Input

2
4 2
3 1 2 4
10 5
0 3 4 5 2 1 6 7 8 9

Sample Output

5
28

        
  

Hint

First Sample, the satisfied groups include:[1,1]、[2,2]、[3,3]、[4,4] 、[2,3] 
        

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
西门子全系列软件授权工具Sim EKB Install v2018.11.14 最新绿色版 Sim EKB Install是一款西门子软件授权key,可对西门子下的所有软件进行破解授权,如:Wincc、Step5、Step7、TIA Portal(博途)等等,本站不仅为大家提供了Sim EKB Install下载地址,同时也为大家带来了西门子全系列软件授权工具的使用方法,欢迎有使用西门子软件的朋友们前来下载使用。 功能介绍 Sim EKB Install(西门子授权软件)是一款针对西门子软件的授权过期问题的解决辅助工具,支持西门子旗下的全部软件进行授权使用,运行西门子授权软件后会自动检测你当前安装在计算机上的西门子软件,并自动给出推荐的授权。 使用方法: 西门子授权工具Sim EKB Install使用方法有两种: 第一种:左侧有所有可授权的分类栏目,选择某个需要授权的软件版本,之后点击右侧的选择——安装长密钥即可; 第二种:点击“需要的密钥”勾选全部,然后选择安装长密钥; 详细步骤 1、在软件界面的左上角处选择 要安装的盘符,如C盘。 2、在软件界面左侧栏目内快速查找要按照的授权软件名称,并单击选中。 3、在软件界面的窗口中部,选择要安装的授权,并在前面打上勾。 4、最后点击软件界面上面的“安装长密钥”按钮即可安装,也可点击“安装短密钥”按钮进行安装,不影响授权使用,但是前提是,安装短密钥对应的表框里面,要有短密钥才可以。 许可证类型: 网络中的浮动授权 "SIFL" 工作地的单一授权"SISL" 365天的租用授权"SIRT"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值