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 ...
随机推荐
- 浅谈js原型
前言 突发奇想,想写一篇原型的文章,也可能是因为对原型有更深的理解吧,在这里做个记录,来记录下自己的理解加深下记忆. 总之,希望本文的内容能够对您的学习或者工作有所帮助.另,如果有任何的错误或者不足请 ...
- AddDbContext was called with configuration, but the context type 'MyDBContext' only declares a parameterless constructor
System.ArgumentException HResult=0x80070057 Message=AddDbContext was called with configuration, but ...
- Flask g 对象
1.什么是g对象? 在 flask 中,有一个专门用来存储用户信息的 g 对象,g的全称的为global. g 对象在一次请求中的所有的代码的地方,都是可以使用的. 赋值方式 from flask i ...
- Asp.Net Core入门之自定义服务注册
谈到服务注册,首先我们先了解一下服务注册时使用的三种方式,也代表了不同的服务生命周期: AddTransient AddScoped AddSingleton AddSingleton生命周期最长,其 ...
- mysql域名解析引起的远程访问过慢?
MYSQL远程连接速度慢的解决方法 PHP远程连接MYSQL速度慢,有时远程连接到MYSQL用时4-20秒不等,本地连接MYSQL正常,出现这种问题的主要原因是, 默认安装的MYSQL开启了DNS的反 ...
- KVM的web
如果报错!就如下操作 [root@kvm images]# chmod +x /etc/rc.local [root@kvm images]# chmod +x /etc/rc.d/rc.local ...
- C Primer Plus(三)
重读C Primer Plus ,查漏补缺 重读C Primer Plus,记录遗漏的.未掌握的.不清楚的知识点 文件输入/输出 1.fgets函数在读取文件内容时会将换行符读入,但gets不会,fp ...
- 如何获取Apollo上项目下的所有namespace?
背景 项目配置迁移到Apollo之后,通过统一的配置管理及配置监听使得项目配置修改的成本大大降低. 但是,在使用Apollo的过程中,强哥也遇到一个问题:如果我们要获取Apollo下的namespac ...
- cb38a_c++_STL_算法_transform
cb38a_c++_STL_算法_transformtransform()算法有两种形式:transform(b1,e1,b2,op);//b1(源区间)的数据通过op函数处理,存放在b2(目标区间) ...
- Ray射线检测和Recources.Load
记录射线检测常用的方法,以及Rocources.Load的常用用法 使用代码实现鼠标点击在鼠标点击处生成制定gameObject RayCastHit hit; void Update() { Ray ...