NC25044. [USACO 2007 Jan S]Tallest Cow
描述
输入描述
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)