0
点赞
收藏
分享

微信扫一扫

算法进阶指南 POJ - 3263Tallest Cow (前缀和+差分)

飞鸟不急 2023-02-07 阅读 78


有 NN 头牛站成一行,被编队为1、2、3…N,每头牛的身高都为整数。

当且仅当两头牛中间的牛身高都比它们矮时,两头牛方可看到对方。

现在,我们只知道其中最高的牛是第 PP 头,它的身高是 HH ,剩余牛的身高未知。

但是,我们还知道这群牛之中存在着 MM 对关系,每对关系都指明了某两头牛 AA 和 BB 可以相互看见。

求每头牛的身高的最大可能值是多少。

输入格式

第一行输入整数N,P,H,MN,P,H,M,数据用空格隔开。

接下来M行,每行输出两个整数 AA 和 BB ,代表牛 AA 和牛 BB 可以相互看见,数据用空格隔开。

输出格式

一共输出 NN 行数据,每行输出一个整数。

第 ii 行输出的整数代表第 ii 头牛可能的最大身高。

数据范围

1≤N≤100001≤N≤10000,
1≤H≤10000001≤H≤1000000,
1≤A,B≤100001≤A,B≤10000,
0≤M≤100000≤M≤10000

输入样例:

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

输出样例:

5
4
5
3
4
4
5
5
5

注意:

  • 此题中给出的关系对可能存在重复

题意:

出n头牛的身高,和m对关系:a与b可以相互看见。

已知最高的牛为第p头,身高为h。求每头牛的身高最大可能是多少?

分析:
第p头最高h,比他矮的最高一定是h-1,即求出每一头比最高的那头牛矮多少d[i];

a与b相互看见:他们中间的牛(a[i+1]到b[i-1])身高都比他们矮,于是把他们身高都减去1即可。

维护一个差分数列即可。

坑点:输入数据可能有重复和可能位置顺序不对

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int N = 2e5 + 100;
map<pair<int,int>, bool>vis;
int c[N], d[N];

int main()
{
int n, p, h, m;
cin>>n>>p>>h>>m;
for(int i = 1; i <= m; i++)
{
int a, b;
cin>>a>>b;
if(a > b)
swap(a, b);
if(vis[make_pair(a,b)])
continue;
d[a+1]--;
d[b]++;
vis[make_pair(a,b)] = 1;
}
for(int i = 1; i <= n; i++)
{
c[i] = c[i-1]+d[i];
cout<<h+c[i]<<'\n';
}
return 0;
}

class pair:
pass

n,p,h,m=input().split()
n=int(n)
h=int(h)
p=int(p)
m=int(m)
dict={}
array=[]
for i in range(0,n+1):
array.append(0)

for i in range(0,m):
a,b=input().split()
if (int(a) > int(b)):
a, b = b, a

if(a+b in dict.keys()):continue
#print(a+" "+b)
dict[a + b] = 1

a=int(a)
b=int(b)
array[a+1]-=1
array[b]+=1

sum=[]
for i in range(0,n+1):
sum.append(0)
for i in range(1,n+1):
sum[i]=sum[i-1]+array[i]
print(h+sum[i])

 

举报

相关推荐

0 条评论