Newcoder 4 A.Contest(逆序对-BIT)

比赛排名与二元组逆序对算法
本文探讨了一种算法问题,涉及通过比赛排名确定队伍间的相对强度,并使用二元组逆序对概念计算特定条件下队伍组合的数量。算法采用时间复杂度为O(nlogn)的方法,通过计算每场比赛的排名逆序对来找出满足条件的队伍组合。

Description

nnn支队伍一共参加了三场比赛。

一支队伍xxx认为自己比另一支队伍yyy强当且仅当xxx在至少一场比赛中比yyy的排名高。

求有多少组(x,y)(x,y)(x,y),使得xxx自己觉得比yyy强,yyy自己也觉得比xxx强。

$ (x, y), (y, x)$算一组。

Input

第一行一个整数nnn,表示队伍数; 接下来nnn行,每行三个整数a[i],b[i],c[i]a[i], b[i], c[i]a[i],b[i],c[i],分别表示iii在第一场、第二场和第三场比赛中的名次;nnn 最大不超过200000200000200000

Output

输出一个整数表示满足条件的(x,y)(x,y)(x,y)数;64bit64bit64bit请用lldlldlld

Sample Input

4
1 3 1
2 2 4
4 1 2
3 4 3

Sample Output

5

Solution

(x,y)(x,y)(x,y)之间必然是xxx两败一胜或两胜一败,考虑因a,b,ca,b,ca,b,c其中之一败因a,b,ca,b,ca,b,c其中另一胜的二元组个数之和(即二元组逆序对个数),那么一组合法解(x,y)(x,y)(x,y)会被计算四次,所求答案除以四即为答案,时间复杂度O(nlogn)O(nlogn)O(nlogn)

Code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=200005;
struct BIT 
{
	#define lowbit(x) (x&(-x))
	int b[maxn],n;
	void init(int _n)
	{
		n=_n;
		for(int i=1;i<=n;i++)b[i]=0;
	}
	void update(int x,int v)
	{
		while(x<=n)
		{
			b[x]+=v;
			x+=lowbit(x);
		}
	}
	int query(int x)
	{
		int ans=0;
		while(x)
		{
			ans+=b[x];
			x-=lowbit(x);
		}
		return ans;
	}
}bit;
int n,x[3][maxn];
P a[maxn];
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d%d%d",&x[0][i],&x[1][i],&x[2][i]);
	ll ans=0;
	for(int i=0;i<3;i++)
		for(int j=0;j<3;j++)
			if(i!=j)
			{
				for(int k=1;k<=n;k++)a[k]=P(x[i][k],x[j][k]);
				sort(a+1,a+n+1);
				bit.init(n);
				for(int k=n;k>=1;k--)
				{
					ans+=bit.query(a[k].second);
					bit.update(a[k].second,1);
				}
			}
	printf("%lld\n",ans/4);
	return 0;
}

请你针对这部分修改,为避免样式改变 把样式代码也修改:.csw-h5-contest-racesSlots { padding: 12px 24px 0; .csw-opensource-h5-learningHeader { height: 24px; width: 100%; margin-bottom: 10px; display: flex; justify-content: space-between; align-items: center; .csw-opensource-h5-learningHeader-title { color: rgb(0, 0, 0); font-size: 18px; font-weight: 500; line-height: 20px; } .csw-opensource-h5-learningHeader-more { color: rgb(153, 153, 153); font-size: 12px; font-weight: 400; line-height: 16px; .csw-opensource-h5-learningHeader-moreText { padding-right: 4px; } } } .csw-h5-contest-racesSlots-content { position: relative; .adm-jumbo-tabs-header { border: 0; .adm-jumbo-tabs-tab-list { padding: 0; .adm-jumbo-tabs-tab-wrapper { flex: none; padding: 0; margin-right: 16px; .adm-jumbo-tabs-tab { font-weight: 500; color: rgb(102, 102, 102); padding: 0; } .adm-jumbo-tabs-tab-active { color: rgb(0, 65, 211); } } } } .adm-jumbo-tabs-content { padding: 16px 0 12px; } .csw-h5-contest-topSummitsCarousel { width: 100%; padding-bottom: 12px; .topSummitsList { display: flex !important; justify-content: space-between; .topSummitsItem { width: 48%; position: relative; img { width: 100%; aspect-ratio: 1.785; border-radius: 4px; } .mask { width: 100%; height: 100%; position: absolute; bottom: 0; border-radius: 4px; } .maskCourse { position: absolute; top: 6px; right: 6px; display: inline-block; height: 20px; text-align: center; line-height: 20px; padding: 2px 8px 2px 8px; border-radius: 53px; background: rgba(0, 65, 211, 0.7); color: rgb(255, 255, 255); font-size: 11px; font-weight: 400; line-height: 16px; } .topSummitsItemTitle { display: inline-block; margin-top: 8px; width: 100%; color: rgb(0, 0, 0); font-weight: 400; line-height: 24px; } .itemSubTitle { font-family: HarmonyHeiTi; color: #666666; font-size: 12px; font-weight: 400; line-height: 19px; } } } .slick-dots-bottom { bottom: 3px; z-index: 9; li { height: 2px !important; width: 6px !important; } button { width: 6px !important; background: #f2f2f2 !important; opacity: 1 !important; border-radius: 30px !important; height: 2px !important; } .slick-active { width: 12px !important; border-radius: 1px !important; button { background-color: rgb(0, 65, 211) !important; width: 12px !important; } } } } .csw-racesSlots-h5-more { color: rgb(153, 153, 153); font-size: 12px; font-weight: 400; line-height: 16px; position: absolute; right: 0; top: 17px; .anticon-right { margin-left: 3px; } } .moreDomCss { position: absolute; top: 4px; right: 0; color: rgb(153, 153, 153); font-size: 12px; font-weight: 400; line-height: 16px; .moreText { padding-right: 4px; } } } }
最新发布
11-21
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值