Submission #1118299

#TimeUsernameProblemLanguageResultExecution timeMemory
1118299crafticatDetecting Molecules (IOI16_molecules)C++17
9 / 100
1 ms504 KiB
#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; ll newSum = 0; F0R(i, ptrStart) { vals.push_back(i); newSum += w[i]; } FOR(i, ptrEnd, w.size()) { vals.push_back(i); newSum += w[i]; } if (newSum != curSum or vals.size() != k) exit(5); return vals; } return {}; } std::vector<int> find_subset(int l2, int u2, std::vector<int> w2) { ll l = l2, u = u2; 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 (stderr)

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++)
......
   37 |         FOR(i, ptrEnd, w.size()) {
      |             ~~~~~~~~~~~~~~~~~~~          
molecules.cpp:37:9: note: in expansion of macro 'FOR'
   37 |         FOR(i, ptrEnd, w.size()) {
      |         ^~~
molecules.cpp:41:45: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   41 |         if (newSum != curSum or vals.size() != k) exit(5);
      |                                 ~~~~~~~~~~~~^~~~
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++)
......
   50 |     F0R(i, w.size()) w[i] = w2[i];
      |         ~~~~~~~~~~~                  
molecules.cpp:50:5: note: in expansion of macro 'F0R'
   50 |     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++)
......
   59 |     F0R(i, w.size()) {
      |         ~~~~~~~~~~~                  
molecules.cpp:59:5: note: in expansion of macro 'F0R'
   59 |     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++)
......
   62 |     F0R(i, w.size()) {
      |         ~~~~~~~~~~~                  
molecules.cpp:62:5: note: in expansion of macro 'F0R'
   62 |     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++)
......
   66 |     FOR(k, 1, w.size() + 1) {
      |         ~~~~~~~~~~~~~~~~~~               
molecules.cpp:66:5: note: in expansion of macro 'FOR'
   66 |     FOR(k, 1, w.size() + 1) {
      |     ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...