题目链接:http://codeforces.com/problemset/problem/60/A

A. Where Are My Flakes?
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

The answer should contain exactly one integer — the number of boxes that should necessarily be checked or "-1" if the hints are contradictory.

Examples
input
2 1
To the left of 2
output
1
input
3 2
To the right of 1
To the right of 2
output
1
input
3 1
To the left of 3
output
2
input
3 2
To the left of 2
To the right of 1
output
-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? —— 贪心的更多相关文章

  1. Codeforces Beta Round #52 (Div. 2)

    Codeforces Beta Round #52 (Div. 2) http://codeforces.com/contest/56 A #include<bits/stdc++.h> ...

  2. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  3. 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]中有多少个数 ...

  4. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  5. Codeforces Beta Round #13 C. Sequence (DP)

    题目大意 给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降 数列中每个数为 -109-109 中的一个数 做法分析 先这样考 ...

  6. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

  7. Codeforces Beta Round #77 (Div. 2 Only)

    Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...

  8. Codeforces Beta Round #76 (Div. 2 Only)

    Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...

  9. Codeforces Beta Round #75 (Div. 2 Only)

    Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...

随机推荐

  1. 洛谷—— P1407 工资

    https://www.luogu.org/problemnew/show/P1407 题目描述 有一家世界级大企业,他们经过调查,发现了一个奇特的现象,竟然在自己的公司里,有超过一半的雇员,他们的工 ...

  2. 空扫描Idle Scanning

    空扫描Idle Scanning   空扫描Idle Scanning是一种借助第三方实施的端口扫描技术,可以很好的隐蔽扫描主机本身.它的实现基于以下两个TCP工作机制.   (1)在TCP三次握手阶 ...

  3. TreeSet, LinkedHashSet and HashSet 的区别

    1. 介绍 TreeSet, LinkedHashSet and HashSet 在java中都是实现Set的数据结构 # TreeSet的主要功能用于排序 # LinkedHashSet的主要功能用 ...

  4. CDOJ 1171 两句话题意

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1171 题解: 这道题应该从gcd出来的值入手. 我们要求所有子集的gcd的和 首先我们先统计一下每个数字出现 ...

  5. 2012-2013 ACM-ICPC, NEERC, Central Subregional Contest

    A Hanoi Tower 递归 题意: 大家都很熟悉汉诺塔的递归程序,现在给你一个组合,询问你这个组合是否会出现在汉诺塔的递归过程中. 题解: 将汉诺塔的递归程序反过来思考,考虑当前最大的那个盘,我 ...

  6. Window10下Apache2.4的安装和运行

    以前用Python运行的Web框架都是要运行在Linux下,加上WSGI服务器,比如Gunicorn+Flask,后来了解到了Apache,看看能不能基于Apache这个Web服务器下给python提 ...

  7. 安装惠普M1136打印机一直处于“新设备已连接”状态怎么办?

    百度的答案是从控制面板的添加打印机入手,我试了遇到找不到设备的问题. 其实问题的原因是在安装驱动时一直把打印机到电脑的USB插着.我的解决方案是: 1.点击M1130MFP_M1210MFP开始安装, ...

  8. 百科知识 hta文件如何打开

    后缀名为hta是什么文件,谢谢? 2006-10-11 21:36 提问者: tanhailong2006 | 浏览次数:2092次 我来帮他解答 输入内容已经达到长度限制 还能输入 9999 字 插 ...

  9. PS 基础知识 如何绘制几何图形

    注意:规则的几何图形必须用路径工具,如果使用简单的椭圆工具再描边,则效果是像素堆砌起来的.图像一旦放大就是出现明显的失真.   使用钢笔工具,然后选择路径工具,然后选择需要绘制的图形.   如果需要找 ...

  10. linux实现php定时执行cron任务详解(转)

    对于PHP本身并没有一套解决方案来执行定时任务,不过是借助sleep函数完成的.这种方就是要提前做一些配置,如实现过程: 复制代码 代码如下: ignore_user_abort();//关掉浏览器, ...