[USACO15OPEN]haybales Trappe…
刚开始我以为如果这头牛撞开一个干草堆的话,获得的冲刺距离只有新增的部分,但实际上是加上原来的部分的。
暴力很好写,区间排完序后一次判断每一个区间是否能逃脱,复杂度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…的更多相关文章
- Counting Haybales
Counting Haybales 题目描述 Farmer John is trying to hire contractors to help rearrange his farm, but so ...
- P3130 [USACO15DEC]计数haybalesCounting Haybales
P3130 [USACO15DEC]计数haybalesCounting Haybales 1)给定一段连续的田地,给每一个田地都增加一些新的草包. 2)给定一段连续的田地,找出草包最少的田地有多少草 ...
- 【BZOJ4101】[Usaco2015 Open]Trapped in the Haybales Silver 二分
[BZOJ4101][Usaco2015 Open]Trapped in the Haybales (Silver) Description Farmer John has received a sh ...
- 【BZOJ4099】Trapped in the Haybales Gold STL
[BZOJ4099]Trapped in the Haybales Gold Description Farmer John has received a shipment of N large ha ...
- Counting Haybales (线段树)
Counting Haybales 时间限制: 50 Sec 内存限制: 256 MB提交: 52 解决: 18[提交][状态][讨论版] 题目描述 Farmer John is trying t ...
- [USACO15OPEN]回文的路径Palindromic Paths
[USACO15OPEN]回文的路径Palindromic Paths 题目描述 Farmer John's farm is in the shape of an N \times NN×N grid ...
- 洛谷 P3184 [USACO16DEC]Counting Haybales数草垛
P3184 [USACO16DEC]Counting Haybales数草垛 题目描述 Farmer John has just arranged his NN haybales (1 \leq N ...
- 洛谷 P3130 [USACO15DEC]计数haybalesCounting Haybales
P3130 [USACO15DEC]计数haybalesCounting Haybales 题目描述 Farmer John is trying to hire contractors to help ...
- 题解 P3126 【[USACO15OPEN]回文的路径Palindromic Paths】
P3126 [USACO15OPEN]回文的路径Palindromic Paths 看到这题题解不多,蒟蒻便想更加通俗易懂地分享一点自己的心得,欢迎大佬批评指正^_^ 像这种棋盘形的两边同时做的dp还 ...
随机推荐
- 千图网爬图片(BeautifulSoup)
import requests from bs4 import BeautifulSoup import os #导入os模块 class TuKuSpider(): ""&quo ...
- TOJ 2861 Octal Fractions
描述 Fractions in octal (base 8) notation can be expressed exactly in decimal notation. For example, 0 ...
- VS中为什么不同的项目类型属性查看和设置的界面不一样
在VS中,存在ATL.MFC.Win32.CLR.常规等等各种工程模板,这些工程模板对应于开发不同类型的应用,比如要开发com,你应该选ATL:开发最原始的通过API代用操作系统的应用,应该用Win3 ...
- 原生js与jquery的区别
1.选择器: js: $('.car_img_con img')[0]; var jsObj = document.getElementsByClassName('sel_index_block')[ ...
- ajax返回数据之前的loading等待
首先,我们通过ajax请求,向后台传递参数,然后后台经过一系列的运算之后向前台返还数据,我希望在等待数据成功返还之前可以展示一个loading.gif图 不废话,在页面上执行点击事件(<a sc ...
- WPF的依赖项属性
WPF的依赖项属性 属性与事件是.NET抽象模型的核心部分.WPF使用了更高级的依赖项属性(Dependency Property)功能来替换原来.NET的属性,实现了更高效率的保存机制,还添加了附加 ...
- 获取当前的日期时间的js函数,格式为“yyyy-MM-dd hh:mm:ss”
//获取当前的日期时间函数,格式为“yyyy-MM-dd hh:mm:ss” function getNowFormatDate(date) { if (date == null) { var dat ...
- Python爬虫之三种数据解析方式
一.引入 二.回顾requests实现数据爬取的流程 指定url 基于requests模块发起请求 获取响应对象中的数据 进行持久化存储 其实,在上述流程中还需要较为重要的一步,就是在持久化存储之前需 ...
- php有经纬度计算距离
/** * @desc 根据两点间的经纬度计算距离 * @param float $lat 纬度值 * @param float $lng 经度值 */ function getDistanc ...
- jQuery动态添加删除CSS样式
jQuery框架提供了两个CSS样式操作方法,一个是追加样式addClass,一个是移除样式removeClass,下面通过一个小例子讲解用法. jQuery动态追加移除CSS样式 <!DOCT ...