POJ 3263 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\).
题解
首先假设每头牛都和最高的牛一样高, 然后输入两个数\(a,b\), \(a,b\)之间的牛都比\(a,b\)低, 但由于要每头牛尽可能高, 所以都是低\(1\), 用差分就能完成,\(a+1\)的位置\(-1\),\(b\)的位置\(+1\), 输出的时候, 输出前缀和加上最高牛的高度即可.
有一点要注意, 如果一个相同的区间输入两次, 就会多计算一次, \(a,b\)之间的牛高度就会\(-2\), 所以要排序去重, 但是奇怪的是, 如果你不排序, 光去重, 也能A
比如这个代码:
#include <cstdio>
int N, H, R, cow[100005], p[100005][2];
int main() {
int a, b;
scanf("%d%*d%d%d", &N, &H, &R);
for (int i = 0; i < R; i++) {
scanf("%d%d", &a, &b);
p[i][0] = (a > b) ? b : a, p[i][1] = (a > b) ? a : b;
}
for (int i = 0; i < R; i++)
if (!i || p[i][0] != p[i - 1][0] || p[i][1] != p[i - 1][1])
cow[p[i][0] + 1]--, cow[p[i][1]]++;
for (int i = 1, d = 0; i <= N; i++) printf("%d\n", (d += cow[i]) + H);
return 0;
}
但是如果不排序, 不去重又会WA, 所以只能理解为输入数据把相同的都放在了一起...
代码
加了排序的正解
#include <cstdio>
#include <algorithm>
using namespace std;
int N, H, R, cow[100005];
pair<int, int> p[100005];
int main() {
int a, b;
scanf("%d%*d%d%d", &N, &H, &R);
for (int i = 0; i < R; i++) {
scanf("%d%d", &a, &b);
p[i].first = (a > b) ? b : a, p[i].second = (a > b) ? a : b;
}
sort(p, p + R);
for (int i = 0; i < R; i++)
if (!i || p[i].first != p[i - 1].first || p[i].second != p[i - 1].second)
cow[p[i].first + 1]--, cow[p[i].second]++;
for (int i = 1, d = 0; i <= N; i++)
printf("%d\n", ( d += cow[i] ) + H);
return 0;
}
POJ 3263 Tallest Cow 题解的更多相关文章
- poj 3263 Tallest Cow(线段树)
Language: Default Tallest Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1964 Ac ...
- 【差分】POJ 3263 Tallest Cow
题目大意 POJ链接 给出\(n\)头牛的身高,和\(m\)对关系,表示牛\(a[i]\)与\(b[i]\)可以相互看见.已知最高的牛为第\(p\)头,身高为\(h\). 求每头牛的身高最大可能是多少 ...
- poj 3263 Tallest Cow
一个压了很久的题目,确实很难想,看了别人的做法后总算明白了. 首先要明白一点,因为题目说明了不会有矛盾,所以题目给出来的区间是不能相交的,否则是矛盾的.(原因自己想) 然后既然区间只能是包含的,就很明 ...
- Tallest Cow POJ - 3263 (区间点修改)
FJ's N (1 ≤ N ≤ 10,000) cows conveniently indexed 1..N are standing in a line. Each cow has a positi ...
- POJ 3617 Best Cow Line(最佳奶牛队伍)
POJ 3617 Best Cow Line Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] FJ is about to t ...
- BZOJ1635: [Usaco2007 Jan]Tallest Cow 最高的牛
1635: [Usaco2007 Jan]Tallest Cow 最高的牛 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 346 Solved: 184 ...
- BZOJ 1635: [Usaco2007 Jan]Tallest Cow 最高的牛
题目 1635: [Usaco2007 Jan]Tallest Cow 最高的牛 Time Limit: 5 Sec Memory Limit: 64 MB Description FJ's N ( ...
- 1635: [Usaco2007 Jan]Tallest Cow 最高的牛
1635: [Usaco2007 Jan]Tallest Cow 最高的牛 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 383 Solved: 211 ...
- poj 3264 Balanced Lineup 题解
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Subm ...
随机推荐
- UVIYN MMDVM充电宝支持APRS与 YSF
需求就是要在APRS地图上显示对讲机位置 1.打开pi-star首页链接配置的专家(EXPERT)设置 下面链接快速打开 http://ip/admin/expert/edit_ysfgateway. ...
- (二)CRLF注入
01 漏洞描述 在<HTTP | HTTP报文>一文中,我们介绍了HTTP报文的结构:状态行和首部中的每行以CRLF结束,首部与主体之间由一空行分隔.或者理解为首部最后一个字段有两个CRL ...
- 给Linux小白的CentOS8.1基本安装说明
写在前面的话:用过Linux的同学应该都会觉得Linux安装是件非常简单的事情,根本不值得用博客记下来!但是我发现,其实没接触过Linux的同学还真不一定会装,就像在IT行业工作过几年但一直用Wind ...
- mysql中drop、delete、truncate的区别简述
一.区别 1.去什么? truncate table 和 delete只删除数据(记录)不删除表的结构;drop语句将删除表的数据(记录)和表结构依赖的约束(constrain),触发器(trigge ...
- python3 源码阅读-虚拟机运行原理
阅读源码版本python 3.8.3 参考书籍<<Python源码剖析>> 参考书籍<<Python学习手册 第4版>> 官网文档目录介绍 Doc目录主 ...
- ubuntu安装ssh服务器
1.安装 sudo apt-get install openssh-server 2.配置文件路径 / etc/ssh/sshd_config 3.操作 sudo /etc/init.d/ssh st ...
- 【javascript的那些事】等待加载完js后执行方法
很多时候,你也许会碰到 使用的情景: js文件b.js还没有从服务器端加载到web端,而吧a.js中已经调用了b.js中的方法 实例: 这里是加载echart的时候碰到的具体实例 引入js " ...
- Tensorflow从0到1(4)之神经网络
一维数据集上的神经网络 # 1 引入包,创建会话 import tensorflow as tf import numpy as np sess = tf.Session() # 2 初始化数据 da ...
- pycharm安装破解方法
1.pycharm专业版官方下载链接:http://www.jetbrains.com/pycharm/download/#section=windows正常下载并安装 2.从https://gith ...
- SpringMVC的url-pattern配置及原理剖析
SpringMVC的url-pattern配置及原理剖析 xml里面配置标签: <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc./ ...