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> ...
随机推荐
- html的诸多标签
1.p和br标签 p表示段落,默认段落之间是有间隔的! br是换行 hr是一条水平线 2.a标签,超链接 <a href="http://www.baidu.com" tar ...
- Dance In Heap(二):一些堆利用的方法(上)
0×00 前面的话 在前面的文章里我们稍微有点啰嗦的讲解了堆中的一些细节,包括malloc.free的详细过程,以及一些检查保护机制,那在这篇文章里,我们就开始结合这些机制,以64位为例来看一看如何对 ...
- 拒绝ssh远程暴力破解
拒绝ssh远程暴力破解 简介 在网络技术日益发展的今天,网络上的安全问题日益严重.当你在公网上使用Linux服务器时,很有可能你的服务器正在遭受ssh暴力破解. 曾经有一次我的同伴将给客户提供监控服务 ...
- (全然背包)小P寻宝记——好基友一起走
题目描写叙述 话说.上次小P到伊利哇呀国旅行得到了一批宝藏.他是相当开心啊.回来就告诉了他的好基友小鑫.于是他们又结伴去伊利哇呀国寻宝. 这次小P的寻宝之路可没有那么的轻松,他们走到了一个森林,小鑫一 ...
- SpringSecurity学习笔记(一):搭建最简单的SpringSecurity应用
学习过程参考自:http://www.mossle.com/docs/auth/html/pt01-quickstart.html 一.搭建Maven项目: 所需引用的jar包如下: pom.xml文 ...
- 关于移动端文字无法垂直居中(或line-height不起作用)的问题的解决方案(网摘)
最近开发移动端APP,发现安卓端对于文字垂直居中的问题兼容性不好,网上也搜了很多方法,但是都比较麻烦,自己摸索出来了最终的解决方案: 1.首先在html头部把我们常用的lang="en&qu ...
- C语言的面向对象设计之 X264,FFMPEG 架构探讨
FFMPEG架构分析 使用面向对象的办法来设想这样一个编解码库,首先让人想到的是构造各种编解码器的类,然后对于它们的抽象基类确定运行数据流的规则,根据算法转换输入输出对象. 在实际的代码,将这些编解码 ...
- Bubble Cup X - Finals [Online Mirror] B. Neural Network country 矩阵快速幂加速转移
B. Neural Network country time limit per test 2 seconds memory limit per test 256 megabytes Due to t ...
- Open Source Streaming Server--EasyDarwin
Welcome to EasyDarwin Streaming Server, which is an open source Streaming Server Based On Appple's D ...
- 在Eclipse中建立Maven Web项目
一.软件版本 Eclipse Java EE IDE for Web Developers. Version: Neon Release (4.6.0) Maven 3.3.9 Servlet 2.5 ...