QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#200905#6355. 5ucup-team859TL 463ms4200kbC++142.3kb2023-10-04 23:34:212023-10-04 23:34:21

Judging History

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

  • [2023-10-04 23:34:21]
  • 评测
  • 测评结果:TL
  • 用时:463ms
  • 内存:4200kb
  • [2023-10-04 23:34:21]
  • 提交

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 inside(pair<int, int> &x, pair<int, int> &y) {
  return (x.first <= y.first && y.second <= x.second);
}

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

inline void merge(vector<pair<int, int>> &x, vector<pair<int, int>> &y, int add = 1) {
  for (auto interval : y) {
    auto to_merge = make_pair(interval.first + add, interval.second + add);
    bool bad = false;
    for (auto it = x.begin(); !x.empty() && it != x.end();) {
      if (intersect(*it, to_merge)) {
        if (inside(*it, to_merge)) {
          bad = true;
          break;
        }
        to_merge.first = min(to_merge.first, it->first);
        to_merge.second = max(to_merge.second, it->second);
        auto tmp = next(it);
        x.erase(it);
        it = tmp;
      } else {
        it = next(it);
      }
    }

    if (!bad)
      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);
  int sx = 0;
  for (int val = 0; val <= s; ++val) {
    if (!cnt[val])
      continue;

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

    sx += val * cnt[val];
    for (int j = sx; 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;
}

詳細信息

Test #1:

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

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: 3744kb

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: 3808kb

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: 3460kb

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: 3532kb

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: 3608kb

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: 52ms
memory: 3892kb

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: 463ms
memory: 4200kb

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: