2019ICPC 沈阳重现 A-Leftbest(树状数组+二分)

7-1 A-Leftbest
Jack is worried about being single for his whole life, so he begins to use a famous dating app. In this app, the user is shown single men/women’s photos one by one, and the user may choose between “yes” and “no”. Choosing “yes” means an invitation while choosing “no” means nothing. The photos would be shown one by one until the number of rest photos to be shown reaches zero. Of course, efficient and single Jack would always choose “yes”.

When viewing photos, Jack would have a “fake impression point” on every photo, which is not accurate. To calculate the “true impression point” of one photo, Jack would recall the “fake impression point” of every previous photo whose “fake impression point” is larger than this photo, and regard the smallest “fake impression point” of them as the “true impression point” of this photo. Jack would like to sum the “true impression point” of all photos as the outcome of his effort.

Note that if such a larger “fake impression point” does not exist, the “true impression point” of this photo is zero.

思路:这道题给我想复杂了,其实是个水题。。。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define lowbit(i) ((i)&(-i))
const int maxn=1e5+1;
map<ll,ll>p;
ll a[maxn],b[maxn],c[maxn];
void update(int x,ll v)
{
	while(x<maxn) c[x]+=v,x+=lowbit(x);
}
ll query(int x)
{
	ll res=0;
	while(x>0) res+=c[x],x-=lowbit(x);
	return res;
}
ll check(int l,int r)
{
	int t=l;
	ll sum=query(t);
	while(l<=r)
	{
		int mid=(l+r)>>1;
		if(query(mid)-sum>0) r=mid-1;
		else l=mid+1;
	}
	return query(l)-sum;
}
int main()
{
	int n;
	ll sum=0;
	scanf("%d",&n);
	for(int i=1;i<=n;++i) scanf("%lld",&a[i]),b[i]=a[i];
	sort(b+1,b+1+n);
	int size=unique(b+1,b+1+n)-b-1;
	for(int i=1;i<=n;++i)
	{
		int pos=lower_bound(b+1,b+1+size,a[i])-b;
		sum+=check(pos,size);
		if(!p[a[i]]) update(pos,a[i]);
		p[a[i]]=1;
	}
	printf("%lld\n",sum);
}
发布了328 篇原创文章 · 获赞 1 · 访问量 9112

猜你喜欢

转载自blog.csdn.net/qq_42479630/article/details/105169274
今日推荐