洛谷 P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver
题目描述
The cows are out exercising their hooves again! There are N cows
jogging on an infinitely-long single-lane track (1 <= N <= 100,000).
Each cow starts at a distinct position on the track, and some cows jog
at different speeds.
With only one lane in the track, cows cannot pass each other. When a
faster cow catches up to another cow, she has to slow down to avoid
running into the other cow, becoming part of the same running group.
The cows will run for T minutes (1 <= T <= 1,000,000,000). Please
help Farmer John determine how many groups will be left at this time.
Two cows should be considered part of the same group if they are at
the same position at the end of T minutes.
有N (1 <= N <= 100,000)头奶牛在一个单人的超长跑道上慢跑,每头牛的起点位置都不同。由于是单人跑道,所有他们之间不能相互超越。当一头速度快的奶牛追上另外一头奶牛的时候,他必须降速成同等速度。我们把这些跑走同一个位置而且同等速度的牛看成一个小组。
请计算T (1 <= T <= 1,000,000,000)时间后,奶牛们将分为多少小组。
输入输出格式
输入格式:
INPUT: (file cowjog.in)
The first line of input contains the two integers N and T.
The following N lines each contain the initial position and speed of a
single cow. Position is a nonnegative integer and speed is a positive
integer; both numbers are at most 1 billion. All cows start at
distinct positions, and these will be given in increasing order in
the input.
输出格式:
OUTPUT: (file cowjog.out)
A single integer indicating how many groups remain after T minutes.
输入输出样例
5 3
0 1
1 2
2 3
3 2
6 1
3
思路:计算最后她所能到达的位置。然后倒序处理。
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define MAXN 100010 using namespace std; int n,t,ans; long long last[MAXN]; int main(){ scanf("%d%d",&n,&t); for(int i=1;i<=n;i++){ int pos,v; scanf("%d%d",&pos,&v); last[i]=pos+1ll*v*t; } ans=1; for(int i=n-1;i>=1;i--){ if(last[i]>=last[i+1]) last[i]=last[i+1]; else ans++; } cout<<ans; }