题目

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实现第四届蓝桥杯三部排序

    三部排序 题目描述 一般的排序有许多经典算法,如快速排序.希尔排序等. 但实际应用时,经常会或多或少有一些特殊的要求.我们没必要套用那些经典算法,可以根据实际情况建立更好的解法. 比如,对一个整型数组 ...

  2. 基于Nginx实现访问控制、连接限制

    0 前言 Nginx自带的模块支持对并发请求数进行限制, 还有对请求来源进行限制.可以用来防止DDOS攻击.阅读本文须知道nginx的配置文件结构和语法. 1. 默认配置语法 nginx.conf作为 ...

  3. (十二)DVWA全等级SQL Injection(Blind)盲注--SQLMap测试过程解析

    一.测试前分析 前文<DVWA全等级SQL Injection(Blind)盲注-手工测试过程解析> 通过手工测试的方式详细分析了SQL Injection(Blind)盲注漏洞的利用过程 ...

  4. 20184302 2019-2020-2 《Python程序设计》实验四报告

    20184302 2019-2020-2 <Python程序设计>实验四报告 课程:<Python程序设计> 班级: 1843 姓名: 李新锐 学号:184302 实验教师:王 ...

  5. Logstash下字段以及嵌套Json字段类型转换

    前言 从filebeat传输到Logstash的数据,某个字段需要由string类型装换成float类型.但是不管怎么改logstash的配置文件都不生效,其实官方文档都有,但是具体细节方面的东西就得 ...

  6. router路由配置

    vue项目中router路由配置   介绍 路由:控制组件之间的跳转,不会实现请求.不用页面刷新,直接跳转-切换组件>>> 安装 本地环境安装路由插件vue-router:    c ...

  7. Unit2-窝窝牌电梯

    全文共2329字,推荐阅读时间10~15分钟. 文章共分四个部分: 作业分析 评测相关 重构策略 课程体验感受 作业分析 Unit2要求我们模拟现实生活中的电梯调度情景,迭代路径是单电梯->多电 ...

  8. (七)logback 异步输出日志

    <!-- 异步输出 --> <appender name="ASYNC-INFO" class="ch.qos.logback.classic.Asyn ...

  9. 用了那么多年的 Master 分支或因种族歧视而成为历史?

    最近真的是活久见了...不知道你是否也有碰到之前Fork过的国外开源项目,最近突然崩了,原因居然是好多项目都把master分支改为了main分支!更可怕的是修改原因居然是涉及种族歧视.用了那么多年的m ...

  10. cute-cnblogs 一期样式原文

    cute-cnblogs 说明 "我经常有那种感觉,如果这个事情来了,你却没有勇敢地去解决掉,它一定会再来.生活真是这样,它会一次次地让你去做这个功课直到你学会为止." -- &l ...