Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
这题陷阱真多,贴一下 JAVA 的解答,供有需要的后来者参考这题陷阱真多,贴一下 JAVA 的解答, 供后来者参考。 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub int N = 0; int T = 0; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = br.readLine(); StringTokenizer stoken = new StringTokenizer(str," "); N = Integer.valueOf(stoken.nextToken()); T = Integer.valueOf(stoken.nextToken()); List<Shift> list = new ArrayList<Shift>(); for (int i = 0; i < N; i++) { str = br.readLine(); stoken= new StringTokenizer(str, " "); list.add(new Shift( Integer.valueOf(stoken.nextToken()), Integer.valueOf(stoken.nextToken()) )); } Collections.sort(list,new MyComparator()); // System.out.println(list); int count= 0 ; if( list.get(0).begin != 1 ) { System.out.println("-1"); return ; } int tmp = list.get(0).end; if( list.size() == 1 && tmp < T ) { System.out.println("-1"); return ; } if( tmp >= T ) { System.out.println("1"); return ; } count ++; int i = 1; int max = 0; while( i < list.size() ) { boolean flag = false; max = list.get(i).begin; while( i < list.size() && list.get(i).begin <= tmp + 1 ) { flag = true; if( max< list.get(i).end ) max = list.get(i).end; i++; } // System.out.println("max = " + max ); if( flag == false ) { System.out.println("-1"); return ; } count++; if( max >= T ) { System.out.println(count); return ; } tmp = max; } if( max < T ) System.out.println("-1"); else System.out.println(count); br.close(); } } class Shift { int begin = 0; int end = 0; int du = 0; public Shift( int in_begin , int in_end ) { begin = in_begin; end = in_end; du = getDu(); } public String toString() { return "(" + begin + "," + end + "," + du + ")"; } public int getDu() { return end - begin; } } class MyComparator implements Comparator<Shift> { public int compare(Shift a , Shift b ) { if( a.begin > b.begin ) return 1; else if( a.begin == b.begin ) { if( a.end > b.end ) return -1; else if( a.end == b.end ) return 0; else return 1; } else return -1; } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator