[3664] Election Time 排序
Language:
Election Time
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1136 Accepted: 662
Description
The cows are having their first election after overthrowing the tyrannical Farmer John, and Bessie is one of N cows (1 ≤ N ≤ 50,000) running for President. Before the election actually happens, however, Bessie wants to determine who has the best chance of winning.
The election consists of two rounds. In the first round, the K cows (1 ≤ K ≤ N) cows with the most votes advance to the second round. In the second round, the cow with the most votes becomes President.
Given that cow i expects to get Ai votes (1 ≤ Ai ≤ 1,000,000,000) in the first round and Bi votes (1 ≤ Bi ≤ 1,000,000,000) in the second round (if he or she makes it), determine which cow is expected to win the election. Happily for you, no vote count appears twice in the Ai list; likewise, no vote count appears twice in the Bi list.
Input
* Line 1: Two space-separated integers: N and K
* Lines 2..N+1: Line i+1 contains two space-separated integers: Ai and Bi
Output
* Line 1: The index of the cow that is expected to win the election.
Sample Input
5 3
3 10
9 2
5 6
8 4
6 5
Sample Output
5
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct cow
{
int a,b,id;
}c[50001];
bool cmp1(const cow x,const cow y)
{
return x.a > y.a;
}
bool cmp2(const cow x,const cow y)
{
return x.b > y.b;
}
int main()
{
// freopen("data.txt","r",stdin);
int n,k;
int i;
while(scanf("%d%d",&n,&k) != EOF)
{
vector < cow > ans;
for(i = 0; i < n; ++i)
{
scanf("%d%d",&c[i].a,&c[i].b);
c[i].id = i;
}
sort(c,c + n,cmp1);
for(i = 0; i < k; ++i)ans.push_back(c[i]);
sort(ans.begin(),ans.end(),cmp2);
cout<<ans[0].id+1<<endl;
}
return 0;
}
[2299] Ultra-QuickSort 排序
Ultra-QuickSort
Time Limit: 7000MS Memory Limit: 65536K
Total Submissions: 6414 Accepted: 2102
http://acm.pku.edu.cn/JudgeOnline/problem?id=2299
lynncui 2299 Accepted 3744K 360MS C++ 786B 2008-06-24 17:33:20
求排序的交换次数,使用合并排序求每个数的逆序数,才不超时。
Description
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 ≤ 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 <iostream>
#include <vector>
using namespace std;
int a[500100];
int l[500100];
int f[500100];
__int64 num;
void megre(int a[],int p,int q,int r)
{
int i,j,k;
int n1=q-p+1;
int n2=r-q;
for(i=1;i<=n1;i++)l[i]=a[p+i-1];
for(j=1;j<=n2;j++)f[j]=a[q+j];
l[n1+1]=1999999999;
f[n2+1]=1999999999;
i=1;j=1;
for(k=p;k<=r;k++){
if(l[i]<=f[j])
{a[k]=l[i];i++;}
else {a[k]=f[j];j++;if(l[i]!=1999999999)num=num+n1-i+1;}
}
}
void megre_sort(int a[],int p,int r)
{
int q;
if(p<r)
{
q=(p+r)/2;
megre_sort(a,p,q);
megre_sort(a,q+1,r);
megre(a,p,q,r);
}
}
int main()
{
int n,i;
while(scanf("%d",&n),n!=0)
{
num=0;
for(i=0;i<n;i++)scanf("%d",&a[i]);
megre_sort(a,0,n-1);
printf("%I64d\n",num);
}
return 0;
}