身高
文件名tallest.c/cpp/pas 时间限制1s 内存限制128M
题目描述
N个士兵排成一排(从1到N标号)。最高的士兵(之一)标号为I,身高为H。YZM提供了R条信息,每条信息描述Ai号士兵能看到Bi号士兵,这意味着Bi号士兵身高大等于Ai号士兵且他们之间的士兵身高小于Ai号士兵。
现在对于N个士兵。依次输出最高的合法身高。
输入格式(输入文件tallest.in)
第一行四个数字N,I,H,R。
接下来R行,每行两个数字,Ai,Bi。
输出格式(输出文件tallest.out)
N行,每行一个数字,按标号依次输出最高的合法身高。
样例数据
Input
9 3 5 5
1 3
5 3
4 3
扫描二维码关注公众号,回复:
1055668 查看本文章
3 7
9 8
output
5
4
5
3
4
4
5
5
5
数据规模
1<=N<=100000
0<=R<=10000
------------------------------------------------------------------
先假设所有人高度为H。当A看向B时,显然A==B最优,因为要保证A~B<A&B, 所以A~B之间减一
所以只要支持区间修改,单点查询即可
因为只在最后查询,可以直接差分。必须判重,因为他已经满足,不需要减啦
#include <cstdio> #include <algorithm> using namespace std; const int N=1e5+7; struct edg { int u,v; }e[N]; int tr[N]; int n,I,h,m; bool cmp(edg a, edg b) {return a.u==b.u?a.v<b.v:a.u<b.u;} int main() { freopen("tallest.in","r",stdin); freopen("tallest.out","w",stdout); scanf("%d%d%d%d",&n,&I,&h,&m); for (int i=1,a,b;i<=m;i++) { scanf("%d%d",&a,&b); if (a>b) swap(a,b); e[i].u=a;e[i].v=b; } sort(e+1,e+m+1,cmp); for (int i=1;i<=m;i++) { int a=e[i].u,b=e[i].v; if (a==e[i-1].u && b==e[i-1].v) continue; tr[a+1]--;tr[b]++; } for (int i=1;i<=n;i++) { tr[i]+=tr[i-1]; printf("%d\n",h+tr[i]); } }