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> ...
随机推荐
- Careercup | Chapter 3
3.1 Describe how you could use a single array to implement three stacks. Flexible Divisions的方案,当某个栈满 ...
- Ext 上传文件
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"/> <title> ...
- pv,uv
1.PV PV(page view),即页面浏览量:用户每1次对网站中的每个网页访问均被记录1次.用户对同一页面的多次访问,访问量累计. 2.什么是UV uv(unique visitor),指访问某 ...
- 邁向IT專家成功之路的三十則鐵律 鐵律八:IT人學習之道-基礎功
修練過中國武術的人都知道,任何一種拳法的學習最重要的就是基礎功,而基礎功又可分為內在與外在的修練,內在的修練強調在平心.靜氣.不爭的調息.至於外在這首重在站樁.鬆沉.不疾不徐的應對能力.有了深厚基礎的 ...
- ok6410[002] ubuntu1604系统下搭配ckermit和dnw基于RAM的裸机程序调试环境
ubuntu1604系统下搭配ckermit和dnw基于RAM的裸机程序调试环境 系统: ubuntu16.04 裸板: 飞凌公司OK6410开发板 目标:搭建基于ubuntu1604系统和基于RA ...
- Ubuntu16.04下屏幕侧边栏的设置
ubnutu的任务栏都是在左侧: zhang@zhang-virtual-machine:~$ gsettings set com.canonical.Unity.Launcher launcher- ...
- kubernetes对象之Job
系列目录 job是什么 对于ReplicaSet.ReplicationController等类型的控制器而言,它希望pod保持预期数目.持久运行下去,除非用户明确删除,否则这些对象一直存在,它们针对 ...
- angular input file 上传文件
<body > <div ng-controller="fileCtrl"> <form ng-submit="submit(obj)&qu ...
- C语言预处理条件语句的 与或运算
1.#ifdef 与或运算 #ifdef (MIN) && (MAX) ----------------------------错误使用 #if defined(MIN) & ...
- vue 表单输入与绑定 v-model
vue使用 v-model 指令在表单 <input>.<textarea> 及 <select> 元素上创建双向数据绑定.下面我们通过示例先了解下基本用法: &l ...