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 ...
随机推荐
- vue+js清除定时器
注意data数据里面一定要定义Timeout Timeout:Function,//定时器 methods里面 moseovefalse(){//需要执行的方法 var that=this; that ...
- 一篇文章快速入门React框架
视频教程 本文章在B站配有视频教程 课程目标 了解最常用的React概念和相关术语,例如JSX,组件,属性(Props),状态(state). 构建一个非常简单的React应用程序,以阐述上述概念. ...
- STL常用序列容器
这里简要的记述一下STL常用容器的实现原理,要点等内容. vector vector是比较常用的stl容器,用法与数组是非类似,其内部实现是连续空间分配,与数组的不同之处在于可弹性增加空间,而arra ...
- 终于我用JOL打破了你对java对象的所有想象
目录 简介 JOL简介 使用JOL分析VM信息 使用JOL分析String 使用JOL分析数组 使用JOL分析自动装箱 使用JOL分析引用关系 总结 简介 使用面向对象的编程语言的好处就是,虽然没有女 ...
- 小师妹学JavaIO之:NIO中Channel的妙用
目录 简介 Channel的分类 FileChannel Selector和Channel DatagramChannel SocketChannel ServerSocketChannel Asyn ...
- 【转载】图解NumPy
目录 1. 读写文件 2. 内建向量/矩阵 3. 切片操作 4. 聚合函数 4.1. 向量 4.2. 矩阵 5. 矩阵的转置和重构 6. 常用操作API 7. 应用实例 7.1. 生成向量.矩阵 7. ...
- Express4.x之API:express
express()表达式 express的方法 express功能分析 一.express()表达式 创建Express应用程序.express()函数是express模块导出的顶级函数.(相当于HT ...
- [每日一题2020.06.09] leetcode #97 交错字符串 dp
题目链接 利用动态规划的思想, 对于每种状态(i, j)来说都有(i-1, j) 和 (i,j-1) 需要注意的问题 : 初始化的问题,先把i=0和j=0的状态都初始化后才可以进行dp否则发生数组越界 ...
- (六)TestNg中的软断言和硬断言
原文链接:https://cloud.tencent.com/developer/article/1479172 前言 在执行自动化测试脚本的时候,我们需要自动判断测试脚本执行完成后的实际结果是否与预 ...
- numpy中transpose的功能
看了网上一堆解释,有用相互交换来解释的,我看了半天也看不出所以然来.心想着自己试验一下. numpy.transpose的用法很简单:假如你有一个四维的数组,那么四个维度就是0,1,2,3.风格会像下 ...