2013
11-11

Before bridges were common, ferries were used to transport cars across rivers. River ferries, unlike their larger cousins, run on a guide line and are powered by the river’s current. Cars drive onto the ferry from one end, the ferry crosses the river, and the cars exit from the other end of the ferry.

There is a ferry across the river that can take n cars across the river in t minutes and return in t minutes. m cars arrive at the ferry terminal by a given schedule. What is the earliest time that all the cars can be transported across the river? What is the minimum number of trips that the operator must make to deliver all cars by that time?

The first line of input contains c, the number of test cases. Each test case begins with n, t, m. m lines follow, each giving the arrival time for a car (in minutes since the beginning of the day). The operator can run the ferry whenever he or she wishes, but can take only the cars that have arrived up to that time.

For each test case, output a single line with two integers: the time, in minutes since the beginning of the day, when the last car is delivered to the other side of the river, and the minimum number of trips made by the ferry to carry the cars within that time.

You may assume that 0 < n, t, m < 1440. The arrival times for each test case are in non-decreasing order.

2
2 10 10
0
10
20
30
40
50
60
70
80
90
2 10 3
10
30
40


100 5
50 2


//* @author 洪晓鹏<hongxp11@163.com>
import java.util.Scanner;

public class Main {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int num = in.nextInt();
for(int i = 0; i< num; i++)
{
int n = in.nextInt();
int t = in.nextInt();
int m = in.nextInt();
int[] time = new int[m];
int starttime = 0;
for(int j = 0; j< m; j++)
{
time[j] = in.nextInt();
}
if(m % n == 0)
{
starttime = time[n-1];
for(int k = 1; k< m/n; k++)
{
if(time[n*(k+1)-1] > starttime + 2*t)
starttime = time[n*(k+1)-1];
else
starttime += 2*t;

}
}
else
{
starttime = time[m%n -1];
int p = m/n;
int index = m%n -1;
for(int k = 0; k< p; k++)
{
if(time[n*(k+1)+ index] > starttime + 2*t)
starttime = time[n*(k+1)+ index];
else
starttime += 2*t;
}
}
int result = (int)Math.ceil(1.0*m/n);
System.out.println(starttime+t+" "+ result);
}
}

}

1. 约瑟夫也用说这么长……很成熟的一个问题了，分治的方法解起来o(n)就可以了，有兴趣可以看看具体数学的第一章，关于约瑟夫问题推导出了一系列的结论，很漂亮

2. 在方法1里面：

//遍历所有的边，计算入度
for(int i=0; i<V; i++)
{
degree = 0;
{
degree[*j]++;
}
}

为什么每遍历一条链表，要首先将每个链表头的顶点的入度置为0呢？
比如顶点5，若在顶点1、2、3、4的链表中出现过顶点5，那么要增加顶点5的入度，但是在遍历顶点5的链表时，又将顶点5的入度置为0了，那之前的从顶点1234到顶点5的边不是都没了吗？

3. 学算法中的数据结构学到一定程度会乐此不疲的，比如其中的2－3树，类似的红黑树，我甚至可以自己写个逻辑文件系统结构来。

4. #include <cstdio>

int main() {