sorting (java大数+自定义排序+刷题)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_41061455/article/details/82347126

Sorting

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1155    Accepted Submission(s): 312

Problem Description

Bobo has n tuples (a1,b1,c1),(a2,b2,c2),…,(an,bn,cn).
He would like to find the lexicographically smallest permutation p1,p2,…,pn of 1,2,…,n such that for i∈{2,3,…,n} it holds that

api−1+bpi−1api−1+bpi−1+cpi−1≤api+bpiapi+bpi+cpi.

Input

The input consists of several test cases and is terminated by end-of-file.
The first line of each test case contains an integer n.
The i-th of the following n lines contains 3 integers ai, bi and ci.

Output

For each test case, print n integers p1,p2,…,pn seperated by spaces.
DO NOT print trailing spaces.
## Constraint
* 1≤n≤103
* 1≤ai,bi,ci≤2×109
* The sum of n does not exceed 104.

Sample Input

2

1 1 1

1 1 2

2

1 1 2

1 1 1

3

1 3 1

2 2 1

3 1 1

Sample Output

2 1

1 2

1 2 3

Source

CCPC2018-湖南全国邀请赛-重现赛(感谢湘潭大学)

遇到的问题:

c++大数乘法忘记了,没带模板,高精度不知道有long double,java没学,精度卡到爆....

解决方法:

这题的解法很3种.分式化简,避免long long溢出.c++大数模拟.java大整数.

这题旨在练习java大数+自定义排序用法.

//package ldm;
import java.util.*;
import java.math.*;
class student implements Comparable{
	long a;
	long b;
	long c;
	int index;
	public student(int index,long a,long b,long c) {
		this.index=index;
		this.a=a;
		this.b=b;
		this.c=c;
	}
	public int compareTo(Object o) {
		student t=(student)o;
		long x1=this.a+this.b;
		long x2=t.a+t.b;
		long y1=x1+this.c;
		long y2=x2+t.c;
		BigInteger x=BigInteger.valueOf(x1);
		x=x.multiply(BigInteger.valueOf(y2));
		BigInteger y=BigInteger.valueOf(x2);
		y=y.multiply(BigInteger.valueOf(y1));
		return x.compareTo(y);
	}
}
public class Main {

	public static void main(String[] args) {
		Scanner cin=new Scanner(System.in);
		while(cin.hasNext()) {
			int n=cin.nextInt();
			List<student>list=new ArrayList<>();
			for(int i=0;i<n;i++) {
				list.add(new student(i,cin.nextLong(),cin.nextLong(),cin.nextLong()));
			}
			Collections.sort(list);
			Iterator<student>it=list.iterator();
			boolean flag=true;
			while(it.hasNext()) {
				student t=it.next();
				int index=t.index+1;
				if(flag) {
					System.out.print(index);
					flag=false;
				}
				else System.out.print(" "+index);
			}
			System.out.println();	
		}
	}

}

猜你喜欢

转载自blog.csdn.net/qq_41061455/article/details/82347126
今日推荐