Problem Specification Value
Time Limit 10 Sec
Memory Limit 162 MB
Submit 2226
Solved 1173

Description

第 XXXX 届 NOI 期间, 为了加强各省选手之间的交流, 组委会决定组织一场省际电子竞技大赛, 每一个省的代表队由 n 名选手组成, 比赛的项目是老少咸宜的网络游戏泡泡堂. 每一场比赛前, 对阵双方的教练向组委会提交一份参赛选手的名单, 决定了选手上场的顺序, 一经确定, 不得修改. 比赛中, 双方的一号选手, 二号选手...... , n 号选手捉对厮杀, 共进行 n 场比赛. 每胜一场比赛得 2 分, 平一场得 1 分, 输一场不得分. 最终将双方的单场得分相加得出总分, 总分高的队伍晋级 (总分相同抽签决定). 作为浙江队的领队, 你已经在事先将各省所有选手的泡泡堂水平了解的一清二楚, 并将其用一个实力值来衡量. 为简化问题, 我们假定选手在游戏中完全不受任何外界因素干扰, 即实力强的选手一定可以战胜实力弱的选手, 而两个实力相同的选手一定会战平. 由于完全不知道对手会使用何种策略来确定出场顺序, 所以所有的队伍都采取了这样一种策略, 就是完全随机决定出场顺序. 当然你不想这样不明不白的进行比赛. 你想事先了解一下在最好与最坏的情况下, 浙江队最终分别能得到多少分.

Input

输入的第一行为一个整数 n, 表示每支代表队的人数. 接下来 n 行, 每行一个整数, 描述了 n 位浙江队的选手的实力值. 接下来 n 行, 每行一个整数, 描述了你的对手的 n 位选手的实力值. 20 %的数据中, 1<=n<=10; 40 %的数据中, 1<=n<=100; 60 %的数据中, 1<=n<=1000; 100 %的数据中, 1<=n<=100000, 且所有选手的实力值在 0 到 10000000 之间.

Output

包括两个用空格隔开的整数, 分别表示浙江队在最好与最坏的情况下分别能得多少分. 不要在行末输出多余的空白字符.

Sample Input

2
1
3
2
4

Sample Output

2 0
样例说明
我们分别称4位选手为A,B,C,D。则可能出现以下4种对战方式,最好情况下可得2分,最坏情况下得0分。
一二三四
浙江???结果浙江???结果浙江???结果浙江???结果
一号选手AC负AD负BC胜BD负
二号选手BD负BC胜AD负AC负
总得分0220

HINT

Source


按照顺序贪心, 然后 田忌赛马用大的对大的, 小的对小的, 其余废掉, 两边一比就可以了.

有一个特性, 就是两边所得的分数加起来恒等于 2 * n.



#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>

using namespace std;
const int maxn = 100100;

int n, a[maxn], b[maxn];

int solve(int* a, int* b)
{
    // Self = a, while Opponent = b
    int l1 = 1, r1 = n, l2 = 1, r2 = n;
    int res = 0;
    while (l1 <= r1 && l2 <= r2) {
        if (a[l1] > b[l2]) {
            res += 2;
            l1++, l2++;
        } else if (a[r1] > b[r2]) {
            res += 2;
            r1--, r2--;
        } else {
            if (a[l1] == b[r2])
                res += 1;
            l1++, r2--;
        }
    }
    return res;
}

int main(int argc, char** argv)
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    for (int i = 1; i <= n; i++)
        scanf("%d", &b[i]);
    sort(a + 1, a + n + 1);
    sort(b + 1, b + n + 1);
    int res1 = solve(a, b),
        res2 = 2 * n - solve(b, a);
    printf("%d %d\n", res1, res2);
    return 0;
}

这一段是报废的代码 (连对拍都没有拍过)

不过注释很有用, 仔细看一看就可以了


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
const int maxn = 200100;

struct per { int org, val; };

bool operator < (per a, per b) {
    return a.val < b.val;
}

int n, cnt[maxn], cntp;
per arr[maxn];

int get_final_val(int org)
{
    // Input the origin, and calculate the (earnings) of the 'org' team.
    // Scan from the people of the lowest scores
    memset(cnt, 0, sizeof(cnt));
    cntp = 0;
    // Processing distinct array. This array is only related to the participants'
    // relative position, and +1 represents one member on the self side, -1
    // represents one member on the other side, 0 represents no winners and so on.
    for (int i = 1; i <= 2 * n; i++) {
        if (arr[i].val != arr[i - 1].val)
            cntp++;
        if (arr[i].org == org)
            cnt[cntp]++;
        else
            cnt[cntp]--;
    }
    // Now we've done sorting out the cnt[] and cntp things...
    // We are about to calculate the values.
    // Suppose the example data is arranged in such a way:
    //     arr(self):     50    40    30 25 20 15    5 0
    //     arr(other): 55    45    35       20 15 10 5 0
    //     cnt[]:       +  +  -  +  -  +  +  x  x  - x x
    // (Whereas + represents cnt[] = +1, - represents cnt[] = -1, x represents cnt[] = 0)
    // When we work through the cnt[] array, we iterate from the smaller side.
    //     It is supposed, that when some time the sum reaches 0 (i.e. greater than 0),
    //     the addition that makes it overpass 0 should not be counted. This is
    //     obvious, because you may not win someone with a higher score than you.
    int tmpsum = 0;
    int res = 0;
    for (int i = 1; i <= cntp; i++) {
        int t = cnt[i];
        if (t <= 0) {
            tmpsum += t;
            continue;
        } else {
            // It should not exceed this value, as described before in the
            // documentation.
            if (tmpsum + t > 0) {
                // This overexceeding will not be patched by the same team after this
                res -= (tmpsum + t);
                t = -tmpsum;
            }
            res += t;
            tmpsum += t;
        }
    }
    return res + n;
}

int main(int argc, char** argv)
{
    scanf("%d", &n);
    // The power values of ZheJiang participants
    for (int i = 1; i <= n; i++) {
        arr[i].org = 1;
        scanf("%d", &arr[i].val);
    }
    // The power values of opponents'
    for (int i = 1; i <= n; i++) {
        arr[i + n].org = 2;
        scanf("%d", &arr[i + n].val);
    }
    // Begin brute-forcing when we need to sort
    sort(arr + 1, arr + 2 * n);
    // Now it's the real time to calculate with "work"
    printf("%d %d\n", get_final_val(1), 2 * n - get_final_val(2));
    return 0;
}

from pydatagen import *
n = randrange(2, 100000)
a = randlist(n, range(1, 10 ** 8))
b = randlist(n, range(1, 10 ** 8))
printf('%d\n' % n)
for i in a + b:
    printf('%d\n' % i)
fclose()