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 |
这道题是用并查集么?为什么我一直tle -.- 还是说 java 过不了import java.util.Arrays; import java.util.HashMap; public class Main { static final int M=11000; static class P implements Comparable<P>{ static final HashMap<Integer,P> ps=new HashMap<Integer,P>(); int x,y,c=0;P p; P(int i,int j){ x=i;y=j; ps.put(x*M+y,this); b(this,ps.get((x+1)*M+y)); b(this,ps.get((x-1)*M+y)); b(this,ps.get(x*M+y+1)); b(this,ps.get(x*M+y-1)); } P r(int t){ if(p==null) return this; if(t>2000){//防止 stack over follow while(p.p!=null) p=p.p; return p; } while(p.r(t+1)!=p) p=p.r(t+1); return p; } static void b(P a,P b){ if(a==null || b==null) return ; if(a.x*M+a.y>b.x*M+b.y) a.r(0).p=b.r(0); else b.r(0).p=a.r(0); } public boolean equals(Object obj) {return ((P)obj).x==x &&((P)obj).y==y;} public int compareTo(P o) {return c>o.c?-1:(c==o.c?0:1);} } final static int ni() { int v = 0; char t = 0; boolean neg = false; try { while (!Character.isDigit(t = (char) System.in.read()) && t != '-') ; if (t == '-') { neg = true; t = '0'; } do { v *= 10; v += t - '0'; } while (Character.isDigit(t = (char) System.in.read())); } catch (Exception e) { } return neg ? -v : v; } public static void main(String[] args) { int n=ni(),k=ni(); for(int i=0;i<n;i++) new P(ni(),ni()); P ps[]=new P[P.ps.values().size()]; ps=P.ps.values().toArray(ps); for(P p:ps) p.r(0).c++; Arrays.sort(ps); int r=0; for(int i=0;i<k;i++)r+=ps[i].c; System.out.println(r); } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator