A. Nearest Minimums

You are given an array of n integer numbers a0, a1, …, an - 1. Find the distance between two closest (nearest) minimums in it. It is guaranteed that in the array a minimum occurs at least two times.

Input
The first line contains positive integer n (2 ≤ n ≤ 105) — size of the given array. The second line contains n integers a0, a1, …, an - 1 (1 ≤ ai ≤ 109) — elements of the array. It is guaranteed that in the array a minimum occurs at least two times.

Output
Print the only number — distance between two nearest minimums in the array.

Examples
input
2
3 3
output
1
input
3
5 6 5
output
2
input
9
2 1 3 5 4 1 2 3 1
output
3

  • 题意:问你最近的两个最小数的距离是多少
  • 题解:。。。。直接xjb求。。。
#include<bits/stdc++.h>
using namespace std;int main()
{int n;while (cin >> n){vector<int> v;int Min = 1e9 + 7;for (int i = 0; i < n; i ++){int x;cin >> x;v.push_back(x);Min = min(x, Min);}int cur = -1;int dis = 1e9;for (int i = 0; i < n; i ++){if (v[i] != Min)continue;if (cur == -1)cur = i;else{dis = min(i - cur, dis);cur = i;}}cout << dis << endl;}
}

B. Two Cakes

It’s New Year’s Eve soon, so Ivan decided it’s high time he started setting the table. Ivan has bought two cakes and cut them into pieces: the first cake has been cut into a pieces, and the second one — into b pieces.

Ivan knows that there will be n people at the celebration (including himself), so Ivan has set n plates for the cakes. Now he is thinking about how to distribute the cakes between the plates. Ivan wants to do it in such a way that all following conditions are met:

Each piece of each cake is put on some plate;
Each plate contains at least one piece of cake;
No plate contains pieces of both cakes.
To make his guests happy, Ivan wants to distribute the cakes in such a way that the minimum number of pieces on the plate is maximized. Formally, Ivan wants to know the maximum possible number x such that he can distribute the cakes according to the aforementioned conditions, and each plate will contain at least x pieces of cake.

Help Ivan to calculate this number x!

Input
The first line contains three integers n, a and b (1 ≤ a, b ≤ 100, 2 ≤ n ≤ a + b) — the number of plates, the number of pieces of the first cake, and the number of pieces of the second cake, respectively.

Output
Print the maximum possible number x such that Ivan can distribute the cake in such a way that each plate will contain at least x pieces of cake.

Examples
input
5 2 3
output
1
input
4 7 10
output
3
Note
In the first example there is only one way to distribute cakes to plates, all of them will have 1 cake on it.

In the second example you can have two plates with 3 and 4 pieces of the first cake and two plates both with 5 pieces of the second cake. Minimal number of pieces is 3.

  • 题意:现在有n个盘子,a个第一种披萨,b个第二种披萨,每个盘子只能装若干块同种披萨,问你最多能让最少的盘子里有几个披萨。
  • 题解:不如再瞎搞一下。对他就是问你哪种分配策略能让最少的盘子里装的最多。我们确定一种分配需要确定有几个盘子放a或者几个盘子放b,然后再贪心放披萨就能得到当前的最优质,在所有的最优里面取最优就行了。我们暴力i个盘子放a种披萨。
#include<bits/stdc++.h>
using namespace std;int main()
{int n, a, b;while (cin >> n >> a >> b){int Max = 0;for (int i = 1; i < n; i++){int ret = n - i;Max = max(Max, min(b / ret, a / i));}cout << Max << endl;}
}

C. Three Garlands

Mishka is decorating the Christmas tree. He has got three garlands, and all of them will be put on the tree. After that Mishka will switch these garlands on.

When a garland is switched on, it periodically changes its state — sometimes it is lit, sometimes not. Formally, if i-th garland is switched on during x-th second, then it is lit only during seconds x, x + ki, x + 2ki, x + 3ki and so on.

Mishka wants to switch on the garlands in such a way that during each second after switching the garlands on there would be at least one lit garland. Formally, Mishka wants to choose three integers x1, x2 and x3 (not necessarily distinct) so that he will switch on the first garland during x1-th second, the second one — during x2-th second, and the third one — during x3-th second, respectively, and during each second starting from max(x1, x2, x3) at least one garland will be lit.

Help Mishka by telling him if it is possible to do this!

Input
The first line contains three integers k1, k2 and k3 (1 ≤ ki ≤ 1500) — time intervals of the garlands.

Output
If Mishka can choose moments of time to switch on the garlands in such a way that each second after switching the garlands on at least one garland will be lit, print YES.

Otherwise, print NO.

Examples
input
2 2 3
output
YES
input
4 2 3
output
NO
Note
In the first example Mishka can choose x1 = 1, x2 = 2, x3 = 1. The first garland will be lit during seconds 1, 3, 5, 7, …, the second — 2, 4, 6, 8, …, which already cover all the seconds after the 2-nd one. It doesn’t even matter what x3 is chosen. Our choice will lead third to be lit during seconds 1, 4, 7, 10, …, though.

In the second example there is no way to choose such moments of time, there always be some seconds when no garland is lit.

  • 题意:给你3个灯,每个灯有一个闪烁间隔,问你能不能找到一种让每个灯的开始时间,使在三个灯全部亮过一遍之后每个时刻都至少有一个灯在亮。
  • 题解:不会 下一个。
#include<bits/stdc++.h>
using namespace std;int main()
{int a, b, c;while (cin >> a >> b >> c){int cnt[5];memset(cnt, 0, sizeof(cnt));if (a < 5)cnt[a] ++;if (b < 5)cnt[b] ++;if (c < 5)cnt[c] ++;if (cnt[1])cout << "YES" << endl;else if (cnt[2] >= 2)cout << "YES" << endl;else if (cnt[3] == 3)cout << "YES" << endl;else if (cnt[4] == 2 && cnt[2] == 1)cout << "YES" << endl;elsecout << "NO" << endl;}
}

D. Inversion Counting

A permutation of size n is an array of size n such that each integer from 1 to n occurs exactly once in this array. An inversion in a permutation p is a pair of indices (i, j) such that i > j and ai < aj. For example, a permutation [4, 1, 3, 2] contains 4 inversions: (2, 1), (3, 1), (4, 1), (4, 3).

You are given a permutation a of size n and m queries to it. Each query is represented by two indices l and r denoting that you have to reverse the segment [l, r] of the permutation. For example, if a = [1, 2, 3, 4] and a query l = 2, r = 4 is applied, then the resulting permutation is [1, 4, 3, 2].

After each query you have to determine whether the number of inversions is odd or even.

Input
The first line contains one integer n (1 ≤ n ≤ 1500) — the size of the permutation.

The second line contains n integers a1, a2, …, an (1 ≤ ai ≤ n) — the elements of the permutation. These integers are pairwise distinct.

The third line contains one integer m (1 ≤ m ≤ 2·105) — the number of queries to process.

Then m lines follow, i-th line containing two integers li, ri (1 ≤ li ≤ ri ≤ n) denoting that i-th query is to reverse a segment [li, ri] of the permutation. All queries are performed one after another.

Output
Print m lines. i-th of them must be equal to odd if the number of inversions in the permutation after i-th query is odd, and even otherwise.

Examples
input
3
1 2 3
2
1 2
2 3
output
odd
even
input
4
1 2 4 3
4
1 1
1 4
1 4
2 3
output
odd
odd
odd
even

  • 题意:给你一个排列,有m次操作,每次操作翻转一个区间,问你翻转后的排列逆序对是奇数个还是偶数个
  • 题解:给定的区间将原排列分为三段,翻转区间,区间左侧区间,区间右侧区间。显然,区间翻转对该区间左右侧的区间的逆序对没有影响。只需要考虑翻转区间。设翻转区间的逆序对有x个,那么翻转后逆序对都变成顺序对,顺序对变成逆序对。对整个排列的贡献为原排列逆序对 - 翻转前逆序对 + 翻转前顺序对。又可知对一个区间,他的总数对 = 顺序对 + 逆序对 = 区间长度 * (区间长度 - 1) / 2。分类讨论如果区间总对数为偶数,那么顺序对逆序对奇偶性相同,不影响总的奇偶性。同理为奇数会影响,所以我们只需要计算区间总数对就可以了。
#include<bits/stdc++.h>
using namespace std;int a[10000];
int dp[2000][2000];
int main()
{int n;while (cin >> n){for (int i = 1; i <= n; i++)cin >> a[i];int ans = 0;for (int i = 1; i <= n; i++){for (int j = i + 1; j <= n; j++){if (a[i] > a[j])ans ++;}}int flag ;if (ans % 2)flag = 1;else flag = 0;int m;cin >> m;for (int i = 0; i < m; i++){int l, r;cin >> l >> r;long long temp = (r - l + 1) * (r - l) / 2;flag ^= (temp % 2);if (flag)cout << "odd" << endl;elsecout << "even" << endl;}}
}

E. Stack Sorting

  • 题意:给你一排列的一部分,让你补全整个排列使其字典序最大并且经过一个栈调整顺序之后能够顺序输出
  • 题解:他给了你开头,我们先检验他给的部分会不会已经不满足条件,如果可能可以满足条件,那么我们自己开始构造。他给你的序列是完整序列被切割成几段,每段使其倒序,就可以构造出字典序最大,最后再检验一下可不可以就行了。
    不过我看我们学校大佬都不是这样做的好像?
#include<bits/stdc++.h>
using namespace std;const int N = 1e6;int a[N];
int vis[N];
int main()
{int n, k;while (cin >> n >> k){int Max = 0;memset(vis, 0, sizeof(vis));for (int i = 1; i <= k; i++){cin >> a[i];Max = max(a[i], Max);}int cur = 1;stack<int> st;for (int i = 1; i <= k; i++){vis[a[i]] = 1;if (a[i] == cur){cur ++;while (!st.empty() && st.top() == cur){st.pop();cur ++;}}elsest.push(a[i]);}if (vis[cur] && st.top() != cur)cout << -1 << endl;else{int curcnt = k;vector<int> v;for (int i = 1; i <= Max; i++){if (!vis[i]){v.clear();while (!vis[i])v.push_back(i++);reverse(v.begin(), v.end());for (auto x : v)a[++curcnt] = x;i--;}}for (int i = n; i > Max; i--)a[++curcnt] = i;for (int i = k + 1; i <= n; i++){vis[a[i]] = 1;if (a[i] == cur){cur ++;while (!st.empty() && st.top() == cur)st.pop(), cur ++;}elsest.push(a[i]);}if (!st.empty())cout << -1 << endl;else{for (int i = 1; i <= n; i++){if (i != 1)cout << " ";cout << a[i];}}cout << endl;}}
}

F. Tree Destruction

You are given an unweighted tree with n vertices. Then n - 1 following operations are applied to the tree. A single operation consists of the following steps:

choose two leaves;
add the length of the simple path between them to the answer;
remove one of the chosen leaves from the tree.
Initial answer (before applying operations) is 0. Obviously after n - 1 such operations the tree will consist of a single vertex.

Calculate the maximal possible answer you can achieve, and construct a sequence of operations that allows you to achieve this answer!

Input
The first line contains one integer number n (2 ≤ n ≤ 2·105) — the number of vertices in the tree.

Next n - 1 lines describe the edges of the tree in form ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi). It is guaranteed that given graph is a tree.

Output
In the first line print one integer number — maximal possible answer.

In the next n - 1 lines print the operations in order of their applying in format ai, bi, ci, where ai, bi — pair of the leaves that are chosen in the current operation (1 ≤ ai, bi ≤ n), ci (1 ≤ ci ≤ n, ci = ai or ci = bi) — choosen leaf that is removed from the tree in the current operation.

See the examples for better understanding.

Examples
input
3
1 2
1 3
output
3
2 3 3
2 1 1
input
5
1 2
1 3
2 4
2 5
output
9
3 5 5
4 3 3
4 1 1
4 2 2

  • 题意:给你一棵树,每次挑选这棵树的两个叶子,加上他们之间的边数,然后将其中一个点去掉,问你边数之和最大可以是多少。
  • 题解:其实。。。很简单。首先离每个叶子最远的点必定是在树的直径上。那么我们可以先把这棵树的直径找出来,把树的点分为在直径上和不在直径上两种。首先我们如果 先处理直径直径会变短贡献肯定会变小。所以我们先处理非直径上的点,他必定会挑直径两端点中的一个点然后把自己去掉。这样处理完之后只剩直径了,直径只能慢慢缩短。。。从哪边都一样。
#include<bits/stdc++.h>
using namespace std;const int N = 3e5 + 7;vector<int> path;
vector<int> G[N];
int dis[N];
int fa[N][22];
int inpath[N];
int L[N];
int tot = 0;
int Max_Len = 0;
int root_u, root_v;
void init()
{Max_Len = 0;path.clear();tot = 0;memset(inpath,0,sizeof(inpath));for(int i = 0;i < N;i++)G[i].clear();
}
void add_edge(int u,int v)
{G[u].push_back(v);G[v].push_back(u);
}
void init_lca(int u,int f,int d)
{dis[u] = d;fa[u][0] = f;if(dis[u] >= Max_Len)Max_Len = dis[u], root_u = u;for(int i = 1;i<22;i++)fa[u][i] = fa[fa[u][i - 1]][i - 1];for(auto v : G[u]){if(v == f)continue;init_lca(v,u,d+1);}
}
int lca(int u,int v)
{if(dis[u] < dis[v])swap(u,v);for(int i = 21;i >= 0;i--)if(dis[fa[u][i]] >= dis[v])u = fa[u][i];if(u == v)return u;for(int i = 21;i >= 0;i--)if(fa[u][i] != fa[v][i])u = fa[u][i], v = fa[v][i];return fa[u][0];
}
void find_root(int u,int fa,int d)
{L[u] = ++ tot;if(d >= Max_Len)root_v = u,Max_Len = d;for(auto v : G[u]){if(v == fa)continue;find_root(v,u,d + 1);}
}
bool find_path(int u,int fa)
{if(u == root_v){inpath[u] = 1;path.push_back(u);return true;}for(auto v : G[u]){if(v == fa)continue;if(find_path(v,u)){inpath[u] = 1;path.push_back(u);return true;}}return false;
}
vector<pair<int,int> > ans;
int main()
{ios :: sync_with_stdio(false);int n;while(cin >> n){init();for(int i = 0;i < n - 1;i++){int u,v;cin >> u >> v;add_edge(u,v);      }init_lca(1,1,0);find_root(root_u,root_u,0);find_path(root_u,root_u);ans.clear();long long sum = 0;//cout << root_u << " " << root_v << " " << Max_Len << endl;for(int i = 1;i <= n;i++){if(!inpath[i]){long long ans1 = dis[root_u] + dis[i] - 2 * dis[lca(root_u,i)];long long ans2 = dis[root_v] + dis[i] - 2 * dis[lca(root_v,i)];sum += max(ans1,ans2);if(ans1 > ans2)ans.push_back(make_pair(root_u,i));elseans.push_back(make_pair(root_v,i));}}sort(ans.begin(),ans.end(),[](pair<int,int> a,pair<int,int> b){return L[a.second] > L[b.second];});for(int i = 0;i < path.size() - 1;i ++){sum += dis[path[i]] + dis[root_u] - 2 * dis[lca(path[i],root_u)];ans.push_back(make_pair(root_u,path[i]));}cout << sum << endl;for(auto temp : ans)cout << temp.first <<  " " << temp.second << " " << temp.second << endl;}
}

G. Mass Change Queries

You are given an array a consisting of n integers. You have to process q queries to this array; each query is given as four numbers l, r, x and y, denoting that for every i such that l ≤ i ≤ r and ai = x you have to set ai equal to y.

Print the array after all queries are processed.

Input
The first line contains one integer n (1 ≤ n ≤ 200000) — the size of array a.

The second line contains n integers a1, a2, …, an (1 ≤ ai ≤ 100) — the elements of array a.

The third line contains one integer q (1 ≤ q ≤ 200000) — the number of queries you have to process.

Then q lines follow. i-th line contains four integers l, r, x and y denoting i-th query (1 ≤ l ≤ r ≤ n, 1 ≤ x, y ≤ 100).

Output
Print n integers — elements of array a after all changes are made.

Example
input
5
1 2 3 4 5
3
3 5 3 5
1 5 5 1
1 5 1 5
output
5 2 5 4 5

  • 题意:给你一个数组,若干次更新,每次更新l,r区间内值为x的元素将它们改为y
  • 题解:观察到元素只有100种,我们先不更新元素,考虑更新这个变换。初始变换posipos_i = i可暴力更新变换。最后查询的时候将所有变换全部更新到叶子,就是这个位置最终的变换。
#include<bits/stdc++.h>
using namespace std;const int N = 3e5 + 7;int a[N];struct Tree
{int l,r;int pos[101];int vis;
}t[N*4];
void build(int l,int r,int step)
{t[step].l = l, t[step].r = r;for(int i = 1;i <= 100;i++)t[step].pos[i] = i;if(l == r)return;int mid = (l + r) / 2;build(l, mid, step * 2);build(mid + 1, r,step * 2 + 1);
}
void push_down(int step)
{if(!t[step].vis)return;for(int i = 1;i <= 100;i++){t[step * 2].pos[i] = t[step].pos[t[step * 2].pos[i]];t[step * 2 + 1].pos[i] = t[step].pos[t[step * 2 + 1].pos[i]];}for(int i = 1;i <= 100;i++)t[step].pos[i] = i;t[step].vis = 0;t[step * 2].vis = t[step * 2 + 1].vis = 1;
}
void update(int l,int r,pair<int,int> change,int step)
{if(t[step].l == l && t[step].r == r){for(int i = 1;i <= 100;i++){if(t[step].pos[i] == change.first)t[step].pos[i] = change.second;}t[step].vis = 1;return;}int mid = (t[step].l + t[step].r) / 2;push_down(step);if(r <= mid)update(l,r,change,step * 2);else if(l > mid )update(l,r,change,step * 2 + 1);elseupdate(l,mid,change,step * 2), update(mid + 1,r,change,step * 2 + 1);
}
void query(int l,int r,int step)
{if(l == r){a[l] = t[step].pos[a[l]];return;}push_down(step);int mid = (t[step].l + t[step].r) / 2;query(l,mid,step*2);query(mid + 1,r,step *2 + 1);
}
int main()
{ios :: sync_with_stdio(false);int n;while(cin >> n){for(int i = 1;i <= n;i ++)cin >> a[i];build(1,n,1);int m;cin >> m;while(m --){int l,r,x,y;cin >> l >> r >> x >> y;update(l,r,make_pair(x,y),1);}query(1,n,1);for(int i = 1;i <= n;i++){if(i != 1)cout << " ";cout << a[i];}cout << endl;}
}

Codeforces911题解相关推荐

  1. [JS][dfs]题解 | #迷宫问题#

    题解 | #迷宫问题# 题目链接 迷宫问题 题目描述 定义一个二维数组 N*M ,如 5 × 5 数组下所示: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 1, 1 ...

  2. [JS][dp]题解 | #打家劫舍(一)#

    题解 | #打家劫舍(一)# 题目链接 打家劫舍(一) 题目描述 描述 你是一个经验丰富的小偷,准备偷沿街的一排房间,每个房间都存有一定的现金,为了防止被发现,你不能偷相邻的两家,即,如果偷了第一家, ...

  3. [JS]题解 | #魔法数字#

    题解 | #魔法数字# 题目链接 魔法数字 题目描述 牛妹给牛牛写了一个数字n,然后又给自己写了一个数字m,她希望牛牛能执行最少的操作将他的数字转化成自己的. 操作共有三种,如下: 在当前数字的基础上 ...

  4. [JS]题解 | #岛屿数量#

    题解 | #岛屿数量# 题目链接 岛屿数量 题目描述 时间限制:1秒 空间限制:256M 描述 给一个01矩阵,1代表是陆地,0代表海洋, 如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右 ...

  5. [JS] 题解:提取不重复的整数

    题解:提取不重复的整数 https://www.nowcoder.com/practice/253986e66d114d378ae8de2e6c4577c1 时间限制:1秒 空间限制:32M 描述 输 ...

  6. 洛谷-题解 P2672 【推销员】

    独门思路!链表加优先队列! 这题一望,贪心是跑不掉了,但是我贪心并不好,所以想到了一个复杂一些但思路更保稳的做法 思路: 1 因为是离线操作,所以我们可以倒着求,先求x=n的情况,因为那样直接就知道了 ...

  7. [洛谷1383]高级打字机 题解

    题解 这道题一看就珂以用主席树啊 这是一道神奇的题目,那么我们先敲一个主席树,然后维护一个数组len,表示下一次应该在len + 1插入, 之后对于T操作,在上一个版本的len + 1上直接执行插入 ...

  8. luogu P1549 棋盘问题(2) 题解

    luogu P1549 棋盘问题(2) 题解 题目描述 在\(N * N\)的棋盘上\((1≤N≤10)\),填入\(1,2,-,N^2\)共\(N^2\)个数,使得任意两个相邻的数之和为素数. 例如 ...

  9. 【题解搬运】PAT_L1-009 N个数求和

    从我原来的博客上搬运.原先blog作废. (伪)水题+1,旨在继续摸清这个blog(囧 题目 就是求N个数字的和.麻烦的是,这些数字是以有理数"分子/分母"的形式给出的,你输出的和 ...

最新文章

  1. Netflix创始人:我不要求996,一样市值1万亿
  2. android ndk platform,Android NDK Platform Build and Application
  3. ubuntu 16.04 LTS 搜狗输入法安装
  4. opencv清晰度,色偏等评价函数
  5. 爱奇艺java 架构师_爱奇艺个性化实时推荐系统整体流程图 拿去细品
  6. 深度学习笔记第二门课 改善深层神经网络 第一周:深度学习的实践层面
  7. bic,orr——设置某些位为0或者1
  8. 你是否适合当一个Java程序员?一文看清
  9. 本人工作性质已改变,技术文摘随笔已经全部下线
  10. 并发 锁和隔离等级的关系
  11. WinRAR 去除广告弹窗,简单4步亲测有效!
  12. 人类迁徙计划 (feat.译者补完篇)
  13. Python 计算 AMR 文件的时长
  14. python 拼音搜索中文_汉语转拼音工具、新华字典API——两个支持Python的中文资源...
  15. “左手画圆,右手画方”
  16. 4K、2K、1080p、720p、480p、240p常见视频清晰度
  17. LRC歌词原理和实现高仿Android网易云音乐
  18. 基于随机森林、svm、CNN机器学习的风控欺诈识别模型
  19. 创业穷途,我要加入丐帮
  20. Latex写论文时图片脚注的fig后冒号如何改为句号

热门文章

  1. CKEditor插件的使用
  2. springboot整合缓存Redis
  3. 房屋租赁合同中押金的法律性质分析
  4. pr里面怎么加css样式,放入pr剪辑中画面不全怎么办?
  5. TDD模式下的射频前端VSWR检测功能难点
  6. 流程定义语言(BPMN)
  7. 基于Linux的音乐音效软件,在Arch Linux系统上可用Pipewire取代PulseEffects音效软件
  8. C语言 计算签到所得积分
  9. 西北乱跑娃 --- python opencv图像祛噪
  10. 2022-2027年中国中医药行业发展监测及投资战略研究报告