一:
D - Ultra-QuickSort
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
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
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]