博客
关于我
CSUOJ Water Drinking
阅读量:410 次
发布时间:2019-03-06

本文共 2072 字,大约阅读时间需要 6 分钟。

Description

The Happy Desert is full of sands. There is only a kind of animal called camel living on the Happy Desert. Cause they live here, they need water here. Fortunately, they find a pond which is full of water in the east corner of the desert. Though small, but enough. However, they still need to stand in lines to drink the water in the pond.

Now we mark the pond with number 0, and each of the camels with a specific number, starting from 1. And we use a pair of number to show the two adjacent camels, in which the former number is closer to the pond. There may be more than one lines starting from the pond and ending in a certain camel, but each line is always straight and has no forks.

Input

There’re multiple test cases. In each case, there is a number N (1≤N≤100000) followed by N lines of number pairs presenting the proximate two camels. There are 99999 camels at most.

Output

For each test case, output the camel’s number who is standing in the last position of its line but is closest to the pond. If there are more than one answer, output the one with the minimal number.

Sample Input

10 150 20 11 42 33 551 30 20 10 44 5

Sample Output

142

Hint

思路:两个数组,一个指向头,另一个维护后面数字的指向

#include
#include
#include
#include
using namespace std;#define MAXN 100000int nex[MAXN],h[MAXN];int main(){ int t; int n, m; while (~scanf("%d", &t)) { int cnt = 0; int flag = 1; memset(nex, -1, sizeof(nex)); memset(h, 0, sizeof(h)); for (int i = 0; i < t; i++) { scanf("%d%d", &n, &m); if (n == 0) h[cnt++] = m;//当n为0时表示出现另一队,头+1 else nex[n] = m;//否则n的后面为m } int ans = t; while (flag) { for (int i = 0; i < cnt; i++)//每次每队往后移一位 { if (nex[h[i]] == -1) { ans = min(ans, h[i]);//当下一个为-1时表示这队已经到末尾了 flag = 0; } h[i] = nex[h[i]];//移动头指针 } } printf("%d\n", ans); } return 0;}/********************************************************************** Problem: 1010 User: leo6033 Language: C++ Result: AC Time:52 ms Memory:2804 kb**********************************************************************/

转载地址:http://yuwuz.baihongyu.com/

你可能感兴趣的文章
80. Remove Duplicates from Sorted Array II
查看>>
83. Remove Duplicates from Sorted List
查看>>
410. Split Array Largest Sum
查看>>
开源项目在闲鱼、b 站上被倒卖?这是什么骚操作?
查看>>
Vue3发布半年我不学,摸鱼爽歪歪,哎~就是玩儿
查看>>
《实战java高并发程序设计》源码整理及读书笔记
查看>>
Java开源博客My-Blog(SpringBoot+Docker)系列文章
查看>>
程序员视角:鹿晗公布恋情是如何把微博搞炸的?
查看>>
Spring+SpringMVC+MyBatis+easyUI整合进阶篇(七)一次线上Mysql数据库崩溃事故的记录
查看>>
【JavaScript】动态原型模式创建对象 ||为何不能用字面量创建原型对象?
查看>>
ClickHouse源码笔记4:FilterBlockInputStream, 探寻where,having的实现
查看>>
Linux应用-线程操作
查看>>
多态体验,和探索爷爷类指针的多态性
查看>>
系统编程-进程间通信-无名管道
查看>>
记2020年初对SimpleGUI源码的阅读成果
查看>>
C语言实现面向对象方法学的GLib、GObject-初体验
查看>>
系统编程-进程-ps命令、进程调度、优先级翻转、进程状态
查看>>
为什么我觉得需要熟悉vim使用,难道仅仅是为了耍酷?
查看>>
一个支持高网络吞吐量、基于机器性能评分的TCP负载均衡器gobalan
查看>>
HDOJ2017_字符串统计
查看>>