嘟嘟嘟

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

暴力很好写,区间排完序后一次判断每一个区间是否能逃脱,复杂度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. HashMap和Hashtable的实现原理

    HashMap和Hashtable的底层实现都是数组+链表结构实现的,这点上完全一致 添加.删除.获取元素时都是先计算hash,根据hash和table.length计算index也就是table数组 ...

  2. (转)CentOS 7 sytemctl 自定义服务开机启动

    CentOS 7 sytemctl 自定义服务开机启动 原文:http://blog.csdn.net/ithomer/article/details/51766319 CentOS 7继承了RHEL ...

  3. (转)Linux curl命令参数详解

    Linux curl命令参数详解 命令:curl在Linux中curl是一个利用URL规则在命令行下工作的文件传输工具,可以说是一款很强大的http命令行工具.它支持文件的上传和下载,是综合传输工具, ...

  4. tabs(标签页的现成页面)原生js写法

    直接上代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...

  5. 【算法】K-Means聚类算法(k-平均或k-均值)

    1.聚类算法和分类算法的区别 a)分类 分类(Categorization or Classification)就是按照某种标准给对象贴标签(label),再根据标签来区分归类. 举例: 假如你有一堆 ...

  6. this,super,和继承

    this是指当前对象的引用,super是指直接父类的引用 比如 我建造一个类 public class Person(){ private String name; private  int age; ...

  7. pat00-自测2. 素数对猜想 (20)

    00-自测2. 素数对猜想 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 让我们定义 dn 为:dn  ...

  8. ajax数据获取与传送

    function service_ajax(){ var migrate_style = $('#myTab').find('.active').attr('value'); //jquery获取页面 ...

  9. 修改ThinkPHP的验证码类

    今天用ThinkPHP重新开发一个系统,用到了ThinkPHP的验证码类,由于我希望验证码别太复杂,希望验证码里边只有数字,却发现该Verify类并未提供设置验证码中使用的字符的配置的方法,于是查看源 ...

  10. DJango小总结一

    views.py                        def func(request):                # 包含所有的请求数据                ...     ...