原題下載
答案
(Analysis by Jonathan Paulson - jonathanpaulson@gmail.com)
If a faster cow is behind a slower cow, the faster cow is eventually going to catch up and join the slower cow's group (or join a group in the middle).
So we want to count the number of cows who have no slower 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 faster. But this is too slow: there are about N2N2 pairs of cows, and N = 105105. So N2N2 is about 10101010, and computers only do about 109109 operations per second (which usually translates to about 107107iterations of a simple loop in one second of time).
Luckily, there is a faster way: start from the back, and keep track of the slowest cow as you go. This only takes about NN operations, which is very fast.
Here is my C++ code for the fast approach:
#include <cstdio>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
ll n;
scanf("%lld", &n);
vector<ll> S;
for(ll i=0; i<n; i++) {
ll x, s;
scanf("%lld %lld\n", &x, &s);
S.push_back(s);
}
ll ans = 1;
ll slow = S[n-1];
for(ll i=n-2; i>=0; i--) {
if(S[i] > slow) {
// cows group up
} else {
ans++;
}
slow = min(slow, S[i]);
}
printf("%lld\n", ans);
}
以上就是關(guān)于【USACO 2014 December Contest, Bronze Problem 3. Cow Jog】的解答,如需了解學(xué)校/賽事/課程動態(tài),可至翰林教育官網(wǎng)獲取更多信息。
往期文章閱讀推薦:
2027 USAAIO人工智能奧賽報名進行中!Round 1新規(guī)出爐,2月開賽如何備考?
外灘大會見證AI爆發(fā)!緊跟智能時代,從USACO計算機奧賽起步!

? 2026. All Rights Reserved. 滬ICP備2023009024號-1