题解:AT_arc210_e [ARC210E] Subset Sum Gaps

· · 题解

题目传送门

分析

这道题可以这么考虑:每次加入一个 a_i,就暴力记录,然后考虑合并。

每次输入一个 a_i 后,暴力加入。把满足 s_i\times 1.01 \le s_{i+1} 的 i,进行合并,表示这一块都不能输出。

那么输出的时候就输出相邻的两个块的左右端点即可。

因为 n 就只有 5000,而且还是指数增长的,所以复杂度只有 O(\log_{1.01} \sum a_i)。

代码

#include<bits/stdc++.h>
using namespace std ;
#define ll long long
const ll kMaxN = 1e5 + 5 , MOD = 998244353 , INF = 1e18 ;
int n ;
struct node
{
  ll x , y , w ;
  bool operator < (const node &a) const
  {
    if(x == a.x)
    {
      if(y == a.y) return w < a.w ;
      return y < a.y ;
    }
    return x < a.x ;
  }
};
vector<node> ve1 , ve2 ;
void solve()
{
  cin >> n ;
  ve1.push_back({0 , 0 , 1}) ;
  for( int i = 1 ; i <= n ; i++ )
  {
    ll x ;
    cin >> x ;
    ve2 = ve1 ;
    for( auto it : ve1 )
    {
      ve2.push_back({it.x + x , it.y + x , it.w}) ;
    }
    sort(ve2.begin() , ve2.end()) ;
    ve1.clear() ;
    for( auto it : ve2 )
    {
      if(ve1.empty() || ve1.back().y * 101 <= it.x * 100) 
      {
        ve1.push_back(it) ;
      }
      else
      {
        ve1.back().y = max(ve1.back().y , it.y) ;
        ve1.back().w = (ve1.back().w + it.w) % MOD ;
      }
    }
  } 
  cout << ve1.size() - 1 << "\n" ;
  ll nw = 0 ;
  for( int i = 0 ; i + 1 < ve1.size() ; i++ )
  {
    nw = (nw + ve1[i].w) % MOD ;
    cout << ve1[i].y << " " << ve1[i + 1].x << " " << nw << "\n" ;
  }
}
signed main()
{
    ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
    freopen("subset.in" , "r" , stdin) ;
    freopen("subset.out" , "w" , stdout) ;
    int oT_To = 1 ;
//  cin >> oT_To ;
    while(oT_To--) solve() ;
    return 0 ;
}