[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还 ...
随机推荐
- spring IoC源码分析 (3)Resource解析
引自 spring IoC源码分析 (3)Resource解析 定义好了Resource之后,看到XmlFactoryBean的构造函数 public XmlBeanFactory(Resource ...
- 队列同步器AbstractQueuedSynchronizer
AQS是用来构建锁或者其它同步组件的基础框架,它使用一个int变量来表示同步状态,通过内置的FIFO队列来完成获取线程的排队工作,concurrent包的作者Doug Lea期望它能称为实现大部分同步 ...
- Android中用Java代码实现zip文件解压缩
如果需要下载的文件有很多是中文名的,解压时有中文名的文件出现乱码,试了很多方法不能解决问题.据说有一个Java插件包,用这个插件包可以解决中文名乱码的问题,但不知解压的文件是否要用它提供的类压缩后的文 ...
- mysql中字段类型是datetime时比较大小问题
select sum(studychj) as tofflinejz from afterline where studybegin >= '2010-01-01 00:00:00' and s ...
- nyoj 791——Color the fence——————【贪心】
Color the fence 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 Tom has fallen in love with Mary. Now Tom w ...
- nyoj 47——过河问题——————【贪心】
过河问题 时间限制:1000 ms | 内存限制:65535 KB 难度:5 描述 在漆黑的夜里,N位旅行者来到了一座狭窄而且没有护栏的桥边.如果不借助手电筒的话,大家是无论如何也不敢过桥去的 ...
- C#生成二维码的内容
生成二维码的内容 using QRCoder; // 生成二维码的内容 string strCode = this.txtQr.Text.Trim(); if (string.IsNullOrWhit ...
- Js COOkie 读取
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- VMware装Linux系统全屏问题
在VMware上出装Linux,有强迫症的患者总是无法接受它不能全屏的问题,当然网上也有该问题的解决方案,但是搜索出来的答案总是零零散散,让很多初学者望而却步!今天笔者根据自己的机遇总结一遍最完备的解 ...
- C#程序执行时间
Stopwatch类 using System.Diagnostics; static void Main(string[] args) { Stopwatch stopWatch = new Sto ...