第四周周末总结

这两天感觉一直在打酱油,好几个题做了就是不对,唉。

1、Train Seats Reservation

You are given a list of train stations, say from the station 111 to the station 100100100.

The passengers can order several tickets from one station to another before the train leaves the station one. We will issue one train from the station 111 to the station 100100100 after all reservations have been made. Write a program to determine the minimum number of seats required for all passengers so that all reservations are satisfied without any conflict.

Note that one single seat can be used by several passengers as long as there are no conflicts between them. For example, a passenger from station 111 to station 101010 can share a seat with another passenger from station 303030 to 606060.

Input Format

Several sets of ticket reservations. The inputs are a list of integers. Within each set, the first integer (in a single line) represents the number of orders, nnn, which can be as large as 100010001000. After nnn, there will be nnn lines representing the nnn reservations; each line contains three integers s,t,ks, t, ks,t,k, which means that the reservation needs kkk seats from the station sss to the station ttt .These ticket reservations occur repetitively in the input as the pattern described above. An integer n=0n = 0n=0 (zero) signifies the end of input.

Output Format

For each set of ticket reservations appeared in the input, calculate the minimum number of seats required so that all reservations are satisfied without conflicts. Output a single star '*' to signify the end of outputs.

样例输入
2
1 10 8
20 50 20
3
2 30 5
20 80 20
40 90 40
0
样例输出
20
60
*

题意:就是有人预定A站—B站的座位,然后问你同时预定的最大座位数,注意在边缘的情况比如有1-5站的预定,而5-10站的预定是不跟1-5重合的。

import java.util.Scanner;
public class Main
	{
	public static void main(String[] args)	
		{
		Scanner cin=new Scanner(System.in);
		int n,a,b,c;
		int i,j;
		int[] x=new int[101];
		while(1!=2)
			{
			n=cin.nextInt();
			if(n==0)
				{
				System.out.println("*");
				break;
				}
			for(i=0;i<101;i++)
				x[i]=0;
			while(n--!=0)
				{
				a=cin.nextInt();
				b=cin.nextInt();
				c=cin.nextInt();
				for(i=a;i<b;i++)
					x[i]+=c;
				}
			int max=0;
			for(j=0;j<101;j++)
				if(x[j]>max)
					max=x[j];
			System.out.println(max);
			}
		}
	}

2、Bounce

For Argo, it is very interesting watching a circle bouncing in a rectangle.

As shown in the figure below, the rectangle is divided into N×M grids, and the circle fits exactly one grid.

The bouncing rule is simple:

1. The circle always starts from the left upper corner and moves towards lower right.

2. If the circle touches any edge of the rectangle, it will bounce.

3. If the circle reaches any corner of the rectangle after starting, it will stop there.


Argo wants to know how many grids the circle will go through only once until it first reaches another corner. Can you help him?

输入

The input consists of multiple test cases. (Up to 105)

For each test case:

One line contains two integers N and M, indicating the number of rows and columns of the rectangle. (2 ≤ N, M ≤ 109)

输出

For each test case, output one line containing one integer, the number of grids that the circle will go through exactly once until it stops (the starting grid and the ending grid also count).

样例输入
2 2
2 3
3 4
3 5
4 5
4 6
4 7
5 6
5 7
9 15
样例输出
2
3
5
5
7
8
7
9
11

39

题意:小球从左上角往右下角弹出,然后经过弹射,最后弹到一个角的时候结束,问不重复走过的格子有多少个。

这道题一看就是找规律,然而推了半天还是没有推出来,昨天看大佬们的博客,简简单单就推出来了,那么复杂的式子随便就推出了了......

import java.util.Scanner;
public class Main
	{
	public static long gcd(long a, long b)
		{
	    return b==0?a:gcd(b,a%b);
		}
	public static long lcm(long a, long b)
		{
	    return a*b/gcd(a,b);
		}
	public static void main(String[] args)
		{
		Scanner cin=new Scanner(System.in);
		long n,m,l,a,b;
		while(cin.hasNext())
			{
			n=cin.nextLong();
			m=cin.nextLong();
			l=lcm(n-1,m-1);
			a=lcm(n-1,m-1)/(m-1);
			b=lcm(n-1,m-1)/(n-1);
			System.out.println((l-a*b+a+b));
			}
		}
	}

3、Overlapping Rectangles

There are nnn rectangles on the plane. The problem is to find the area of the union of these rectangles. Note that these rectangles might overlap with each other, and the overlapped areas of these rectangles shall not be counted more than once. For example, given a rectangle AAA with the bottom left corner located at (0,0)(0, 0)(0,0) and the top right corner at (2,2)(2, 2)(2,2), and the other rectangle BBB with the bottom left corner located at (1,1)(1,1)(1,1) and the top right corner at (3,3)(3,3)(3,3), it follows that the area of the union of AAA and BBB should be 777, instead of 888.

Although the problem looks simple at the first glance, it might take a while to figure out how to do it correctly. Note that the shape of the union can be very complicated, and the intersected areas can be overlapped by more than two rectangles.

Note:

(1) The coordinates of these rectangles are given in integers. So you do not have to worry about the floating point round-off errors. However, these integers can be as large as 1,000,0001,000,0001,000,000.

(2) To make the problem easier, you do not have to worry about the sum of the areas exceeding the long integer precision. That is, you can assume that the total area does not result in integer overflow.

Input Format

Several sets of rectangles configurations. The inputs are a list of integers. Within each set, the first integer (in a single line) represents the number of rectangles, n, which can be as large as 100010001000. After n, there will be n lines representing the n rectangles; each line contains four integers <a,b,c,d><a, b, c, d><a,b,c,d> , which means that the bottom left corner of the rectangle is located at (a,b)(a, b)(a,b), and the top right corner of the rectangle is located at (c,d)(c, d)(c,d). Note that integers aaa, bbb, ccc, ddd can be as large as 1,000,0001,000,0001,000,000.

These configurations of rectangles occur repetitively in the input as the pattern described above. An integer n=0n = 0n=0 (zero) signifies the end of input.

Output Format

For each set of the rectangles configurations appeared in the input, calculate the total area of the union of the rectangles. Again, these rectangles might overlap each other, and the intersecting areas of these rectangles can only be counted once. Output a single star '*' to signify the end of outputs.

样例输入
2
0 0 2 2
1 1 3 3
3
0 0 1 1
2 2 3 3
4 4 5 5
0
样例输出
7
3
*

题意:求无数矩形组成的面积,重复的面积只计算一次。

这道题刚开始看了觉得还听简单的,因为每个重复的格子只会重复一次,以前做过一个类似的题,不过那个只有两个矩形,然后我把以前得到的结论套上,然后就错了,然后现在看了看别人的题解,是用线段树解决的,找到错误修改了再来贴代码。

从明天开始努力看线段树吧。











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值