列表

详情


NC25044. [USACO 2007 Jan S]Tallest Cow

描述

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

输出:

5
4
5
3
4
4
5
5
5

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 29ms, 内存消耗: 20480K, 提交时间: 2021-09-05 10:47:46

#include<bits/stdc++.h>
using namespace std;
int n,p,h,m,sz[10005];
bool pd[10005][10005];
int main(){
	cin>>n>>p>>h>>m;
	sz[0]=h;
	while(m--){
		int a,b;
		cin>>a>>b;
		if(a>b)swap(a,b);
		if(pd[a][b])continue;
		pd[a][b]=1;
		sz[a+1]--;
		sz[b]++;
	}
	int de=sz[0];
	for(int i=1;i<=n;i++){
		de+=sz[i];
		cout<<de<<endl;
	}
	return 0;
}

Python3 解法, 执行用时: 99ms, 内存消耗: 5940K, 提交时间: 2023-06-29 22:43:45

N, I, H, R = map(int,input().split())
arr=set()
for i in range(R):
    arr.add(tuple(sorted(list(map(int,input().split())))))
res=[0]*(1+N) 
for x,y in arr:
    res[x+1]-=1
    res[y]+=1
# print(res)
# print(arr)
ans=[0]*(1+N)
ans[I]=H
for i in range(1,N+1):
    ans[i]=ans[i-1]+res[i]
    print(ans[i]+H)
    

上一题