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
#include
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;
}
}
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.
#include
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;
}
}
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.
#include
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;
else
cout << "NO" << endl;
}
}
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
#include
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;
else
cout << "even" << endl;
}
}
}
#include
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 ++;
}
}
else
st.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 ++;
}
else
st.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;
}
}
}
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
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;
}
vectorint ,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));
else
ans.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;
}
}
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
#include
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);
else
update(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;
}
}