[HAOI2014] posters

Title Description

To Bytetown city mayor elections, all voters can speak freely to the speech of the candidates running for mayor. In order to unify management of the city council as voters prepared a poster of electoral wall.

Posted rules are as follows:

electoral墙是一个长度为N个单位的长方形,每个单位记为一个格子;

所有张贴的海报的高度必须与electoral墙的高度一致的;

每张海报以“A B”表示,即从第A个格子到第B个格子张贴海报;

后贴的海报可以覆盖前面已贴的海报或部分海报。

Now you judge, after you put all the posters, the electoral posters on the wall can also see how much.

Input Format

The first line: NM respectively represent the length and the number of wall posters electoral

Next M rows: Ai Bi poster showing a position of each
output format

After output had finished all posters, the number of electoral posters in the wall can be seen.

I not quite believe this is a green theme. . .

Gangster solution to a problem you are too right, I'll give it one of the most violent!

  1. Poster represented by k, each time a new one is distinguished by k ++.

  2. Within the interval [a, b], and to each of t [i] are assigned to a new k (overwriting the original value).

  3. Analyzing [1, n] are all set to be covered. Is then let w = 0, otherwise w = 1. (The last to lose w).

  4. If there are different poster exposed (i.e., g [i] have different values ​​appear), sum ++ (count).

  5. sum-w is the answer.


I thought this was a segment tree, the results cheated.


#include<bits/stdc++.h>
#define maxn 10000009
using namespace std;
long long int n,m,sum,w,k;
int t[maxn],g[maxn];

template<typename T>
void read(T &x){
    x=0;int f=1;char ch=getchar();
    while(ch>'9'||ch<'0'){
        if (ch=='-') f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=x*10+ch-'0';
        ch=getchar();
    }
    x*=f;
}
int main(){
    read(n),read(m);
    for (int i=1;i<=m;i++){
        int a,b;
        read(a),read(b);
        k++;//更新k。
        for (int j=a;j<=b;j++)
            t[j]=k;//在[a,b]的区间内,给每一个t[i]都赋值为新的k(原有值的覆盖掉)。
        
    }
    for (int i=1;i<=n;i++){
        if(t[i]==0){
            w=1;break;//判断[1,n]是否全被覆盖。是则令w=0,否则w=1。(最后要减掉w)。
        }
    }
    for (int i=1;i<=n;i++){
        if(g[t[i]]==0){
            sum++;g[t[i]]=1;//如果有不同的海报露出来(即g[i]有不同的值出现),sum++(计数)。
        }
    }
    cout<<sum-w;//sum-w即为答案。
    return 0;
}

Guess you like

Origin www.cnblogs.com/zhangdading/p/11564797.html