QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#200895#6355. 5ucup-team859TL 771ms4232kbC++142.0kb2023-10-04 23:14:372023-10-04 23:14:38

Judging History

你现在查看的是最新测评结果

  • [2023-10-04 23:14:38]
  • 评测
  • 测评结果:TL
  • 用时:771ms
  • 内存:4232kb
  • [2023-10-04 23:14:37]
  • 提交

answer

#include <bits/stdc++.h>
#include <iterator>

#define lsb(x) (x & (-x))

using ull = unsigned long long;
using ll = long long;

using namespace std;

inline bool intersect(const pair<int, int> &x, const pair<int, int> &y) {
  return !(x.second < y.first || y.second < x.first);
}

inline void merge(vector<pair<int, int>> &x, const vector<pair<int, int>> &y, int add = 1) {
  for (auto interval : y) {
    auto to_merge = make_pair(interval.first + add, interval.second + add);
    for (int i = 0; i < (int)x.size();) {
      const auto &it = x[i];
      if (intersect(it, to_merge)) {
        to_merge.first = min(to_merge.first, it.first);
        to_merge.second = max(to_merge.second, it.second);
        swap(x[i], x.back());
        x.pop_back();
      } else {
        ++i;
      }
    }
    x.push_back(to_merge);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, s;
  cin >> n >> s;
  vector<int> cnt(s + 1, 0);
  vector<int> a(n + 1, 0);
  int cnt_neg = 0, new_s = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    a[i] -= 1;
    if (a[i] >= 0) {
      ++cnt[a[i]];
      new_s += a[i];
    }
    else
      ++cnt_neg;
  }

  s = new_s;
  vector<vector<pair<int, int>>> dp(s + 1);
  for (int val = 0; val <= s; ++val) {
    if (!cnt[val])
      continue;

    if (val == 0) {
      dp[0].push_back({0, cnt[val]});
      continue;
    }

    for (int j = s; j >= val; --j) {
      for (int x = val, nr = 1; x <= j && nr <= cnt[val]; x += val, ++nr)
        merge(dp[j], dp[j - x], nr);
    }
  }

  ll ans = 0;
  for (int i = 1; i <= cnt_neg; ++i) {
    for (auto interval : dp[0])
      ans += interval.second - interval.first + 1;
    for (int j = 0; j < s; ++j) {
      if (!dp[j + 1].empty())
        merge(dp[j], dp[j + 1]);
    }
  }


  for (auto it : dp)
    for (auto interval : it)
      ans += interval.second - interval.first + 1;

  cout << ans << endl;


  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3780kb

input:

7 9
0 0 0 1 1 2 5

output:

42

result:

ok 1 number(s): "42"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3608kb

input:

10 33
9 9 8 1 1 1 1 1 1 1

output:

48

result:

ok 1 number(s): "48"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3812kb

input:

10 14
2 4 4 1 0 1 0 1 0 1

output:

81

result:

ok 1 number(s): "81"

Test #4:

score: 0
Accepted
time: 0ms
memory: 3748kb

input:

10 14
3 5 3 0 1 0 1 0 1 0

output:

87

result:

ok 1 number(s): "87"

Test #5:

score: 0
Accepted
time: 0ms
memory: 3608kb

input:

40 50
1 1 1 1 3 3 0 3 1 1 0 0 2 1 0 0 1 0 0 2 7 1 2 1 3 0 2 2 3 1 1 0 0 2 0 1 1 0 1 1

output:

1067

result:

ok 1 number(s): "1067"

Test #6:

score: 0
Accepted
time: 1ms
memory: 3648kb

input:

1200 1000
1 1 2 3 0 1 0 0 1 1 0 2 3 0 1 2 0 0 1 0 4 1 1 2 1 1 0 0 1 1 0 1 0 0 1 0 1 0 0 0 1 1 0 1 0 0 1 0 0 1 1 0 1 2 0 4 0 3 1 6 0 1 1 0 0 0 0 4 0 0 0 0 0 0 1 0 0 1 7 1 1 1 0 1 0 1 0 1 1 0 0 1 1 1 3 0 1 0 1 0 0 1 1 2 2 0 1 1 0 0 1 4 1 2 0 0 0 3 0 0 2 1 0 2 0 0 0 1 1 0 0 2 0 0 0 0 1 1 0 1 0 1 6 1 1 ...

output:

737899

result:

ok 1 number(s): "737899"

Test #7:

score: 0
Accepted
time: 87ms
memory: 3816kb

input:

12000 10000
1 1 0 0 1 0 2 1 3 0 0 1 0 3 1 1 0 1 1 1 1 1 2 1 0 1 2 1 0 1 2 0 5 1 1 1 0 2 0 1 0 1 0 3 2 0 1 0 1 1 2 1 0 0 1 1 0 1 0 0 0 1 0 1 0 1 0 4 0 1 3 1 0 0 1 0 1 2 1 0 0 1 1 0 2 1 1 0 1 0 1 0 0 2 1 1 3 0 1 1 1 0 0 0 1 1 1 0 3 0 0 0 2 0 0 0 1 0 2 0 1 1 1 0 0 1 0 1 0 2 0 0 0 0 0 0 0 1 0 1 0 0 4 1 ...

output:

73685347

result:

ok 1 number(s): "73685347"

Test #8:

score: 0
Accepted
time: 771ms
memory: 4232kb

input:

36000 30000
0 3 4 1 2 1 1 0 0 1 1 0 1 0 2 1 0 0 0 0 2 1 0 2 0 0 0 0 0 1 1 4 1 4 0 0 2 0 0 0 1 1 0 0 1 1 0 1 0 0 0 0 1 1 0 3 1 1 1 0 0 0 0 0 0 1 2 0 2 3 0 0 0 0 3 1 0 0 0 1 0 1 2 0 0 2 0 1 0 0 2 1 1 0 3 1 6 0 0 1 1 2 0 1 2 0 0 1 0 1 1 0 0 1 0 0 0 1 0 2 0 1 1 1 0 0 5 2 0 5 1 0 0 0 0 1 1 1 8 0 1 1 0 1 ...

output:

658813003

result:

ok 1 number(s): "658813003"

Test #9:

score: -100
Time Limit Exceeded

input:

200000 200000
0 1 1 1 1 1 0 1 0 3 1 0 0 1 1 0 1 1 1 2 3 0 1 0 1 0 2 5 0 1 1 4 1 1 0 0 0 0 0 0 2 1 0 0 2 1 1 2 0 3 0 1 3 0 1 1 1 0 1 0 1 2 0 1 1 0 0 2 2 1 0 1 1 2 4 1 0 2 0 5 1 2 0 0 1 0 2 3 1 0 1 1 1 1 0 0 0 5 1 0 0 1 2 1 1 0 0 0 1 0 0 1 2 1 0 0 2 1 2 3 0 0 3 1 0 1 0 0 1 1 1 0 1 0 1 0 0 1 0 0 1 1 1 ...

output:


result: