1. 程式人生 > >洛谷P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver

洛谷P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver

ret string 跑步 opened amp color get clu font

傳送門

題目大意: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

洛谷P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver