题解:P3127 [USACO15OPEN] Trapped in the Haybales G
Sunset_afterglow · · 题解
好像是个错解,但是居然过了,管他的这么小众的题目,应该没有人加强数据吧!!!
题目
没读题就看题解,小心被机房里的人举报哦。
思路
写一个暴力 check,判断每两个相邻的草垛之间的位置是否可以冲出去,如果不可以,那么答案就加上这个区间的大小。
check 函数主要就是一个大暴力,模拟往两边冲的过程,值得注意的就是说蓄力的记录,它可以左右横冲(发疯了),最后就是一个神秘优化,也不算是优化吧,就是说如果在模拟时,两边冲不过去了,就直接返回不行,如果在中途冲出去了,那么也返回,就这样,在数据不完美的情况下,我们 Accepted 了。实测在随机数据下跑的是挺快的(假的)。
代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read() {
int x = 0 ,f = 1;
char ch = getchar();
while('0' > ch || ch > '9') {
if(ch == '-') f = -1;
ch = getchar();
}
while('0' <= ch && ch <= '9') {
x = (x << 3) + (x << 1) + (ch & 15);
ch = getchar();
}
return x * f;
}
const int N = 1e5 + 5;
int n ,answer;
bool vis[N];
pair<int ,int>dt[N];
bool check(int dtl) {
int post = dt[dtl + 1].first - dt[dtl].first;
for(int Left = dtl ,Right = dtl + 1;Left >= 1 && Right <= n;) {
bool flag = false;
if(post > dt[Left].second) {
flag = true;
-- Left;
if(vis[Left]) {vis[dtl] = true; return 0;}
post += dt[Left + 1].first - dt[Left].first;
}
if(post > dt[Right].second) {
flag = true;
++ Right;
if(vis[Right]) {vis[dtl] = true; return 0;}
post += dt[Right].first - dt[Right - 1].first;
}
if(!flag) return 1;
}
return 1;
}
signed main() {
n = read();
for(int i = 1;i <= n;++ i)
dt[i] = make_pair(read() ,read());
sort(dt + 1 ,dt + n + 1);
vis[0] = vis[n + 1] = 1;
for(int i = 1;i < n;++ i) if(check(i)) answer += dt[i + 1].first - dt[i].first;
cout << answer;
return 0;
}