原题下载
答案
(Analysis by Jonathan Paulson - jonathanpaulson@gmail.com)
If the cows didn't have to slow down, after TT seconds cow ii will be at position xi+T∗sixi+T∗si. If a faster cow started behind a slower cow, but the faster cow ended up ahead, that means the faster cow will join the slower cow's group (or a group in the middle).
So we want to count the number of cows don't "cross" any cows ahead of them; this is the number of cows who won't join another group (and hence will start their own group).
The most obvious way to to go through each cow and check if any of the cows ahead of them are going to end up behind them. But this is too slow: there are O(N^2) pairs of cows, and N = 10^5. So O(N^2) ~ 10^10, and computers only do about 10^9 operations per second.
Luckily, there is a faster way: start from the back, and keep track of the least far ending position as you go. This only takes about N operations, which is very fast.
Here is my code for the fast approach:
#include <cstdio>
#include <vector>
#define PB push_back
using namespace std;
typedef long long ll;
int main() {
ll n, t;
scanf("%lld %lld", &n, &t);
vector<ll> S;
for(ll i=0; i<n; i++) {
ll x, s;
scanf("%lld %lld\n", &x, &s);
S.PB(x + s*t);
}
ll ans = 1;
ll slow = S[n-1];
for(ll i=n-1; i>=0; i--) {
if(S[i] < slow) { ans++; }
slow = min(slow, S[i]);
}
printf("%lld\n", ans);
}
以上就是关于【USACO 2014 December Contest, Silver Problem 3. Cow Jog】的解答,如需了解学校/赛事/课程动态,可至翰林教育官网获取更多信息。
往期文章阅读推荐:
美国AI奥赛首次新增低龄组!2027 USAAIO报名中,2月正式开考!
不考编程实操?2026 CAICP人工智能能力认证首轮11月22日开考,备考指南速递!

© 2026. All Rights Reserved. 沪ICP备2023009024号-1