题一
http://exam.upc.edu.cn/problem.php?id=2094
题意:
输入T组数据,每组数据第一行为n(共有2*n选手个数),r(回合次数),q(经过r回合第q名是谁)。第二行每个选手的初始分数。第三行,每个选手的能力(保证都不相同)。
每个回合都要对选手按照当前的分数进行排序。之后第a[i]个选手与第a[i + 1]个选手打。谁的能力强谁的分数加一,输的一方分数不变。排序的时候按照分数的多少递减,如果分数一样则按照选手的序号及第i个选手递增。
分析:此题直接模拟的话每次排序O(nlongn)*每次对打O(n),总时间复杂度O(n^2logn)大于10的10次方注定超时。
破题点是每次赢的人顺序不变,输的人顺序也不变。
//注意排序时候用的 bool operator 和比较时的<是不同的要单独的写
#include <iostream>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <string>
#include <algorithm>
#include <list>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdlib>
using namespace std;
struct node
{
int num;
int score;
int ability;
bool operator < (const node t)const
{
if(t.score == score)
{
return num < t.num;
}
return score > t.score;
}
} a[2000005],ying[2000005],shu[2000005];
int main()
{
//freopen("in.txt","r",stdin);
int T;
int n,r,q;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&r,&q);
for(int i = 0; i < 2*n; i ++)
{
scanf("%d",&a[i].score);
a[i].num = i + 1;
}
for(int i = 0; i < 2*n; i ++)
{
scanf("%d",&a[i].ability);
}
sort(a,a + n * 2);
while(r--)
{
//分别将输的和赢的存在两个不同的数组中
int ky = 0,ks = 0;
for(int i = 0; i < n; i ++)
{
if(a[2*i].ability > a[2 * i + 1].ability)
{
a[2*i].score ++;//不要忘记加分了
ying[ky ++] = a[2*i];
shu[ks ++] = a[2 * i + 1];
}
else
{
a[2*i + 1].score ++;
ying[ky ++] = a[2*i + 1];
shu[ks ++] = a[2 * i];
}
}
//惊现归并
int ii = 0,jj = 0;
int k = 0;
while(ii < ky && jj < ks)
{
if(ying[ii].score < shu[jj].score || (ying[ii].score == shu[jj].score && ying[ii].num > shu[jj].num))
{
a[k ++] = shu[jj ++];
}
else
{
a[k ++] = ying[ii ++];
}
}
while(ii < ky)a[k ++] = ying[ii ++];
while(jj <ks)a[k ++] = shu[jj ++];
}
printf("%d\n",a[q - 1].num);
}
return 0;
}
题二
http://acm.nyist.net/JudgeOnline/problem.php?pid=117
题意:就给T组数据长度为n的数组,求数组中有几个逆序数
分析:裸归并排序 + 左右数组合并的时候当右数组在比做数组的数字小的时候出现逆序数,且逆序数的个数为当前左数组的未遍历的个数
//此题注意两点 1.ans可能会超int
//2.合并两个数组的时候,每个需要将右侧的数组放在左边的位置逆序对数都要+mid - i + 1;
//i = first时,逆序对数为整个左侧数组的长度 如左侧数组为 3 4 5,右侧数组为1 。逆序数为3(注first是第一个数)
//i = first + 1时 左侧数组1 3 4 右侧数组2
//j位置的数比从当前位置到mid的数都笑这就意味着有mid - i + 1个逆序数
//与j的位置是无关的。因为表示的是当前位置右侧数组的某一个数,与整个数组所能产生的逆序数的个数
#include <iostream>
#include <cstdio>
using namespace std;
long long ans = 0;
//将有二个有序数列a[first...mid]和a[mid...last]合并。
void mergearray(int a[], int first, int mid, int last, int temp[])//a表示待排序的数组。first表示第一个元素,last表示最后一个元素
{
int i = first, j = mid + 1;//将从first到last的数组a分为两部分,左侧和右侧,且两侧都已经是排好序的了
int k = 0;
while (i <= mid && j <= last)
{
if (a[i] <= a[j])
temp[k++] = a[i++];
else
{
ans = ans + mid - i + 1;
temp[k++] = a[j++];
}
}
while (i <= mid)
{
temp[k++] = a[i++];
}
while (j <= last)
temp[k++] = a[j++];
//将中间数组的内容全部写入到待排序数组中
for (i = 0; i < k; i++)
a[first + i] = temp[i];
}
void mergesort(int a[], int first, int last, int temp[])//a为待排序的数组.first表示第一个元素,last表示最后一个元素
{
if (first < last)
{
int mid = (first + last) / 2;
mergesort(a, first, mid, temp); //左边有序
mergesort(a, mid + 1, last, temp); //右边有序
mergearray(a, first, mid, last, temp); //再将二个有序数列合并
}
}
int a[2000005];
int p[2000005];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
ans = 0;
int n;
cin>>n;
for(int i = 0; i < n; i ++)
{
cin>>a[i];
}
mergesort(a, 0, n - 1, p);
cout<<ans<<endl;
}
}