1. 程式人生 > 實用技巧 >《省賽補題》

《省賽補題》

B:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<double,int> pii;
const int N = 1e6+5;
const int M = 1e6+5;
const LL Mod = 1e9+7;
#define rg register
#define pi acos(-1)
#define INF 1e18
#define CT0 cin.tie(0),cout.tie(0)
#define IO ios::sync_with_stdio(false)
#define
dbg(ax) cout << "now this num is " << ax << endl; namespace FASTIO{ inline int read() { int x = 0,f = 1;char c = getchar(); while(c < '0' || c > '9'){if(c == '-')f = -1;c = getchar();} while(c >= '0' && c <= '9'){x = (x << 1) + (x << 3
) + (c ^ 48);c = getchar();} return x * f; } void print(int x){ if(x < 0){x = -x;putchar('-');} if(x > 9) print(x/10); putchar(x%10+'0'); } } using namespace FASTIO; int a[N]; struct Node{int L,r,maxx;}node[N << 2]; void Pushup(int idx){node[idx].maxx = max(node[idx << 1
].maxx,node[idx << 1 | 1].maxx);} void build(int L,int r,int idx) { node[idx].L = L,node[idx].r = r,node[idx].maxx = 0; if(L == r) return ; int mid = (L + r) >> 1; build(L,mid,idx << 1); build(mid + 1,r,idx << 1 | 1); } int query(int x,int idx) { if(node[idx].L == node[idx].r) return node[idx].L; int mid = (node[idx].L + node[idx].r) >> 1; if(node[idx << 1].maxx >= x) return query(x,idx << 1); else return query(x,idx << 1 | 1); } void update(int x,int val,int idx) { if(node[idx].L == node[idx].r) { node[idx].maxx += val; return ; } int mid = (node[idx].L + node[idx].r) >> 1; if(mid >= x) update(x,val,idx << 1); else update(x,val,idx << 1 | 1); Pushup(idx); } set<int> S; map<int,int> mp; int main() { int ca;ca = read(); while(ca--) { int n,c;n = read(),c = read(); for(rg int i = 1;i <= n;++i) a[i] = read(); int num1 = 0,num2 = 0; build(1,n,1); for(rg int i = 1;i <= n;++i) { int pos = -1; if(node[1].maxx >= a[i]) pos = query(a[i],1); if(pos == -1) ++num1,update(num1,c - a[i],1); else update(pos,-a[i],1); } S.clear();mp.clear(); for(rg int i = 1;i <= n;++i) { auto ip = S.end(); if(S.size() == 0 || *(--ip) < a[i]) num2++,S.insert(c - a[i]),mp[c - a[i]]++; else { auto pos = S.lower_bound(a[i]); int tmp = *pos - a[i]; mp[*pos]--; mp[tmp]++; if(mp[*pos] == 0) S.erase(*pos); if(mp[tmp] == 1) S.insert(tmp); } } printf("%d %d\n",num1,num2); } system("pause"); return 0; }
View Code