Codeforces Beta Round #56 A. Where Are My Flakes? —— 贪心

题目链接: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);
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值