結果
問題 | No.1144 Triangles |
ユーザー | 37zigen |
提出日時 | 2020-08-04 18:11:15 |
言語 | Java21 (openjdk 21) |
結果 |
TLE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 5,118 bytes |
コンパイル時間 | 5,063 ms |
コンパイル使用メモリ | 84,640 KB |
実行使用メモリ | 59,152 KB |
最終ジャッジ日時 | 2024-09-14 19:40:27 |
合計ジャッジ時間 | 44,147 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 51 ms
36,872 KB |
testcase_01 | AC | 53 ms
36,764 KB |
testcase_02 | AC | 53 ms
36,800 KB |
testcase_03 | AC | 54 ms
37,092 KB |
testcase_04 | AC | 2,863 ms
47,320 KB |
testcase_05 | AC | 2,901 ms
47,288 KB |
testcase_06 | AC | 2,884 ms
47,068 KB |
testcase_07 | AC | 2,796 ms
47,148 KB |
testcase_08 | AC | 2,941 ms
47,308 KB |
testcase_09 | AC | 53 ms
36,744 KB |
testcase_10 | AC | 53 ms
36,652 KB |
testcase_11 | AC | 53 ms
36,744 KB |
testcase_12 | AC | 52 ms
36,484 KB |
testcase_13 | AC | 52 ms
36,864 KB |
testcase_14 | AC | 2,738 ms
47,072 KB |
testcase_15 | AC | 2,784 ms
47,144 KB |
testcase_16 | AC | 2,995 ms
59,152 KB |
testcase_17 | AC | 2,852 ms
58,600 KB |
testcase_18 | TLE | - |
testcase_19 | AC | 363 ms
57,260 KB |
testcase_20 | AC | 93 ms
52,056 KB |
testcase_21 | AC | 105 ms
51,884 KB |
testcase_22 | AC | 2,872 ms
58,752 KB |
testcase_23 | AC | 420 ms
57,008 KB |
testcase_24 | AC | 2,907 ms
58,764 KB |
testcase_25 | AC | 1,688 ms
58,624 KB |
testcase_26 | AC | 259 ms
56,912 KB |
testcase_27 | AC | 135 ms
52,380 KB |
testcase_28 | AC | 1,613 ms
58,476 KB |
ソースコード
import java.awt.geom.Line2D; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; import java.util.Comparator; import java.util.NoSuchElementException; import java.util.Random; public class Main { public static void main(String[] args) { new Main().run(); } final long MOD=(long)1e9+7; long pow(long a,long n) { return n!=0?pow(a*a%MOD,n/2)*(n%2==1?a:1)%MOD:1; } long inv(long a) { return pow(a,MOD-2); } long ADD(long a,long b) { long ret=a+b; if (ret<0) ret+=MOD; return ret<MOD?ret:ret-MOD; } int quadrant(long x,long y) { if (x==0 && y==0) return -1; if (y>=0 && x> 0) return 0; if (y> 0 && x<=0) return 1; if (y<=0 && x< 0) return 2; if (y< 0 && x>=0) return 3; throw new AssertionError(); } void run() { FastScanner sc=new FastScanner(); int n=sc.nextInt(); long[][] a=new long[n][2]; for (int i=0;i<n;++i) { a[i][0]=sc.nextLong(); a[i][1]=sc.nextLong(); } System.out.println(solve(n, a)); // rnd(); } void rnd() { while (true) { Random seed=new Random(); int n=10; long[][] a=new long[n][2]; for (int i=0;i<n;++i) { a[i][0]=seed.nextInt(10)-5; a[i][1]=seed.nextInt(10)-5; } long ans1=solve(n,a); long ans2=brute(n,a); if (ans1!=ans2) { System.out.println("ans1 "+ans1); System.out.println("ans2 "+ans2); tr(a); return; } } } long brute(int n,long[][] a) { long ret=0; for (int i=0;i<n;++i) { for (int j=i+1;j<n;++j) { for (int k=j+1;k<n;++k) { long x1=a[j][0]-a[i][0]; long y1=a[j][1]-a[i][1]; long x2=a[k][0]-a[i][0]; long y2=a[k][1]-a[i][1]; long add=x1*y2-x2*y1; if (add<0) add=MOD-add%MOD; add%=MOD; ret=(ret+add)%MOD; } } } return ret; } long solve(int n,long[][] a) { long[] sumx=new long[2*(n-1)+1]; long[] sumy=new long[2*(n-1)+1]; long ans=0; for (int i=0;i<n;++i) { int zero=0; for (int j=0;j<n;++j) if (a[j][0]-a[i][0]==0&&a[j][1]-a[i][1]==0) ++zero; long[][] p=new long[n-zero][2]; int sz=0; { for (int j=0;j<n;++j) { long dx=a[j][0]-a[i][0]; long dy=a[j][1]-a[i][1]; if (dx==0 && dy==0) continue; p[sz][0]=dx; p[sz][1]=dy; ++sz; } } Arrays.sort(p,new Comparator<long[]>() { @Override public int compare(long[] o1, long[] o2) { if (quadrant(o1[0], o1[1])==quadrant(o2[0], o2[1])) { return Line2D.relativeCCW(0, 0, o1[0], o1[1], o2[0], o2[1]); } else { return Integer.compare(quadrant(o1[0], o1[1]), quadrant(o2[0], o2[1])); } } }); for (int j=1;j<=2*p.length;++j) { sumx[j]=ADD(p[(j-1)%sz][0],sumx[j-1]); sumy[j]=ADD(p[(j-1)%sz][1],sumy[j-1]); } int t=0; for (int j=0;j<sz;++j) { t=Math.max(t, j+1); long x0=p[j][0]; long y0=p[j][1]; assert(x0!=0||y0!=0); while (t!=j+sz) { if ((p[j][0]*p[t%sz][1]==p[j][1]*p[t%sz][0])) { if (p[j][0]*p[(t+sz-1)%sz][1]==p[j][1]*p[(t+sz-1)%sz][0]) { ++t; } else break; } else if (Line2D.relativeCCW(0, 0, p[j][0], p[j][1], p[t%sz][0], p[t%sz][1])<=0) { ++t; } else { break; } } long dsx=sumx[t]-sumx[j+1]; long dsy=sumy[t]-sumy[j+1]; long add=x0*dsy%MOD-y0*dsx%MOD; if (add<0) add+=MOD; ans=ADD(ans,add); } } return ans*inv(3)%MOD; } void tr(Object... objects) { System.out.println(Arrays.deepToString(objects)); } class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) { return true; } else { ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } private void skipUnprintable() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; } public boolean hasNext() { skipUnprintable(); return hasNextByte(); } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { return (int) nextLong(); } } }