题目

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 题解的更多相关文章

  1. poj 3263 Tallest Cow(线段树)

    Language: Default Tallest Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1964   Ac ...

  2. 【差分】POJ 3263 Tallest Cow

    题目大意 POJ链接 给出\(n\)头牛的身高,和\(m\)对关系,表示牛\(a[i]\)与\(b[i]\)可以相互看见.已知最高的牛为第\(p\)头,身高为\(h\). 求每头牛的身高最大可能是多少 ...

  3. poj 3263 Tallest Cow

    一个压了很久的题目,确实很难想,看了别人的做法后总算明白了. 首先要明白一点,因为题目说明了不会有矛盾,所以题目给出来的区间是不能相交的,否则是矛盾的.(原因自己想) 然后既然区间只能是包含的,就很明 ...

  4. 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 ...

  5. POJ 3617 Best Cow Line(最佳奶牛队伍)

    POJ 3617 Best Cow Line Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] FJ is about to t ...

  6. BZOJ1635: [Usaco2007 Jan]Tallest Cow 最高的牛

    1635: [Usaco2007 Jan]Tallest Cow 最高的牛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 346  Solved: 184 ...

  7. BZOJ 1635: [Usaco2007 Jan]Tallest Cow 最高的牛

    题目 1635: [Usaco2007 Jan]Tallest Cow 最高的牛 Time Limit: 5 Sec  Memory Limit: 64 MB Description FJ's N ( ...

  8. 1635: [Usaco2007 Jan]Tallest Cow 最高的牛

    1635: [Usaco2007 Jan]Tallest Cow 最高的牛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 383  Solved: 211 ...

  9. poj 3264 Balanced Lineup 题解

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Subm ...

随机推荐

  1. java实现第四届蓝桥杯核桃的数量

    核桃的数量 题目描述 小张是软件项目经理,他带领3个开发组.工期紧,今天都在加班呢.为鼓舞士气,小张打算给每个组发一袋核桃(据传言能补脑).他的要求是: 各组的核桃数量必须相同 各组内必须能平分核桃( ...

  2. xmake v2.3.4 发布, 更加完善的工具链支持

    为了让xmake更好得支持交叉编译,这个版本我重构了整个工具链,使得工具链的切换更加的方便快捷,并且现在用户可以很方便地在xmake.lua中扩展自己的工具链. 关于平台的支持上,我们新增了对*BSD ...

  3. Cypress系列(16)- 查找页面元素的基本方法

    如果想从头学起Cypress,可以看下面的系列文章哦 https://www.cnblogs.com/poloyy/category/1768839.html 前端页面代码 后面写的 Cypress ...

  4. HTTP协议浅析(一)

    先来看看百度百科对HTTP的解释 http是一个简单的请求-响应协议,它通常运行在TCP之上.它指定了客户端可能发送给服务器什么样的消息以及得到什么样的响应.请求和响应消息的头以ASCII码形式给出: ...

  5. C++中类成员的访问控制

    结论 首先给出结论,请看下图,看图说话最容易理解了. 类眼中的自己 类中定义的所有成员,不论是以public, protected还是private修饰,对类自身而言,它们都是可见的. 对象眼中的类 ...

  6. AJAX的GET请求、POST请求

    感谢:链接(视频讲解很详细) AJAX(Asynchronous Javascript and XML):异步的JavaScript和XML(不需要刷新网页就可以更新网页数据) XML:百度百科 是一 ...

  7. (十)HttpClient以multipart/form-data上传文件

    原文链接:https://blog.csdn.net/wsdtq123/article/details/78888734 POST上传文件 最早的HTTP POST是不支持文件上传的,给编程开发带来很 ...

  8. 基于docker-compose搭建gitlab

    安装及配置 修改docker-compose文件 vim docker-compose.yml gitlab: image: 'gitlab/gitlab-ce:latest' restart: al ...

  9. VS Code WebApi系列——3、发布

    上两篇已经实现了WebApi及基于jwt的Token设置,那么功能做完了,该发布WebApi了.为什么要对发布进行一下说明呢,因为是基于vscode和.netcore的发布,所以可能会遇到莫名奇妙的问 ...

  10. ca12a_c++顺序容器的操作5_访问容器中的数据元素

    ca12a_c++顺序容器的操作5_访问容器中的数据元素访问元素:c.back()..显示最后一个数据c.front() ..显示第一个数据c[n],n就是下标,适合vector与dequec.at( ...