Submission #950033
Source Code Expand
import java.util.*; import java.util.function.BiFunction; import java.util.function.Function; import java.util.function.Supplier; public class Main { void test() { TreeSet<Integer> set = new TreeSet<>(); set.add(1); set.add(3); set.add(0); debug(set.tailSet(2), set.headSet(2)); } void run() { test(); int n = ni(); TreeSet<Integer> notExistInPset = new TreeSet<>(); for (int i = 0; i < n; ++i) { notExistInPset.add(i); } int[] p = new int[n]; int k = 0; int[] countWildCardUntilPrev = new int[n]; for (int i = 0; i < n; ++i) { countWildCardUntilPrev[i] = k; p[i] = ni() - 1; if (p[i] < 0) { ++k; } else { notExistInPset.remove(p[i]); } } int sumOfNotExistInP = notExistInPset.stream().reduce(0, (a, b) -> a + b); long[] factorial = new long[n + 1]; factorial[0] = 1; for (int i = 1; i <= n; ++i) { factorial[i] = i * factorial[i - 1]; factorial[i] %= MOD; } int[] countIfSjSmallerThanSi = new int[n]; BIT<Integer> bit = new BIT<>(n, (a, b) -> a + b, () -> 0); for (int i = 0; i < n; ++i) { if (p[i] >= 0) { bit.update(p[i] + 1, 1); countIfSjSmallerThanSi[i] = bit.reduce(p[i], () -> 0); } } long sum = 0; for (int i = 0; i < n; ++i) { long sub = 0; if (p[i] >= 0) { sub += p[i] * factorial[k]; sub %= MOD; sub -= countIfSjSmallerThanSi[i] * factorial[k]; sub += MOD; sub %= MOD; if (k > 0) { int num = notExistInPset.headSet(p[i]).size(); sub -= num * factorial[k - 1] * countWildCardUntilPrev[i]; sub += MOD; sub %= MOD; } } else { sub += sumOfNotExistInP * factorial[k - 1]; sub %= MOD; for (int j = 0; j < i; ++j) { if (p[j] < 0) continue; int num = notExistInPset.tailSet(p[j]).size(); sub -= num * factorial[k - 1]; sub += MOD; sub %= MOD; } ArrayList<Integer> list = new ArrayList<>(notExistInPset); if (k > 1) { for (int si : list) { notExistInPset.remove(si); int num = notExistInPset.headSet(si).size(); sub -= num * factorial[k - 2] * countWildCardUntilPrev[i]; sub += MOD; sub %= MOD; notExistInPset.add(si); } } } sub *= factorial[n - 1 - i]; sub %= MOD; sum += sub; sum %= MOD; } sum += factorial[k]; sum %= MOD; System.out.println(sum); } Scanner sc = new Scanner(System.in); public static void main(String[] args) { new Main().run(); } int ni() { return Integer.parseInt(sc.next()); } void debug(Object... os) { System.err.println(Arrays.deepToString(os)); } class BIT<T> { int n; ArrayList<T> bit; BiFunction<T, T, T> bif; BIT(int n, BiFunction<T, T, T> bif, Supplier<T> sup) { this.n = n; bit = new ArrayList<>(n + 1); for (int i = 0; i < n + 1; ++i) { bit.add(sup.get()); } this.bif = bif; } void update(int i, T v) { for (int x = i; x <= n; x += x & -x) { bit.set(x, bif.apply(bit.get(x), v)); } } T reduce(int i, Supplier<T> sup) { T ret = sup.get(); for (int x = i; x > 0; x -= x & -x) { ret = bif.apply(ret, bit.get(x)); } return ret; } } long MOD = 1_000_000_007; long pow(long a, long r) { long sum = 1; while (r > 0) { if ((r & 1) == 1) { sum *= a; sum %= MOD; } a *= a; a %= MOD; r >>= 1; } return sum; } long C(int n, int r) { long sum = 1; for (int i = n; 0 < i; --i) { sum *= i; sum %= MOD; } long s = 1; for (int i = r; 0 < i; --i) { s *= i; s %= MOD; } sum *= pow(s, MOD - 2); sum %= MOD; long t = 1; for (int i = n - r; 0 < i; --i) { t *= i; t %= MOD; } sum *= pow(t, MOD - 2); sum %= MOD; return sum; } double GOLDEN_RATIO = (1.0 + Math.sqrt(5)) / 2.0; /** * 黄金分割探索 * * @param left 下限 * @param right 上限 * @param f 探索する関数 * @param comp 上に凸な関数を探索するときは、Comparator.comparingDouble(Double::doubleValue) * 下に凸な関数を探索するときは、Comparator.comparingDouble(Double::doubleValue).reversed() * @return 極値の座標x */ double goldenSectionSearch(double left, double right, Function<Double, Double> f, Comparator<Double> comp) { double c1 = divideInternally(left, right, 1, GOLDEN_RATIO); double c2 = divideInternally(left, right, GOLDEN_RATIO, 1); double d1 = f.apply(c1); double d2 = f.apply(c2); while (right - left > 1e-9) { if (comp.compare(d1, d2) > 0) { right = c2; c2 = c1; d2 = d1; c1 = divideInternally(left, right, 1, GOLDEN_RATIO); d1 = f.apply(c1); } else { left = c1; c1 = c2; d1 = d2; c2 = divideInternally(left, right, GOLDEN_RATIO, 1); d2 = f.apply(c2); } } return right; } /** * [a,b]をm:nに内分する点を返す */ double divideInternally(double a, double b, double m, double n) { return (n * a + m * b) / (m + n); } }
Submission Info
Submission Time | |
---|---|
Task | E - Encyclopedia of Permutations |
User | arukuka |
Language | Java8 (OpenJDK 1.8.0) |
Score | 0 |
Code Size | 5678 Byte |
Status | WA |
Exec Time | 2111 ms |
Memory | 128524 KB |
Judge Result
Set Name | Sample | Subtask | All | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 0 / 500 | 0 / 700 | ||||||||||||||
Status |
|
|
|
Set Name | Test Cases |
---|---|
Sample | 0_000.txt, 0_001.txt, 0_002.txt, 0_003.txt, 0_004.txt |
Subtask | 0_000.txt, 0_001.txt, 0_002.txt, 0_003.txt, 0_004.txt, 1_005.txt, 1_006.txt, 1_007.txt, 1_008.txt, 1_009.txt, 1_010.txt, 1_011.txt, 1_012.txt, 1_013.txt, 1_014.txt, 1_015.txt, 1_016.txt, 1_017.txt, 1_018.txt, 1_019.txt, 1_020.txt, 1_021.txt, 1_022.txt, 1_023.txt, 1_024.txt, 1_025.txt, 1_026.txt, 1_027.txt, 1_028.txt, 1_029.txt, 1_030.txt, 1_031.txt, 1_032.txt |
All | 0_000.txt, 0_001.txt, 0_002.txt, 0_003.txt, 0_004.txt, 1_005.txt, 1_006.txt, 1_007.txt, 1_008.txt, 1_009.txt, 1_010.txt, 1_011.txt, 1_012.txt, 1_013.txt, 1_014.txt, 1_015.txt, 1_016.txt, 1_017.txt, 1_018.txt, 1_019.txt, 1_020.txt, 1_021.txt, 1_022.txt, 1_023.txt, 1_024.txt, 1_025.txt, 1_026.txt, 1_027.txt, 1_028.txt, 1_029.txt, 1_030.txt, 1_031.txt, 1_032.txt, 2_033.txt, 2_034.txt, 2_035.txt, 2_036.txt, 2_037.txt, 2_038.txt, 2_039.txt, 2_040.txt, 2_041.txt, 2_042.txt, 2_043.txt, 2_044.txt, 2_045.txt, 2_046.txt, 2_047.txt, 2_048.txt, 2_049.txt, 2_050.txt, 2_051.txt, 2_052.txt, 2_053.txt, 2_054.txt, 2_055.txt, 2_056.txt, 2_057.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
0_000.txt | AC | 258 ms | 16208 KB |
0_001.txt | AC | 222 ms | 15316 KB |
0_002.txt | AC | 220 ms | 15564 KB |
0_003.txt | AC | 219 ms | 15308 KB |
0_004.txt | AC | 217 ms | 15056 KB |
1_005.txt | AC | 219 ms | 15056 KB |
1_006.txt | AC | 212 ms | 14800 KB |
1_007.txt | AC | 212 ms | 14796 KB |
1_008.txt | TLE | 2105 ms | 36316 KB |
1_009.txt | TLE | 2105 ms | 36772 KB |
1_010.txt | TLE | 2105 ms | 38032 KB |
1_011.txt | TLE | 2106 ms | 36988 KB |
1_012.txt | TLE | 2105 ms | 37248 KB |
1_013.txt | WA | 1638 ms | 37532 KB |
1_014.txt | TLE | 2105 ms | 37324 KB |
1_015.txt | TLE | 2106 ms | 44108 KB |
1_016.txt | TLE | 2105 ms | 36476 KB |
1_017.txt | AC | 214 ms | 14804 KB |
1_018.txt | TLE | 2106 ms | 43920 KB |
1_019.txt | WA | 1519 ms | 37948 KB |
1_020.txt | TLE | 2105 ms | 37240 KB |
1_021.txt | AC | 1142 ms | 38336 KB |
1_022.txt | TLE | 2105 ms | 36940 KB |
1_023.txt | WA | 1143 ms | 36212 KB |
1_024.txt | AC | 1021 ms | 36728 KB |
1_025.txt | WA | 244 ms | 15436 KB |
1_026.txt | AC | 337 ms | 19260 KB |
1_027.txt | AC | 298 ms | 17228 KB |
1_028.txt | TLE | 2105 ms | 37556 KB |
1_029.txt | AC | 345 ms | 19004 KB |
1_030.txt | AC | 316 ms | 17976 KB |
1_031.txt | AC | 332 ms | 18216 KB |
1_032.txt | TLE | 2105 ms | 36504 KB |
2_033.txt | TLE | 2109 ms | 94744 KB |
2_034.txt | TLE | 2107 ms | 68792 KB |
2_035.txt | TLE | 2109 ms | 93180 KB |
2_036.txt | TLE | 2108 ms | 84960 KB |
2_037.txt | TLE | 2109 ms | 93288 KB |
2_038.txt | TLE | 2109 ms | 107664 KB |
2_039.txt | TLE | 2109 ms | 107196 KB |
2_040.txt | TLE | 2107 ms | 67176 KB |
2_041.txt | TLE | 2110 ms | 109736 KB |
2_042.txt | TLE | 2106 ms | 41408 KB |
2_043.txt | TLE | 2110 ms | 108656 KB |
2_044.txt | TLE | 2109 ms | 105508 KB |
2_045.txt | TLE | 2110 ms | 109024 KB |
2_046.txt | TLE | 2110 ms | 112696 KB |
2_047.txt | TLE | 2110 ms | 109200 KB |
2_048.txt | TLE | 2107 ms | 69200 KB |
2_049.txt | TLE | 2110 ms | 118828 KB |
2_050.txt | TLE | 2109 ms | 103516 KB |
2_051.txt | AC | 1844 ms | 119028 KB |
2_052.txt | AC | 1381 ms | 113620 KB |
2_053.txt | TLE | 2109 ms | 94676 KB |
2_054.txt | TLE | 2111 ms | 128524 KB |
2_055.txt | AC | 1624 ms | 117764 KB |
2_056.txt | AC | 1663 ms | 107412 KB |
2_057.txt | TLE | 2109 ms | 106972 KB |