题目链接: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. datetimepicker使用总结

    datetimepicker使用总结 2019-03-06    16:55:00 使用效果: 官方教程:http://www.bootcss.com/p/bootstrap-datetimepick ...

  2. Java中PO、BO、VO、DTO、POJO、DAO概念及其作用和项目实例图(转)

    PO(bean.entity等命名): Persistant Object持久对象,数据库表中的记录在java对象中的显示状态 最形象的理解就是一个PO就是数据库中的一条记录. 好处是可以把一条记录作 ...

  3. ios使用http来上传图片实现方法

    if (parameters) {                int genderNumber = 1;        self.token = loginToken;        self.p ...

  4. Android --修改arr文件

    1. 改为zip文件 2. 修改 3. 改后缀

  5. Mac环境下安装运行splash

    http://blog.csdn.net/chenhy8208/article/details/69391097 最近需要使用scrapy爬虫做一些开发,用到了splash.我本机是mac环境,跳着看 ...

  6. 使用cacheBuilder时捕获内部指定异常

    由于cacheBuilder是另起线程获取,对call方法中的抛出的异常进行了封装.所以我们在最外层捕获时是无法直接指定异常类型捕获的, 获取异常的原因判断实例类型 public static voi ...

  7. lucene 自定义评分 (给lucene自带的评分*我们filed的系数) 如搜索结果时间的加权

    参见孔浩 lucene 22讲 步骤 1.写一个类继承于 CostomScoreQuery -->覆盖getCostomSorceProvider 方法 2.写一个自己的provider(重写c ...

  8. python(26)- 面向对象补充Ⅱ

    一 isinstance(obj,cls)和issubclass(sub,super) isinstance(obj,cls)判断obj是否是类 cls 的对象 class Foo(object): ...

  9. 关于Swiper(概念)

    Swiper 是一款免费以及轻量级的移动设备触控滑块的js框架,使用硬件加速过渡(如果该设备支持的话). 主要使用于移动端的网站.移动web apps,native apps和hybrid apps. ...

  10. 使用react全家桶制作博客后台管理系统 网站PWA升级 移动端常见问题处理 循序渐进学.Net Core Web Api开发系列【4】:前端访问WebApi [Abp 源码分析]四、模块配置 [Abp 源码分析]三、依赖注入

    使用react全家桶制作博客后台管理系统   前面的话 笔者在做一个完整的博客上线项目,包括前台.后台.后端接口和服务器配置.本文将详细介绍使用react全家桶制作的博客后台管理系统 概述 该项目是基 ...