洛谷题解:P2879 [USACO07JAN]Tallest Cow S

洛谷题解:P2879 [USACO07JAN]Tallest Cow S

题目:

FJ’s N (1 ≤ N ≤ 10,000) cows conveniently indexed 1…N are standing in a line. Each cow has a positive integer height (which is a bit of secret). You are told only the height H (1 ≤ H ≤ 1,000,000) of the tallest cow along with the index I of that cow.

FJ has made a list of R (0 ≤ R ≤ 10,000) lines of the form “cow 17 sees cow 34”. This means that cow 34 is at least as tall as cow 17, and that every cow between 17 and 34 has a height that is strictly smaller than that of cow 17.

For each cow from 1…N, determine its maximum possible height, such that all of the information given is still correct. It is guaranteed that it is possible to satisfy all the constraints.

FarmerJohn 有n头牛,它们按顺序排成一列。 FarmerJohn 只知道其中最高的奶牛的序号及它的高度,其他奶牛的高度都是未知的。现在 FarmerJohn 手上有RRR条信息,每条信息上有两头奶牛的序号(aaa和bbb),其中bbb奶牛的高度一定大于等于aaa奶牛的高度,且aaa,bbb之间的所有奶牛的高度都比aaa小。现在FarmerJohnFarmerJohnFarmerJohn想让你根据这些信息求出每一头奶牛的可能的最大的高度。(数据保证有解)

输入格式:

第1行:四个以空格分隔的整数:nnn,iii,hhh和RRR(nnn和RRR意义见题面; iii 和 hhh 表示第 iii 头牛的高度为 hhh ,他是最高的奶牛)

接下来R行:两个不同的整数aaa和bbb(1 ≤ aaa,bbb ≤ n)

输出格式:

一共n行,表示每头奶牛的最大可能高度.

样例:

输入:

9 3 5 5
1 3
5 3
4 3
3 7
9 8

输出:

5
4
5
3
4
4
5
5
5

分析:

题的思路很清晰,把数据整理好就行,依然是利用的差分数组的前缀和,对于区间内的元素,从初始值H(最高)往下减。

我的疑惑是,这样不会有bug吗???

例如如下输入:

9 1 5 3
1 3
1 5
2 7

对应输出:

5
3
3
3
4
4
5
5
5

这肯定不对呀。。。当然不对了,这是个无解的数据,,,对于有解的数据,上述方法是完全没有问题的,所以用正常(能想到的)思维写就行了。。至于什么时候无解,我好像看出点端倪,好像是当存在下一个区间包含但不完全包含于上一个区间的时候好像都不成立,也没有严格证明验证过。。。大概就是这个意思。

就是这里耽误了好久。。认真审题呀。

代码如下:

#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>

using namespace std;

typedef struct ne{
    
    
    int l,r;

    friend bool operator<(const ne a, const ne b)
    {
    
    
        if(a.l == b.l)
        {
    
    
            return a.r < b.r;
        }
        else
        {
    
    
            return a.l < b.l;
        }
    }
}node;

int change[1000006];
int n, h, sit, r;
node list[1000006], temp[1000006];

int main()
{
    
    
    cin>>n>>sit>>h>>r;
    for(int i = 1;i <= r;i++)
    {
    
    
        cin>>temp[i].l>>temp[i].r;
        if(temp[i].r < temp[i].l)
        {
    
    
            swap(temp[i].r, temp[i].l);
        }
    }

    int cnt = 1;
    sort(temp + 1,temp + 1 + r);
    for(int i = 1;i <= r;i++)
        if(temp[i].l != temp[i-1].l || temp[i].r != temp[i-1].r)
        {
    
    
            list[cnt++] = temp[i];
        }
    {
    
    
    }

    for(int i = 1;i <= cnt;i++)
    {
    
    
        change[list[i].l + 1]++;
        change[list[i].r]--;
    }

    for(int i = 1;i <= n;i++)
    {
    
    
        change[i] = change[i] + change[i - 1];
        cout<<(h - change[i])<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_46052886/article/details/113483053
今日推荐