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 <= ...
随机推荐
- 【CF771A】Bear and Friendship Condition
题目大意:给定一张无向图,要求如果 A 与 B 之间有边,B 与 C 之间有边,那么 A 与 C 之间也需要有边.问这张图是否满足要求. 题解:根据以上性质,即:A 与 B 有关系,B 与 C 有关系 ...
- div+css感悟
div+css感觉很简单,可是真正做起来一些小细节把握不好,这个网页的布局也是完成不了的.今天学习了一些技巧方法现在分享下: 即一个原则,网页由一个个的大盒子组成,一个个的大盒子里面装着一个个的小盒子 ...
- SQL Server深入理解“锁”机制
相比于 SQL Server 2005(比如快照隔离和改进的锁与死锁监视),SQL Server 2008 并没有在锁的行为和特性上做出任何重大改变.SQL Server 2008 引入的一个主要新特 ...
- 「Django」浏览+1的操作
适应于网页.文章等浏览次数统计 1.Models设置:添加viewed方法 class NewsTitle(models.Model): title = models.CharField(max_le ...
- Linux命令(四)文件传输
1. 将当前目录下的test1.txt文件,复制到 /tmp/Douzi目录下,命名为test1.py 2. 将服务器/tmp/Douzi/hello.py复制到本地上,重命名为 hello_Do ...
- Apache Oozie Coordinator 作业自定义配置定时任务
一,介绍 Oozie是Hadoop的工作流系统,如果使用Oozie来提交MapReduce作业(Oozie 不仅仅支持MapReduce作业,还支持其他类型的作业),可以借助Oozie Coordin ...
- CSS3 响应式布局: @media (min/max-width:***) @font-face
响应式布局 responsive design @media 属性 bootstrap css 分析: @media (min-width:768px){ body{***} } use @medi ...
- python所有基础
下面就不一一列举了,所有的资料都和GitHub对接,到时候我有更新就直接拖到GitHub上面了.入门的小伙伴们可以进来看看,估计后面还会有很多项目,待更新.
- 第八集:魔法阵 NTT求循环卷积
题目来源:http://www.fjutacm.com/Problem.jsp?pid=3283 题意:给两串长度为n的数组a和b,视为环,a和b可以在任意位置开始互相匹配得到这个函数的值,求这个函数 ...
- Linux 定时器应用【转】
Linux 定时器应用 实验目的 阅读 Linux 相关源代码,学习 Linux 系统中的时钟和定时器原理,即,ITIMER_REAL实时计数,ITIMER_VIRTUAL 统计进程在用户模式执行的时 ...