Codeforces Round #381 (Div. 1)
来源:互联网 发布:国家数据保密协议 编辑:程序博客网 时间:2024/06/03 18:59
Alyona's mother wants to present an array of n non-negative integers to Alyona. The array should be special.
Alyona is a capricious girl so after she gets the array, she inspects m of its subarrays. Subarray is a set of some subsequent elements of the array. The i-th subarray is described with two integers li and ri, and its elements are a[li], a[li + 1], ..., a[ri].
Alyona is going to find mex for each of the chosen subarrays. Among these m mexes the girl is going to find the smallest. She wants this minimum mex to be as large as possible.
You are to find an array a of n elements so that the minimum mex among those chosen by Alyona subarrays is as large as possible.
The mex of a set S is a minimum possible non-negative integer that is not in S.
The first line contains two integers n and m (1 ≤ n, m ≤ 105).
The next m lines contain information about the subarrays chosen by Alyona. The i-th of these lines contains two integers li and ri(1 ≤ li ≤ ri ≤ n), that describe the subarray a[li], a[li + 1], ..., a[ri].
In the first line print single integer — the maximum possible minimum mex.
In the second line print n integers — the array a. All the elements in a should be between 0 and 109.
It is guaranteed that there is an optimal answer in which all the elements in a are between 0 and 109.
If there are multiple solutions, print any of them.
5 31 32 54 5
21 0 2 1 0
4 21 42 4
35 2 0 1
The first example: the mex of the subarray (1, 3) is equal to 3, the mex of the subarray (2, 5) is equal to 3, the mex of the subarray (4, 5) is equal to 2 as well, thus the minumal mex among the subarrays chosen by Alyona is equal to 2.
#include <cstdio>#include <algorithm>using namespace std;int main(){int n, m, a, b;scanf("%d%d", &n, &m);int mex = 1000000;for (int i = 0; i < m; i++){scanf("%d%d", &a, &b);mex = min(b - a, mex);}printf("%d\n", mex + 1);for (int i = 1; i <= n; i++)printf("%d ", i % (mex + 1));printf("\n");return 0;}
#include <cstdio>#include <vector>#include <algorithm>using namespace std;const int N = 200005;typedef long long ll;int n, val[N], ans[N], add[N];vector <pair<int, int> > edge[N];vector <pair<ll, int> > DIS;void dfs(int x, ll dis){ DIS.push_back(make_pair(dis, x)); int pos = lower_bound(DIS.begin(), DIS.end(), make_pair(dis - val[x], -1)) - DIS.begin(); if (pos > 0) add[DIS[pos - 1].second] --; if (DIS.size() > 1) add[DIS[DIS.size() - 2].second] ++; for (int i = 0; i < edge[x].size(); i++) { int child = edge[x][i].first; int w = edge[x][i].second; dfs(child, dis + w); add[x] += add[child]; } ans[x] = add[x]; DIS.pop_back();}int main(){ scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &val[i]); for (int i = 2; i <= n; i++) { int a, b; scanf("%d%d", &a, &b); edge[a].push_back(make_pair(i, b)); } dfs (1, 0); for (int i = 1; i <= n; i++) printf("%d ", ans[i]); return 0;}
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 × 1 × 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row.
Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them.
Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≤ k ≤ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar.
After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it.
The first line contain single integer n (1 ≤ n ≤ 3·105) — the number of towers.
The second line contain n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the number of cubes in each tower.
The third line contain single integer m (1 ≤ m ≤ 3·105) — the number of additions.
The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≤ l ≤ r ≤ n, 1 ≤ di ≤ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri.
Print m lines. In i-th line print the maximum width of the hills after the i-th addition.
55 5 5 5 531 3 22 2 14 4 1
245
The first sample is as follows:
After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2.
After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4.
After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
#include <cstdio>#include <algorithm>using namespace std;const int MAXN = 300010;typedef long long ll;int n, M, height[MAXN];ll minus[MAXN];struct TREENODE{ int l, m, r;}node[4 * MAXN];int sign(ll x){ if (x > 0) return 1; if (x < 0) return -1; return 0;}void push_up(int l, int r, int o){ int m = (l + r) / 2; node[o].m = max(node[o * 2].m, node[o * 2 + 1].m); node[o].l = node[o * 2].l; node[o].r = node[o * 2 + 1].r; if (!!minus[m] && !!minus[m + 1] && sign(minus[m]) >= sign(minus[m + 1])) { node[o].m = max(node[o].m, node[o * 2].r + node[o * 2 + 1].l); if (node[o * 2].m == (m - l + 1)) node[o].l = node[o * 2].l + node[o * 2 + 1].l; if (node[o * 2 + 1].m == (r - m)) node[o].r = node[o * 2 + 1].r + node[o * 2].r; }}void build_tree(int l, int r, int o){ if (l == r) { int num = !!minus[l]; node[o] = {num, num, num}; return ; } int m = (l + r) / 2; build_tree(l, m, o * 2); build_tree(m + 1, r, o * 2 + 1); push_up(l, r, o);}void update(int l, int r, int o, int pos, int d){ if (l == r) { minus[pos] += d; int num = !!minus[pos]; node[o] = {num, num, num}; return ; } int m = (l + r) / 2; if (pos <= m) update(l, m, o * 2, pos, d); else update(m + 1, r, o * 2 + 1, pos, d); push_up(l, r, o);}int main(){ scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &height[i]); for (int i = 1; i < n; i++) minus[i] = height[i + 1] - height[i]; if (n != 1) build_tree(1, n - 1 , 1); scanf("%d", &M); int l, r, delta; for (int i = 1; i <= M; i++) { if (n == 1) { printf("1\n"); continue; } scanf("%d%d%d", &l, &r, &delta); if (l != 1) update(1, n - 1, 1, l - 1, delta); if (r != n) update(1, n - 1, 1, r, -delta); printf("%d\n", node[1].m + 1); } return 0;}
- Codeforces Round #381 (Div. 1)
- Codeforces Round #381 (Div. 1)
- Codeforces Round #381 (Div. 1) C
- Codeforces Round #381 (Div. 2)
- Codeforces Round #381 (Div. 2)
- Codeforces Round #381 (Div. 2)
- Codeforces Round #110 (Div. 1)
- Codeforces Round #138 (Div. 1)
- Codeforces Round #140 (Div. 1)
- Codeforces Round #153 (Div. 1)
- Codeforces Round #157 (Div. 1)
- Codeforces Round #160 (Div. 1)
- Codeforces Round #162 (Div. 1)
- Codeforces Round #165 (Div. 1)
- Codeforces Round #165 (Div. 1)
- Codeforces Round #167 (Div. 1)
- Codeforces Round #168 (Div. 1)
- Codeforces Round #174 (Div. 1)
- 设计模式之Iterator和容器与迭代器遍历
- VC 获取打印机与打印作业的状态
- 'module' object has no attribute 在python开发中出现的原因
- Android图表库MPAndroidChart(十三)——简约的底部柱状图
- Vue.js实践:一个Node.js+mongoDB+Vue.js的博客内容管理系统
- Codeforces Round #381 (Div. 1)
- mysql left join
- SSE指令指令集进行程序加速、DCT的优化处理
- 学习Javascript闭包(Closure)
- C++11特性:列表初始化:VS2010中vector<string>的初始化方式
- 登录页面设计(二)
- 图像形状特征(三)--链码及形状数
- 简单的,父子进程间通过消息队列通信。
- Centos7开机自动启动服务和联网