HDU6669 Game(思维,贪心)
维护区间 \([l,r]\) 为完成前 \(i\) 步使用最少步数后可能落在的区间。
初始时区间 \([l,r]\) 为整个坐标轴。
对于第 \(i\) 个任务区间 \([a,b]\),如果两区间相离,那么至少需要 \((length + 1) / 2\) 步。
在第 \(i\) 个任务完成后,区间 \([l,r]\) 将先扩大 \(length\),然后再更新为 \([l,r]\) 和 \([a,b]\) 的交集。
时间复杂度为 \(O(n)\) 。
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
const int inf = 1e6;
int t, n;
long long a, b, l, r, dist, ans;
int main()
{
scanf("%d", &t);
for(int cas = 1; cas <= t; cas++){
ans = 0;
l = 0; r = inf;
scanf("%d", &n);
for(int i = 1; i <= n; i++){
scanf("%lld%lld", &a, &b);
dist = 0;
if(a > r){
dist = (a - r + 1) >> 1;
}
if(b < l){
dist = (l - b + 1) >> 1;
}
ans += dist;
/***l和r实际只需更新一边***/
l -= dist << 1;
r += dist << 1;
l = max(l, a);
r = min(r, b);
}
printf("%lld\n", ans);
}
return 0;
}
HDU6669 Game(思维,贪心)的更多相关文章
- 2018-2019 ACM-ICPC, Asia Xuzhou Regional Contest- H. Rikka with A Long Colour Palette -思维+贪心
2018-2019 ACM-ICPC, Asia Xuzhou Regional Contest- H. Rikka with A Long Colour Palette -思维+贪心 [Proble ...
- E. The Contest ( 简单DP || 思维 + 贪心)
传送门 题意: 有 n 个数 (1 ~ n) 分给了三个人 a, b, c: 其中 a 有 k1 个, b 有 k2 个, c 有 k3 个. 现在问最少需要多少操作,使得 a 中所有数 是 1 ~ ...
- Sorted Adjacent Differences(CodeForces - 1339B)【思维+贪心】
B - Sorted Adjacent Differences(CodeForces - 1339B) 题目链接 算法 思维+贪心 时间复杂度O(nlogn) 1.这道题的题意主要就是让你对一个数组进 ...
- Codeforces Round #768 (Div. 2) D. Range and Partition // 思维 + 贪心 + 二分查找
The link to problem:Problem - D - Codeforces D. Range and Partition time limit per test: 2 second ...
- 【CF1256】Codeforces Round #598 (Div. 3) 【思维+贪心+DP】
https://codeforces.com/contest/1256 A:Payment Without Change[思维] 题意:给你a个价值n的物品和b个价值1的物品,问是否存在取物方案使得价 ...
- CF思维联系--CodeForces -214C (拓扑排序+思维+贪心)
ACM思维题训练集合 Furik and Rubik love playing computer games. Furik has recently found a new game that gre ...
- Codeforces Round #517 (Div. 2) C. Cram Time(思维+贪心)
https://codeforces.com/contest/1065 题意 给你a,b,让你找尽量多的自然数,使得他们的和<=a,<=b,用在a和b的自然数不能重复 思路 假如只有一个数 ...
- Buy Low Sell High CodeForces - 867E (思维,贪心)
大意: 第i天可以花$a_i$元买入或卖出一股或者什么也不干, 初始没钱, 求i天后最大收益 考虑贪心, 对于第$x$股, 如果$x$之前有比它便宜的, 就在之前的那一天买, 直接将$x$卖掉. 并不 ...
- Wannafly交流赛1 B 硬币[数学思维/贪心]
链接:https://www.nowcoder.com/acm/contest/69/B来源:牛客网 蜥蜴的生日快到了,就在这个月底! 今年,蜥蜴的快乐伙伴之一壁虎想要送好多个1元硬币来恶整蜥蜴. 壁 ...
- 【思维+贪心】codeforces Game of the Rows
http://codeforces.com/contest/839/problem/B [题意] 给定n组人,告诉每组人的人数,这些人要在飞机上坐座位 飞机上座位的分布看图可以知道,12 3456 ...
随机推荐
- codeforces#1215E. Marbles(状压DP)
题目大意:给出一个由N个整数组成的序列,通过每次交换相邻的两个数,使这个序列的每个相同的数都相邻.求最小的交换次数. 比如给出序列:1 2 3 2 1 ,那么最终序列应该是 1 1 2 2 3 ,最小 ...
- STL关联容器值hashtable
hashtable(散列表)是一种数据结构,在元素的插入,删除,搜索操作上具有常数平均时间复杂度O(1); hashtable名词 散列函数:负责将某一元素映射为索引. 碰撞(collision):不 ...
- Java-JavaMail邮件开发(1)Java Mail
1.仅使用Java Mail 使用163邮箱作为代理服务器: 引入依赖: <dependency> <groupId>com.sun.mail</groupId> ...
- 数论(lcm)
CodeForces - 1154G You are given an array a consisting of n integers a1,a2,…,an . Your problem is to ...
- HDU-1181 变形课(多种方式,好题)
首先想到的是并查集,然后WA...原因在这,我第一次敲的是Find(1) == Find(12)来作为可以成功的条件,实际上这样是不行的,比方说 bell 和 mail实际上是不满足条件的,可以理 ...
- svn登录问题
转载自: https://www.cnblogs.com/WQX-work24/p/9790260.html 版权归该作者所有.
- hadoop项目开发运行报错(log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory).)
使用hadoop+myeclipse开发项目是测试运行报错: log4j:WARN No appenders could be found for logger (org.apache.hadoop. ...
- restTemplate工具类
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.sprin ...
- hdu 4082 Hou Yi's secret(暴力枚举)
Hou Yi's secret Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- gbase整合mybatis出现: Cause: java.sql.SQLException: Can't convert to: binary stream
参考地址:http://mybatis-user.963551.n3.nabble.com/Map-SQL-Type-LVARCHAR-x-to-JDBC-Type-VARCHAR-globally- ...