嘟嘟嘟

刚开始我以为如果这头牛撞开一个干草堆的话,获得的冲刺距离只有新增的部分,但实际上是加上原来的部分的。

暴力很好写,区间排完序后一次判断每一个区间是否能逃脱,复杂度O(n2)。

优化想起来也不难:如果一个区间 i 能逃脱,区间 j 能到达 i,则 j 也能逃脱。所以对于每个区间开一个标记数组,记录能否逃脱。然后枚举区间的时候向两边扩,如果到达了一个能逃脱的区间就返回,并标记。复杂度虽然不能准确算出来,但能A这道题。

 #include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-;
const int maxn = 1e5 + ;
inline ll read()
{
ll ans = ;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) {last = ch; ch = getchar();}
while(isdigit(ch)) {ans = ans * + ch - ''; ch = getchar();}
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < ) x = -x, putchar('-');
if(x >= ) write(x / );
putchar(x % + '');
} int n;
struct Node
{
int siz, id;
bool operator < (const Node &oth)const
{
return id < oth.id;
}
}t[maxn];
bool vis[maxn]; bool solve(int x)
{
if(vis[x]) return ;
int dis = t[x + ].id - t[x].id;
int L = x, R = x + ;
while(L >= && R <= n)
{
bool flg = ;
if(dis > t[L].siz)
{
flg = ; L--;
if(vis[L]) {vis[x] = ; return ;}
dis += t[L + ].id - t[L].id;
}
if(dis > t[R].siz)
{
flg = ; R++;
if(vis[R - ]) {vis[x] = ; return ;}
dis += t[R].id - t[R - ].id;
}
if(!flg) return ;
}
return ;
} ll ans = ; int main()
{
n = read();
for(int i = ; i <= n; ++i) t[i].siz = read(), t[i].id = read();
sort(t + , t + n + );
vis[] = vis[n] = ;
for(int i = ; i <= n; ++i)
if(!solve(i)) ans += t[i + ].id - t[i].id;
write(ans), enter;
return ;
}

[USACO15OPEN]haybales Trappe…的更多相关文章

  1. Counting Haybales

    Counting Haybales 题目描述 Farmer John is trying to hire contractors to help rearrange his farm, but so ...

  2. P3130 [USACO15DEC]计数haybalesCounting Haybales

    P3130 [USACO15DEC]计数haybalesCounting Haybales 1)给定一段连续的田地,给每一个田地都增加一些新的草包. 2)给定一段连续的田地,找出草包最少的田地有多少草 ...

  3. 【BZOJ4101】[Usaco2015 Open]Trapped in the Haybales Silver 二分

    [BZOJ4101][Usaco2015 Open]Trapped in the Haybales (Silver) Description Farmer John has received a sh ...

  4. 【BZOJ4099】Trapped in the Haybales Gold STL

    [BZOJ4099]Trapped in the Haybales Gold Description Farmer John has received a shipment of N large ha ...

  5. Counting Haybales (线段树)

    Counting Haybales 时间限制: 50 Sec  内存限制: 256 MB提交: 52  解决: 18[提交][状态][讨论版] 题目描述 Farmer John is trying t ...

  6. [USACO15OPEN]回文的路径Palindromic Paths

    [USACO15OPEN]回文的路径Palindromic Paths 题目描述 Farmer John's farm is in the shape of an N \times NN×N grid ...

  7. 洛谷 P3184 [USACO16DEC]Counting Haybales数草垛

    P3184 [USACO16DEC]Counting Haybales数草垛 题目描述 Farmer John has just arranged his NN haybales (1 \leq N ...

  8. 洛谷 P3130 [USACO15DEC]计数haybalesCounting Haybales

    P3130 [USACO15DEC]计数haybalesCounting Haybales 题目描述 Farmer John is trying to hire contractors to help ...

  9. 题解 P3126 【[USACO15OPEN]回文的路径Palindromic Paths】

    P3126 [USACO15OPEN]回文的路径Palindromic Paths 看到这题题解不多,蒟蒻便想更加通俗易懂地分享一点自己的心得,欢迎大佬批评指正^_^ 像这种棋盘形的两边同时做的dp还 ...

随机推荐

  1. Missing artifact com.oracle:ojdbc6:jar:11.2.0.1.0问题解决 ojdbc包pom.xml出错

    <!-- 添加oracle jdbc driver --> <dependency> <groupId>com.oracle</groupId> < ...

  2. 依赖倒置(Dependence Inversion Principle)DIP

    关于抽象类和接口的区别,可以参考之前的文章~http://www.cnblogs.com/leestar54/p/4593173.html using System; using System.Col ...

  3. Python的静态方法和类成员方法都可以被类或实例访问,两者概念不容易理清,但还是有区别的

    转:http://www.cnblogs.com/2gua/ Python的静态方法和类成员方法都可以被类或实例访问,两者概念不容易理清,但还是有区别的: 1)静态方法无需传入self参数,类成员方法 ...

  4. unity烘焙记录

    1.Unity Android 阴影不显示.阴影显示不正确 解决 http://blog.csdn.net/xuetao0605/article/details/50626181 2.阴影强度问题 不 ...

  5. Spring课程 Spring入门篇 4-9 Spring bean装配之对jsr支持的说明

    1 解析 1.1 疑问:2.2去掉@resource注解,为什么不能赋值?不是有set方法了吗? 1.2 @resource注解版本支持 1.3 没有显式指定@resource的那么,默认名称从何获得 ...

  6. python数据类型(集合)

    一.集合概念 集合是一个数学概念:由一个或多个确定的元素所构成的整体叫做集合. 集合中的元素三个特征: 确定性(元素必须可hash) 互异性(去重)——将一个列表变为集合,就自动去重了 无序性(集合中 ...

  7. H5新特性-----type=file文件上传

    1.语法 单文件上传:<input type="file" id="file1"/> 多文件上传:<input type="file ...

  8. SharePoint 2013 - Add-ins

    1. App Web & Host Web The special website to which the app is deployed is called an App Web. The ...

  9. 随学笔记 partAdded

    随学笔记: RectangularDropShadow为矩形对象添加阴影,DropShadowFilter可以为任意形状对象添加阴影. BorderContainer和Panel等容器使用的就是Rec ...

  10. Android NestedScrollView与RecyclerView嵌套,以及NestedScrollView不会滚动到屏幕顶部解决

    ①NestedScrollView与RecyclerView嵌套,导致滚动惯性消失 解决:mRecyclerView.setNestedScrollingEnabled(false); ②Nested ...