Meetings//二分搜索//排位1

Meetings//二分搜索


题目

Two barns are located at positions 0 and L (1≤L≤109) on a one-dimensional number line. There are also N cows (1≤N≤5⋅104) at distinct locations on this number line (think of the barns and cows effectively as points). Each cow i is initially located at some position xi and moving in a positive or negative direction at a speed of one unit per second, represented by an integer di that is either 1 or −1. Each cow also has a weight wi in the range [1,103]. All cows always move at a constant velocity until one of the following events occur: If cow i reaches a barn, then cow i stops moving. A meeting occurs when two cows i and j occupy the same point, where that point is not a barn. In this case, cow i is assigned cow j’s previous velocity and vice versa. Note that cows could potentially meet at points that are not integers. Let T be the earliest point in time when the sum of the weights of the cows that have stopped moving (due to reaching one of the barns) is at least half of the sum of the weights of all cows. Please determine the total number of meetings between pairs of cows during the range of time 0…T (including at time T).

Input
The first line contains two space-separated integers N and L. The next N lines each contain three space-separated integers wi, xi, and di. All locations xi are distinct and satisfy 0<xi<L.
Output
Print a single line containing the answer.

Example
inputCopy
3 5
1 1 1
2 2 -1
3 3 -1
outputCopy
2
Note
The cows in this example move as follows:

The first and second cows meet at position 1.5 at time 0.5. The first cow now has velocity −1 and the second has velocity 1.
The second and third cows meet at position 2 at time 1. The second cow now has velocity −1 and the third has velocity 1.
The first cow reaches the left barn at time 2.

The second cow reaches the left barn at time 3.

The process now terminates since the sum of the weights of the cows that have reached a barn is at least half of the sum of the weights of all cows. The third cow would have reached the right barn at time 4.

Exactly two meetings occurred.
题意
蚂蚁爬行,相遇掉头,出去的蚂蚁总体重达到一半停止。求蚂蚁相遇次数

思路

利用两次二分搜索,求出时间和对应碰面次数

代码

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <deque>
#include <queue>
#include <cmath>
#include <stack>
#define pi 3.1415926
using namespace std;
typedef long long ll;
const ll mod = 1e9+7;
int n,l;
ll sum;
ll a[50100];
int alln = 0;
int w[50100],px[50100],d[50100];
bool check(int t){                      //检查次时体重是否到一半
	int tw=0,c1=0,c2=0;
	for(int i=1;i<=n;i++){
		if(d[i]==1&&l-px[i]<=t) c1++;
		if(d[i]==-1&&px[i]<=t) c2++;
	}
	for(int i=1;i<=c2;i++) tw+=w[i];
	for(int i=n;i>n-c1;i--) tw+=w[i];
	return tw*2<sum;
}
int dfen(ll x){
	int l=1,r=alln;
	while(l<=r){
		int mid = l+r>>1;
		if(a[mid]>=x) r=mid-1;
		else l=mid+1;
	}
	return l;
}
int cal(ll t){
	int res=0;
	alln=0;
	for(int i=1;i<=n;i++)
		if(d[i]==-1)
            a[++alln] = px[i];
	for(int i=1;i<=n;i++)
		if(d[i]==1)
            res+=dfen(px[i]+2*t+1)-dfen(px[i]);
	return res;
}
struct cow{
	int w,x,d;
}c[50100];
bool cmp(cow c1,cow c2){
    return c1.x<c2.x;
}
int main(){
	scanf("%d%d",&n,&l);
	for(int i=1;i<=n;i++)
		scanf("%d%d%d",&c[i].w,&c[i].x,&c[i].d);
	sort(c+1,c+1+n,cmp);
	for(int i=1;i<=n;i++)
        w[i]=c[i].w,px[i]=c[i].x,d[i]=c[i].d;
	for(int i=1;i<=n;i++)
        sum+=w[i];
	int tl=0,tr=l;
	while(tl<=tr){            //利用二分找到体重达到一半的时间
		int mid=tl+tr>>1;
		if(check(mid)) tl=mid+1;
		else tr=mid-1;
	}
	int ans=cal(tl);
	printf("%d\n",ans);
	return 0;
}

注意

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值