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);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值