洛谷P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver

传送门

题目大意:n头牛在单行道n个位置,开始用不同的速度跑步。

当后面的牛追上前面的牛,后面的牛会和前面的牛以一样的速度

跑,称为一个小团体。问:ts后有多少个小团体。

题解:模拟

倒着扫一遍,因为某头牛后面的牛对这头牛的速度没影响。

计算出ts后牛的终点,如果能撞上前面最慢的牛,那么小团体数+1

注意开long long 

一开始不理解为什么倒着扫,

因为如果正着扫,看第i头牛能否撞上i+1头,

我们不确定第i+1头牛的速度,可能第i+1头牛

速度很快,追上i+2头牛速度减缓,从而被第i头牛追上。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#define N 800009
#define LL long long
using namespace std;

LL n,t,ans,now;

struct M{
    LL p,v;
}a[N];

bool cmp(M a,M b){
    return a.p<b.p;
}

int main(){
    scanf("%lld%lld",&n,&t);ans=n;now=n;
    for(int i=1;i<=n;i++)scanf("%lld%lld",&a[i].p,&a[i].v);
    for(int i=n-1;i>=1;i--){
        if(a[i].p+a[i].v*t>=a[now].p+a[now].v*t)ans--;
        else now=i;
    }
    cout<<ans<<endl;
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/zzyh/p/7788215.html