結果
問題 | No.1144 Triangles |
ユーザー | 37zigen |
提出日時 | 2020-08-04 17:49:10 |
言語 | Java21 (openjdk 21) |
結果 |
WA
|
実行時間 | - |
コード長 | 5,050 bytes |
コンパイル時間 | 2,846 ms |
コンパイル使用メモリ | 86,100 KB |
実行使用メモリ | 47,664 KB |
最終ジャッジ日時 | 2024-09-14 19:38:25 |
合計ジャッジ時間 | 43,653 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 54 ms
37,056 KB |
testcase_01 | AC | 54 ms
36,864 KB |
testcase_02 | AC | 53 ms
36,536 KB |
testcase_03 | AC | 53 ms
36,888 KB |
testcase_04 | AC | 2,910 ms
47,112 KB |
testcase_05 | AC | 2,983 ms
47,312 KB |
testcase_06 | AC | 2,898 ms
47,092 KB |
testcase_07 | WA | - |
testcase_08 | AC | 2,908 ms
47,168 KB |
testcase_09 | AC | 53 ms
37,012 KB |
testcase_10 | AC | 54 ms
36,884 KB |
testcase_11 | AC | 54 ms
37,112 KB |
testcase_12 | AC | 53 ms
36,536 KB |
testcase_13 | AC | 54 ms
36,536 KB |
testcase_14 | AC | 2,676 ms
47,156 KB |
testcase_15 | AC | 2,773 ms
47,032 KB |
testcase_16 | TLE | - |
testcase_17 | AC | 2,896 ms
47,408 KB |
testcase_18 | TLE | - |
testcase_19 | AC | 308 ms
45,120 KB |
testcase_20 | AC | 102 ms
40,352 KB |
testcase_21 | AC | 103 ms
40,396 KB |
testcase_22 | AC | 2,827 ms
47,304 KB |
testcase_23 | AC | 427 ms
45,496 KB |
testcase_24 | AC | 2,920 ms
47,164 KB |
testcase_25 | AC | 1,623 ms
46,880 KB |
testcase_26 | AC | 255 ms
45,240 KB |
testcase_27 | AC | 440 ms
44,812 KB |
testcase_28 | WA | - |
ソースコード
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=5; long[][] a=new long[n][2]; for (int i=0;i<n;++i) { a[i][0]=seed.nextInt(2)-1; a[i][1]=seed.nextInt(2)-1; } 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) { long[][] p=new long[n-1][2]; { int pointer=0; for (int j=0;j<n;++j) { if (i==j) continue; p[pointer][0]=a[j][0]-a[i][0]; p[pointer][1]=a[j][1]-a[i][1]; ++pointer; } } 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)%(n-1)][0],sumx[j-1]); sumy[j]=ADD(p[(j-1)%(n-1)][1],sumy[j-1]); } int t=0; for (int j=0;j<n-1;++j) { t=Math.max(t, j+1); long x0=p[j][0]; long y0=p[j][1]; if (x0==0&&y0==0) continue; while (t!=j+(n-1)) { if ((p[j][0]*p[t%(n-1)][1]==p[j][1]*p[t%(n-1)][0])) { if (p[j][0]*p[(t+n-2)%(n-1)][1]==p[j][1]*p[(t+n-2)%(n-1)][0]) { ++t; } else break; } else if (Line2D.relativeCCW(0, 0, p[j][0], p[j][1], p[t%(n-1)][0], p[t%(n-1)][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; // tr("add"+add); 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(); } } }