[洛谷]P2879 [USACO07JAN]区间统计Tallest Cow (#模拟)

64 篇文章 1 订阅
36 篇文章 0 订阅

题目描述:

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

输入格式:

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

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

输出格式:

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

数据范围:

1 ≤ n ≤ 10000 ; 1 ≤ h ≤ 1000000 ; 0 ≤ R ≤ 10000)

Translate provided by @酥皮

题目描述

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.

输入输出格式

输入格式:

Line 1: Four space-separated integers: N, I, H and R

Lines 2..R+1: Two distinct space-separated integers A and B (1 ≤ A, B ≤ N), indicating that cow A can see cow B.

输出格式:

Lines 1..N: Line i contains the maximum possible height of cow i.

输入输出样例

输入样例#1

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

输出样例#1

5
4
5
3
4
4
5
5
5

思路

想到了一种奇特的思路,可以避开差分和前缀和。

令所有奶牛的身高都为最大值highest,然后依题意,2个奶牛能互相看到就把(奶牛a,奶牛b)(注意是闭区间)的奶牛砍了。

#include <stdio.h>
#include <iostream>
#define limit 100000
using namespace std;
typedef struct
{
	int first,second;
}lxydl;
lxydl h[10001];//每个奶牛的高度信息 
bool b[10001][10001];//去重,其实用map就能做到节省空间了 
int n,a[10001],highest,s,r,t;
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	register int i,j,k;
	cin>>n>>t>>highest>>r;
	for(i=1;i<=n;i++)
	{
		a[i]=highest;//令每个奶牛都是最高的 
	}
	for(i=1;i<=r;i++)
	{
		int x,y;
		cin>>x>>y;
		h[i].first=min(x,y);
		h[i].second=max(x,y);//令这二者中小的赋给first,大的赋给second 
		if(b[x][y]==1)//如果算过一次就跳过,一定要去个重 
		{
			continue;
		}
		else//否则标记 
		{
			b[x][y]=1;
		}
		for(j=h[i].first+1;j<=h[i].second-1;j++)//把中间所有奶牛高度-1 
		{
			a[j]--;
		}
	}
	for(i=1;i<=n;i++)
	{
		cout<<a[i]<<endl;
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值