Submission #1118290

# Submission time Handle Problem Language Result Execution time Memory
1118290 2024-11-25T08:20:07 Z crafticat Detecting Molecules (IOI16_molecules) C++17
9 / 100
1 ms 604 KB
#include <bits/stdc++.h>
#include "molecules.h"


using namespace std;

template<typename T>
using V = vector<T>;
using ll = long long;
using vi = V<int>;
using pi = pair<int,int>;
constexpr int INF = 1e9 + 7;

#define F0R(i, n) for (int i = 0; i < n;i++)
#define FOR(i, j , n) for (int i = j ;i < n;i++)

vi possible(ll l, ll u, V<ll> w, int k) {
    int ptrEnd = w.size() - k; //including
    int ptrStart = 0; // Not including

    ll curSum = 0;
    FOR(i, ptrEnd, w.size())
        curSum += w[i];

    while (curSum > u && ptrStart < k) {
        curSum -= w[ptrEnd]; ptrEnd++;
        curSum += w[ptrStart]; ptrStart++;
    }

    if (curSum <= u && curSum >= l) {
        vi vals;
        F0R(i, ptrStart)
            vals.push_back(i);
        FOR(i, ptrEnd, w.size()) {
            vals.push_back(i);
        }
        return vals;
    }
    return {};
}

std::vector<int> find_subset(int l, int u, std::vector<int> w2) {
    V<ll> w(w2.size());
    F0R(i, w.size()) w[i] = w2[i];

    sort(w.begin(), w.end());
    ll offset = w[0];

    for (auto &x : w)
        x -= offset;

    V<ll> prefixSums(w.size() + 1), revPrefixSums(w.size() + 1); // Not including, including
    F0R(i, w.size()) {
        prefixSums[i + 1] = prefixSums[i] + w[i];
    }
    F0R(i, w.size()) {
        prefixSums[i] = prefixSums[i + 1] + w[i];
    }

    FOR(k, 1, w.size() + 1) {
        auto r = possible(l - k * offset, u - k * offset, w, k);
        if (!r.empty()) return r;
    }
    return {};
}

#if DEBUG

int main() {
    int n, l, u;
    assert(3 == scanf("%d %d %d", &n, &l, &u));
    std::vector<int> w(n);
    for (int i = 0; i < n; i++)
        assert(1 == scanf("%d", &w[i]));
    std::vector<int> result = find_subset(l, u, w);

    printf("%d\n", (int)result.size());
    for (int i = 0; i < (int)result.size(); i++)
        printf("%d%c", result[i], " \n"[i == (int)result.size() - 1]);
}
#endif

Compilation message

molecules.cpp: In function 'vi possible(ll, ll, V<long long int>, int)':
molecules.cpp:15:41: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int, std::allocator<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   15 | #define FOR(i, j , n) for (int i = j ;i < n;i++)
......
   22 |     FOR(i, ptrEnd, w.size())
      |         ~~~~~~~~~~~~~~~~~~~              
molecules.cpp:22:5: note: in expansion of macro 'FOR'
   22 |     FOR(i, ptrEnd, w.size())
      |     ^~~
molecules.cpp:15:41: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int, std::allocator<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   15 | #define FOR(i, j , n) for (int i = j ;i < n;i++)
......
   34 |         FOR(i, ptrEnd, w.size()) {
      |             ~~~~~~~~~~~~~~~~~~~          
molecules.cpp:34:9: note: in expansion of macro 'FOR'
   34 |         FOR(i, ptrEnd, w.size()) {
      |         ^~~
molecules.cpp: In function 'std::vector<int> find_subset(int, int, std::vector<int>)':
molecules.cpp:14:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int, std::allocator<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   14 | #define F0R(i, n) for (int i = 0; i < n;i++)
......
   44 |     F0R(i, w.size()) w[i] = w2[i];
      |         ~~~~~~~~~~~                  
molecules.cpp:44:5: note: in expansion of macro 'F0R'
   44 |     F0R(i, w.size()) w[i] = w2[i];
      |     ^~~
molecules.cpp:14:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int, std::allocator<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   14 | #define F0R(i, n) for (int i = 0; i < n;i++)
......
   53 |     F0R(i, w.size()) {
      |         ~~~~~~~~~~~                  
molecules.cpp:53:5: note: in expansion of macro 'F0R'
   53 |     F0R(i, w.size()) {
      |     ^~~
molecules.cpp:14:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int, std::allocator<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   14 | #define F0R(i, n) for (int i = 0; i < n;i++)
......
   56 |     F0R(i, w.size()) {
      |         ~~~~~~~~~~~                  
molecules.cpp:56:5: note: in expansion of macro 'F0R'
   56 |     F0R(i, w.size()) {
      |     ^~~
molecules.cpp:15:41: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int, std::allocator<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   15 | #define FOR(i, j , n) for (int i = j ;i < n;i++)
......
   60 |     FOR(k, 1, w.size() + 1) {
      |         ~~~~~~~~~~~~~~~~~~               
molecules.cpp:60:5: note: in expansion of macro 'FOR'
   60 |     FOR(k, 1, w.size() + 1) {
      |     ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB OK (n = 1, answer = NO)
2 Correct 1 ms 348 KB OK (n = 1, answer = NO)
3 Correct 1 ms 504 KB OK (n = 1, answer = YES)
4 Correct 1 ms 348 KB OK (n = 2, answer = YES)
5 Correct 1 ms 348 KB OK (n = 2, answer = YES)
6 Correct 1 ms 348 KB OK (n = 3, answer = YES)
7 Correct 1 ms 348 KB OK (n = 3, answer = YES)
8 Correct 1 ms 348 KB OK (n = 3, answer = YES)
9 Correct 1 ms 348 KB OK (n = 3, answer = YES)
10 Correct 1 ms 348 KB OK (n = 3, answer = YES)
11 Correct 1 ms 348 KB OK (n = 3, answer = YES)
12 Correct 1 ms 348 KB OK (n = 3, answer = YES)
13 Correct 1 ms 348 KB OK (n = 3, answer = NO)
14 Correct 1 ms 348 KB OK (n = 3, answer = YES)
15 Correct 1 ms 348 KB OK (n = 3, answer = YES)
16 Correct 1 ms 348 KB OK (n = 3, answer = NO)
17 Correct 1 ms 348 KB OK (n = 3, answer = NO)
18 Correct 1 ms 348 KB OK (n = 100, answer = NO)
19 Correct 1 ms 348 KB OK (n = 100, answer = YES)
# Verdict Execution time Memory Grader output
1 Correct 1 ms 504 KB OK (n = 12, answer = YES)
2 Correct 1 ms 348 KB OK (n = 12, answer = YES)
3 Correct 1 ms 348 KB OK (n = 12, answer = NO)
4 Correct 1 ms 352 KB OK (n = 12, answer = NO)
5 Correct 1 ms 348 KB OK (n = 12, answer = YES)
6 Correct 1 ms 348 KB OK (n = 12, answer = YES)
7 Correct 1 ms 348 KB OK (n = 12, answer = YES)
8 Correct 1 ms 604 KB OK (n = 12, answer = YES)
9 Correct 1 ms 348 KB OK (n = 6, answer = YES)
10 Correct 1 ms 348 KB OK (n = 12, answer = YES)
11 Correct 1 ms 348 KB OK (n = 100, answer = NO)
12 Incorrect 1 ms 504 KB sum of weights should be in [50..51] but it is 35
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB OK (n = 1, answer = NO)
2 Correct 1 ms 348 KB OK (n = 1, answer = NO)
3 Correct 1 ms 504 KB OK (n = 1, answer = YES)
4 Correct 1 ms 348 KB OK (n = 2, answer = YES)
5 Correct 1 ms 348 KB OK (n = 2, answer = YES)
6 Correct 1 ms 348 KB OK (n = 3, answer = YES)
7 Correct 1 ms 348 KB OK (n = 3, answer = YES)
8 Correct 1 ms 348 KB OK (n = 3, answer = YES)
9 Correct 1 ms 348 KB OK (n = 3, answer = YES)
10 Correct 1 ms 348 KB OK (n = 3, answer = YES)
11 Correct 1 ms 348 KB OK (n = 3, answer = YES)
12 Correct 1 ms 348 KB OK (n = 3, answer = YES)
13 Correct 1 ms 348 KB OK (n = 3, answer = NO)
14 Correct 1 ms 348 KB OK (n = 3, answer = YES)
15 Correct 1 ms 348 KB OK (n = 3, answer = YES)
16 Correct 1 ms 348 KB OK (n = 3, answer = NO)
17 Correct 1 ms 348 KB OK (n = 3, answer = NO)
18 Correct 1 ms 348 KB OK (n = 100, answer = NO)
19 Correct 1 ms 348 KB OK (n = 100, answer = YES)
20 Correct 1 ms 504 KB OK (n = 12, answer = YES)
21 Correct 1 ms 348 KB OK (n = 12, answer = YES)
22 Correct 1 ms 348 KB OK (n = 12, answer = NO)
23 Correct 1 ms 352 KB OK (n = 12, answer = NO)
24 Correct 1 ms 348 KB OK (n = 12, answer = YES)
25 Correct 1 ms 348 KB OK (n = 12, answer = YES)
26 Correct 1 ms 348 KB OK (n = 12, answer = YES)
27 Correct 1 ms 604 KB OK (n = 12, answer = YES)
28 Correct 1 ms 348 KB OK (n = 6, answer = YES)
29 Correct 1 ms 348 KB OK (n = 12, answer = YES)
30 Correct 1 ms 348 KB OK (n = 100, answer = NO)
31 Incorrect 1 ms 504 KB sum of weights should be in [50..51] but it is 35
32 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB OK (n = 1, answer = NO)
2 Correct 1 ms 348 KB OK (n = 1, answer = NO)
3 Correct 1 ms 504 KB OK (n = 1, answer = YES)
4 Correct 1 ms 348 KB OK (n = 2, answer = YES)
5 Correct 1 ms 348 KB OK (n = 2, answer = YES)
6 Correct 1 ms 348 KB OK (n = 3, answer = YES)
7 Correct 1 ms 348 KB OK (n = 3, answer = YES)
8 Correct 1 ms 348 KB OK (n = 3, answer = YES)
9 Correct 1 ms 348 KB OK (n = 3, answer = YES)
10 Correct 1 ms 348 KB OK (n = 3, answer = YES)
11 Correct 1 ms 348 KB OK (n = 3, answer = YES)
12 Correct 1 ms 348 KB OK (n = 3, answer = YES)
13 Correct 1 ms 348 KB OK (n = 3, answer = NO)
14 Correct 1 ms 348 KB OK (n = 3, answer = YES)
15 Correct 1 ms 348 KB OK (n = 3, answer = YES)
16 Correct 1 ms 348 KB OK (n = 3, answer = NO)
17 Correct 1 ms 348 KB OK (n = 3, answer = NO)
18 Correct 1 ms 348 KB OK (n = 100, answer = NO)
19 Correct 1 ms 348 KB OK (n = 100, answer = YES)
20 Correct 1 ms 504 KB OK (n = 12, answer = YES)
21 Correct 1 ms 348 KB OK (n = 12, answer = YES)
22 Correct 1 ms 348 KB OK (n = 12, answer = NO)
23 Correct 1 ms 352 KB OK (n = 12, answer = NO)
24 Correct 1 ms 348 KB OK (n = 12, answer = YES)
25 Correct 1 ms 348 KB OK (n = 12, answer = YES)
26 Correct 1 ms 348 KB OK (n = 12, answer = YES)
27 Correct 1 ms 604 KB OK (n = 12, answer = YES)
28 Correct 1 ms 348 KB OK (n = 6, answer = YES)
29 Correct 1 ms 348 KB OK (n = 12, answer = YES)
30 Correct 1 ms 348 KB OK (n = 100, answer = NO)
31 Incorrect 1 ms 504 KB sum of weights should be in [50..51] but it is 35
32 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB OK (n = 1, answer = NO)
2 Correct 1 ms 348 KB OK (n = 1, answer = NO)
3 Correct 1 ms 504 KB OK (n = 1, answer = YES)
4 Correct 1 ms 348 KB OK (n = 2, answer = YES)
5 Correct 1 ms 348 KB OK (n = 2, answer = YES)
6 Correct 1 ms 348 KB OK (n = 3, answer = YES)
7 Correct 1 ms 348 KB OK (n = 3, answer = YES)
8 Correct 1 ms 348 KB OK (n = 3, answer = YES)
9 Correct 1 ms 348 KB OK (n = 3, answer = YES)
10 Correct 1 ms 348 KB OK (n = 3, answer = YES)
11 Correct 1 ms 348 KB OK (n = 3, answer = YES)
12 Correct 1 ms 348 KB OK (n = 3, answer = YES)
13 Correct 1 ms 348 KB OK (n = 3, answer = NO)
14 Correct 1 ms 348 KB OK (n = 3, answer = YES)
15 Correct 1 ms 348 KB OK (n = 3, answer = YES)
16 Correct 1 ms 348 KB OK (n = 3, answer = NO)
17 Correct 1 ms 348 KB OK (n = 3, answer = NO)
18 Correct 1 ms 348 KB OK (n = 100, answer = NO)
19 Correct 1 ms 348 KB OK (n = 100, answer = YES)
20 Correct 1 ms 504 KB OK (n = 12, answer = YES)
21 Correct 1 ms 348 KB OK (n = 12, answer = YES)
22 Correct 1 ms 348 KB OK (n = 12, answer = NO)
23 Correct 1 ms 352 KB OK (n = 12, answer = NO)
24 Correct 1 ms 348 KB OK (n = 12, answer = YES)
25 Correct 1 ms 348 KB OK (n = 12, answer = YES)
26 Correct 1 ms 348 KB OK (n = 12, answer = YES)
27 Correct 1 ms 604 KB OK (n = 12, answer = YES)
28 Correct 1 ms 348 KB OK (n = 6, answer = YES)
29 Correct 1 ms 348 KB OK (n = 12, answer = YES)
30 Correct 1 ms 348 KB OK (n = 100, answer = NO)
31 Incorrect 1 ms 504 KB sum of weights should be in [50..51] but it is 35
32 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB OK (n = 1, answer = NO)
2 Correct 1 ms 348 KB OK (n = 1, answer = NO)
3 Correct 1 ms 504 KB OK (n = 1, answer = YES)
4 Correct 1 ms 348 KB OK (n = 2, answer = YES)
5 Correct 1 ms 348 KB OK (n = 2, answer = YES)
6 Correct 1 ms 348 KB OK (n = 3, answer = YES)
7 Correct 1 ms 348 KB OK (n = 3, answer = YES)
8 Correct 1 ms 348 KB OK (n = 3, answer = YES)
9 Correct 1 ms 348 KB OK (n = 3, answer = YES)
10 Correct 1 ms 348 KB OK (n = 3, answer = YES)
11 Correct 1 ms 348 KB OK (n = 3, answer = YES)
12 Correct 1 ms 348 KB OK (n = 3, answer = YES)
13 Correct 1 ms 348 KB OK (n = 3, answer = NO)
14 Correct 1 ms 348 KB OK (n = 3, answer = YES)
15 Correct 1 ms 348 KB OK (n = 3, answer = YES)
16 Correct 1 ms 348 KB OK (n = 3, answer = NO)
17 Correct 1 ms 348 KB OK (n = 3, answer = NO)
18 Correct 1 ms 348 KB OK (n = 100, answer = NO)
19 Correct 1 ms 348 KB OK (n = 100, answer = YES)
20 Correct 1 ms 504 KB OK (n = 12, answer = YES)
21 Correct 1 ms 348 KB OK (n = 12, answer = YES)
22 Correct 1 ms 348 KB OK (n = 12, answer = NO)
23 Correct 1 ms 352 KB OK (n = 12, answer = NO)
24 Correct 1 ms 348 KB OK (n = 12, answer = YES)
25 Correct 1 ms 348 KB OK (n = 12, answer = YES)
26 Correct 1 ms 348 KB OK (n = 12, answer = YES)
27 Correct 1 ms 604 KB OK (n = 12, answer = YES)
28 Correct 1 ms 348 KB OK (n = 6, answer = YES)
29 Correct 1 ms 348 KB OK (n = 12, answer = YES)
30 Correct 1 ms 348 KB OK (n = 100, answer = NO)
31 Incorrect 1 ms 504 KB sum of weights should be in [50..51] but it is 35
32 Halted 0 ms 0 KB -