嘟嘟嘟

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

暴力很好写,区间排完序后一次判断每一个区间是否能逃脱,复杂度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. python_面向对象—代码练习

    """注意:代码切勿照搬,错误请留言指出""" import re ''' class Person: name='xxx' age=20 ...

  2. BNU 4356 ——A Simple But Difficult Problem——————【快速幂、模运算】

    A Simple But Difficult Problem Time Limit: 5000ms Memory Limit: 65536KB 64-bit integer IO format: %l ...

  3. HDU 1394——Minimum Inversion Number——————【线段树单点增减、区间求和】

    Minimum Inversion Number Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & ...

  4. Flask 框架理解(一)

    Flask 框架理解(一) web 服务器 , web 框架 以及 WSGI 这里说的 web 服务器特指纯粹的 python HTTP 服务器(比如 Gunicorn,而不是 Apache,Ngin ...

  5. python pickle命令执行与marshal 任意代码执行

    1.python pickle反序列化漏洞 自己的理解: 由于在类的__reduce__方法中提供了我们可以自定义程序如何去解序列化的方法,因此如果应用程序接受了不可信任的序列化的数据,那么就可能导致 ...

  6. jQuery获取table表中的td标签

    首先我来介绍一下我遇到的问题 1.当有一个table表包含了<tr>标签,<td>标签,大致可以认为是这样的: <tr> <td> @scene.ID ...

  7. SpringSecurity 3.2入门(7)自定义权限控制介绍

    总结Spring Security的使用方法有如下几种: 一种是全部利用配置文件,将用户.权限.资源(url)硬编码在xml文件中. 二种是用户和权限用数据库存储,而资源(url)和权限的对应关系硬编 ...

  8. SpringSecurity 3.2入门(1)框架介绍

    关于Spring Security Spring Security,这是一种基于Spring AOP和Servlet过滤器 [7] 的安全框架.它提供全面的安全性解决方案,同时在 Web 请求级和方法 ...

  9. Cookie的创建、读取、删除

    创建Cookie: HttpCookie cookie =  new HttpCookie(COOKIE_NAME_FOR_USER);cookie.Expires = DateTime.Now.Ad ...

  10. C# ADO.NET 面向对象

    ADO.NET跟面向对象的结合 把面向对象跟数据库连接用 在项目里面创建一个新的文件夹   名字为App_Code 在这个App_Code里面创建几个类 主要为拆分问题,标上序号,先干什么在干什么 实 ...