您的位置:首页 > 其它

Wannafly模拟赛2 A题

2017-09-15 20:17 218 查看
题目衔接:传送门


思路:很直观的我们知道在一场比赛中任意两个人的排名都有大于小于之分,那也就是说对于两个人i,j如果存在两场比赛第i个人的排名比第j个人的排名高另外一场低答案就加1,我们直接考虑三场比赛中第i个人和第j个人的排名,一共有四种情况

1:三场比赛中第i个人的排名都比第j个人的排名高、

2:三场比赛中的两场比赛第i个人的排名比第j个人排名高,另外一场比第j个人排名低

3:三场比赛中只有一场比赛第i个人的排名比第j个人的排名高,另外两场比第j个人排名低

4:三种比赛中第i个人的排名全部比第j个人的排名低

只有2和3两种情况会对答案产生贡献,那么我们直接分析1、2场比赛、1、3场比赛和2、3场比赛中第i个人和第j个人的排名情况,最后对答案除2即可

当比较1、2两场比赛的时候我们固定第一场比赛中人的排名是从高到低的,那么此时我们只需要求出第二场比赛中的逆序数即可

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <bitset>

using namespace std;
#define LL long long
#define pb push_back
#define mk make_pair
#define pill pair<int, int>
#define mst(a, b) memset(a, b, sizeof a)
#define REP(i, x, n) for(int i = x; i <= n; ++i)
const int MOD = 1e9 + 7;
const int qq = 2e5 + 10;
const int INF = 1e9 + 10;
struct Num {
int a, b;
bool operator < (const Num &w) const {
return a < w.a;
}
}num[qq];
int a[qq], b[qq], c[qq];
LL sum[qq];
LL GetSum(int x) {
LL ans = 0;
while(x > 0) {
ans += sum[x];
x -= x & (-x);
}
return ans;
}
void UpDate(int x, int n) {
while(x <= n) {
sum[x] += 1;
x += x & (-x);
}
}
LL Solve(int *t1, int *t2, int n) {
for(int i = 1; i <= n; ++i) {
num[i].a = t1[i];
num[i].b = t2[i];
sum[i] = 0;
}
sort(num + 1, num + 1 + n);
LL ans = 0;
for(int i = n; i >= 1; --i) {
ans += GetSum(num[i].b);
UpDate(num[i].b, n);
}
return ans;
}

int main(){
int n; scanf("%d", &n);
for(int i = 1; i <= n; ++i) {
scanf("%d%d%d", a + i, b + i, c + i);
}
LL ans;
ans = Solve(a, b, n) + Solve(a, c, n) + Solve(b, c, n);
printf("%lld\n", ans / 2);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: