Codeforces Beta Round #56 A. Where Are My Flakes? —— 贪心
题目链接:http://codeforces.com/problemset/problem/60/A
2 seconds
256 megabytes
standard input
standard output
One morning the Cereal Guy found out that all his cereal flakes were gone. He found a note instead of them. It turned out that his smart roommate hid the flakes in one of n boxes.
The boxes stand in one row, they are numbered from 1 to n from
the left to the right. The roommate left hints like "Hidden to the left of the i-th box" ("To
the left of i"), "Hidden to the right of the i-th
box" ("To the right of i").
Such hints mean that there are no flakes in the i-th box as well. The Cereal Guy wants to know the minimal number of boxes he necessarily
needs to check to find the flakes considering all the hints. Or he wants to find out that the hints are contradictory and the roommate lied to him, that is, no box has the flakes.
The first line contains two integers n and m (1 ≤ n ≤ 1000, 0 ≤ m ≤ 1000)
which represent the number of boxes and the number of hints correspondingly. Next m lines contain hints like "To
the left of i" and "To the right
of i", where i is
integer (1 ≤ i ≤ n). The hints may coincide.
The answer should contain exactly one integer — the number of boxes that should necessarily be checked or "-1" if the hints are contradictory.
2 1
To the left of 2
1
3 2
To the right of 1
To the right of 2
1
3 1
To the left of 3
2
3 2
To the left of 2
To the right of 1
-1
题解:
方法一:贪心,由于区间范围只能是连续的,所以可以逐渐缩小范围。
方法二:差分法(还可适用于不连续的区间,比较通用的方法)。
区间并集:
1.当并集区间有一段时,可直接贪心,左右缩小范围。
2.当并集区间有多段时,使用差分法,然后再线段扫描(求前缀和)。
3.有关差分法的另一道题:http://blog.csdn.net/dolfamingo/article/details/72858734
贪心:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const double eps = 1e-6;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int maxn = 1e3+10; int n,m, c[maxn]; int main()
{
cin>>n>>m;
int B = 1;
int l = 1, r = n;
for(int i = 1; i<=m; i++)
{
int t;
string s[5];
cin>>s[0]>>s[1]>>s[2]>>s[3]>>t;
if(s[2]=="left")
r = min(r,t-1);
else
l = max(l, t+1);
} printf("%d\n",(r-l+1>0)?(r-l+1):-1); }
差分法:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const double eps = 1e-6;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int maxn = 1e3+10; int n, m, c[maxn]; int main()
{
cin>>n>>m;
for(int i = 1; i<=m; i++)
{
int t;
string s[5];
cin>>s[0]>>s[1]>>s[2]>>s[3]>>t;
if(s[2]=="left")
c[1]++, c[t]--;
else
c[t+1]++, c[n+1]--;
} int cnt = 0;
for(int i = 1; i<=n; i++)
{
c[i] += c[i-1];
if(c[i]==m)
cnt++;
}
printf("%d\n", cnt?cnt:-1);
}
Codeforces Beta Round #56 A. Where Are My Flakes? —— 贪心的更多相关文章
- Codeforces Beta Round #52 (Div. 2)
Codeforces Beta Round #52 (Div. 2) http://codeforces.com/contest/56 A #include<bits/stdc++.h> ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #62 题解【ABCD】
Codeforces Beta Round #62 A Irrational problem 题意 f(x) = x mod p1 mod p2 mod p3 mod p4 问你[a,b]中有多少个数 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
- Codeforces Beta Round #13 C. Sequence (DP)
题目大意 给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降 数列中每个数为 -109-109 中的一个数 做法分析 先这样考 ...
- Codeforces Beta Round #79 (Div. 2 Only)
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
- Codeforces Beta Round #77 (Div. 2 Only)
Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...
- Codeforces Beta Round #76 (Div. 2 Only)
Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...
- Codeforces Beta Round #75 (Div. 2 Only)
Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...
随机推荐
- 《Python基础教程读书笔记》
第1章 快速构造:基础知识 1.2交互式构造器 不强制分号,一行就是一行.可以加上分号 1.4数字和表达式 加.减.乘.除.整除.求余.指数.取反(-例如-2**2,**的优先级比-大) from _ ...
- 洛谷——P2706 巧克力
P2706 巧克力 题目背景 王7的生日到了,他的弟弟准备送他巧克力. 题目描述 有一个被分成n*m格的巧克力盒,在(i,j)的位置上有a[i,j]块巧克力.就在送出它的前一天晚上,有老鼠夜袭巧克力盒 ...
- 空扫描Idle Scanning
空扫描Idle Scanning 空扫描Idle Scanning是一种借助第三方实施的端口扫描技术,可以很好的隐蔽扫描主机本身.它的实现基于以下两个TCP工作机制. (1)在TCP三次握手阶 ...
- 利用注解和反射,将Bean枚举字段的值填入相应的字段中,并转化为fastjson返回前台
需求:需要将枚举类型的字段例如enable(是否启用)转化为enable:1,enableName:是.这种形式返回给前台. 思路:在bean字段上加上枚举类型的注解,通过字段的值和枚举类反射获取枚举 ...
- 346. Moving Average from Data Stream
/* * 346. Moving Average from Data Stream * 2016-7-11 by Mingyang * 这里注意的就是(double) sum / count * su ...
- GCD CoreData 简化CoreData操作(转)
来自会员带睡帽的青蛙的分享: 短话长说,开始写这个小工具到现在有两个月了,虽然东西少,但是很精练,改了又改,期间有不少问题 在坛子里获得了不少帮助 谢谢各位大大. 就是两个文件一个类 CoreData ...
- ※版本管理※=>☆SVN工具=>※解决地域麻烦※№→搭建自己的网络SVN (SourceForge 免费) [转]
源文 http://blog.csdn.net/xiaoting451292510/article/details/8562570 分类: 版本管理 2013-02-01 14:44 26057人阅读 ...
- npm 更新镜像安装Appium
npm -g --registry http://registry.cnpmjs.org install appium
- 手把手教你安装Hbase,一次成功!
安装环境: OS: Centos 6.5 JDK: jdk1.6.0_18 Hadoop: hadoop-0.20.2 Hbase: hbase-0.90.5 安装准备: 1. Jdk环境 ...
- 网站的根目录下有一个文件robots.txt ,它是啥?
我相信很多人有过这个疑问,这个robots文件是干嘛的? 我想问,各位搜索淘宝时,是否发现(禁止爬虫抓取提供快页) 关于详细语法,请看:http://zhidao.baidu.com/question ...