题解:CF2002E Cosmic Rays
CF2002E Cosmic Rays
首先,我们看到题面之后可以进行一步转化。不难发现,原题等价于给定
- 将所有还存在的二元组的
a_i 变为a_i - 1 。 - 如果存在二元组满足
a_i = 0 , 将其删除,其余二元组相对顺序不变。 - 如果存在两个相邻的二元组满足
b_i = b_{i + 1} , 将二者删除,并插入一个新的二元组(a_i + a_{i + 1},b_i) 。
最后查询多少次操作可以删去全部
首先考虑
又由于题目要求对于
具体的,由于
更进一步的,设当前二元组
令
如果当前
如果当前
对于
于是我们就做完了,别忘了开
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 3e5 + 10;
int T,n,top,stk[N];
ll dp[N];
pair <ll,int> a[N];
void solve() {
cin >> n;
for(int i = 1;i <= n;i++) cin >> a[i].first >> a[i].second;
for(int i = 1;i <= n;i++) {
ll cost = 0;
while(top && (a[i].first > a[stk[top]].first || a[i].second == a[stk[top]].second)) {
if(a[i].second != a[stk[top]].second) cost = a[stk[top]].first;
else {
a[i].first -= cost;
a[stk[top]].first -= cost;
a[i].first += a[stk[top]].first;
a[i].first += cost; cost = 0;
}
top--;
}
if(top) dp[i] = dp[stk[top]];
else dp[i] = a[i].first;
//cout << a[i].first << "\n";
cout << dp[i] << " ";
stk[++top] = i;
}
cout << "\n";
top = 0;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
cin >> T;
while(T--) solve();
return 0;
}