Codeforces Round #576 (Div. 2) D. Welfare State||Codeforces Round #576 (Div. 1) B Welfare State
Welfare State
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
There is a country with n citizens. The i-th of them initially has ai money. The government strictly controls the wealth of its citizens. Whenever a citizen makes a purchase or earns some money, they must send a receipt to the social services mentioning the amount of money they currently have.
Sometimes the government makes payouts to the poor: all citizens who have strictly less money than x are paid accordingly so that after the payout they have exactly x money. In this case the citizens don't send a receipt.
You know the initial wealth of every citizen and the log of all events: receipts and payouts. Restore the amount of money each citizen has after all events.
Input
The first line contains a single integer n (1≤n≤2e5) — the numer of citizens.
The next line contains n integers a1, a2, ..., an (0≤ai≤1e9) — the initial balances of citizens.
The next line contains a single integer q(1≤q≤2e5) — the number of events.
Each of the next q lines contains a single event. The events are given in chronological order.
Each event is described as either 1 p x (1≤p≤n, 0≤x≤1e9), or 2 x (0≤x≤1e9). In the first case we have a receipt that the balance of the p-th person becomes equal to x. In the second case we have a payoff with parameter x.
Output
Print n integers — the balances of all citizens after all events.
题意:
有个国家想劫富济贫,所以会给你n个人的财富ai,然后有两种操作,1,x,y的意思是把ax人的资产修改成y;2,x的意思是将所有资产低于x的人的资产全部改成x。
输出最后所有人的资产
思路:
我们可以分别去想每一个公民,一个公民的最终资产就是,max(最后一次的1操作的值,之后的最大的2操作的值),所以输入的每一个1,修改资产并用pos数组记录这个人这次修改的操作序号,每次2操作,用一个sec数组记录这次的值,然后从序号q向1把最大值更新给前面,因为只有之后的最大值才有用。然后1...n遍历max(a[i],sec[pos[i]+1]
代码:
#include<iostream> using namespace std; const int N=2e5+10; int n,a[N],q,o,p,x,pos[N],sec[N]; int main() { cin>>n; for(int i=1; i<=n; i++)cin>>a[i]; cin>>q; for(int i=1; i<=q; i++) { cin>>o; if(o==1) { cin>>p>>x; a[p]=x;//修改下标为p的人的资产 pos[p]=i;//记录下标为p的人资产修改的最后一次操作序号i } else cin>>sec[i];//记录操作2的值 } for(int i=q; i>=1; i--) sec[i] = max(sec[i],sec[i+1]);//把操作2的最大值向回传递 for(int i=1; i<=n; i++) cout<<max(a[i],sec[pos[i]+1])<< " ";//max(最后一次的1操作的值,之后的最大的2操作的值) return 0; }