CSUOJ Water Drinking
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
1
0 1
5
0 2
0 1
1 4
2 3
3 5
5
1 3
0 2
0 1
0 4
4 5
Sample Output
1
4
2
Hint
思路:两个数组,一个指向头,另一个维护后面数字的指向
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
#define MAXN 100000
int 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
**********************************************************************/
CSUOJ Water Drinking的更多相关文章
- csu1010: Water Drinking
/* 本题的题意: 沙漠中有很多骆驼和一个池塘,0表示池塘,1-N表示骆驼,输入的两个数表示两只骆驼,其中前面的那一头靠近池塘,所有的骆驼队列不交叉不相连,求站在队尾但是离水井最近的骆驼编号 经过分析 ...
- Taking water into exams could boost grades 考试带瓶水可以提高成绩?
Takeing a bottle of water into the exam hall could help students boost their grades, researchers cla ...
- Hihocoder #1095 : HIHO Drinking Game (微软苏州校招笔试)( *【二分搜索最优解】)
#1095 : HIHO Drinking Game 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi and Little Ho are playin ...
- [LeetCode] Pacific Atlantic Water Flow 太平洋大西洋水流
Given an m x n matrix of non-negative integers representing the height of each unit cell in a contin ...
- [LeetCode] Trapping Rain Water II 收集雨水之二
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...
- [LeetCode] Water and Jug Problem 水罐问题
You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...
- [LeetCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LeetCode] Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
随机推荐
- IIS并发连接数及性能优化
如果要查看IIS连接数,最简单方便的方法是通过“网站统计”来查看,“网站统计”的当前在线人数可以认为是当前IIS连接数.然而,“网站统计”的当前在线人数统计时间较长,一般为10分钟或15分钟,再加上统 ...
- activiti复盘重推的一种简单实现方式:
activiti复盘重推的一种简单实现方式: 设置流程的每一步让用户选择,比如一共有6步完成,用户选择从第4步开始复盘重推,那么把原来的推演oldId和4传到后台, 首先,后台生成一个新的推演id n ...
- python学习(28) 浅谈可变对象的单例模式设计
python开发,有时候需要设计单例模式保证操作的唯一性和安全性.理论上python语言底层实现和C/C++不同,python采取的是引用模式,当一个对象是可变对象,对其修改不会更改引用的指向,当一个 ...
- 深入理解JS this,作用域
深入理解JS this 阮一峰博客链接http://www.ruanyifeng.com/blog/2010/04/using_this_keyword_in_javascript.html this ...
- spring boot 事务配置
事务的作用这里不细说,相信很多人也在工作中使用过. 那么在spring-boot是如何配置事务的,事实上非常简便. 直接贴代码吧. 首先配置数据源 myqlDataSource,这个就不说了.之前的 ...
- MySql与对应的Java的时间类型
MySql的时间类型有 Java中与之对应的时间类型date java.sql.Date Date ...
- 论文中“but”与“however”的区别
- MongoDB-3.4搭建副本集
搭建副本集 1:首先创建3台虚拟机作为配置环境 IP1:192.168.101.175 IP2:192.168.101.176 IP3:192.168.101.177 2.下载MongoDB 3.4版 ...
- 跳过复制错误——slave_skip_errors、slave_exec_mode
这一篇写写复制错误处理相关的另两个参数slave_skip_errors.slave_exec_mode,基本环境参考<复制错误处理——sql_slave_skip_counter> 一. ...
- 【工具】用命令行与Python使用YARA规则
1.前言 YARA是一款旨在帮助恶意软件研究人员识别和分类恶意软件样本的开源工具,使用YARA可以基于文本或二进制模式创建恶意软件家族描述与匹配信息.现在已经被多家公司所运用于自身的产品. 2.YAR ...