Submission #638317

# Submission time Handle Problem Language Result Execution time Memory
638317 2022-09-05T09:51:17 Z alvinpiter Lottery (CEOI18_lot) C++14
100 / 100
493 ms 8360 KB
/*
Idea 1:
Assume we know the difference between an interval that starts from A[i] and an interval that starts from A[i + d].
Then it is easy to count the difference between an interval that starts from A[i + 1] and an interval that starts from
A[i + d + 1] because most of their elements are the same except the first and the last. We can do this quickly with
sliding window approach.

Idea 2:
We need to process the query in an offline manner. Assume we have a list of unique sorted queries.
If we are currently processing 2 intervals whose difference is k, then this result will affect the answer to
the queries whose value is >= k. We can do aggregate the result with something similar to prefix sum.
*/

#include<bits/stdc++.h>
using namespace std;
#define MAXN 10000
#define MAXQ 100

int n, l, a[MAXN + 3], q, mapToSortedUniqQuery[MAXN + 3];
int ans[MAXQ + 3][MAXN + 3];
vector<int> queries, sortedUniqQueries;

int main() {
  cin >> n >> l;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    int num;
    cin >> num;

    queries.push_back(num);
    sortedUniqQueries.push_back(num);
  }

  // Populate mapToSortedUniqQuery
  sort(sortedUniqQueries.begin(), sortedUniqQueries.end());
  sortedUniqQueries.erase(unique(sortedUniqQueries.begin(), sortedUniqQueries.end()), sortedUniqQueries.end());
  for (int k = 1; k <= n; k++) {
    mapToSortedUniqQuery[k] = lower_bound(sortedUniqQueries.begin(), sortedUniqQueries.end(), k) - sortedUniqQueries.begin() + 1;
  }

  memset(ans, 0, sizeof(ans));

  for (int d = 1; (1 + d) + (l - 1) <= n; d++) {
    int currentDiff = 0;
    for (int i = 1; i < l; i++) {
      currentDiff += (a[i] != a[i + d]);
    }

    int leftPtr = 1;
    for (int i = l; i + d <= n; i++) {
      currentDiff += (a[i] != a[i + d]);
      while (i - leftPtr + 1 > l) {
        currentDiff -= (a[leftPtr] != a[leftPtr + d]);
        leftPtr += 1;
      }

      ans[mapToSortedUniqQuery[currentDiff]][leftPtr] += 1;
      ans[mapToSortedUniqQuery[currentDiff]][leftPtr + d] += 1;
    }
  }

  // Apply the prefix sum
  for (int idx = 1; idx <= n - l + 1; idx++) {
    for (int qIndex = 1; qIndex <= sortedUniqQueries.size(); qIndex++) {
      ans[qIndex][idx] += ans[qIndex - 1][idx];
    }
  }

  for (int i = 0; i < q; i++) {
    int queryIndex = mapToSortedUniqQuery[queries[i]];
    for (int j = 1; j <= n - l + 1; j++) {
      if (j > 1) {
        cout << " ";
      }
      cout << ans[queryIndex][j];
    }
    cout << endl;
  }
}

Compilation message

lot.cpp: In function 'int main()':
lot.cpp:68:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |     for (int qIndex = 1; qIndex <= sortedUniqQueries.size(); qIndex++) {
      |                          ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4308 KB Output is correct
2 Correct 2 ms 4308 KB Output is correct
3 Correct 2 ms 4308 KB Output is correct
4 Correct 3 ms 4308 KB Output is correct
5 Correct 2 ms 4308 KB Output is correct
6 Correct 2 ms 4308 KB Output is correct
7 Correct 2 ms 4308 KB Output is correct
8 Correct 2 ms 4308 KB Output is correct
9 Correct 2 ms 4308 KB Output is correct
10 Correct 3 ms 4308 KB Output is correct
11 Correct 3 ms 4308 KB Output is correct
12 Correct 3 ms 4308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4308 KB Output is correct
2 Correct 2 ms 4308 KB Output is correct
3 Correct 2 ms 4308 KB Output is correct
4 Correct 3 ms 4308 KB Output is correct
5 Correct 2 ms 4308 KB Output is correct
6 Correct 2 ms 4308 KB Output is correct
7 Correct 2 ms 4308 KB Output is correct
8 Correct 2 ms 4308 KB Output is correct
9 Correct 2 ms 4308 KB Output is correct
10 Correct 3 ms 4308 KB Output is correct
11 Correct 3 ms 4308 KB Output is correct
12 Correct 3 ms 4308 KB Output is correct
13 Correct 19 ms 4308 KB Output is correct
14 Correct 12 ms 4308 KB Output is correct
15 Correct 12 ms 4352 KB Output is correct
16 Correct 19 ms 4348 KB Output is correct
17 Correct 17 ms 4352 KB Output is correct
18 Correct 18 ms 4404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 396 ms 4388 KB Output is correct
2 Correct 397 ms 4384 KB Output is correct
3 Correct 398 ms 4392 KB Output is correct
4 Correct 385 ms 4388 KB Output is correct
5 Correct 120 ms 4384 KB Output is correct
6 Correct 342 ms 4404 KB Output is correct
7 Correct 121 ms 4404 KB Output is correct
8 Correct 216 ms 4384 KB Output is correct
9 Correct 370 ms 4392 KB Output is correct
10 Correct 384 ms 4384 KB Output is correct
11 Correct 20 ms 4308 KB Output is correct
12 Correct 209 ms 4384 KB Output is correct
13 Correct 160 ms 4392 KB Output is correct
14 Correct 147 ms 4388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 396 ms 4388 KB Output is correct
2 Correct 397 ms 4384 KB Output is correct
3 Correct 398 ms 4392 KB Output is correct
4 Correct 385 ms 4388 KB Output is correct
5 Correct 120 ms 4384 KB Output is correct
6 Correct 342 ms 4404 KB Output is correct
7 Correct 121 ms 4404 KB Output is correct
8 Correct 216 ms 4384 KB Output is correct
9 Correct 370 ms 4392 KB Output is correct
10 Correct 384 ms 4384 KB Output is correct
11 Correct 20 ms 4308 KB Output is correct
12 Correct 209 ms 4384 KB Output is correct
13 Correct 160 ms 4392 KB Output is correct
14 Correct 147 ms 4388 KB Output is correct
15 Correct 371 ms 4384 KB Output is correct
16 Correct 323 ms 4392 KB Output is correct
17 Correct 407 ms 4392 KB Output is correct
18 Correct 386 ms 4428 KB Output is correct
19 Correct 391 ms 4404 KB Output is correct
20 Correct 385 ms 4388 KB Output is correct
21 Correct 384 ms 4384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4308 KB Output is correct
2 Correct 2 ms 4308 KB Output is correct
3 Correct 2 ms 4308 KB Output is correct
4 Correct 3 ms 4308 KB Output is correct
5 Correct 2 ms 4308 KB Output is correct
6 Correct 2 ms 4308 KB Output is correct
7 Correct 2 ms 4308 KB Output is correct
8 Correct 2 ms 4308 KB Output is correct
9 Correct 2 ms 4308 KB Output is correct
10 Correct 3 ms 4308 KB Output is correct
11 Correct 3 ms 4308 KB Output is correct
12 Correct 3 ms 4308 KB Output is correct
13 Correct 19 ms 4308 KB Output is correct
14 Correct 12 ms 4308 KB Output is correct
15 Correct 12 ms 4352 KB Output is correct
16 Correct 19 ms 4348 KB Output is correct
17 Correct 17 ms 4352 KB Output is correct
18 Correct 18 ms 4404 KB Output is correct
19 Correct 396 ms 4388 KB Output is correct
20 Correct 397 ms 4384 KB Output is correct
21 Correct 398 ms 4392 KB Output is correct
22 Correct 385 ms 4388 KB Output is correct
23 Correct 120 ms 4384 KB Output is correct
24 Correct 342 ms 4404 KB Output is correct
25 Correct 121 ms 4404 KB Output is correct
26 Correct 216 ms 4384 KB Output is correct
27 Correct 370 ms 4392 KB Output is correct
28 Correct 384 ms 4384 KB Output is correct
29 Correct 20 ms 4308 KB Output is correct
30 Correct 209 ms 4384 KB Output is correct
31 Correct 160 ms 4392 KB Output is correct
32 Correct 147 ms 4388 KB Output is correct
33 Correct 371 ms 4384 KB Output is correct
34 Correct 323 ms 4392 KB Output is correct
35 Correct 407 ms 4392 KB Output is correct
36 Correct 386 ms 4428 KB Output is correct
37 Correct 391 ms 4404 KB Output is correct
38 Correct 385 ms 4388 KB Output is correct
39 Correct 384 ms 4384 KB Output is correct
40 Correct 411 ms 5196 KB Output is correct
41 Correct 17 ms 4436 KB Output is correct
42 Correct 408 ms 5112 KB Output is correct
43 Correct 386 ms 4776 KB Output is correct
44 Correct 477 ms 4908 KB Output is correct
45 Correct 493 ms 8232 KB Output is correct
46 Correct 24 ms 4664 KB Output is correct
47 Correct 472 ms 8360 KB Output is correct
48 Correct 444 ms 6392 KB Output is correct
49 Correct 449 ms 7116 KB Output is correct