关于java语言写题的总结1.0

关于java语言写题的总结1.0,第1张

关于java语言写题的总结1.0
import java.io.*;

public class Rook{
	
	

	public static void main(String[] args) throws IOException{
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int t = Integer.parseInt(br.readLine());
		while(t-- > 0) {
			int n = Integer.parseInt(br.readLine());
			int smallest = Integer.MAX_VALUE;
			int costSmallest = 0;
			int largest = Integer.MIN_VALUE;
			int costLargest = 0;
			int longest = Integer.MIN_VALUE;
			int costLongest = 0;
			while(n-- > 0) {
				String[] input = br.readLine().split(" ");
				
				if(Integer.parseInt(input[0]) < smallest) {
					smallest = Integer.parseInt(input[0]);
					costSmallest = Integer.parseInt(input[2]);
				} else if(Integer.parseInt(input[0]) == smallest) {
					costSmallest = Math.min(costSmallest, Integer.parseInt(input[2]));
				}
				
				if(Integer.parseInt(input[1]) > largest) {
					largest = Integer.parseInt(input[1]);
					costLargest = Integer.parseInt(input[2]);
				} else if(Integer.parseInt(input[1]) == largest) {
					costLargest = Math.min(costLargest, Integer.parseInt(input[2]));
				}
				
				if(longest < (Integer.parseInt(input[1]) - Integer.parseInt(input[0]) + 1)) {
					longest = Integer.parseInt(input[1]) - Integer.parseInt(input[0]) + 1;
					costLongest = Integer.parseInt(input[2]);
				} else if(longest == (Integer.parseInt(input[1]) - Integer.parseInt(input[0]) + 1)) {
					costLongest = Math.min(costLongest, Integer.parseInt(input[2]));
				}
				
				int answer = costLargest + costSmallest;
				if(longest == largest - smallest + 1) {
					answer = Math.min(answer, costLongest);
				}
				
				System.out.println(answer);
			}
		}
	}

}

这是最近codeforces里比赛的一道题,在比赛中由于一直时间超限,导致没完成,上述的代码来自一位外国的大佬,作为以后做题的模板
总结:
1.在写题的过程中能不用for就不用。
2.输入数据大时,BufferedReader是一个好选择,注意要抓取IOEXCEPTION
3.向大佬学习

 
					
										


					

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/zaji/5697352.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存