output
stringlengths
52
181k
instruction
stringlengths
296
182k
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int a, b, c, d; cin >> a >> b >> c >> d; if (a <= c && b <= d) cout << "Polycarp"; else if (a + b <= c + d - min(c, d)) cout << "Polycarp"; else cout << "Vasiliy"; }
### Prompt Develop a solution in Cpp to the problem described below: Polycarp and Vasiliy love simple logical games. Today they play a game with infinite chessboard and one pawn for each player. Polycarp and Vasiliy move in turns, Polycarp starts. In each turn Polycarp can move his pawn from cell (x, y) to (x - 1, y) or (x, y - 1). Vasiliy can move his pawn from (x, y) to one of cells: (x - 1, y), (x - 1, y - 1) and (x, y - 1). Both players are also allowed to skip move. There are some additional restrictions β€” a player is forbidden to move his pawn to a cell with negative x-coordinate or y-coordinate or to the cell containing opponent's pawn The winner is the first person to reach cell (0, 0). You are given the starting coordinates of both pawns. Determine who will win if both of them play optimally well. Input The first line contains four integers: xp, yp, xv, yv (0 ≀ xp, yp, xv, yv ≀ 105) β€” Polycarp's and Vasiliy's starting coordinates. It is guaranteed that in the beginning the pawns are in different cells and none of them is in the cell (0, 0). Output Output the name of the winner: "Polycarp" or "Vasiliy". Examples Input 2 1 2 2 Output Polycarp Input 4 7 7 4 Output Vasiliy Note In the first sample test Polycarp starts in (2, 1) and will move to (1, 1) in the first turn. No matter what his opponent is doing, in the second turn Polycarp can move to (1, 0) and finally to (0, 0) in the third turn. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int a, b, c, d; cin >> a >> b >> c >> d; if (a <= c && b <= d) cout << "Polycarp"; else if (a + b <= c + d - min(c, d)) cout << "Polycarp"; else cout << "Vasiliy"; } ```
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; cin >> a >> b >> c >> d; if (a + b <= max(c, d) || a < c && b - 1 < d || a - 1 < c && b < d) { puts("Polycarp"); } else { puts("Vasiliy"); } return 0; }
### Prompt Your task is to create a CPP solution to the following problem: Polycarp and Vasiliy love simple logical games. Today they play a game with infinite chessboard and one pawn for each player. Polycarp and Vasiliy move in turns, Polycarp starts. In each turn Polycarp can move his pawn from cell (x, y) to (x - 1, y) or (x, y - 1). Vasiliy can move his pawn from (x, y) to one of cells: (x - 1, y), (x - 1, y - 1) and (x, y - 1). Both players are also allowed to skip move. There are some additional restrictions β€” a player is forbidden to move his pawn to a cell with negative x-coordinate or y-coordinate or to the cell containing opponent's pawn The winner is the first person to reach cell (0, 0). You are given the starting coordinates of both pawns. Determine who will win if both of them play optimally well. Input The first line contains four integers: xp, yp, xv, yv (0 ≀ xp, yp, xv, yv ≀ 105) β€” Polycarp's and Vasiliy's starting coordinates. It is guaranteed that in the beginning the pawns are in different cells and none of them is in the cell (0, 0). Output Output the name of the winner: "Polycarp" or "Vasiliy". Examples Input 2 1 2 2 Output Polycarp Input 4 7 7 4 Output Vasiliy Note In the first sample test Polycarp starts in (2, 1) and will move to (1, 1) in the first turn. No matter what his opponent is doing, in the second turn Polycarp can move to (1, 0) and finally to (0, 0) in the third turn. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; cin >> a >> b >> c >> d; if (a + b <= max(c, d) || a < c && b - 1 < d || a - 1 < c && b < d) { puts("Polycarp"); } else { puts("Vasiliy"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; void outputSmallest(int pd, int vd) { if (pd <= vd) { cout << "Polycarp"; } else { cout << "Vasiliy"; } } int main() { int px, py, vx, vy; cin >> px >> py >> vx >> vy; int pd = px + py; int vd = max(vx, vy); int cx, cy, cd; if (vy - vx > py - px) { cx = px - ((vy - vx) - (py - px)); cy = py; cd = (vy - vx) - (py - px); } else { cy = py - ((py - px) - (vy - vx)); cx = px; cd = ((py - px) - (vy - vx)); } if (cx < 0 || cy < 0 || cd > (vy - cy) || cx > vx || cy > vy || cx > px || cy > py) { outputSmallest(pd, vd); } else { cout << "Polycarp"; } }
### Prompt Your challenge is to write a Cpp solution to the following problem: Polycarp and Vasiliy love simple logical games. Today they play a game with infinite chessboard and one pawn for each player. Polycarp and Vasiliy move in turns, Polycarp starts. In each turn Polycarp can move his pawn from cell (x, y) to (x - 1, y) or (x, y - 1). Vasiliy can move his pawn from (x, y) to one of cells: (x - 1, y), (x - 1, y - 1) and (x, y - 1). Both players are also allowed to skip move. There are some additional restrictions β€” a player is forbidden to move his pawn to a cell with negative x-coordinate or y-coordinate or to the cell containing opponent's pawn The winner is the first person to reach cell (0, 0). You are given the starting coordinates of both pawns. Determine who will win if both of them play optimally well. Input The first line contains four integers: xp, yp, xv, yv (0 ≀ xp, yp, xv, yv ≀ 105) β€” Polycarp's and Vasiliy's starting coordinates. It is guaranteed that in the beginning the pawns are in different cells and none of them is in the cell (0, 0). Output Output the name of the winner: "Polycarp" or "Vasiliy". Examples Input 2 1 2 2 Output Polycarp Input 4 7 7 4 Output Vasiliy Note In the first sample test Polycarp starts in (2, 1) and will move to (1, 1) in the first turn. No matter what his opponent is doing, in the second turn Polycarp can move to (1, 0) and finally to (0, 0) in the third turn. ### Response ```cpp #include <bits/stdc++.h> using namespace std; void outputSmallest(int pd, int vd) { if (pd <= vd) { cout << "Polycarp"; } else { cout << "Vasiliy"; } } int main() { int px, py, vx, vy; cin >> px >> py >> vx >> vy; int pd = px + py; int vd = max(vx, vy); int cx, cy, cd; if (vy - vx > py - px) { cx = px - ((vy - vx) - (py - px)); cy = py; cd = (vy - vx) - (py - px); } else { cy = py - ((py - px) - (vy - vx)); cx = px; cd = ((py - px) - (vy - vx)); } if (cx < 0 || cy < 0 || cd > (vy - cy) || cx > vx || cy > vy || cx > px || cy > py) { outputSmallest(pd, vd); } else { cout << "Polycarp"; } } ```
#include <bits/stdc++.h> using namespace std; int xp, yp, xd, yd; int main() { cin.sync_with_stdio(false); cin >> xp >> yp >> xd >> yd; while (true) { if (xp <= xd && yp <= yd) { cout << "Polycarp"; return 0; } if (xp > xd) xp--; else yp--; if (xp == 0 && yp == 0) { cout << "Polycarp"; return 0; } if (xp >= xd && yp >= yd) { cout << "Vasiliy"; return 0; } if (xd && yd) { xd--; yd--; } else { xd = max(0, xd - 1); yd = max(0, yd - 1); } if (xd == 0 && yd == 0) { cout << "Vasiliy"; return 0; } } return 0; }
### Prompt Develop a solution in cpp to the problem described below: Polycarp and Vasiliy love simple logical games. Today they play a game with infinite chessboard and one pawn for each player. Polycarp and Vasiliy move in turns, Polycarp starts. In each turn Polycarp can move his pawn from cell (x, y) to (x - 1, y) or (x, y - 1). Vasiliy can move his pawn from (x, y) to one of cells: (x - 1, y), (x - 1, y - 1) and (x, y - 1). Both players are also allowed to skip move. There are some additional restrictions β€” a player is forbidden to move his pawn to a cell with negative x-coordinate or y-coordinate or to the cell containing opponent's pawn The winner is the first person to reach cell (0, 0). You are given the starting coordinates of both pawns. Determine who will win if both of them play optimally well. Input The first line contains four integers: xp, yp, xv, yv (0 ≀ xp, yp, xv, yv ≀ 105) β€” Polycarp's and Vasiliy's starting coordinates. It is guaranteed that in the beginning the pawns are in different cells and none of them is in the cell (0, 0). Output Output the name of the winner: "Polycarp" or "Vasiliy". Examples Input 2 1 2 2 Output Polycarp Input 4 7 7 4 Output Vasiliy Note In the first sample test Polycarp starts in (2, 1) and will move to (1, 1) in the first turn. No matter what his opponent is doing, in the second turn Polycarp can move to (1, 0) and finally to (0, 0) in the third turn. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int xp, yp, xd, yd; int main() { cin.sync_with_stdio(false); cin >> xp >> yp >> xd >> yd; while (true) { if (xp <= xd && yp <= yd) { cout << "Polycarp"; return 0; } if (xp > xd) xp--; else yp--; if (xp == 0 && yp == 0) { cout << "Polycarp"; return 0; } if (xp >= xd && yp >= yd) { cout << "Vasiliy"; return 0; } if (xd && yd) { xd--; yd--; } else { xd = max(0, xd - 1); yd = max(0, yd - 1); } if (xd == 0 && yd == 0) { cout << "Vasiliy"; return 0; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int xp, yp, xv, yv; scanf("%d %d %d %d", &xp, &yp, &xv, &yv); if (((xp + yp) <= max(xv, yv)) || ((xp <= xv) && (yp <= yv))) printf("Polycarp"); else printf("Vasiliy"); return 0; }
### Prompt In CPP, your task is to solve the following problem: Polycarp and Vasiliy love simple logical games. Today they play a game with infinite chessboard and one pawn for each player. Polycarp and Vasiliy move in turns, Polycarp starts. In each turn Polycarp can move his pawn from cell (x, y) to (x - 1, y) or (x, y - 1). Vasiliy can move his pawn from (x, y) to one of cells: (x - 1, y), (x - 1, y - 1) and (x, y - 1). Both players are also allowed to skip move. There are some additional restrictions β€” a player is forbidden to move his pawn to a cell with negative x-coordinate or y-coordinate or to the cell containing opponent's pawn The winner is the first person to reach cell (0, 0). You are given the starting coordinates of both pawns. Determine who will win if both of them play optimally well. Input The first line contains four integers: xp, yp, xv, yv (0 ≀ xp, yp, xv, yv ≀ 105) β€” Polycarp's and Vasiliy's starting coordinates. It is guaranteed that in the beginning the pawns are in different cells and none of them is in the cell (0, 0). Output Output the name of the winner: "Polycarp" or "Vasiliy". Examples Input 2 1 2 2 Output Polycarp Input 4 7 7 4 Output Vasiliy Note In the first sample test Polycarp starts in (2, 1) and will move to (1, 1) in the first turn. No matter what his opponent is doing, in the second turn Polycarp can move to (1, 0) and finally to (0, 0) in the third turn. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int xp, yp, xv, yv; scanf("%d %d %d %d", &xp, &yp, &xv, &yv); if (((xp + yp) <= max(xv, yv)) || ((xp <= xv) && (yp <= yv))) printf("Polycarp"); else printf("Vasiliy"); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; bool f = 0; if (x1 + y1 <= max(x2, y2)) f = 1; for (int i = 0; i < min(x2, y2); i++) { int nx = x2 - i; int ny = y2 - i; if (abs(nx - x1) + abs(ny - y1) <= i) f = 1; } cout << (f ? "Polycarp" : "Vasiliy"); return 0; }
### Prompt In cpp, your task is to solve the following problem: Polycarp and Vasiliy love simple logical games. Today they play a game with infinite chessboard and one pawn for each player. Polycarp and Vasiliy move in turns, Polycarp starts. In each turn Polycarp can move his pawn from cell (x, y) to (x - 1, y) or (x, y - 1). Vasiliy can move his pawn from (x, y) to one of cells: (x - 1, y), (x - 1, y - 1) and (x, y - 1). Both players are also allowed to skip move. There are some additional restrictions β€” a player is forbidden to move his pawn to a cell with negative x-coordinate or y-coordinate or to the cell containing opponent's pawn The winner is the first person to reach cell (0, 0). You are given the starting coordinates of both pawns. Determine who will win if both of them play optimally well. Input The first line contains four integers: xp, yp, xv, yv (0 ≀ xp, yp, xv, yv ≀ 105) β€” Polycarp's and Vasiliy's starting coordinates. It is guaranteed that in the beginning the pawns are in different cells and none of them is in the cell (0, 0). Output Output the name of the winner: "Polycarp" or "Vasiliy". Examples Input 2 1 2 2 Output Polycarp Input 4 7 7 4 Output Vasiliy Note In the first sample test Polycarp starts in (2, 1) and will move to (1, 1) in the first turn. No matter what his opponent is doing, in the second turn Polycarp can move to (1, 0) and finally to (0, 0) in the third turn. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; bool f = 0; if (x1 + y1 <= max(x2, y2)) f = 1; for (int i = 0; i < min(x2, y2); i++) { int nx = x2 - i; int ny = y2 - i; if (abs(nx - x1) + abs(ny - y1) <= i) f = 1; } cout << (f ? "Polycarp" : "Vasiliy"); return 0; } ```
#include <bits/stdc++.h> using namespace std; int dx[] = {0, 0, 1, -1, -1, -1, 1, 1}; int dy[] = {1, -1, 0, 0, -1, 1, 1, -1}; template <class T> inline T biton(T n, T pos) { return n | ((T)1 << pos); } template <class T> inline T bitoff(T n, T pos) { return n & ~((T)1 << pos); } template <class T> inline T ison(T n, T pos) { return (bool)(n & ((T)1 << pos)); } template <class T> inline T gcd(T a, T b) { while (b) { a %= b; swap(a, b); } return a; } template <typename T> string NumberToString(T Number) { ostringstream second; second << Number; return second.str(); } inline int nxt() { int aaa; scanf("%d", &aaa); return aaa; } inline long long int lxt() { long long int aaa; scanf("%lld", &aaa); return aaa; } inline double dxt() { double aaa; scanf("%lf", &aaa); return aaa; } template <class T> inline T bigmod(T p, T e, T m) { T ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p) % m; p = (p * p) % m; } return (T)ret; } int ar[200010]; string s, t; void go(int st, int ed, string tmp, int& ans) { int j = 0; int i = st; while (i <= ed && j < tmp.size()) { if (s[i] == tmp[j]) i++, j++; else j++; } int k = st; j = 0; while (k <= ed && j < tmp.size()) { if (t[k] == tmp[j]) k++, j++; else j++; } ans += k > ed && i > ed; } int main() { int px = nxt(), py = nxt(), vx = nxt(), vy = nxt(); if (px + py <= max(vx, vy)) printf("Polycarp\n"); else { if (px <= vx && py <= vy) printf("Polycarp\n"); else printf("Vasiliy\n"); } return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: Polycarp and Vasiliy love simple logical games. Today they play a game with infinite chessboard and one pawn for each player. Polycarp and Vasiliy move in turns, Polycarp starts. In each turn Polycarp can move his pawn from cell (x, y) to (x - 1, y) or (x, y - 1). Vasiliy can move his pawn from (x, y) to one of cells: (x - 1, y), (x - 1, y - 1) and (x, y - 1). Both players are also allowed to skip move. There are some additional restrictions β€” a player is forbidden to move his pawn to a cell with negative x-coordinate or y-coordinate or to the cell containing opponent's pawn The winner is the first person to reach cell (0, 0). You are given the starting coordinates of both pawns. Determine who will win if both of them play optimally well. Input The first line contains four integers: xp, yp, xv, yv (0 ≀ xp, yp, xv, yv ≀ 105) β€” Polycarp's and Vasiliy's starting coordinates. It is guaranteed that in the beginning the pawns are in different cells and none of them is in the cell (0, 0). Output Output the name of the winner: "Polycarp" or "Vasiliy". Examples Input 2 1 2 2 Output Polycarp Input 4 7 7 4 Output Vasiliy Note In the first sample test Polycarp starts in (2, 1) and will move to (1, 1) in the first turn. No matter what his opponent is doing, in the second turn Polycarp can move to (1, 0) and finally to (0, 0) in the third turn. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int dx[] = {0, 0, 1, -1, -1, -1, 1, 1}; int dy[] = {1, -1, 0, 0, -1, 1, 1, -1}; template <class T> inline T biton(T n, T pos) { return n | ((T)1 << pos); } template <class T> inline T bitoff(T n, T pos) { return n & ~((T)1 << pos); } template <class T> inline T ison(T n, T pos) { return (bool)(n & ((T)1 << pos)); } template <class T> inline T gcd(T a, T b) { while (b) { a %= b; swap(a, b); } return a; } template <typename T> string NumberToString(T Number) { ostringstream second; second << Number; return second.str(); } inline int nxt() { int aaa; scanf("%d", &aaa); return aaa; } inline long long int lxt() { long long int aaa; scanf("%lld", &aaa); return aaa; } inline double dxt() { double aaa; scanf("%lf", &aaa); return aaa; } template <class T> inline T bigmod(T p, T e, T m) { T ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p) % m; p = (p * p) % m; } return (T)ret; } int ar[200010]; string s, t; void go(int st, int ed, string tmp, int& ans) { int j = 0; int i = st; while (i <= ed && j < tmp.size()) { if (s[i] == tmp[j]) i++, j++; else j++; } int k = st; j = 0; while (k <= ed && j < tmp.size()) { if (t[k] == tmp[j]) k++, j++; else j++; } ans += k > ed && i > ed; } int main() { int px = nxt(), py = nxt(), vx = nxt(), vy = nxt(); if (px + py <= max(vx, vy)) printf("Polycarp\n"); else { if (px <= vx && py <= vy) printf("Polycarp\n"); else printf("Vasiliy\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int mod = 1e9 + 7; const double eps = 1e-8; const double pi = acos(-1.0); const int dx[] = {-1, 1, 0, 0}; const int dy[] = {0, 0, -1, 1}; int main() { ios_base::sync_with_stdio(0); int xp, yp, xv, yv, d; while (cin >> xp >> yp >> xv >> yv) { d = min(xv, yv); if (xp + yp <= xv + yv - d) cout << "Polycarp\n"; else if (xp <= xv && yp <= yv) cout << "Polycarp\n"; else cout << "Vasiliy\n"; } return 0; }
### Prompt Please formulate a Cpp solution to the following problem: Polycarp and Vasiliy love simple logical games. Today they play a game with infinite chessboard and one pawn for each player. Polycarp and Vasiliy move in turns, Polycarp starts. In each turn Polycarp can move his pawn from cell (x, y) to (x - 1, y) or (x, y - 1). Vasiliy can move his pawn from (x, y) to one of cells: (x - 1, y), (x - 1, y - 1) and (x, y - 1). Both players are also allowed to skip move. There are some additional restrictions β€” a player is forbidden to move his pawn to a cell with negative x-coordinate or y-coordinate or to the cell containing opponent's pawn The winner is the first person to reach cell (0, 0). You are given the starting coordinates of both pawns. Determine who will win if both of them play optimally well. Input The first line contains four integers: xp, yp, xv, yv (0 ≀ xp, yp, xv, yv ≀ 105) β€” Polycarp's and Vasiliy's starting coordinates. It is guaranteed that in the beginning the pawns are in different cells and none of them is in the cell (0, 0). Output Output the name of the winner: "Polycarp" or "Vasiliy". Examples Input 2 1 2 2 Output Polycarp Input 4 7 7 4 Output Vasiliy Note In the first sample test Polycarp starts in (2, 1) and will move to (1, 1) in the first turn. No matter what his opponent is doing, in the second turn Polycarp can move to (1, 0) and finally to (0, 0) in the third turn. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int mod = 1e9 + 7; const double eps = 1e-8; const double pi = acos(-1.0); const int dx[] = {-1, 1, 0, 0}; const int dy[] = {0, 0, -1, 1}; int main() { ios_base::sync_with_stdio(0); int xp, yp, xv, yv, d; while (cin >> xp >> yp >> xv >> yv) { d = min(xv, yv); if (xp + yp <= xv + yv - d) cout << "Polycarp\n"; else if (xp <= xv && yp <= yv) cout << "Polycarp\n"; else cout << "Vasiliy\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { pair<int, int> f_p, s_p; cin >> f_p.first >> f_p.second >> s_p.first >> s_p.second; if (s_p.first >= f_p.first && s_p.second >= f_p.second) { cout << "Polycarp" << endl; return 0; } f_p.first += f_p.second; if (s_p.first < s_p.second) s_p.first = s_p.second; if (f_p.first <= s_p.first) cout << "Polycarp" << endl; else cout << "Vasiliy" << endl; }
### Prompt Please formulate a Cpp solution to the following problem: Polycarp and Vasiliy love simple logical games. Today they play a game with infinite chessboard and one pawn for each player. Polycarp and Vasiliy move in turns, Polycarp starts. In each turn Polycarp can move his pawn from cell (x, y) to (x - 1, y) or (x, y - 1). Vasiliy can move his pawn from (x, y) to one of cells: (x - 1, y), (x - 1, y - 1) and (x, y - 1). Both players are also allowed to skip move. There are some additional restrictions β€” a player is forbidden to move his pawn to a cell with negative x-coordinate or y-coordinate or to the cell containing opponent's pawn The winner is the first person to reach cell (0, 0). You are given the starting coordinates of both pawns. Determine who will win if both of them play optimally well. Input The first line contains four integers: xp, yp, xv, yv (0 ≀ xp, yp, xv, yv ≀ 105) β€” Polycarp's and Vasiliy's starting coordinates. It is guaranteed that in the beginning the pawns are in different cells and none of them is in the cell (0, 0). Output Output the name of the winner: "Polycarp" or "Vasiliy". Examples Input 2 1 2 2 Output Polycarp Input 4 7 7 4 Output Vasiliy Note In the first sample test Polycarp starts in (2, 1) and will move to (1, 1) in the first turn. No matter what his opponent is doing, in the second turn Polycarp can move to (1, 0) and finally to (0, 0) in the third turn. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { pair<int, int> f_p, s_p; cin >> f_p.first >> f_p.second >> s_p.first >> s_p.second; if (s_p.first >= f_p.first && s_p.second >= f_p.second) { cout << "Polycarp" << endl; return 0; } f_p.first += f_p.second; if (s_p.first < s_p.second) s_p.first = s_p.second; if (f_p.first <= s_p.first) cout << "Polycarp" << endl; else cout << "Vasiliy" << endl; } ```
#include <bits/stdc++.h> using namespace std; const string g = "Polycarp"; const string h = "Vasiliy"; int main(void) { int x, y, a, b; cin >> x >> y >> a >> b; if (x + y <= max(a, b) || (x <= a && y <= b)) return cout << g << '\n', 0; else return cout << h << '\n', 0; }
### Prompt Create a solution in CPP for the following problem: Polycarp and Vasiliy love simple logical games. Today they play a game with infinite chessboard and one pawn for each player. Polycarp and Vasiliy move in turns, Polycarp starts. In each turn Polycarp can move his pawn from cell (x, y) to (x - 1, y) or (x, y - 1). Vasiliy can move his pawn from (x, y) to one of cells: (x - 1, y), (x - 1, y - 1) and (x, y - 1). Both players are also allowed to skip move. There are some additional restrictions β€” a player is forbidden to move his pawn to a cell with negative x-coordinate or y-coordinate or to the cell containing opponent's pawn The winner is the first person to reach cell (0, 0). You are given the starting coordinates of both pawns. Determine who will win if both of them play optimally well. Input The first line contains four integers: xp, yp, xv, yv (0 ≀ xp, yp, xv, yv ≀ 105) β€” Polycarp's and Vasiliy's starting coordinates. It is guaranteed that in the beginning the pawns are in different cells and none of them is in the cell (0, 0). Output Output the name of the winner: "Polycarp" or "Vasiliy". Examples Input 2 1 2 2 Output Polycarp Input 4 7 7 4 Output Vasiliy Note In the first sample test Polycarp starts in (2, 1) and will move to (1, 1) in the first turn. No matter what his opponent is doing, in the second turn Polycarp can move to (1, 0) and finally to (0, 0) in the third turn. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const string g = "Polycarp"; const string h = "Vasiliy"; int main(void) { int x, y, a, b; cin >> x >> y >> a >> b; if (x + y <= max(a, b) || (x <= a && y <= b)) return cout << g << '\n', 0; else return cout << h << '\n', 0; } ```
#include <bits/stdc++.h> using namespace std; const int INF = 100000000; inline void solve(void) { int px, py, vx, vy; cin >> px >> py >> vx >> vy; if ((px <= vx && py <= vy) || (px + py <= max(vx, vy))) cout << "Polycarp"; else cout << "Vasiliy"; } int main(int argc, const char* argv[]) { ios_base::sync_with_stdio(false); solve(); return 0; }
### Prompt In Cpp, your task is to solve the following problem: Polycarp and Vasiliy love simple logical games. Today they play a game with infinite chessboard and one pawn for each player. Polycarp and Vasiliy move in turns, Polycarp starts. In each turn Polycarp can move his pawn from cell (x, y) to (x - 1, y) or (x, y - 1). Vasiliy can move his pawn from (x, y) to one of cells: (x - 1, y), (x - 1, y - 1) and (x, y - 1). Both players are also allowed to skip move. There are some additional restrictions β€” a player is forbidden to move his pawn to a cell with negative x-coordinate or y-coordinate or to the cell containing opponent's pawn The winner is the first person to reach cell (0, 0). You are given the starting coordinates of both pawns. Determine who will win if both of them play optimally well. Input The first line contains four integers: xp, yp, xv, yv (0 ≀ xp, yp, xv, yv ≀ 105) β€” Polycarp's and Vasiliy's starting coordinates. It is guaranteed that in the beginning the pawns are in different cells and none of them is in the cell (0, 0). Output Output the name of the winner: "Polycarp" or "Vasiliy". Examples Input 2 1 2 2 Output Polycarp Input 4 7 7 4 Output Vasiliy Note In the first sample test Polycarp starts in (2, 1) and will move to (1, 1) in the first turn. No matter what his opponent is doing, in the second turn Polycarp can move to (1, 0) and finally to (0, 0) in the third turn. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int INF = 100000000; inline void solve(void) { int px, py, vx, vy; cin >> px >> py >> vx >> vy; if ((px <= vx && py <= vy) || (px + py <= max(vx, vy))) cout << "Polycarp"; else cout << "Vasiliy"; } int main(int argc, const char* argv[]) { ios_base::sync_with_stdio(false); solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; inline int read() { int ret = 0; char c; bool f = 0; while (!isdigit(c = getchar()) && c != '-') ; if (c == '-') f = 1, c = getchar(); do ret = ret * 10 + c - '0'; while (isdigit(c = getchar())); return f ? -ret : ret; } char name[2][10] = {"Polycarp", "Vasiliy"}; bool check(int x1, int y1, int x2, int y2) { if (x1 <= x2 && y1 <= y2) return 0; if (x1 >= x2 && y1 >= y2) return 1; int p1 = x1 + y1; int p2 = x2 + y2 - min(x2, y2); return p1 > p2; } int main() { int xp = read(), yp = read(); int xv = read(), yv = read(); puts(name[check(xp, yp, xv, yv)]); return 0; }
### Prompt In CPP, your task is to solve the following problem: Polycarp and Vasiliy love simple logical games. Today they play a game with infinite chessboard and one pawn for each player. Polycarp and Vasiliy move in turns, Polycarp starts. In each turn Polycarp can move his pawn from cell (x, y) to (x - 1, y) or (x, y - 1). Vasiliy can move his pawn from (x, y) to one of cells: (x - 1, y), (x - 1, y - 1) and (x, y - 1). Both players are also allowed to skip move. There are some additional restrictions β€” a player is forbidden to move his pawn to a cell with negative x-coordinate or y-coordinate or to the cell containing opponent's pawn The winner is the first person to reach cell (0, 0). You are given the starting coordinates of both pawns. Determine who will win if both of them play optimally well. Input The first line contains four integers: xp, yp, xv, yv (0 ≀ xp, yp, xv, yv ≀ 105) β€” Polycarp's and Vasiliy's starting coordinates. It is guaranteed that in the beginning the pawns are in different cells and none of them is in the cell (0, 0). Output Output the name of the winner: "Polycarp" or "Vasiliy". Examples Input 2 1 2 2 Output Polycarp Input 4 7 7 4 Output Vasiliy Note In the first sample test Polycarp starts in (2, 1) and will move to (1, 1) in the first turn. No matter what his opponent is doing, in the second turn Polycarp can move to (1, 0) and finally to (0, 0) in the third turn. ### Response ```cpp #include <bits/stdc++.h> using namespace std; inline int read() { int ret = 0; char c; bool f = 0; while (!isdigit(c = getchar()) && c != '-') ; if (c == '-') f = 1, c = getchar(); do ret = ret * 10 + c - '0'; while (isdigit(c = getchar())); return f ? -ret : ret; } char name[2][10] = {"Polycarp", "Vasiliy"}; bool check(int x1, int y1, int x2, int y2) { if (x1 <= x2 && y1 <= y2) return 0; if (x1 >= x2 && y1 >= y2) return 1; int p1 = x1 + y1; int p2 = x2 + y2 - min(x2, y2); return p1 > p2; } int main() { int xp = read(), yp = read(); int xv = read(), yv = read(); puts(name[check(xp, yp, xv, yv)]); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); ; int a, b, c, d; cin >> a >> b >> c >> d; if (c >= a && d >= b) cout << "Polycarp"; else if (c < a + b && d < a + b) cout << "Vasiliy"; else cout << "Polycarp"; return 0; }
### Prompt Develop a solution in CPP to the problem described below: Polycarp and Vasiliy love simple logical games. Today they play a game with infinite chessboard and one pawn for each player. Polycarp and Vasiliy move in turns, Polycarp starts. In each turn Polycarp can move his pawn from cell (x, y) to (x - 1, y) or (x, y - 1). Vasiliy can move his pawn from (x, y) to one of cells: (x - 1, y), (x - 1, y - 1) and (x, y - 1). Both players are also allowed to skip move. There are some additional restrictions β€” a player is forbidden to move his pawn to a cell with negative x-coordinate or y-coordinate or to the cell containing opponent's pawn The winner is the first person to reach cell (0, 0). You are given the starting coordinates of both pawns. Determine who will win if both of them play optimally well. Input The first line contains four integers: xp, yp, xv, yv (0 ≀ xp, yp, xv, yv ≀ 105) β€” Polycarp's and Vasiliy's starting coordinates. It is guaranteed that in the beginning the pawns are in different cells and none of them is in the cell (0, 0). Output Output the name of the winner: "Polycarp" or "Vasiliy". Examples Input 2 1 2 2 Output Polycarp Input 4 7 7 4 Output Vasiliy Note In the first sample test Polycarp starts in (2, 1) and will move to (1, 1) in the first turn. No matter what his opponent is doing, in the second turn Polycarp can move to (1, 0) and finally to (0, 0) in the third turn. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); ; int a, b, c, d; cin >> a >> b >> c >> d; if (c >= a && d >= b) cout << "Polycarp"; else if (c < a + b && d < a + b) cout << "Vasiliy"; else cout << "Polycarp"; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int x1, y1, x2, y2; scanf("%d%d%d%d", &x1, &y1, &x2, &y2); if (x1 == x2) { if (y1 < y2) printf("Polycarp\n"); else printf("Vasiliy\n"); return 0; } if (y1 == y2) { if (x1 < x2) printf("Polycarp\n"); else printf("Vasiliy\n"); return 0; } if (x1 < x2 && y1 < y2) { printf("Polycarp\n"); return 0; } if (x2 < x1 && y2 < y1) { printf("Vasiliy\n"); return 0; } if (x2 < x1 && y1 < y2) { int yy = y2 - y1; int maxx = max(yy, x2); if (maxx >= x1) { printf("Polycarp\n"); return 0; } else { printf("Vasiliy\n"); return 0; } } int maxx = max(y2, x2 - x1); if (maxx >= y1) printf("Polycarp\n"); else printf("Vasiliy\n"); }
### Prompt Your challenge is to write a cpp solution to the following problem: Polycarp and Vasiliy love simple logical games. Today they play a game with infinite chessboard and one pawn for each player. Polycarp and Vasiliy move in turns, Polycarp starts. In each turn Polycarp can move his pawn from cell (x, y) to (x - 1, y) or (x, y - 1). Vasiliy can move his pawn from (x, y) to one of cells: (x - 1, y), (x - 1, y - 1) and (x, y - 1). Both players are also allowed to skip move. There are some additional restrictions β€” a player is forbidden to move his pawn to a cell with negative x-coordinate or y-coordinate or to the cell containing opponent's pawn The winner is the first person to reach cell (0, 0). You are given the starting coordinates of both pawns. Determine who will win if both of them play optimally well. Input The first line contains four integers: xp, yp, xv, yv (0 ≀ xp, yp, xv, yv ≀ 105) β€” Polycarp's and Vasiliy's starting coordinates. It is guaranteed that in the beginning the pawns are in different cells and none of them is in the cell (0, 0). Output Output the name of the winner: "Polycarp" or "Vasiliy". Examples Input 2 1 2 2 Output Polycarp Input 4 7 7 4 Output Vasiliy Note In the first sample test Polycarp starts in (2, 1) and will move to (1, 1) in the first turn. No matter what his opponent is doing, in the second turn Polycarp can move to (1, 0) and finally to (0, 0) in the third turn. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int x1, y1, x2, y2; scanf("%d%d%d%d", &x1, &y1, &x2, &y2); if (x1 == x2) { if (y1 < y2) printf("Polycarp\n"); else printf("Vasiliy\n"); return 0; } if (y1 == y2) { if (x1 < x2) printf("Polycarp\n"); else printf("Vasiliy\n"); return 0; } if (x1 < x2 && y1 < y2) { printf("Polycarp\n"); return 0; } if (x2 < x1 && y2 < y1) { printf("Vasiliy\n"); return 0; } if (x2 < x1 && y1 < y2) { int yy = y2 - y1; int maxx = max(yy, x2); if (maxx >= x1) { printf("Polycarp\n"); return 0; } else { printf("Vasiliy\n"); return 0; } } int maxx = max(y2, x2 - x1); if (maxx >= y1) printf("Polycarp\n"); else printf("Vasiliy\n"); } ```
#include <bits/stdc++.h> using namespace std; template <class T> inline void mini(T &a, T b) { if (b < a) a = b; } template <class T> inline void maxi(T &a, T b) { if (b > a) a = b; } int a, b, c, d; bool ok() { return a + b <= max(c, d) || (a <= c && b <= d); } int main() { cin >> a >> b >> c >> d; puts(ok() ? "Polycarp" : "Vasiliy"); }
### Prompt Generate a cpp solution to the following problem: Polycarp and Vasiliy love simple logical games. Today they play a game with infinite chessboard and one pawn for each player. Polycarp and Vasiliy move in turns, Polycarp starts. In each turn Polycarp can move his pawn from cell (x, y) to (x - 1, y) or (x, y - 1). Vasiliy can move his pawn from (x, y) to one of cells: (x - 1, y), (x - 1, y - 1) and (x, y - 1). Both players are also allowed to skip move. There are some additional restrictions β€” a player is forbidden to move his pawn to a cell with negative x-coordinate or y-coordinate or to the cell containing opponent's pawn The winner is the first person to reach cell (0, 0). You are given the starting coordinates of both pawns. Determine who will win if both of them play optimally well. Input The first line contains four integers: xp, yp, xv, yv (0 ≀ xp, yp, xv, yv ≀ 105) β€” Polycarp's and Vasiliy's starting coordinates. It is guaranteed that in the beginning the pawns are in different cells and none of them is in the cell (0, 0). Output Output the name of the winner: "Polycarp" or "Vasiliy". Examples Input 2 1 2 2 Output Polycarp Input 4 7 7 4 Output Vasiliy Note In the first sample test Polycarp starts in (2, 1) and will move to (1, 1) in the first turn. No matter what his opponent is doing, in the second turn Polycarp can move to (1, 0) and finally to (0, 0) in the third turn. ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <class T> inline void mini(T &a, T b) { if (b < a) a = b; } template <class T> inline void maxi(T &a, T b) { if (b > a) a = b; } int a, b, c, d; bool ok() { return a + b <= max(c, d) || (a <= c && b <= d); } int main() { cin >> a >> b >> c >> d; puts(ok() ? "Polycarp" : "Vasiliy"); } ```
#include <bits/stdc++.h> using namespace std; const double eps = 1e-7; const double PI = acos(-1); const long long INFF = (long long)1e18; const int INF = (int)1e9; const int mod = (int)1e9 + 7; const int MAX = (int)1e5 + 7; int main(void) { int px, py; scanf("%d %d", &px, &py); ; int vx, vy; scanf("%d %d", &vx, &vy); ; int v1 = px + py; int v2 = max(vx, vy); if (px + py > vx + vy) puts("Vasiliy"); else { if (v2 >= v1) { puts("Polycarp"); return 0; } for (int x = vx, y = vy, t = 0; x && y; --x, --y, ++t) { int v = abs(x - px) + abs(y - py); if (t >= v) { puts("Polycarp"); return 0; } } puts("Vasiliy"); } return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: Polycarp and Vasiliy love simple logical games. Today they play a game with infinite chessboard and one pawn for each player. Polycarp and Vasiliy move in turns, Polycarp starts. In each turn Polycarp can move his pawn from cell (x, y) to (x - 1, y) or (x, y - 1). Vasiliy can move his pawn from (x, y) to one of cells: (x - 1, y), (x - 1, y - 1) and (x, y - 1). Both players are also allowed to skip move. There are some additional restrictions β€” a player is forbidden to move his pawn to a cell with negative x-coordinate or y-coordinate or to the cell containing opponent's pawn The winner is the first person to reach cell (0, 0). You are given the starting coordinates of both pawns. Determine who will win if both of them play optimally well. Input The first line contains four integers: xp, yp, xv, yv (0 ≀ xp, yp, xv, yv ≀ 105) β€” Polycarp's and Vasiliy's starting coordinates. It is guaranteed that in the beginning the pawns are in different cells and none of them is in the cell (0, 0). Output Output the name of the winner: "Polycarp" or "Vasiliy". Examples Input 2 1 2 2 Output Polycarp Input 4 7 7 4 Output Vasiliy Note In the first sample test Polycarp starts in (2, 1) and will move to (1, 1) in the first turn. No matter what his opponent is doing, in the second turn Polycarp can move to (1, 0) and finally to (0, 0) in the third turn. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const double eps = 1e-7; const double PI = acos(-1); const long long INFF = (long long)1e18; const int INF = (int)1e9; const int mod = (int)1e9 + 7; const int MAX = (int)1e5 + 7; int main(void) { int px, py; scanf("%d %d", &px, &py); ; int vx, vy; scanf("%d %d", &vx, &vy); ; int v1 = px + py; int v2 = max(vx, vy); if (px + py > vx + vy) puts("Vasiliy"); else { if (v2 >= v1) { puts("Polycarp"); return 0; } for (int x = vx, y = vy, t = 0; x && y; --x, --y, ++t) { int v = abs(x - px) + abs(y - py); if (t >= v) { puts("Polycarp"); return 0; } } puts("Vasiliy"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int arr[100000]; int main() { int n; cin >> n; vector<int> vc; map<int, int> mp; map<int, int> fs; map<int, int> ls; int cnt = 0; for (int i = 0; i < n; i++) { cin >> arr[i]; if (mp[arr[i]] == 0) { fs[arr[i]] = i; vc.push_back(arr[i]); cnt++; } mp[arr[i]]++; ls[arr[i]] = i; } if (cnt == n) cout << "1 1" << endl; else { int mx = 0; for (int i = 0; i < cnt; i++) { mx = max(mx, mp[vc[i]]); } vector<int> p; for (int i = 0; i < cnt; i++) { if (mp[vc[i]] == mx) p.push_back(vc[i]); } int l = 0; int r = n - 1; for (int i = 0; i < p.size(); i++) { int xx = p[i]; int ll, rr; ll = fs[xx]; rr = ls[xx]; int d1, d2; d1 = rr - ll; d2 = r - l; if (d1 < d2) { l = ll; r = rr; } } l++; r++; cout << l << " " << r << endl; } }
### Prompt In cpp, your task is to solve the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int arr[100000]; int main() { int n; cin >> n; vector<int> vc; map<int, int> mp; map<int, int> fs; map<int, int> ls; int cnt = 0; for (int i = 0; i < n; i++) { cin >> arr[i]; if (mp[arr[i]] == 0) { fs[arr[i]] = i; vc.push_back(arr[i]); cnt++; } mp[arr[i]]++; ls[arr[i]] = i; } if (cnt == n) cout << "1 1" << endl; else { int mx = 0; for (int i = 0; i < cnt; i++) { mx = max(mx, mp[vc[i]]); } vector<int> p; for (int i = 0; i < cnt; i++) { if (mp[vc[i]] == mx) p.push_back(vc[i]); } int l = 0; int r = n - 1; for (int i = 0; i < p.size(); i++) { int xx = p[i]; int ll, rr; ll = fs[xx]; rr = ls[xx]; int d1, d2; d1 = rr - ll; d2 = r - l; if (d1 < d2) { l = ll; r = rr; } } l++; r++; cout << l << " " << r << endl; } } ```
#include <bits/stdc++.h> using namespace std; int n, mx = INT_MIN, x, arr[1000005], mn = INT_MAX, ansf, anse; unordered_set<int> st; vector<int> vv; vector<pair<int, int>> v(1000005); int main() { ios::sync_with_stdio(0), ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); ; cin >> n; for (int i = 0; i < n; ++i) { cin >> x; vv.push_back(x); if (!st.count(x)) { st.insert(x); v[x].first = i; } arr[x]++; mx = max(mx, arr[x]); } st.clear(); for (int i = n - 1; i >= 0; --i) { if (!st.count(vv[i])) { st.insert(vv[i]); v[vv[i]].second = i; } } for (int i = 0; i <= 1000000; ++i) { if (arr[i] == mx) { if (v[i].second - v[i].first < mn) { mn = v[i].second - v[i].first; ansf = v[i].first; anse = v[i].second; } } } cout << (ansf + 1) << " " << (anse + 1) << "\n"; return 0; }
### Prompt Construct a cpp code solution to the problem outlined: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, mx = INT_MIN, x, arr[1000005], mn = INT_MAX, ansf, anse; unordered_set<int> st; vector<int> vv; vector<pair<int, int>> v(1000005); int main() { ios::sync_with_stdio(0), ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); ; cin >> n; for (int i = 0; i < n; ++i) { cin >> x; vv.push_back(x); if (!st.count(x)) { st.insert(x); v[x].first = i; } arr[x]++; mx = max(mx, arr[x]); } st.clear(); for (int i = n - 1; i >= 0; --i) { if (!st.count(vv[i])) { st.insert(vv[i]); v[vv[i]].second = i; } } for (int i = 0; i <= 1000000; ++i) { if (arr[i] == mx) { if (v[i].second - v[i].first < mn) { mn = v[i].second - v[i].first; ansf = v[i].first; anse = v[i].second; } } } cout << (ansf + 1) << " " << (anse + 1) << "\n"; return 0; } ```
#include <bits/stdc++.h> using namespace std; struct A { int cnt; int left, right; }; int main() { int n; while (cin >> n) { map<int, A> m; int ans = -1, mx = 0; for (int i = 1; i <= n; i++) { int a; cin >> a; if (m.find(a) == m.end()) { m[a] = (A){1, i, i}; } else { m[a].cnt++; m[a].right = i; } if (m[a].cnt > mx) { mx = m[a].cnt; ans = a; } else if (m[a].cnt == mx) { if (ans != -1 && m[a].right - m[a].left < m[ans].right - m[ans].left) { mx = m[a].cnt; ans = a; } } } printf("%d %d\n", m[ans].left, m[ans].right); } return 0; }
### Prompt Please create a solution in CPP to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct A { int cnt; int left, right; }; int main() { int n; while (cin >> n) { map<int, A> m; int ans = -1, mx = 0; for (int i = 1; i <= n; i++) { int a; cin >> a; if (m.find(a) == m.end()) { m[a] = (A){1, i, i}; } else { m[a].cnt++; m[a].right = i; } if (m[a].cnt > mx) { mx = m[a].cnt; ans = a; } else if (m[a].cnt == mx) { if (ans != -1 && m[a].right - m[a].left < m[ans].right - m[ans].left) { mx = m[a].cnt; ans = a; } } } printf("%d %d\n", m[ans].left, m[ans].right); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int kol[1000005] = {}, a[1000005], l[1000005] = {}, r[1000005] = {}; int main() { int n; cin >> n; int m = 0; for (int i = 1; i <= n; i++) { int x; cin >> x; a[i] = x; kol[x]++; m = max(m, kol[x]); } for (int i = 1; i <= n; i++) if (kol[a[i]] == m && l[a[i]] == 0) { l[a[i]] = i; } int i1 = -1, m1 = 1000000; for (int i = n; i >= 1; i--) if (kol[a[i]] == m && r[a[i]] == 0) { r[a[i]] = i; if (r[a[i]] - l[a[i]] < m1) { m1 = r[a[i]] - l[a[i]]; i1 = a[i]; } } cout << l[i1] << " " << r[i1]; return 0; }
### Prompt Create a solution in CPP for the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int kol[1000005] = {}, a[1000005], l[1000005] = {}, r[1000005] = {}; int main() { int n; cin >> n; int m = 0; for (int i = 1; i <= n; i++) { int x; cin >> x; a[i] = x; kol[x]++; m = max(m, kol[x]); } for (int i = 1; i <= n; i++) if (kol[a[i]] == m && l[a[i]] == 0) { l[a[i]] = i; } int i1 = -1, m1 = 1000000; for (int i = n; i >= 1; i--) if (kol[a[i]] == m && r[a[i]] == 0) { r[a[i]] = i; if (r[a[i]] - l[a[i]] < m1) { m1 = r[a[i]] - l[a[i]]; i1 = a[i]; } } cout << l[i1] << " " << r[i1]; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n + 1, 0), l(1e6 + 1, -1), r(1e6 + 1, 0), cnt(1e6 + 1, 0); int ans = 0; a[0] = 0; for (int i = 1; i <= n; ++i) { cin >> a[i]; ++cnt[a[i]]; if (l[a[i]] == -1) l[a[i]] = i; r[a[i]] = i; if (cnt[a[i]] > cnt[a[ans]]) ans = i; if (cnt[a[i]] == cnt[a[ans]] && r[a[ans]] - l[a[ans]] > r[a[i]] - l[a[i]]) ans = i; } cout << l[a[ans]] << " " << r[a[ans]]; }
### Prompt Your challenge is to write a CPP solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n + 1, 0), l(1e6 + 1, -1), r(1e6 + 1, 0), cnt(1e6 + 1, 0); int ans = 0; a[0] = 0; for (int i = 1; i <= n; ++i) { cin >> a[i]; ++cnt[a[i]]; if (l[a[i]] == -1) l[a[i]] = i; r[a[i]] = i; if (cnt[a[i]] > cnt[a[ans]]) ans = i; if (cnt[a[i]] == cnt[a[ans]] && r[a[ans]] - l[a[ans]] > r[a[i]] - l[a[i]]) ans = i; } cout << l[a[ans]] << " " << r[a[ans]]; } ```
#include <bits/stdc++.h> using namespace std; void Suhaib_Sawalha() { long long n; cin >> n; map<long long, vector<long long>> m; long long a, Max(0); pair<long long, long long> ans; for (long long i = 0; i < n; ++i) { cin >> a; m[a].push_back(i + 1); if (m[a].size() > Max) Max = m[a].size(), ans.first = m[a].front(), ans.second = m[a].back(); else if (m[a].size() == Max && m[a].back() - m[a].front() < ans.second - ans.first) ans.first = m[a].front(), ans.second = m[a].back(); } cout << ans.first << " " << ans.second; } void Suhaib_Sawalha1() { long long t; cin >> t; while (t--) { Suhaib_Sawalha(); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); Suhaib_Sawalha(); return 0; }
### Prompt Generate a Cpp solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; void Suhaib_Sawalha() { long long n; cin >> n; map<long long, vector<long long>> m; long long a, Max(0); pair<long long, long long> ans; for (long long i = 0; i < n; ++i) { cin >> a; m[a].push_back(i + 1); if (m[a].size() > Max) Max = m[a].size(), ans.first = m[a].front(), ans.second = m[a].back(); else if (m[a].size() == Max && m[a].back() - m[a].front() < ans.second - ans.first) ans.first = m[a].front(), ans.second = m[a].back(); } cout << ans.first << " " << ans.second; } void Suhaib_Sawalha1() { long long t; cin >> t; while (t--) { Suhaib_Sawalha(); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); Suhaib_Sawalha(); return 0; } ```
#include <bits/stdc++.h> using namespace std; long long n, sz, l, r; vector<long long> veci[1000004]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) { long long a; cin >> a; veci[a].push_back(i); } for (int i = 1; i <= 1000000; i++) if (veci[i].size() > sz) { sz = veci[i].size(); l = veci[i][0]; r = veci[i][veci[i].size() - 1]; } else if (sz > 0 && veci[i].size() == sz && r - l > veci[i][veci[i].size() - 1] - veci[i][0]) { l = veci[i][0], r = veci[i][veci[i].size() - 1]; } cout << l << " " << r; }
### Prompt In Cpp, your task is to solve the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n, sz, l, r; vector<long long> veci[1000004]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) { long long a; cin >> a; veci[a].push_back(i); } for (int i = 1; i <= 1000000; i++) if (veci[i].size() > sz) { sz = veci[i].size(); l = veci[i][0]; r = veci[i][veci[i].size() - 1]; } else if (sz > 0 && veci[i].size() == sz && r - l > veci[i][veci[i].size() - 1] - veci[i][0]) { l = veci[i][0], r = veci[i][veci[i].size() - 1]; } cout << l << " " << r; } ```
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; pair<long long, long long> a[n]; for (long long i = 0; i < n; i++) { cin >> a[i].first; a[i].second = i + 1; } sort(a, a + n); long long mx = 0, mx_start = 0, mx_end = 0; long long j; long long mn = n; for (long long i = 0; i < n; i++) { long long c = a[i].second; long long d, ck = 0; for (j = i; j < n; j++) { if (a[i].first != a[j].first) { d = a[j - 1].second; break; } else ck++; } if (j == n) { d = a[j - 1].second; i = n - 1; } else i = j - 1; if (ck > mx) { mx = ck; mx_start = c; mx_end = d; mn = d - c + 1; } else if (ck == mx && (d - c + 1) < mn) { mx = ck; mx_start = c; mx_end = d; mn = d - c + 1; } } cout << mx_start << ' ' << mx_end << endl; return 0; }
### Prompt Please create a solution in Cpp to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; pair<long long, long long> a[n]; for (long long i = 0; i < n; i++) { cin >> a[i].first; a[i].second = i + 1; } sort(a, a + n); long long mx = 0, mx_start = 0, mx_end = 0; long long j; long long mn = n; for (long long i = 0; i < n; i++) { long long c = a[i].second; long long d, ck = 0; for (j = i; j < n; j++) { if (a[i].first != a[j].first) { d = a[j - 1].second; break; } else ck++; } if (j == n) { d = a[j - 1].second; i = n - 1; } else i = j - 1; if (ck > mx) { mx = ck; mx_start = c; mx_end = d; mn = d - c + 1; } else if (ck == mx && (d - c + 1) < mn) { mx = ck; mx_start = c; mx_end = d; mn = d - c + 1; } } cout << mx_start << ' ' << mx_end << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; template <typename T, typename T1> ostream& operator<<(ostream& out, pair<T, T1> obj) { return out << "(" << obj.first << "," << obj.second << ")"; } template <typename T, typename T1> ostream& operator<<(ostream& out, map<T, T1> cont) { typename map<T, T1>::const_iterator itr = cont.begin(); typename map<T, T1>::const_iterator ends = cont.end(); for (; itr != ends; ++itr) out << *itr << " "; out << endl; } template <typename T, template <typename ELEM, typename ALLOC = allocator<ELEM>> class CONT> ostream& operator<<(ostream& out, CONT<T> cont) { typename CONT<T>::const_iterator itr = cont.begin(); typename CONT<T>::const_iterator ends = cont.end(); for (; itr != ends; ++itr) out << *itr << " "; out << endl; } template <typename T> T gcd(T a, T b) { T min_v = min(a, b); T max_v = max(a, b); while (min_v) { T temp = max_v % min_v; max_v = min_v; min_v = temp; } return max_v; } int N, maxTime, min_length = -1, startPos, endPos; int elements[100010], counts[1000010], start_pos[1000010]; vector<pair<int, int>> keep; int main() { scanf("%d", &N); for (auto i = 0 - (0 > N); i != N - (0 > N); i += 1 - 2 * (0 > N)) scanf("%d", elements + i); for (auto i = 0 - (0 > 1000010); i != 1000010 - (0 > 1000010); i += 1 - 2 * (0 > 1000010)) start_pos[i] = -1; for (auto i = 0 - (0 > N); i != N - (0 > N); i += 1 - 2 * (0 > N)) if (start_pos[elements[i]] == -1) start_pos[elements[i]] = i + 1; for (auto i = 0 - (0 > N); i != N - (0 > N); i += 1 - 2 * (0 > N)) { ++counts[elements[i]]; if (counts[elements[i]] > maxTime) maxTime = counts[elements[i]]; } for (auto i = 0 - (0 > 1000010); i != 1000010 - (0 > 1000010); i += 1 - 2 * (0 > 1000010)) counts[i] = 0; for (auto i = 0 - (0 > N); i != N - (0 > N); i += 1 - 2 * (0 > N)) { ++counts[elements[i]]; if (counts[elements[i]] == maxTime) keep.emplace_back(i + 1, elements[i]); } if (keep.size()) { for (auto i = 0 - (0 > keep.size()); i != keep.size() - (0 > keep.size()); i += 1 - 2 * (0 > keep.size())) { int len = keep[i].first - start_pos[keep[i].second]; if (min_length == -1 || len < min_length) { min_length = len; startPos = start_pos[keep[i].second]; endPos = keep[i].first; } } printf("%d %d\n", startPos, endPos); } else printf("%d %d\n", 1, N); return 0; }
### Prompt Construct a cpp code solution to the problem outlined: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <typename T, typename T1> ostream& operator<<(ostream& out, pair<T, T1> obj) { return out << "(" << obj.first << "," << obj.second << ")"; } template <typename T, typename T1> ostream& operator<<(ostream& out, map<T, T1> cont) { typename map<T, T1>::const_iterator itr = cont.begin(); typename map<T, T1>::const_iterator ends = cont.end(); for (; itr != ends; ++itr) out << *itr << " "; out << endl; } template <typename T, template <typename ELEM, typename ALLOC = allocator<ELEM>> class CONT> ostream& operator<<(ostream& out, CONT<T> cont) { typename CONT<T>::const_iterator itr = cont.begin(); typename CONT<T>::const_iterator ends = cont.end(); for (; itr != ends; ++itr) out << *itr << " "; out << endl; } template <typename T> T gcd(T a, T b) { T min_v = min(a, b); T max_v = max(a, b); while (min_v) { T temp = max_v % min_v; max_v = min_v; min_v = temp; } return max_v; } int N, maxTime, min_length = -1, startPos, endPos; int elements[100010], counts[1000010], start_pos[1000010]; vector<pair<int, int>> keep; int main() { scanf("%d", &N); for (auto i = 0 - (0 > N); i != N - (0 > N); i += 1 - 2 * (0 > N)) scanf("%d", elements + i); for (auto i = 0 - (0 > 1000010); i != 1000010 - (0 > 1000010); i += 1 - 2 * (0 > 1000010)) start_pos[i] = -1; for (auto i = 0 - (0 > N); i != N - (0 > N); i += 1 - 2 * (0 > N)) if (start_pos[elements[i]] == -1) start_pos[elements[i]] = i + 1; for (auto i = 0 - (0 > N); i != N - (0 > N); i += 1 - 2 * (0 > N)) { ++counts[elements[i]]; if (counts[elements[i]] > maxTime) maxTime = counts[elements[i]]; } for (auto i = 0 - (0 > 1000010); i != 1000010 - (0 > 1000010); i += 1 - 2 * (0 > 1000010)) counts[i] = 0; for (auto i = 0 - (0 > N); i != N - (0 > N); i += 1 - 2 * (0 > N)) { ++counts[elements[i]]; if (counts[elements[i]] == maxTime) keep.emplace_back(i + 1, elements[i]); } if (keep.size()) { for (auto i = 0 - (0 > keep.size()); i != keep.size() - (0 > keep.size()); i += 1 - 2 * (0 > keep.size())) { int len = keep[i].first - start_pos[keep[i].second]; if (min_length == -1 || len < min_length) { min_length = len; startPos = start_pos[keep[i].second]; endPos = keep[i].first; } } printf("%d %d\n", startPos, endPos); } else printf("%d %d\n", 1, N); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e6 + 5; int a[MAXN]; int num[MAXN]; int l[MAXN], r[MAXN]; int main() { int m, n, i, j, k; ios::sync_with_stdio(false); ; cin >> m; for (i = 1; i <= m; i++) cin >> a[i]; for (i = 0; i <= 1e6; i++) l[i] = 999999999; for (i = 1; i <= m; i++) { num[a[i]]++; l[a[i]] = min(l[a[i]], i); r[a[i]] = max(r[a[i]], i); } int ansl = 1, ansr = m, ans = 0; for (i = 0; i <= 1e6; i++) { if (num[i] > ans || num[i] == ans && (r[i] - l[i]) < (ansr - ansl)) { ansl = l[i], ansr = r[i], ans = num[i]; } } cout << ansl << ' ' << ansr << endl; }
### Prompt In Cpp, your task is to solve the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 1e6 + 5; int a[MAXN]; int num[MAXN]; int l[MAXN], r[MAXN]; int main() { int m, n, i, j, k; ios::sync_with_stdio(false); ; cin >> m; for (i = 1; i <= m; i++) cin >> a[i]; for (i = 0; i <= 1e6; i++) l[i] = 999999999; for (i = 1; i <= m; i++) { num[a[i]]++; l[a[i]] = min(l[a[i]], i); r[a[i]] = max(r[a[i]], i); } int ansl = 1, ansr = m, ans = 0; for (i = 0; i <= 1e6; i++) { if (num[i] > ans || num[i] == ans && (r[i] - l[i]) < (ansr - ansl)) { ansl = l[i], ansr = r[i], ans = num[i]; } } cout << ansl << ' ' << ansr << endl; } ```
#include <bits/stdc++.h> using namespace std; const int MAX = 1000100; vector<pair<int, pair<int, int> > > b(MAX); int main() { int n; scanf("%d", &n); int x; for (int i = 1; i <= n; i++) { scanf("%d", &x); if (b[x].first == 0) b[x].second.first = i; b[x].second.second = i; b[x].first++; } int ansl = 1, ansr = n, m = 0, len = n; for (int i = 0; i < MAX; i++) if (b[i].first > m) m = b[i].first; for (int i = 0; i < MAX; i++) { if (b[i].first == m) { if (b[i].second.second - b[i].second.first <= len) { ansl = b[i].second.first; ansr = b[i].second.second; len = b[i].second.second - b[i].second.first; } } } printf("%d %d\n", ansl, ansr); return 0; }
### Prompt Please formulate a Cpp solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAX = 1000100; vector<pair<int, pair<int, int> > > b(MAX); int main() { int n; scanf("%d", &n); int x; for (int i = 1; i <= n; i++) { scanf("%d", &x); if (b[x].first == 0) b[x].second.first = i; b[x].second.second = i; b[x].first++; } int ansl = 1, ansr = n, m = 0, len = n; for (int i = 0; i < MAX; i++) if (b[i].first > m) m = b[i].first; for (int i = 0; i < MAX; i++) { if (b[i].first == m) { if (b[i].second.second - b[i].second.first <= len) { ansl = b[i].second.first; ansr = b[i].second.second; len = b[i].second.second - b[i].second.first; } } } printf("%d %d\n", ansl, ansr); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int BASE = 10007; const int MAXN = 100005; const int MAXC = 1000006; int read() { int x; scanf("%d", &x); return x; } int a[MAXN]; int f[MAXC]; int l[MAXC]; int r[MAXC]; int main() { memset(f, 0, sizeof(f)); int n = read(); int maxf = 0; for (int i = 0, sz = (n); i < sz; ++i) { a[i] = read(); if (f[a[i]] == 0) l[a[i]] = i; f[a[i]]++; r[a[i]] = i; maxf = max(maxf, f[a[i]]); } int maxl = n + 1; int left = 0; int right = n - 1; for (int i = 0, sz = (n); i < sz; ++i) { if (f[a[i]] == maxf && (r[a[i]] - l[a[i]] + 1 < maxl)) { maxl = r[a[i]] - l[a[i]] + 1; left = l[a[i]]; right = r[a[i]]; } } printf("%d %d", left + 1, right + 1); return 0; }
### Prompt Create a solution in CPP for the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int BASE = 10007; const int MAXN = 100005; const int MAXC = 1000006; int read() { int x; scanf("%d", &x); return x; } int a[MAXN]; int f[MAXC]; int l[MAXC]; int r[MAXC]; int main() { memset(f, 0, sizeof(f)); int n = read(); int maxf = 0; for (int i = 0, sz = (n); i < sz; ++i) { a[i] = read(); if (f[a[i]] == 0) l[a[i]] = i; f[a[i]]++; r[a[i]] = i; maxf = max(maxf, f[a[i]]); } int maxl = n + 1; int left = 0; int right = n - 1; for (int i = 0, sz = (n); i < sz; ++i) { if (f[a[i]] == maxf && (r[a[i]] - l[a[i]] + 1 < maxl)) { maxl = r[a[i]] - l[a[i]] + 1; left = l[a[i]]; right = r[a[i]]; } } printf("%d %d", left + 1, right + 1); return 0; } ```
#include <bits/stdc++.h> using namespace std; long long int li[1000005], A[100011], ri[1000005], c[1000005]; int main() { long long int n, i, loci, locj; cin >> n; long long int maxi = INT_MIN; for (i = 0; i < n; i++) { cin >> A[i]; if (!c[A[i]]) { c[A[i]]++; li[A[i]] = i + 1; ri[A[i]] = i + 1; } else { c[A[i]]++; ri[A[i]] = i + 1; } if (maxi < c[A[i]]) { maxi = c[A[i]]; } } long long int ans = INT_MAX; for (i = 0; i < 1000005; i++) { if (c[i] == maxi) { if (ans > (ri[i] - li[i])) { ans = (ri[i] - li[i]); loci = li[i]; locj = ri[i]; } } } cout << loci << " " << locj << endl; return 0; }
### Prompt Create a solution in CPP for the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long int li[1000005], A[100011], ri[1000005], c[1000005]; int main() { long long int n, i, loci, locj; cin >> n; long long int maxi = INT_MIN; for (i = 0; i < n; i++) { cin >> A[i]; if (!c[A[i]]) { c[A[i]]++; li[A[i]] = i + 1; ri[A[i]] = i + 1; } else { c[A[i]]++; ri[A[i]] = i + 1; } if (maxi < c[A[i]]) { maxi = c[A[i]]; } } long long int ans = INT_MAX; for (i = 0; i < 1000005; i++) { if (c[i] == maxi) { if (ans > (ri[i] - li[i])) { ans = (ri[i] - li[i]); loci = li[i]; locj = ri[i]; } } } cout << loci << " " << locj << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int M[1000005] = {}, m[1000005] = {}, n[1000005] = {}; int main() { int a; while (cin >> a) { int x; int big = 0, R = 0, L = 0; for (int i = 0; i < a; i++) { cin >> x; if (n[x] == 0) { M[x] = i; m[x] = i; } else { M[x] = i; } n[x]++; int del = R - L; if ((big < n[x]) || ((big == n[x]) && del > (M[x] - m[x]))) { big = n[x]; R = M[x]; L = m[x]; } } cout << (L + 1) << " " << (R + 1) << endl; } }
### Prompt Your task is to create a CPP solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int M[1000005] = {}, m[1000005] = {}, n[1000005] = {}; int main() { int a; while (cin >> a) { int x; int big = 0, R = 0, L = 0; for (int i = 0; i < a; i++) { cin >> x; if (n[x] == 0) { M[x] = i; m[x] = i; } else { M[x] = i; } n[x]++; int del = R - L; if ((big < n[x]) || ((big == n[x]) && del > (M[x] - m[x]))) { big = n[x]; R = M[x]; L = m[x]; } } cout << (L + 1) << " " << (R + 1) << endl; } } ```
#include <bits/stdc++.h> using namespace std; struct Reader { private: static const int BUF_SIZE = 1 << 22; char BUF_R[BUF_SIZE], *csy1, *csy2; template <class T> inline void RI(T& t) { int u = 0; char c = (csy1 == csy2 && (csy2 = (csy1 = BUF_R) + fread(BUF_R, 1, BUF_SIZE, stdin), csy1 == csy2) ? EOF : *csy1++); for (t = 0; c < 48 || c > 57; c = (csy1 == csy2 && (csy2 = (csy1 = BUF_R) + fread(BUF_R, 1, BUF_SIZE, stdin), csy1 == csy2) ? EOF : *csy1++)) { if (c == EOF) return; u = c == '-' ? 1 : 0; } for (; c > 47 && c < 58; c = (csy1 == csy2 && (csy2 = (csy1 = BUF_R) + fread(BUF_R, 1, BUF_SIZE, stdin), csy1 == csy2) ? EOF : *csy1++)) t = (t << 1) + (t << 3) + c - 48; t = u ? -t : t; } template <class T> inline void RU(T& t) { char c = (csy1 == csy2 && (csy2 = (csy1 = BUF_R) + fread(BUF_R, 1, BUF_SIZE, stdin), csy1 == csy2) ? EOF : *csy1++); for (t = 0; c < 48 || c > 57; c = (csy1 == csy2 && (csy2 = (csy1 = BUF_R) + fread(BUF_R, 1, BUF_SIZE, stdin), csy1 == csy2) ? EOF : *csy1++)) if (c == EOF) return; for (; c > 47 && c < 58; c = (csy1 == csy2 && (csy2 = (csy1 = BUF_R) + fread(BUF_R, 1, BUF_SIZE, stdin), csy1 == csy2) ? EOF : *csy1++)) t = (t << 1) + (t << 3) + c - 48; } public: inline Reader& operator>>(char& c) { c = (csy1 == csy2 && (csy2 = (csy1 = BUF_R) + fread(BUF_R, 1, BUF_SIZE, stdin), csy1 == csy2) ? EOF : *csy1++); return *this; } inline Reader& operator>>(int& t) { RI(t); return *this; } inline Reader& operator>>(long long& t) { RI(t); return *this; } inline Reader& operator>>(char* s) { char c = (csy1 == csy2 && (csy2 = (csy1 = BUF_R) + fread(BUF_R, 1, BUF_SIZE, stdin), csy1 == csy2) ? EOF : *csy1++); for (*s = 0; c < 33; c = (csy1 == csy2 && (csy2 = (csy1 = BUF_R) + fread(BUF_R, 1, BUF_SIZE, stdin), csy1 == csy2) ? EOF : *csy1++)) if (c == EOF) return *this; for (; c > 32; c = (csy1 == csy2 && (csy2 = (csy1 = BUF_R) + fread(BUF_R, 1, BUF_SIZE, stdin), csy1 == csy2) ? EOF : *csy1++)) *s++ = c; *s = 0; return *this; } } Dieulouard; struct Writer { private: static const int BUF_SIZE = 1 << 22; char BUF_W[BUF_SIZE], *csy; inline void WC(const char c) { if (csy - BUF_W == BUF_SIZE) fwrite(BUF_W, 1, BUF_SIZE, stdout), csy = BUF_W; *csy++ = c; } template <class T> inline void WI(T x) { static int sta[38]; int top = 0; if (x < 0) { WC('-'); do sta[top++] = -(x % 10); while (x /= 10); } else do sta[top++] = x % 10; while (x /= 10); while (top) WC(sta[--top] + '0'); } public: Writer() : csy(BUF_W) {} ~Writer() { fwrite(BUF_W, 1, csy - BUF_W, stdout); } inline void flush() { fwrite(BUF_W, 1, csy - BUF_W, stdout); csy = BUF_W; } inline Writer& operator<<(const char c) { WC(c); return *this; } inline Writer& operator<<(int x) { WI(x); return *this; } inline Writer& operator<<(long long x) { WI(x); return *this; } inline Writer& operator<<(const char* s) { while (*s) WC(*s++); return *this; } } Houffalize; const int N = 100005; const int M = 1000005; int n, a[N], cnt[M], left_pos[M], right_pos[M], maxcnt; int len = 1e9, _l, _r; int main() { Dieulouard >> n; for (int i = 1; i <= n; ++i) { Dieulouard >> a[i]; if (!left_pos[a[i]]) left_pos[a[i]] = i; cnt[a[i]]++; } for (int i = n; i > 0; --i) { if (!right_pos[a[i]]) right_pos[a[i]] = i; maxcnt = max(maxcnt, cnt[a[i]]); } for (int i = 1; i < M; ++i) { if (cnt[i] == maxcnt) { if (len > right_pos[i] - left_pos[i] + 1) { len = right_pos[i] - left_pos[i] + 1; _l = left_pos[i], _r = right_pos[i]; } else if (len == right_pos[i] - left_pos[i] + 1 && _l > left_pos[i]) { _l = left_pos[i]; _r = right_pos[i]; } } } Houffalize << _l << " " << _r << '\n'; return 0; }
### Prompt Please create a solution in Cpp to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct Reader { private: static const int BUF_SIZE = 1 << 22; char BUF_R[BUF_SIZE], *csy1, *csy2; template <class T> inline void RI(T& t) { int u = 0; char c = (csy1 == csy2 && (csy2 = (csy1 = BUF_R) + fread(BUF_R, 1, BUF_SIZE, stdin), csy1 == csy2) ? EOF : *csy1++); for (t = 0; c < 48 || c > 57; c = (csy1 == csy2 && (csy2 = (csy1 = BUF_R) + fread(BUF_R, 1, BUF_SIZE, stdin), csy1 == csy2) ? EOF : *csy1++)) { if (c == EOF) return; u = c == '-' ? 1 : 0; } for (; c > 47 && c < 58; c = (csy1 == csy2 && (csy2 = (csy1 = BUF_R) + fread(BUF_R, 1, BUF_SIZE, stdin), csy1 == csy2) ? EOF : *csy1++)) t = (t << 1) + (t << 3) + c - 48; t = u ? -t : t; } template <class T> inline void RU(T& t) { char c = (csy1 == csy2 && (csy2 = (csy1 = BUF_R) + fread(BUF_R, 1, BUF_SIZE, stdin), csy1 == csy2) ? EOF : *csy1++); for (t = 0; c < 48 || c > 57; c = (csy1 == csy2 && (csy2 = (csy1 = BUF_R) + fread(BUF_R, 1, BUF_SIZE, stdin), csy1 == csy2) ? EOF : *csy1++)) if (c == EOF) return; for (; c > 47 && c < 58; c = (csy1 == csy2 && (csy2 = (csy1 = BUF_R) + fread(BUF_R, 1, BUF_SIZE, stdin), csy1 == csy2) ? EOF : *csy1++)) t = (t << 1) + (t << 3) + c - 48; } public: inline Reader& operator>>(char& c) { c = (csy1 == csy2 && (csy2 = (csy1 = BUF_R) + fread(BUF_R, 1, BUF_SIZE, stdin), csy1 == csy2) ? EOF : *csy1++); return *this; } inline Reader& operator>>(int& t) { RI(t); return *this; } inline Reader& operator>>(long long& t) { RI(t); return *this; } inline Reader& operator>>(char* s) { char c = (csy1 == csy2 && (csy2 = (csy1 = BUF_R) + fread(BUF_R, 1, BUF_SIZE, stdin), csy1 == csy2) ? EOF : *csy1++); for (*s = 0; c < 33; c = (csy1 == csy2 && (csy2 = (csy1 = BUF_R) + fread(BUF_R, 1, BUF_SIZE, stdin), csy1 == csy2) ? EOF : *csy1++)) if (c == EOF) return *this; for (; c > 32; c = (csy1 == csy2 && (csy2 = (csy1 = BUF_R) + fread(BUF_R, 1, BUF_SIZE, stdin), csy1 == csy2) ? EOF : *csy1++)) *s++ = c; *s = 0; return *this; } } Dieulouard; struct Writer { private: static const int BUF_SIZE = 1 << 22; char BUF_W[BUF_SIZE], *csy; inline void WC(const char c) { if (csy - BUF_W == BUF_SIZE) fwrite(BUF_W, 1, BUF_SIZE, stdout), csy = BUF_W; *csy++ = c; } template <class T> inline void WI(T x) { static int sta[38]; int top = 0; if (x < 0) { WC('-'); do sta[top++] = -(x % 10); while (x /= 10); } else do sta[top++] = x % 10; while (x /= 10); while (top) WC(sta[--top] + '0'); } public: Writer() : csy(BUF_W) {} ~Writer() { fwrite(BUF_W, 1, csy - BUF_W, stdout); } inline void flush() { fwrite(BUF_W, 1, csy - BUF_W, stdout); csy = BUF_W; } inline Writer& operator<<(const char c) { WC(c); return *this; } inline Writer& operator<<(int x) { WI(x); return *this; } inline Writer& operator<<(long long x) { WI(x); return *this; } inline Writer& operator<<(const char* s) { while (*s) WC(*s++); return *this; } } Houffalize; const int N = 100005; const int M = 1000005; int n, a[N], cnt[M], left_pos[M], right_pos[M], maxcnt; int len = 1e9, _l, _r; int main() { Dieulouard >> n; for (int i = 1; i <= n; ++i) { Dieulouard >> a[i]; if (!left_pos[a[i]]) left_pos[a[i]] = i; cnt[a[i]]++; } for (int i = n; i > 0; --i) { if (!right_pos[a[i]]) right_pos[a[i]] = i; maxcnt = max(maxcnt, cnt[a[i]]); } for (int i = 1; i < M; ++i) { if (cnt[i] == maxcnt) { if (len > right_pos[i] - left_pos[i] + 1) { len = right_pos[i] - left_pos[i] + 1; _l = left_pos[i], _r = right_pos[i]; } else if (len == right_pos[i] - left_pos[i] + 1 && _l > left_pos[i]) { _l = left_pos[i]; _r = right_pos[i]; } } } Houffalize << _l << " " << _r << '\n'; return 0; } ```
#include <bits/stdc++.h> using namespace std; map<long long, long long> m1, m2, m3, m4; int main() { ios_base::sync_with_stdio(false); long long n, i, j, k, mx = INT_MIN; cin >> n; long long a[n + 1]; vector<long long> v; for (i = 0; i < n; i++) { cin >> a[i]; m1[a[i]]++; mx = max(mx, m1[a[i]]); v.push_back(a[i]); } for (i = 0; i < n; i++) { m2[a[i]]++; if (m2[a[i]] == 1) { m3[a[i]] = i + 1; } if (m2[a[i]] == m1[a[i]]) { m4[a[i]] = i + 1; } } sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); long long dif = INT_MAX, a1, b1; for (i = 0; i < v.size(); i++) { long long x = v[i]; long long d = m1[x], b = m3[x], c = m4[x]; if (c - b < dif && d == mx) { dif = c - b; a1 = b; b1 = c; } } cout << a1 << " " << b1 << endl; return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; map<long long, long long> m1, m2, m3, m4; int main() { ios_base::sync_with_stdio(false); long long n, i, j, k, mx = INT_MIN; cin >> n; long long a[n + 1]; vector<long long> v; for (i = 0; i < n; i++) { cin >> a[i]; m1[a[i]]++; mx = max(mx, m1[a[i]]); v.push_back(a[i]); } for (i = 0; i < n; i++) { m2[a[i]]++; if (m2[a[i]] == 1) { m3[a[i]] = i + 1; } if (m2[a[i]] == m1[a[i]]) { m4[a[i]] = i + 1; } } sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); long long dif = INT_MAX, a1, b1; for (i = 0; i < v.size(); i++) { long long x = v[i]; long long d = m1[x], b = m3[x], c = m4[x]; if (c - b < dif && d == mx) { dif = c - b; a1 = b; b1 = c; } } cout << a1 << " " << b1 << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main(int argc, char const *argv[]) { int n; cin >> n; map<int, int> m; pair<int, int> maxx; pair<int, int> idx; map<int, int> fo; idx.first = 0; idx.second = INT_MAX; maxx.second = -456; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; if (!m.count(arr[i])) { fo[arr[i]] = i; m[arr[i]] = 1; } else m[arr[i]]++; if (m[arr[i]] >= maxx.second) { if (i - fo[arr[i]] < (idx.second - idx.first) || (m[arr[i]]) > maxx.second) { maxx.first = arr[i]; maxx.second = m[arr[i]]; idx.first = fo[arr[i]]; idx.second = i; } } } cout << idx.first + 1 << " " << idx.second + 1; return 0; }
### Prompt Please formulate a Cpp solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main(int argc, char const *argv[]) { int n; cin >> n; map<int, int> m; pair<int, int> maxx; pair<int, int> idx; map<int, int> fo; idx.first = 0; idx.second = INT_MAX; maxx.second = -456; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; if (!m.count(arr[i])) { fo[arr[i]] = i; m[arr[i]] = 1; } else m[arr[i]]++; if (m[arr[i]] >= maxx.second) { if (i - fo[arr[i]] < (idx.second - idx.first) || (m[arr[i]]) > maxx.second) { maxx.first = arr[i]; maxx.second = m[arr[i]]; idx.first = fo[arr[i]]; idx.second = i; } } } cout << idx.first + 1 << " " << idx.second + 1; return 0; } ```
#include <bits/stdc++.h> using namespace std; int a[2000000]; struct node { int num, fir, ed; } b[2000000]; int n; int maxb = 0, maxi; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); if (b[a[i]].num == 0) { b[a[i]].fir = i; b[a[i]].ed = i; } else { b[a[i]].ed = i; } b[a[i]].num++; } for (int i = 1; i <= 1000000; i++) { if (b[i].num > maxb) { maxb = b[i].num; maxi = i; } else if (b[i].num == maxb) { if (b[i].ed - b[i].fir < b[maxi].ed - b[maxi].fir) { maxb = b[i].num; maxi = i; } } } printf("%d %d\n", b[maxi].fir, b[maxi].ed); }
### Prompt Your task is to create a CPP solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int a[2000000]; struct node { int num, fir, ed; } b[2000000]; int n; int maxb = 0, maxi; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); if (b[a[i]].num == 0) { b[a[i]].fir = i; b[a[i]].ed = i; } else { b[a[i]].ed = i; } b[a[i]].num++; } for (int i = 1; i <= 1000000; i++) { if (b[i].num > maxb) { maxb = b[i].num; maxi = i; } else if (b[i].num == maxb) { if (b[i].ed - b[i].fir < b[maxi].ed - b[maxi].fir) { maxb = b[i].num; maxi = i; } } } printf("%d %d\n", b[maxi].fir, b[maxi].ed); } ```
#include <bits/stdc++.h> using namespace std; vector<long long> pos[1000 * 1000 + 2]; int32_t main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); long long n; cin >> n; for (long long i = 0; i < n; i++) { long long x; cin >> x; pos[x].push_back(i); } long long mx = 0; for (long long i = 1; i <= 1000 * 1000; i++) mx = max(mx, (long long)pos[i].size()); long long mn = n, l = 1, r = n; for (long long i = 1; i <= 1000 * 1000; i++) { if (pos[i].size() == mx) { if (pos[i][pos[i].size() - 1] - pos[i][0] + 1 < mn) { mn = pos[i][pos[i].size() - 1] - pos[i][0] + 1; l = pos[i][0] + 1, r = pos[i][pos[i].size() - 1] + 1; } } } cout << l << " " << r << endl; return 0; }
### Prompt Construct a cpp code solution to the problem outlined: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<long long> pos[1000 * 1000 + 2]; int32_t main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); long long n; cin >> n; for (long long i = 0; i < n; i++) { long long x; cin >> x; pos[x].push_back(i); } long long mx = 0; for (long long i = 1; i <= 1000 * 1000; i++) mx = max(mx, (long long)pos[i].size()); long long mn = n, l = 1, r = n; for (long long i = 1; i <= 1000 * 1000; i++) { if (pos[i].size() == mx) { if (pos[i][pos[i].size() - 1] - pos[i][0] + 1 < mn) { mn = pos[i][pos[i].size() - 1] - pos[i][0] + 1; l = pos[i][0] + 1, r = pos[i][pos[i].size() - 1] + 1; } } } cout << l << " " << r << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; long long a[100001]; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%I64d", &a[i]); a[i] = a[i] * 1000001 + i; } sort(a, a + n); a[n] = a[n - 1] * 2; int m = 0, l, j = 0, ta, tb; for (int i = 1; i <= n; i++) if ((a[i] / 1000001 - a[j] / 1000001) > 0) { if (i - j > m || (i - j == m && a[i - 1] % 1000001 - a[j] % 1000001 < tb - ta)) { m = i - j; tb = a[i - 1] % 1000001; ta = a[j] % 1000001; } j = i; } printf("%d %d\n", ta + 1, tb + 1); return 0; }
### Prompt Please create a solution in Cpp to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; long long a[100001]; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%I64d", &a[i]); a[i] = a[i] * 1000001 + i; } sort(a, a + n); a[n] = a[n - 1] * 2; int m = 0, l, j = 0, ta, tb; for (int i = 1; i <= n; i++) if ((a[i] / 1000001 - a[j] / 1000001) > 0) { if (i - j > m || (i - j == m && a[i - 1] % 1000001 - a[j] % 1000001 < tb - ta)) { m = i - j; tb = a[i - 1] % 1000001; ta = a[j] % 1000001; } j = i; } printf("%d %d\n", ta + 1, tb + 1); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e6 + 5; struct elem { int cnt; int lo, hi; }; int n; elem a[MAXN]; int main() { scanf("%d", &n); int x; for (int i = 0; i < MAXN; ++i) { a[i].cnt = 0; a[i].lo = 1e9; a[i].hi = -1e9; } for (int i = 1; i <= n; ++i) { scanf("%d", &x); a[x].cnt += 1; a[x].lo = min(a[x].lo, i); a[x].hi = max(a[x].hi, i); } int len, rlo, rhi; len = 1e9; int maxC = 0; for (int i = 0; i < MAXN; ++i) { if (a[i].cnt > maxC || a[i].cnt == maxC && len > a[i].hi - a[i].lo + 1) { maxC = a[i].cnt; len = a[i].hi - a[i].lo + 1; rlo = a[i].lo; rhi = a[i].hi; } } cout << rlo << " " << rhi; return 0; }
### Prompt Please create a solution in Cpp to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 1e6 + 5; struct elem { int cnt; int lo, hi; }; int n; elem a[MAXN]; int main() { scanf("%d", &n); int x; for (int i = 0; i < MAXN; ++i) { a[i].cnt = 0; a[i].lo = 1e9; a[i].hi = -1e9; } for (int i = 1; i <= n; ++i) { scanf("%d", &x); a[x].cnt += 1; a[x].lo = min(a[x].lo, i); a[x].hi = max(a[x].hi, i); } int len, rlo, rhi; len = 1e9; int maxC = 0; for (int i = 0; i < MAXN; ++i) { if (a[i].cnt > maxC || a[i].cnt == maxC && len > a[i].hi - a[i].lo + 1) { maxC = a[i].cnt; len = a[i].hi - a[i].lo + 1; rlo = a[i].lo; rhi = a[i].hi; } } cout << rlo << " " << rhi; return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, a[100005], seq[100005]; int beh[100005], tail[100005], num[100005]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]), seq[i] = a[i]; sort(seq + 1, seq + 1 + n); for (int i = 1; i <= n; i++) a[i] = lower_bound(seq + 1, seq + 1 + n, a[i]) - seq; for (int i = 1; i <= n; i++) { if (beh[a[i]] == 0) beh[a[i]] = i; tail[a[i]] = i; num[a[i]]++; } int Max = 0; for (int i = 1; i <= n; i++) Max = max(Max, num[i]); int L, R, ans = 0x3f3f3f3f; for (int i = 1; i <= n; i++) if (num[i] == Max) { if (tail[i] - beh[i] + 1 >= ans) continue; ans = tail[i] - beh[i] + 1; L = beh[i], R = tail[i]; } printf("%d %d\n", L, R); return 0; }
### Prompt Please formulate a cpp solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, a[100005], seq[100005]; int beh[100005], tail[100005], num[100005]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]), seq[i] = a[i]; sort(seq + 1, seq + 1 + n); for (int i = 1; i <= n; i++) a[i] = lower_bound(seq + 1, seq + 1 + n, a[i]) - seq; for (int i = 1; i <= n; i++) { if (beh[a[i]] == 0) beh[a[i]] = i; tail[a[i]] = i; num[a[i]]++; } int Max = 0; for (int i = 1; i <= n; i++) Max = max(Max, num[i]); int L, R, ans = 0x3f3f3f3f; for (int i = 1; i <= n; i++) if (num[i] == Max) { if (tail[i] - beh[i] + 1 >= ans) continue; ans = tail[i] - beh[i] + 1; L = beh[i], R = tail[i]; } printf("%d %d\n", L, R); return 0; } ```
#include <bits/stdc++.h> using namespace std; map<int, int> A; int AA[1000000]; int n, M = 0, a, awal, akhir; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a; A[a]++; if (A[a] == 1) AA[a] = i; if (A[a] == M) { if (akhir - awal > i - AA[a]) { awal = AA[a]; akhir = i; } } else if (M < A[a]) { M = A[a]; awal = AA[a]; akhir = i; } } cout << awal << " " << akhir; }
### Prompt Create a solution in Cpp for the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; map<int, int> A; int AA[1000000]; int n, M = 0, a, awal, akhir; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a; A[a]++; if (A[a] == 1) AA[a] = i; if (A[a] == M) { if (akhir - awal > i - AA[a]) { awal = AA[a]; akhir = i; } } else if (M < A[a]) { M = A[a]; awal = AA[a]; akhir = i; } } cout << awal << " " << akhir; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; vector<int> pos[N]; int main() { int n; cin >> n; int mx = 0, l = 1, r = n; for (int i = 0; i < n; i++) { int x; cin >> x; pos[x].push_back(i); mx = max(mx, (int)pos[x].size()); } for (int i = 0; i < N; i++) if (pos[i].size() == mx && pos[i].back() - pos[i][0] < r - l) { r = pos[i].back() + 1; l = pos[i][0] + 1; } cout << l << ' ' << r << endl; }
### Prompt Please provide a CPP coded solution to the problem described below: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; vector<int> pos[N]; int main() { int n; cin >> n; int mx = 0, l = 1, r = n; for (int i = 0; i < n; i++) { int x; cin >> x; pos[x].push_back(i); mx = max(mx, (int)pos[x].size()); } for (int i = 0; i < N; i++) if (pos[i].size() == mx && pos[i].back() - pos[i][0] < r - l) { r = pos[i].back() + 1; l = pos[i][0] + 1; } cout << l << ' ' << r << endl; } ```
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 9, mod = 1e9 + 7, inf = 1e17; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k; long long arr[100005]; map<int, int> freq; cin >> n; map<int, pair<int, int>> m; set<int> s; for (int i = 0; i < n; i++) { cin >> arr[i]; freq[arr[i]]++; if (!s.count(arr[i])) m[arr[i]].first = i + 1; m[arr[i]].second = i + 1; s.insert(arr[i]); } int min = 1e8, l = -1, r = -1, max = -1; for (auto el : s) { if (freq[el] >= max) { if (freq[el] == max) { if ((m[el].second - m[el].first) < min) { min = (m[el].second - m[el].first); l = m[el].first; r = m[el].second; } } else { min = (m[el].second - m[el].first); l = m[el].first; r = m[el].second; } max = freq[el]; } } cout << l << " " << r; }
### Prompt Your task is to create a Cpp solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 9, mod = 1e9 + 7, inf = 1e17; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k; long long arr[100005]; map<int, int> freq; cin >> n; map<int, pair<int, int>> m; set<int> s; for (int i = 0; i < n; i++) { cin >> arr[i]; freq[arr[i]]++; if (!s.count(arr[i])) m[arr[i]].first = i + 1; m[arr[i]].second = i + 1; s.insert(arr[i]); } int min = 1e8, l = -1, r = -1, max = -1; for (auto el : s) { if (freq[el] >= max) { if (freq[el] == max) { if ((m[el].second - m[el].first) < min) { min = (m[el].second - m[el].first); l = m[el].first; r = m[el].second; } } else { min = (m[el].second - m[el].first); l = m[el].first; r = m[el].second; } max = freq[el]; } } cout << l << " " << r; } ```
#include <bits/stdc++.h> using namespace std; void setIO(const string& name = "") { ios_base::sync_with_stdio(false); cin.tie(nullptr); if ((int)(name).size()) { freopen((name + ".in").c_str(), "r", stdin); freopen((name + ".out").c_str(), "w", stdout); } } const int sz = 1e6 + 1; int counts[sz]; bool rvis[sz]; bool lvis[sz]; int lpos[sz]; int rpos[sz]; int main() { setIO(""); int n; cin >> n; vector<int> v(n); for (int i = 0; i < n; ++i) { cin >> v[i]; counts[v[i]]++; } int mx = 0; for (int i = 0; i < n; ++i) { if (counts[v[i]] > mx) { mx = counts[v[i]]; } } int l, r; for (int i = 0; i < n; ++i) { if (counts[v[i]] == mx and !lvis[v[i]]) { lpos[v[i]] = i + 1; lvis[v[i]] = true; } } for (int i = n - 1; i >= 0; --i) { if (counts[v[i]] == mx and !rvis[v[i]]) { rpos[v[i]] = i + 1; rvis[v[i]] = true; } } int mn = 1e9 + 1; for (int i = 0; i < n; ++i) { if (counts[v[i]] == mx) { if (rpos[v[i]] - lpos[v[i]] < mn) { mn = rpos[v[i]] - lpos[v[i]]; l = lpos[v[i]]; r = rpos[v[i]]; } } } cout << l << " " << r; }
### Prompt Please formulate a Cpp solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; void setIO(const string& name = "") { ios_base::sync_with_stdio(false); cin.tie(nullptr); if ((int)(name).size()) { freopen((name + ".in").c_str(), "r", stdin); freopen((name + ".out").c_str(), "w", stdout); } } const int sz = 1e6 + 1; int counts[sz]; bool rvis[sz]; bool lvis[sz]; int lpos[sz]; int rpos[sz]; int main() { setIO(""); int n; cin >> n; vector<int> v(n); for (int i = 0; i < n; ++i) { cin >> v[i]; counts[v[i]]++; } int mx = 0; for (int i = 0; i < n; ++i) { if (counts[v[i]] > mx) { mx = counts[v[i]]; } } int l, r; for (int i = 0; i < n; ++i) { if (counts[v[i]] == mx and !lvis[v[i]]) { lpos[v[i]] = i + 1; lvis[v[i]] = true; } } for (int i = n - 1; i >= 0; --i) { if (counts[v[i]] == mx and !rvis[v[i]]) { rpos[v[i]] = i + 1; rvis[v[i]] = true; } } int mn = 1e9 + 1; for (int i = 0; i < n; ++i) { if (counts[v[i]] == mx) { if (rpos[v[i]] - lpos[v[i]] < mn) { mn = rpos[v[i]] - lpos[v[i]]; l = lpos[v[i]]; r = rpos[v[i]]; } } } cout << l << " " << r; } ```
#include <bits/stdc++.h> using namespace std; struct point { int p, x; } pt[100007]; int n; bool cmp(point a, point b) { if (a.x == b.x) return a.p < b.p; return a.x < b.x; } struct node { int num, r, l; } nd[100007]; int cnt = 0; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &pt[i].x); pt[i].p = i; } sort(pt + 1, pt + 1 + n, cmp); nd[cnt].l = pt[1].p; int sum = 1; for (int i = 1; i <= n; i++) { if (pt[i].x != pt[i + 1].x) { nd[cnt].r = pt[i].p; nd[cnt].num = sum; nd[++cnt].l = pt[i + 1].p; sum = 0; } sum++; } int msum = 0; for (int i = 0; i < cnt; i++) msum = max(msum, nd[i].num); int mlen = n + 1, p = -1; for (int i = 0; i < cnt; i++) if (msum == nd[i].num) if (nd[i].r - nd[i].l < mlen) mlen = nd[i].r - nd[i].l, p = i; printf("%d %d\n", nd[p].l, nd[p].r); }
### Prompt Create a solution in CPP for the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct point { int p, x; } pt[100007]; int n; bool cmp(point a, point b) { if (a.x == b.x) return a.p < b.p; return a.x < b.x; } struct node { int num, r, l; } nd[100007]; int cnt = 0; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &pt[i].x); pt[i].p = i; } sort(pt + 1, pt + 1 + n, cmp); nd[cnt].l = pt[1].p; int sum = 1; for (int i = 1; i <= n; i++) { if (pt[i].x != pt[i + 1].x) { nd[cnt].r = pt[i].p; nd[cnt].num = sum; nd[++cnt].l = pt[i + 1].p; sum = 0; } sum++; } int msum = 0; for (int i = 0; i < cnt; i++) msum = max(msum, nd[i].num); int mlen = n + 1, p = -1; for (int i = 0; i < cnt; i++) if (msum == nd[i].num) if (nd[i].r - nd[i].l < mlen) mlen = nd[i].r - nd[i].l, p = i; printf("%d %d\n", nd[p].l, nd[p].r); } ```
#include <bits/stdc++.h> using namespace std; long long a[100004], cnt[1000004]; pair<long long, long long> p[1000002]; int main() { long long n, k = INT_MIN, temp = INT_MIN, x, y; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; k = max(k, a[i]); } for (int i = 1; i <= k; i++) p[i] = make_pair(INT_MAX, INT_MIN); for (long long i = 0; i < n; i++) { cnt[a[i]]++; p[a[i]].first = min(p[a[i]].first, i); p[a[i]].second = max(p[a[i]].second, i); } for (int i = 1; i <= k; i++) { if (temp == cnt[i] && (y - x) > p[i].second - p[i].first) { x = p[i].first; y = p[i].second; temp = cnt[i]; } else if (temp < cnt[i]) { x = p[i].first; y = p[i].second; temp = cnt[i]; } } cout << x + 1 << ' ' << y + 1 << "\n"; }
### Prompt Please create a solution in cpp to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long a[100004], cnt[1000004]; pair<long long, long long> p[1000002]; int main() { long long n, k = INT_MIN, temp = INT_MIN, x, y; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; k = max(k, a[i]); } for (int i = 1; i <= k; i++) p[i] = make_pair(INT_MAX, INT_MIN); for (long long i = 0; i < n; i++) { cnt[a[i]]++; p[a[i]].first = min(p[a[i]].first, i); p[a[i]].second = max(p[a[i]].second, i); } for (int i = 1; i <= k; i++) { if (temp == cnt[i] && (y - x) > p[i].second - p[i].first) { x = p[i].first; y = p[i].second; temp = cnt[i]; } else if (temp < cnt[i]) { x = p[i].first; y = p[i].second; temp = cnt[i]; } } cout << x + 1 << ' ' << y + 1 << "\n"; } ```
#include <bits/stdc++.h> using namespace std; map<int, int> rep; map<int, pair<int, int> > indx; map<int, int>::iterator it; int main() { int n, flag; cin >> n; for (int i = 1; i <= n; i++) { int num; cin >> num; if (indx.find(num) == indx.end()) { indx[num].first = i; indx[num].second = i; } else { if (i < indx[num].first) { indx[num].first = i; } if (i > indx[num].second) { indx[num].second = i; } } rep[num] += 1; } int max = 0; int diff; for (it = rep.begin(); it != rep.end(); it++) { if (it->second > max) { max = it->second; flag = it->first; diff = indx[it->first].second - indx[it->first].first; } if (it->second == max) { if ((indx[it->first].second - indx[it->first].first) < diff) { flag = it->first; diff = indx[it->first].second - indx[it->first].first; } } } cout << indx[flag].first << " " << indx[flag].second << endl; return 0; }
### Prompt Please create a solution in CPP to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; map<int, int> rep; map<int, pair<int, int> > indx; map<int, int>::iterator it; int main() { int n, flag; cin >> n; for (int i = 1; i <= n; i++) { int num; cin >> num; if (indx.find(num) == indx.end()) { indx[num].first = i; indx[num].second = i; } else { if (i < indx[num].first) { indx[num].first = i; } if (i > indx[num].second) { indx[num].second = i; } } rep[num] += 1; } int max = 0; int diff; for (it = rep.begin(); it != rep.end(); it++) { if (it->second > max) { max = it->second; flag = it->first; diff = indx[it->first].second - indx[it->first].first; } if (it->second == max) { if ((indx[it->first].second - indx[it->first].first) < diff) { flag = it->first; diff = indx[it->first].second - indx[it->first].first; } } } cout << indx[flag].first << " " << indx[flag].second << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; struct node { long long start; long long end; long long count; }; node arr[1000010]; long long ans[1000010]; long long maxi = -1; long long mini = 10000000; int main() { for (long long i = 1; i <= 1000000; i++) arr[i].count = 0; long long n; cin >> n; for (long long i = 0; i < n; i++) { long long x; cin >> x; if (arr[x].count == 0) { arr[x].count++; arr[x].start = i + 1; arr[x].end = i + 1; } else { arr[x].count++; arr[x].end = i + 1; } maxi = max(maxi, arr[x].count); } long long ans1, ans2; for (long long i = 1; i <= 1000000; i++) { if (arr[i].count == maxi) { long long x1 = arr[i].start; long long x2 = arr[i].end; if ((x2 - x1) < mini) { mini = x2 - x1; ans1 = x1; ans2 = x2; } } } cout << ans1 << " " << ans2 << endl; }
### Prompt Generate a Cpp solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct node { long long start; long long end; long long count; }; node arr[1000010]; long long ans[1000010]; long long maxi = -1; long long mini = 10000000; int main() { for (long long i = 1; i <= 1000000; i++) arr[i].count = 0; long long n; cin >> n; for (long long i = 0; i < n; i++) { long long x; cin >> x; if (arr[x].count == 0) { arr[x].count++; arr[x].start = i + 1; arr[x].end = i + 1; } else { arr[x].count++; arr[x].end = i + 1; } maxi = max(maxi, arr[x].count); } long long ans1, ans2; for (long long i = 1; i <= 1000000; i++) { if (arr[i].count == maxi) { long long x1 = arr[i].start; long long x2 = arr[i].end; if ((x2 - x1) < mini) { mini = x2 - x1; ans1 = x1; ans2 = x2; } } } cout << ans1 << " " << ans2 << endl; } ```
#include <bits/stdc++.h> using namespace std; long long int n, p, k; long long int st1, st2, en1, en2; long long int arr[1000009]; bool vis[31][31]; vector<pair<long long int, long long int>> moves = { {-1, 0}, {1, 0}, {0, -1}, {0, 1}}; long long int dx[] = {-1, 0, 1, 0}; long long int dy[] = {0, 1, 0, -1}; long long int solve(long long int arr[], long long int n, long long int m) { long long int dp[n + 2][m + 2]; memset(dp, 0, sizeof dp); for (long long int i = 0; i < n; i++) { for (long long int x = 1; x <= m; x++) { if (i == 0) { if (arr[i] == 0 || arr[i] == x) { dp[i][x] = 1; } else { dp[i][x] = 0; } } else { if (arr[i] == 0 || arr[i] == x) { dp[i][x] = (dp[i - 1][x - 1] + dp[i - 1][x]) % 1000000007 + dp[i - 1][x + 1] % 1000000007; } else { dp[i][x] = 0; } } } } long long int ans = 0; for (long long int x = 1; x <= m; x++) { ans += dp[n - 1][x] % 1000000007; } return ans % 1000000007; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); ; long long int n, m; cin >> n; long long int a[n]; for (long long int i = 0; i < n; i++) { cin >> a[i]; } long long int l = INT_MAX, r = INT_MIN; long long int ans = 0; map<long long int, long long int> mp, mp1; for (long long int i = 0; i < n; i++) { mp[a[i]]++; if (mp[a[i]] > ans) { ans = mp[a[i]]; } mp1[a[i]] = i + 1; } long long int sum = INT_MAX; for (long long int i = 0; i < n; i++) { if (ans == mp[a[i]]) { if (mp1[a[i]] - (i + 1) < sum) { l = i + 1; r = mp1[a[i]]; sum = mp1[a[i]] - (i + 1); } mp[a[i]] = 0; } } cout << l << " " << r << "\n"; }
### Prompt Generate a cpp solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long int n, p, k; long long int st1, st2, en1, en2; long long int arr[1000009]; bool vis[31][31]; vector<pair<long long int, long long int>> moves = { {-1, 0}, {1, 0}, {0, -1}, {0, 1}}; long long int dx[] = {-1, 0, 1, 0}; long long int dy[] = {0, 1, 0, -1}; long long int solve(long long int arr[], long long int n, long long int m) { long long int dp[n + 2][m + 2]; memset(dp, 0, sizeof dp); for (long long int i = 0; i < n; i++) { for (long long int x = 1; x <= m; x++) { if (i == 0) { if (arr[i] == 0 || arr[i] == x) { dp[i][x] = 1; } else { dp[i][x] = 0; } } else { if (arr[i] == 0 || arr[i] == x) { dp[i][x] = (dp[i - 1][x - 1] + dp[i - 1][x]) % 1000000007 + dp[i - 1][x + 1] % 1000000007; } else { dp[i][x] = 0; } } } } long long int ans = 0; for (long long int x = 1; x <= m; x++) { ans += dp[n - 1][x] % 1000000007; } return ans % 1000000007; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); ; long long int n, m; cin >> n; long long int a[n]; for (long long int i = 0; i < n; i++) { cin >> a[i]; } long long int l = INT_MAX, r = INT_MIN; long long int ans = 0; map<long long int, long long int> mp, mp1; for (long long int i = 0; i < n; i++) { mp[a[i]]++; if (mp[a[i]] > ans) { ans = mp[a[i]]; } mp1[a[i]] = i + 1; } long long int sum = INT_MAX; for (long long int i = 0; i < n; i++) { if (ans == mp[a[i]]) { if (mp1[a[i]] - (i + 1) < sum) { l = i + 1; r = mp1[a[i]]; sum = mp1[a[i]] - (i + 1); } mp[a[i]] = 0; } } cout << l << " " << r << "\n"; } ```
#include <bits/stdc++.h> using namespace std; const int MAX_VAL = 1000000; int N; struct Number { int val, l, r, times; } nums[MAX_VAL + 1]; int main() { for (int i = 1; i <= MAX_VAL; i++) { nums[i].val = i; nums[i].times = 0; nums[i].l = 0x3f3f3f3f; nums[i].r = -1; } scanf("%d", &N); for (int i = 0; i < N; i++) { int x; scanf("%d", &x); nums[x].times++; nums[x].l = min(nums[x].l, i); nums[x].r = max(nums[x].r, i); } vector<Number> valid; for (int i = 1; i <= MAX_VAL; i++) if (nums[i].l <= nums[i].r) valid.push_back(nums[i]); sort(valid.begin(), valid.end(), [](const Number& a, const Number& b) -> bool { if (a.times != b.times) return a.times > b.times; return (a.r - a.l) < (b.r - b.l); }); printf("%d %d\n", valid[0].l + 1, valid[0].r + 1); return 0; }
### Prompt Please formulate a CPP solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAX_VAL = 1000000; int N; struct Number { int val, l, r, times; } nums[MAX_VAL + 1]; int main() { for (int i = 1; i <= MAX_VAL; i++) { nums[i].val = i; nums[i].times = 0; nums[i].l = 0x3f3f3f3f; nums[i].r = -1; } scanf("%d", &N); for (int i = 0; i < N; i++) { int x; scanf("%d", &x); nums[x].times++; nums[x].l = min(nums[x].l, i); nums[x].r = max(nums[x].r, i); } vector<Number> valid; for (int i = 1; i <= MAX_VAL; i++) if (nums[i].l <= nums[i].r) valid.push_back(nums[i]); sort(valid.begin(), valid.end(), [](const Number& a, const Number& b) -> bool { if (a.times != b.times) return a.times > b.times; return (a.r - a.l) < (b.r - b.l); }); printf("%d %d\n", valid[0].l + 1, valid[0].r + 1); return 0; } ```
#include <bits/stdc++.h> using namespace std; bool cmp(pair<int, int> a, pair<int, int> b) { return abs(a.second - a.first) < abs(b.second - b.first); } int main() { ios_base::sync_with_stdio(); cin.tie(0); cout.tie(0); int n; cin >> n; pair<int, int> pos[1000001] = {{0, 0}}; std::map<int, int> mp; for (int i = 0; i < n; i++) { int x; cin >> x; if (mp[x] == 0) { pos[x].first = i + 1; } else { pos[x].second = i + 1; } ++mp[x]; } for (int i = 0; i < 1000001; i++) { if (pos[i].second == 0) pos[i].second = pos[i].first; } std::vector<pair<int, int>> nv; int mx = 0; for (auto i : mp) { mx = max(mx, i.second); } for (auto i : mp) { if (i.second == mx) { nv.push_back(pos[i.first]); } } sort(nv.begin(), nv.end(), cmp); cout << nv[0].first << " " << nv[0].second; return 0; }
### Prompt In cpp, your task is to solve the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool cmp(pair<int, int> a, pair<int, int> b) { return abs(a.second - a.first) < abs(b.second - b.first); } int main() { ios_base::sync_with_stdio(); cin.tie(0); cout.tie(0); int n; cin >> n; pair<int, int> pos[1000001] = {{0, 0}}; std::map<int, int> mp; for (int i = 0; i < n; i++) { int x; cin >> x; if (mp[x] == 0) { pos[x].first = i + 1; } else { pos[x].second = i + 1; } ++mp[x]; } for (int i = 0; i < 1000001; i++) { if (pos[i].second == 0) pos[i].second = pos[i].first; } std::vector<pair<int, int>> nv; int mx = 0; for (auto i : mp) { mx = max(mx, i.second); } for (auto i : mp) { if (i.second == mx) { nv.push_back(pos[i.first]); } } sort(nv.begin(), nv.end(), cmp); cout << nv[0].first << " " << nv[0].second; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int A = 1e6 + 5; const int N = 1e5 + 5; int cnt[A]; int n, a[N]; int main() { cin >> n; for (int i = 0; i < n; ++i) { scanf("%d", a + i); cnt[a[i]] += 1; } int ma = 0; for (int i = 1; i <= A - 5; ++i) { ma = max(ma, cnt[i]); cnt[i] = 0; } int i = 0, j = 0; int mi = INT_MAX; int l, r; while (i < n) { cnt[a[i]] += 1; if (cnt[a[i]] == ma) { while (j <= i) { if (a[i] == a[j] and cnt[a[j]] == ma) break; cnt[a[j]] -= 1; ++j; } if (i - j + 1 < mi) { mi = i - j + 1; l = j; r = i; cnt[a[j]] -= 1; ++j; } } ++i; } cout << l + 1 << " " << r + 1 << endl; return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int A = 1e6 + 5; const int N = 1e5 + 5; int cnt[A]; int n, a[N]; int main() { cin >> n; for (int i = 0; i < n; ++i) { scanf("%d", a + i); cnt[a[i]] += 1; } int ma = 0; for (int i = 1; i <= A - 5; ++i) { ma = max(ma, cnt[i]); cnt[i] = 0; } int i = 0, j = 0; int mi = INT_MAX; int l, r; while (i < n) { cnt[a[i]] += 1; if (cnt[a[i]] == ma) { while (j <= i) { if (a[i] == a[j] and cnt[a[j]] == ma) break; cnt[a[j]] -= 1; ++j; } if (i - j + 1 < mi) { mi = i - j + 1; l = j; r = i; cnt[a[j]] -= 1; ++j; } } ++i; } cout << l + 1 << " " << r + 1 << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, a[1000001], b[1000001], c[1000001], x, ans; int main() { cin >> n; for (int i = 1; i <= n; ++i) { cin >> x; ++a[x]; if (b[x] == 0) b[x] = i; c[x] = i; if (a[x] > a[ans]) ans = x; if (a[x] == a[ans] && c[x] - b[x] < c[ans] - b[ans]) ans = x; } cout << b[ans] << ' ' << c[ans]; }
### Prompt Construct a Cpp code solution to the problem outlined: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, a[1000001], b[1000001], c[1000001], x, ans; int main() { cin >> n; for (int i = 1; i <= n; ++i) { cin >> x; ++a[x]; if (b[x] == 0) b[x] = i; c[x] = i; if (a[x] > a[ans]) ans = x; if (a[x] == a[ans] && c[x] - b[x] < c[ans] - b[ans]) ans = x; } cout << b[ans] << ' ' << c[ans]; } ```
#include <bits/stdc++.h> using namespace std; int a[1000007]; int main() { map<int, int> make_pair, mpp, mppp; int m; cin >> m; for (int i = 0; i < m; i++) { cin >> a[i]; make_pair[a[i]]++; if (!mpp[a[i]]) mpp[a[i]] = i + 1; mppp[a[i]] = i + 1; } int maxx = 0; int l = 0, r = 0; for (int i = 0; i < m; i++) if (make_pair[a[i]] > maxx || make_pair[a[i]] == maxx && mppp[a[i]] - mpp[a[i]] < r - l) maxx = make_pair[a[i]], l = mpp[a[i]], r = mppp[a[i]]; cout << l << " " << r; return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int a[1000007]; int main() { map<int, int> make_pair, mpp, mppp; int m; cin >> m; for (int i = 0; i < m; i++) { cin >> a[i]; make_pair[a[i]]++; if (!mpp[a[i]]) mpp[a[i]] = i + 1; mppp[a[i]] = i + 1; } int maxx = 0; int l = 0, r = 0; for (int i = 0; i < m; i++) if (make_pair[a[i]] > maxx || make_pair[a[i]] == maxx && mppp[a[i]] - mpp[a[i]] < r - l) maxx = make_pair[a[i]], l = mpp[a[i]], r = mppp[a[i]]; cout << l << " " << r; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxN = 1e5 + 100; int a[maxN], b[maxN * 10]; int st[maxN * 10], fi[maxN * 10]; int n; int main() { cin >> n; for (int i = 0; i < n; i++) { scanf("%i", &a[i]); ++b[a[i]]; fi[a[i]] = i; } int res = 0; for (int i = 0; i <= 1000000; i++) res = max(res, b[i]); for (int i = n - 1; i >= 0; i--) st[a[i]] = i; int kq = 100000000, ll = 0, rr = 0; for (int i = 0; i < n; i++) if (res == b[a[i]] && kq > fi[a[i]] - st[a[i]]) { kq = fi[a[i]] - st[a[i]]; ll = st[a[i]]; rr = fi[a[i]]; } cout << ll + 1 << " " << rr + 1; }
### Prompt Create a solution in Cpp for the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxN = 1e5 + 100; int a[maxN], b[maxN * 10]; int st[maxN * 10], fi[maxN * 10]; int n; int main() { cin >> n; for (int i = 0; i < n; i++) { scanf("%i", &a[i]); ++b[a[i]]; fi[a[i]] = i; } int res = 0; for (int i = 0; i <= 1000000; i++) res = max(res, b[i]); for (int i = n - 1; i >= 0; i--) st[a[i]] = i; int kq = 100000000, ll = 0, rr = 0; for (int i = 0; i < n; i++) if (res == b[a[i]] && kq > fi[a[i]] - st[a[i]]) { kq = fi[a[i]] - st[a[i]]; ll = st[a[i]]; rr = fi[a[i]]; } cout << ll + 1 << " " << rr + 1; } ```
#include <bits/stdc++.h> using namespace std; struct node { int val, l, r; node() { val = 0, r = 0, l = 0; } bool operator<(const node& w) const { if (val == w.val) { if (r - l == w.r - w.l) { return l < w.l; } return r - l < w.r - w.l; } return val > w.val; } } a[1230456]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { int x; cin >> x; a[x].val++; if (a[x].l == 0) a[x].l = i; a[x].r = i; } sort(a + 1, a + 1 + 1000001); cout << a[1].l << " " << a[1].r; return 0; }
### Prompt In CPP, your task is to solve the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct node { int val, l, r; node() { val = 0, r = 0, l = 0; } bool operator<(const node& w) const { if (val == w.val) { if (r - l == w.r - w.l) { return l < w.l; } return r - l < w.r - w.l; } return val > w.val; } } a[1230456]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { int x; cin >> x; a[x].val++; if (a[x].l == 0) a[x].l = i; a[x].r = i; } sort(a + 1, a + 1 + 1000001); cout << a[1].l << " " << a[1].r; return 0; } ```
#include <bits/stdc++.h> int c[1111111]; int l[1111111]; int r[1111111]; int main() { int i, n, t; for (i = 0; i < 1111111; i++) l[i] = 1e9; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d", &t); c[t]++; l[t] = std::min(l[t], i); r[t] = std::max(r[t], i); } t = 0; for (i = 0; i < 1111111; i++) { if (c[i] > c[t]) t = i; else if (c[i] == c[t] && r[i] - l[i] < r[t] - l[t]) t = i; } printf("%d %d\n", l[t], r[t]); }
### Prompt Develop a solution in cpp to the problem described below: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> int c[1111111]; int l[1111111]; int r[1111111]; int main() { int i, n, t; for (i = 0; i < 1111111; i++) l[i] = 1e9; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d", &t); c[t]++; l[t] = std::min(l[t], i); r[t] = std::max(r[t], i); } t = 0; for (i = 0; i < 1111111; i++) { if (c[i] > c[t]) t = i; else if (c[i] == c[t] && r[i] - l[i] < r[t] - l[t]) t = i; } printf("%d %d\n", l[t], r[t]); } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; pair<int, int> b[100005]; for (int i = 0; i < n; ++i) { cin >> b[i].first; b[i].second = i; } sort(b, b + n); b[n].first = -1; int st = 0, en = 0, mx = 1, mxst = 0, mxen = 0; for (int i = 1; i <= n; ++i) { if (b[i].first != b[i - 1].first) { if (mx < i - st || (mx == i - st && mxen - mxst > b[i - 1].second - b[st].second)) { mx = i - st; mxst = b[st].second; mxen = b[i - 1].second; } st = i; } } cout << mxst + 1 << " " << mxen + 1; }
### Prompt Please formulate a cpp solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; pair<int, int> b[100005]; for (int i = 0; i < n; ++i) { cin >> b[i].first; b[i].second = i; } sort(b, b + n); b[n].first = -1; int st = 0, en = 0, mx = 1, mxst = 0, mxen = 0; for (int i = 1; i <= n; ++i) { if (b[i].first != b[i - 1].first) { if (mx < i - st || (mx == i - st && mxen - mxst > b[i - 1].second - b[st].second)) { mx = i - st; mxst = b[st].second; mxen = b[i - 1].second; } st = i; } } cout << mxst + 1 << " " << mxen + 1; } ```
#include <bits/stdc++.h> using namespace std; int main() { map<int, int> a, b, c; map<int, int>::iterator it; int n, i, x, m, k, l, r; cin >> n; m = 1; for (i = 1; i <= n; i++) { cin >> x; a[x]++; m = max(m, a[x]); if (a[x] == 1) b[x] = i; c[x] = i; } k = n; l = 1; r = n; for (it = a.begin(); it != a.end(); it++) { if (it->second == m) { x = it->first; if (c[x] - b[x] + 1 < k) { k = c[x] - b[x] + 1; l = b[x]; r = c[x]; } } } cout << l << " " << r << endl; return 0; }
### Prompt Develop a solution in cpp to the problem described below: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { map<int, int> a, b, c; map<int, int>::iterator it; int n, i, x, m, k, l, r; cin >> n; m = 1; for (i = 1; i <= n; i++) { cin >> x; a[x]++; m = max(m, a[x]); if (a[x] == 1) b[x] = i; c[x] = i; } k = n; l = 1; r = n; for (it = a.begin(); it != a.end(); it++) { if (it->second == m) { x = it->first; if (c[x] - b[x] + 1 < k) { k = c[x] - b[x] + 1; l = b[x]; r = c[x]; } } } cout << l << " " << r << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { map<int, int> p; map<int, pair<int, int>> c; int n, m, i, ans = 0; map<int, int>::iterator it; cin >> n; for (i = 0; i < n; i++) { cin >> m; if (p[m] == 0) { c[m].first = i + 1; c[m].second = i + 1; } else c[m].second = i + 1; p[m]++; } int a, b; m = -1; for (it = p.begin(); it != p.end(); it++) { if (it->second > m) { a = c[it->first].first; b = c[it->first].second; m = it->second; } else if (it->second == m && c[it->first].second - c[it->first].first < b - a) { a = c[it->first].first; b = c[it->first].second; } } cout << a << " " << b; return 0; }
### Prompt Please formulate a CPP solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { map<int, int> p; map<int, pair<int, int>> c; int n, m, i, ans = 0; map<int, int>::iterator it; cin >> n; for (i = 0; i < n; i++) { cin >> m; if (p[m] == 0) { c[m].first = i + 1; c[m].second = i + 1; } else c[m].second = i + 1; p[m]++; } int a, b; m = -1; for (it = p.begin(); it != p.end(); it++) { if (it->second > m) { a = c[it->first].first; b = c[it->first].second; m = it->second; } else if (it->second == m && c[it->first].second - c[it->first].first < b - a) { a = c[it->first].first; b = c[it->first].second; } } cout << a << " " << b; return 0; } ```
#include <bits/stdc++.h> using namespace std; int a[1000006][3] = {0}; int main() { int n, m, temp, i; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d", &temp); if (a[temp][0]) { a[temp][0]++; a[temp][2] = i; } else { a[temp][0]++; a[temp][1] = i; a[temp][2] = i; } } int ma = 0, l, r; for (i = 0; i <= 1000000; i++) { if (a[i][0]) { if (a[i][0] > ma) { ma = a[i][0]; l = a[i][1]; r = a[i][2]; } else if (a[i][0] == ma && r - l > a[i][2] - a[i][1]) { ma = a[i][0]; l = a[i][1]; r = a[i][2]; } } } printf("%d %d\n", l, r); }
### Prompt Please formulate a Cpp solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int a[1000006][3] = {0}; int main() { int n, m, temp, i; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d", &temp); if (a[temp][0]) { a[temp][0]++; a[temp][2] = i; } else { a[temp][0]++; a[temp][1] = i; a[temp][2] = i; } } int ma = 0, l, r; for (i = 0; i <= 1000000; i++) { if (a[i][0]) { if (a[i][0] > ma) { ma = a[i][0]; l = a[i][1]; r = a[i][2]; } else if (a[i][0] == ma && r - l > a[i][2] - a[i][1]) { ma = a[i][0]; l = a[i][1]; r = a[i][2]; } } } printf("%d %d\n", l, r); } ```
#include <bits/stdc++.h> using namespace std; pair<int, pair<int, int> > a[1000001]; int main() { int n, ans = 0, i, x, l, r; cin >> n; for (i = 1; i <= n; i++) { cin >> x; if (!a[x].first) a[x].first++, a[x].second.first = i, a[x].second.second = i; else a[x].first++, a[x].second.second = i; } sort(a + 1, a + 1000001); l = a[1000000].second.first; r = a[1000000].second.second; for (i = 1000000; i >= 2; i--) { if (a[i].first != a[i - 1].first) break; else { if ((r - l) > (a[i - 1].second.second - a[i - 1].second.first)) r = a[i - 1].second.second, l = a[i - 1].second.first; } } cout << l << " " << r; }
### Prompt Your challenge is to write a cpp solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; pair<int, pair<int, int> > a[1000001]; int main() { int n, ans = 0, i, x, l, r; cin >> n; for (i = 1; i <= n; i++) { cin >> x; if (!a[x].first) a[x].first++, a[x].second.first = i, a[x].second.second = i; else a[x].first++, a[x].second.second = i; } sort(a + 1, a + 1000001); l = a[1000000].second.first; r = a[1000000].second.second; for (i = 1000000; i >= 2; i--) { if (a[i].first != a[i - 1].first) break; else { if ((r - l) > (a[i - 1].second.second - a[i - 1].second.first)) r = a[i - 1].second.second, l = a[i - 1].second.first; } } cout << l << " " << r; } ```
#include <bits/stdc++.h> using namespace std; int m[1000005], L[1000005]; int main(void) { int n, x, i, j, ch = -1, maxi = 0, mini = 2000000000; cin >> n; for (i = 0; i < n; i++) { cin >> x; if (m[x] == 0) { L[x] = i; m[x] = 1; } else m[x]++; if (m[x] > maxi) { maxi = m[x]; mini = i - L[x] + 1; ch = L[x] + 1; } else if (m[x] == maxi && i - L[x] + 1 < mini) { mini = i - L[x] + 1; ch = L[x] + 1; } } cout << ch << " " << ch + mini - 1 << endl; return 0; }
### Prompt Please formulate a Cpp solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int m[1000005], L[1000005]; int main(void) { int n, x, i, j, ch = -1, maxi = 0, mini = 2000000000; cin >> n; for (i = 0; i < n; i++) { cin >> x; if (m[x] == 0) { L[x] = i; m[x] = 1; } else m[x]++; if (m[x] > maxi) { maxi = m[x]; mini = i - L[x] + 1; ch = L[x] + 1; } else if (m[x] == maxi && i - L[x] + 1 < mini) { mini = i - L[x] + 1; ch = L[x] + 1; } } cout << ch << " " << ch + mini - 1 << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } const long long INF = 1e18 + 99; const long long MOD = 1e9 + 7; inline long long two(long long n) { return 1 << n; } inline void set_bit(long long& n, long long b) { n |= two(b); } inline void unset_bit(long long& n, long long b) { n &= ~two(b); } inline long long last_bit(long long n) { return n & (-n); } inline long long ones(long long n) { long long res = 0; while (n && ++res) n -= n & (-n); return res; } template <class T> void chmax(T& a, const T& b) { a = max(a, b); } template <class T> void chmin(T& a, const T& b) { a = min(a, b); } template <class T> T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a % b) : a); } template <class T> T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); } template <typename T> T exp(T b, T p) { T x = 1; while (p) { if (p & 1) x = (x * b); b = (b * b); p = p >> 1; } return x; } void boostIO() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } long long modpow(long long a, long long b) { long long res = 1; a %= MOD; for (; b; b >>= 1) { if (b & 1) res = res * a % MOD; a = a * a % MOD; } return res; } const long long maxx = (long long)1e6 + 9; long long st[maxx], occur[maxx], maxi = 0, mini = -INF, ch = 0; int32_t main() { boostIO(); long long n; cin >> n; for (long long i = 0; i < (n); i++) { long long a; cin >> a; if (occur[a] == 0) { occur[a]++; st[a] = i; } else occur[a]++; if (occur[a] > maxi) { maxi = occur[a]; ch = st[a] + 1; mini = i - st[a] + 1; } if (occur[a] == maxi and i - st[a] + 1 < mini) { ch = st[a] + 1; mini = i - st[a] + 1; } } cout << ch << " " << ch + mini - 1 << "\n"; }
### Prompt Your challenge is to write a CPP solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } const long long INF = 1e18 + 99; const long long MOD = 1e9 + 7; inline long long two(long long n) { return 1 << n; } inline void set_bit(long long& n, long long b) { n |= two(b); } inline void unset_bit(long long& n, long long b) { n &= ~two(b); } inline long long last_bit(long long n) { return n & (-n); } inline long long ones(long long n) { long long res = 0; while (n && ++res) n -= n & (-n); return res; } template <class T> void chmax(T& a, const T& b) { a = max(a, b); } template <class T> void chmin(T& a, const T& b) { a = min(a, b); } template <class T> T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a % b) : a); } template <class T> T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); } template <typename T> T exp(T b, T p) { T x = 1; while (p) { if (p & 1) x = (x * b); b = (b * b); p = p >> 1; } return x; } void boostIO() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } long long modpow(long long a, long long b) { long long res = 1; a %= MOD; for (; b; b >>= 1) { if (b & 1) res = res * a % MOD; a = a * a % MOD; } return res; } const long long maxx = (long long)1e6 + 9; long long st[maxx], occur[maxx], maxi = 0, mini = -INF, ch = 0; int32_t main() { boostIO(); long long n; cin >> n; for (long long i = 0; i < (n); i++) { long long a; cin >> a; if (occur[a] == 0) { occur[a]++; st[a] = i; } else occur[a]++; if (occur[a] > maxi) { maxi = occur[a]; ch = st[a] + 1; mini = i - st[a] + 1; } if (occur[a] == maxi and i - st[a] + 1 < mini) { ch = st[a] + 1; mini = i - st[a] + 1; } } cout << ch << " " << ch + mini - 1 << "\n"; } ```
#include <bits/stdc++.h> using namespace std; int n, a[100001] = {0}, maxb = 0; map<int, int> cnt; map<int, vector<int> > mp; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; cnt[a[i]]++; maxb = max(maxb, cnt[a[i]]); } for (int i = 0; i < n; i++) { if (cnt[a[i]] == maxb) { mp[a[i]].push_back(i); } } int len = INT_MAX, e = 1; for (map<int, vector<int> >::iterator it = mp.begin(); it != mp.end(); it++) { if (it->second.back() - it->second[0] < len) { len = it->second.back() - it->second[0]; e = it->first; } } cout << mp[e][0] + 1 << " " << mp[e].back() + 1 << "\n"; return 0; }
### Prompt Please create a solution in CPP to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, a[100001] = {0}, maxb = 0; map<int, int> cnt; map<int, vector<int> > mp; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; cnt[a[i]]++; maxb = max(maxb, cnt[a[i]]); } for (int i = 0; i < n; i++) { if (cnt[a[i]] == maxb) { mp[a[i]].push_back(i); } } int len = INT_MAX, e = 1; for (map<int, vector<int> >::iterator it = mp.begin(); it != mp.end(); it++) { if (it->second.back() - it->second[0] < len) { len = it->second.back() - it->second[0]; e = it->first; } } cout << mp[e][0] + 1 << " " << mp[e].back() + 1 << "\n"; return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, a[1000005], mayor, x, pos; int primera[1000005], segunda[1000005]; int main() { scanf("%d", &n); fill(primera, primera + 1000005, 1e9); fill(segunda, segunda + 1000005, -1e9); for (int i = 0; i < n; ++i) { scanf("%d", &x); a[x]++; primera[x] = min(primera[x], i); segunda[x] = max(segunda[x], i); if (a[x] > mayor) { mayor = a[x]; pos = x; } else if (a[x] == mayor && ((segunda[pos] - primera[pos]) > (segunda[x] - primera[x]))) { mayor = a[x]; pos = x; } } printf("%d %d\n", primera[pos] + 1, segunda[pos] + 1); return 0; }
### Prompt Please formulate a cpp solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, a[1000005], mayor, x, pos; int primera[1000005], segunda[1000005]; int main() { scanf("%d", &n); fill(primera, primera + 1000005, 1e9); fill(segunda, segunda + 1000005, -1e9); for (int i = 0; i < n; ++i) { scanf("%d", &x); a[x]++; primera[x] = min(primera[x], i); segunda[x] = max(segunda[x], i); if (a[x] > mayor) { mayor = a[x]; pos = x; } else if (a[x] == mayor && ((segunda[pos] - primera[pos]) > (segunda[x] - primera[x]))) { mayor = a[x]; pos = x; } } printf("%d %d\n", primera[pos] + 1, segunda[pos] + 1); return 0; } ```
#include <bits/stdc++.h> using namespace std; vector<int> v[1000005]; int n, ar[100005]; int main() { cin >> n; set<int> st; for (int i = 0; i < n; i++) { cin >> ar[i]; st.insert(ar[i]); v[ar[i]].push_back(i + 1); } vector<int> fr(st.begin(), st.end()); int mx = 0, l = 1, r = n, mnd = INT_MAX; for (int i = 0; i < fr.size(); i++) { int x = fr[i]; if (v[fr[i]].size() >= mx) { if (mx < v[fr[i]].size() || v[x][v[x].size() - 1] - v[x][0] < mnd) { l = v[x][0]; r = v[x][v[x].size() - 1]; mnd = v[x][v[x].size() - 1] - v[x][0]; mx = v[x].size(); } } } cout << l << " " << r << endl; return 0; }
### Prompt Construct a CPP code solution to the problem outlined: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<int> v[1000005]; int n, ar[100005]; int main() { cin >> n; set<int> st; for (int i = 0; i < n; i++) { cin >> ar[i]; st.insert(ar[i]); v[ar[i]].push_back(i + 1); } vector<int> fr(st.begin(), st.end()); int mx = 0, l = 1, r = n, mnd = INT_MAX; for (int i = 0; i < fr.size(); i++) { int x = fr[i]; if (v[fr[i]].size() >= mx) { if (mx < v[fr[i]].size() || v[x][v[x].size() - 1] - v[x][0] < mnd) { l = v[x][0]; r = v[x][v[x].size() - 1]; mnd = v[x][v[x].size() - 1] - v[x][0]; mx = v[x].size(); } } } cout << l << " " << r << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, i, x, cnt[1000001], mx, imx, idx1[1000001], idx2[1000001], l, r; int main() { ios_base::sync_with_stdio(false); cin >> n; for (i = 1; i <= n; i++) { cin >> x; cnt[x]++; if (cnt[x] == 1) idx1[x] = i; idx2[x] = i; } mx = cnt[1]; imx = 1; for (i = 2; i < 1000001; i++) if (cnt[i] > mx) { mx = cnt[i]; imx = i; } l = 1; r = n; for (i = 1; i < 1000001; i++) if (cnt[i] == mx) { if (idx2[i] - idx1[i] < r - l) { r = idx2[i]; l = idx1[i]; } } cout << l << " " << r << endl; return 0; }
### Prompt In CPP, your task is to solve the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, i, x, cnt[1000001], mx, imx, idx1[1000001], idx2[1000001], l, r; int main() { ios_base::sync_with_stdio(false); cin >> n; for (i = 1; i <= n; i++) { cin >> x; cnt[x]++; if (cnt[x] == 1) idx1[x] = i; idx2[x] = i; } mx = cnt[1]; imx = 1; for (i = 2; i < 1000001; i++) if (cnt[i] > mx) { mx = cnt[i]; imx = i; } l = 1; r = n; for (i = 1; i < 1000001; i++) if (cnt[i] == mx) { if (idx2[i] - idx1[i] < r - l) { r = idx2[i]; l = idx1[i]; } } cout << l << " " << r << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; vector<vector<int>> input(1000001); int n; int main() { int num; cin >> n; for (int i = 0; i < n; i++) { cin >> num; input[num].push_back(i); } int max = 0; for (int i = 1; i <= 1000000; i++) { if (max < input[i].size()) { max = input[i].size(); } } vector<int> maximum; for (int i = 1; i <= 1000000; i++) if (input[i].size() == max) maximum.push_back(i); int min = 99999999; int index = 0; for (int i = 0; i < maximum.size(); i++) { if (input[maximum[i]][input[maximum[i]].size() - 1] - input[maximum[i]][0] < min) { min = input[maximum[i]][input[maximum[i]].size() - 1] - input[maximum[i]][0]; index = maximum[i]; } } cout << input[index][0] + 1 << " " << input[index][input[index].size() - 1] + 1 << endl; return 0; }
### Prompt Generate a cpp solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<vector<int>> input(1000001); int n; int main() { int num; cin >> n; for (int i = 0; i < n; i++) { cin >> num; input[num].push_back(i); } int max = 0; for (int i = 1; i <= 1000000; i++) { if (max < input[i].size()) { max = input[i].size(); } } vector<int> maximum; for (int i = 1; i <= 1000000; i++) if (input[i].size() == max) maximum.push_back(i); int min = 99999999; int index = 0; for (int i = 0; i < maximum.size(); i++) { if (input[maximum[i]][input[maximum[i]].size() - 1] - input[maximum[i]][0] < min) { min = input[maximum[i]][input[maximum[i]].size() - 1] - input[maximum[i]][0]; index = maximum[i]; } } cout << input[index][0] + 1 << " " << input[index][input[index].size() - 1] + 1 << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10, MAX = 1e6 + 1; int n; int l[MAX], r[MAX], sl[MAX]; bool mini(int &a, int b) { if (a <= b) return 0; a = b; return 1; } int main() { for (int i = 1; i < MAX; i++) l[i] = N, r[i] = 0; cin >> n; for (int i = 1, x; i <= n; i++) { cin >> x; l[x] = min(l[x], i), r[x] = max(r[x], i); sl[x]++; } int mS = 0, mD, L, R; for (int i = 1; i < MAX; i++) { if (!sl[i] || sl[i] < mS) continue; if (sl[i] == mS) if (mini(mD, r[i] - l[i] + 1)) L = l[i], R = r[i]; if (sl[i] > mS) { mS = sl[i]; mD = r[i] - l[i] + 1; L = l[i], R = r[i]; } } cout << L << ' ' << R; return 0; }
### Prompt Please formulate a Cpp solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10, MAX = 1e6 + 1; int n; int l[MAX], r[MAX], sl[MAX]; bool mini(int &a, int b) { if (a <= b) return 0; a = b; return 1; } int main() { for (int i = 1; i < MAX; i++) l[i] = N, r[i] = 0; cin >> n; for (int i = 1, x; i <= n; i++) { cin >> x; l[x] = min(l[x], i), r[x] = max(r[x], i); sl[x]++; } int mS = 0, mD, L, R; for (int i = 1; i < MAX; i++) { if (!sl[i] || sl[i] < mS) continue; if (sl[i] == mS) if (mini(mD, r[i] - l[i] + 1)) L = l[i], R = r[i]; if (sl[i] > mS) { mS = sl[i]; mD = r[i] - l[i] + 1; L = l[i], R = r[i]; } } cout << L << ' ' << R; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; struct Node { int st, end, len, cnt; Node() { st = end = len = cnt = 0; } Node(int _st, int _end, int _len, int _cnt) { st = _st, end = _end, len = _len; cnt = _cnt; } }; map<int, Node> mp; map<int, Node>::iterator it; vector<Node> vec; bool cmp(Node a, Node b) { if (a.cnt != b.cnt) return a.cnt > b.cnt; return a.len < b.len; } int n; int main() { while (scanf("%d", &n) != EOF) { mp.clear(); vec.clear(); int val; for (int i = 1; i <= n; i++) { scanf("%d", &val); if (!mp.count(val)) { mp[val] = Node(i, i, 1, 1); } else { Node& node = mp[val]; node.end = i; node.len = node.end - node.st + 1; node.cnt++; } } for (it = mp.begin(); it != mp.end(); it++) { vec.push_back(it->second); } sort(vec.begin(), vec.end(), cmp); printf("%d %d\n", vec[0].st, vec[0].end); } return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; struct Node { int st, end, len, cnt; Node() { st = end = len = cnt = 0; } Node(int _st, int _end, int _len, int _cnt) { st = _st, end = _end, len = _len; cnt = _cnt; } }; map<int, Node> mp; map<int, Node>::iterator it; vector<Node> vec; bool cmp(Node a, Node b) { if (a.cnt != b.cnt) return a.cnt > b.cnt; return a.len < b.len; } int n; int main() { while (scanf("%d", &n) != EOF) { mp.clear(); vec.clear(); int val; for (int i = 1; i <= n; i++) { scanf("%d", &val); if (!mp.count(val)) { mp[val] = Node(i, i, 1, 1); } else { Node& node = mp[val]; node.end = i; node.len = node.end - node.st + 1; node.cnt++; } } for (it = mp.begin(); it != mp.end(); it++) { vec.push_back(it->second); } sort(vec.begin(), vec.end(), cmp); printf("%d %d\n", vec[0].st, vec[0].end); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int Nmax = 1e6 + 17; int a[Nmax][3] = {0}; int main(void) { int n, x; scanf("%d", &n); for (int i = 0; i < Nmax; i++) a[i][0] = -1; for (int i = 0; i < n; i++) { scanf("%d", &x); if (a[x][0] == -1) a[x][0] = i; a[x][1] = i; a[x][2]++; } int l = 0, r = 0, count = 0; for (int i = 0; i < Nmax; i++) { if (a[i][2] > count) { l = a[i][0]; r = a[i][1]; count = a[i][2]; } if (a[i][2] == count && r - l > a[i][1] - a[i][0]) { l = a[i][0]; r = a[i][1]; } } printf("%d %d", l + 1, r + 1); }
### Prompt Create a solution in CPP for the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int Nmax = 1e6 + 17; int a[Nmax][3] = {0}; int main(void) { int n, x; scanf("%d", &n); for (int i = 0; i < Nmax; i++) a[i][0] = -1; for (int i = 0; i < n; i++) { scanf("%d", &x); if (a[x][0] == -1) a[x][0] = i; a[x][1] = i; a[x][2]++; } int l = 0, r = 0, count = 0; for (int i = 0; i < Nmax; i++) { if (a[i][2] > count) { l = a[i][0]; r = a[i][1]; count = a[i][2]; } if (a[i][2] == count && r - l > a[i][1] - a[i][0]) { l = a[i][0]; r = a[i][1]; } } printf("%d %d", l + 1, r + 1); } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000000 + 5; int n; int sh[MAXN]; int p[MAXN]; int t[MAXN]; int main() { cin >> n; int mx = 0; for (int i = 0; i < n; i++) { int x; cin >> x; if (t[x] == 0) { sh[x] = i; t[x] = 1; p[x] = i; } else { t[x]++; p[x] = i; } mx = max(mx, t[x]); } int mn = 100000000; int r, l; for (int i = 0; i < MAXN; i++) { if (t[i] == mx) { if (mn > p[i] - sh[i] + 1) { mn = p[i] - sh[i] + 1; r = p[i] + 1; l = sh[i] + 1; } } } cout << l << ' ' << r << endl; }
### Prompt Your task is to create a cpp solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 1000000 + 5; int n; int sh[MAXN]; int p[MAXN]; int t[MAXN]; int main() { cin >> n; int mx = 0; for (int i = 0; i < n; i++) { int x; cin >> x; if (t[x] == 0) { sh[x] = i; t[x] = 1; p[x] = i; } else { t[x]++; p[x] = i; } mx = max(mx, t[x]); } int mn = 100000000; int r, l; for (int i = 0; i < MAXN; i++) { if (t[i] == mx) { if (mn > p[i] - sh[i] + 1) { mn = p[i] - sh[i] + 1; r = p[i] + 1; l = sh[i] + 1; } } } cout << l << ' ' << r << endl; } ```
#include <bits/stdc++.h> using namespace std; inline int in() { int res = 0; char c; while ((c = getchar()) < '0' || c > '9') ; while (c >= '0' && c <= '9') res = res * 10 + c - '0', c = getchar(); return res; } int m[1000010]; int l[1000010]; int main() { int n = in(); int x, ans1, ans2; int mx = 0; int mn = 0x3f3f3f3f; for (int i = 0; i < n; i++) { x = in(); if (m[x] == 0) { m[x] = 1; l[x] = i; } else m[x]++; if (m[x] > mx) { mx = m[x]; ans1 = l[x]; ans2 = i; mn = ans2 - ans1; } else if (m[x] == mx && i - l[x] < mn) { ans1 = l[x]; ans2 = i; mn = ans2 - ans1; } } cout << ans1 + 1 << " " << ans2 + 1; return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; inline int in() { int res = 0; char c; while ((c = getchar()) < '0' || c > '9') ; while (c >= '0' && c <= '9') res = res * 10 + c - '0', c = getchar(); return res; } int m[1000010]; int l[1000010]; int main() { int n = in(); int x, ans1, ans2; int mx = 0; int mn = 0x3f3f3f3f; for (int i = 0; i < n; i++) { x = in(); if (m[x] == 0) { m[x] = 1; l[x] = i; } else m[x]++; if (m[x] > mx) { mx = m[x]; ans1 = l[x]; ans2 = i; mn = ans2 - ans1; } else if (m[x] == mx && i - l[x] < mn) { ans1 = l[x]; ans2 = i; mn = ans2 - ans1; } } cout << ans1 + 1 << " " << ans2 + 1; return 0; } ```
#include <bits/stdc++.h> using namespace std; int a[1000005], l[1000005], r[1000005]; int main() { int n; scanf("%d", &n); int i, maxn = 0; memset(a, 0, sizeof(a)); memset(l, 0, sizeof(l)); memset(r, 0, sizeof(r)); int id1 = 0, id2 = 0x7FFFFFFF, x; for (i = 1; i <= n; i++) { scanf("%d", &x); a[x]++; if (a[x] == 1) l[x] = i; if (a[x] >= maxn) { if (a[x] > maxn) { maxn = a[x]; r[x] = i; id1 = l[x]; id2 = r[x]; } else { r[x] = i; if (id1 == l[x]) { id2 = r[x]; } else if ((id2 - id1) > (r[x] - l[x])) { id1 = l[x]; id2 = r[x]; } else if ((id2 - id1) == (r[x] - l[x])) { if (l[x] < id1) { id1 = l[x]; id2 = r[x]; } } } } } printf("%d %d\n", id1, id2); return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int a[1000005], l[1000005], r[1000005]; int main() { int n; scanf("%d", &n); int i, maxn = 0; memset(a, 0, sizeof(a)); memset(l, 0, sizeof(l)); memset(r, 0, sizeof(r)); int id1 = 0, id2 = 0x7FFFFFFF, x; for (i = 1; i <= n; i++) { scanf("%d", &x); a[x]++; if (a[x] == 1) l[x] = i; if (a[x] >= maxn) { if (a[x] > maxn) { maxn = a[x]; r[x] = i; id1 = l[x]; id2 = r[x]; } else { r[x] = i; if (id1 == l[x]) { id2 = r[x]; } else if ((id2 - id1) > (r[x] - l[x])) { id1 = l[x]; id2 = r[x]; } else if ((id2 - id1) == (r[x] - l[x])) { if (l[x] < id1) { id1 = l[x]; id2 = r[x]; } } } } } printf("%d %d\n", id1, id2); return 0; } ```
#include <bits/stdc++.h> using namespace std; struct item { int f; int s; int c; }; item ab[1000005]; int main() { ios_base::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(10); cout.tie(nullptr); cin.tie(nullptr); int n; cin >> n; for (int i = 1; i <= n; i++) { int item; cin >> item; if (ab[item].f == 0) { ab[item].f = i; ab[item].s = i; } else { ab[item].s = i; } ab[item].c++; } vector<item> items; for (int i = 0; i < 1000005; i++) { if (ab[i].c > 0) items.push_back(ab[i]); } sort(items.begin(), items.end(), [](item &l, item &r) { if (l.c > r.c) { return true; } if (l.c == r.c) { return l.s - l.f < r.s - r.f; } return false; }); cout << items[0].f << " " << items[0].s << endl; int pause; cin >> pause; return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct item { int f; int s; int c; }; item ab[1000005]; int main() { ios_base::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(10); cout.tie(nullptr); cin.tie(nullptr); int n; cin >> n; for (int i = 1; i <= n; i++) { int item; cin >> item; if (ab[item].f == 0) { ab[item].f = i; ab[item].s = i; } else { ab[item].s = i; } ab[item].c++; } vector<item> items; for (int i = 0; i < 1000005; i++) { if (ab[i].c > 0) items.push_back(ab[i]); } sort(items.begin(), items.end(), [](item &l, item &r) { if (l.c > r.c) { return true; } if (l.c == r.c) { return l.s - l.f < r.s - r.f; } return false; }); cout << items[0].f << " " << items[0].s << endl; int pause; cin >> pause; return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, x; struct num { int f_ap = -1; int l_ap = -1; int cont = 0; } nums[1000100]; bool cmp(num a, num b) { return a.cont > b.cont; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &x); nums[x].cont++; if (nums[x].f_ap == -1) nums[x].f_ap = i + 1; nums[x].l_ap = i + 1; } sort(nums, nums + 1000100, cmp); int ini = 1, fim = n; for (int i = 0; i < 1000100; i++) { if (nums[i].cont != nums[0].cont) continue; if (nums[i].l_ap - nums[i].f_ap + 1 < fim - ini + 1) { ini = nums[i].f_ap; fim = nums[i].l_ap; } } printf("%d %d\n", ini, fim); return 0; }
### Prompt Please create a solution in CPP to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, x; struct num { int f_ap = -1; int l_ap = -1; int cont = 0; } nums[1000100]; bool cmp(num a, num b) { return a.cont > b.cont; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &x); nums[x].cont++; if (nums[x].f_ap == -1) nums[x].f_ap = i + 1; nums[x].l_ap = i + 1; } sort(nums, nums + 1000100, cmp); int ini = 1, fim = n; for (int i = 0; i < 1000100; i++) { if (nums[i].cont != nums[0].cont) continue; if (nums[i].l_ap - nums[i].f_ap + 1 < fim - ini + 1) { ini = nums[i].f_ap; fim = nums[i].l_ap; } } printf("%d %d\n", ini, fim); return 0; } ```
#include <bits/stdc++.h> using namespace std; int nums[1000050], b[1000050], e[1000050], ans; int main(void) { memset(nums, 0, sizeof(nums[0])); int n, i, tp; scanf("%d", &n); for (ans = 0, i = 1; i <= n; i++) { scanf("%d", &tp); nums[tp]++; if (nums[tp] == 1) b[tp] = i; e[tp] = i; if (nums[tp] > nums[ans]) ans = tp; if (nums[tp] == nums[ans] && e[tp] - b[tp] < e[ans] - b[ans]) ans = tp; } printf("%d %d\n", b[ans], e[ans]); return 0; }
### Prompt Generate a cpp solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int nums[1000050], b[1000050], e[1000050], ans; int main(void) { memset(nums, 0, sizeof(nums[0])); int n, i, tp; scanf("%d", &n); for (ans = 0, i = 1; i <= n; i++) { scanf("%d", &tp); nums[tp]++; if (nums[tp] == 1) b[tp] = i; e[tp] = i; if (nums[tp] > nums[ans]) ans = tp; if (nums[tp] == nums[ans] && e[tp] - b[tp] < e[ans] - b[ans]) ans = tp; } printf("%d %d\n", b[ans], e[ans]); return 0; } ```
#include <bits/stdc++.h> using namespace std; int n; int a[100005], L[1000006], R[1000006], nr[1000006]; int l, r, sol = 100005, nrc; void read() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); } void solve() { for (int i = 1; i <= n; i++) { nr[a[i]]++; if (nr[a[i]] == 1) L[a[i]] = R[a[i]] = i; else R[a[i]] = i; if (nr[a[i]] > nrc) { nrc = nr[a[i]]; sol = R[a[i]] - L[a[i]] + 1; l = L[a[i]]; r = R[a[i]]; } else if (nr[a[i]] == nrc && R[a[i]] - L[a[i]] + 1 < sol) { sol = R[a[i]] - L[a[i]] + 1; l = L[a[i]]; r = R[a[i]]; } } } int main() { read(); solve(); printf("%d %d", l, r); return 0; }
### Prompt Create a solution in cpp for the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n; int a[100005], L[1000006], R[1000006], nr[1000006]; int l, r, sol = 100005, nrc; void read() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); } void solve() { for (int i = 1; i <= n; i++) { nr[a[i]]++; if (nr[a[i]] == 1) L[a[i]] = R[a[i]] = i; else R[a[i]] = i; if (nr[a[i]] > nrc) { nrc = nr[a[i]]; sol = R[a[i]] - L[a[i]] + 1; l = L[a[i]]; r = R[a[i]]; } else if (nr[a[i]] == nrc && R[a[i]] - L[a[i]] + 1 < sol) { sol = R[a[i]] - L[a[i]] + 1; l = L[a[i]]; r = R[a[i]]; } } } int main() { read(); solve(); printf("%d %d", l, r); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> counts(1000001, 0); vector<int> s(1000001, 0); vector<int> f(1000001, 0); int max_counts = 0; for (int i = 0; i < n; ++i) { int x; cin >> x; if (counts[x] == 0) { s[x] = i; } f[x] = i; ++counts[x]; if (counts[x] > max_counts) { max_counts = counts[x]; } } int res = 1000001; int len = n; for (int i = 0; i < counts.size(); ++i) { if (counts[i] == max_counts && (f[i] - s[i]) < len) { res = i; len = f[i] - s[i]; } } cout << s[res] + 1 << " " << f[res] + 1 << "\n"; return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> counts(1000001, 0); vector<int> s(1000001, 0); vector<int> f(1000001, 0); int max_counts = 0; for (int i = 0; i < n; ++i) { int x; cin >> x; if (counts[x] == 0) { s[x] = i; } f[x] = i; ++counts[x]; if (counts[x] > max_counts) { max_counts = counts[x]; } } int res = 1000001; int len = n; for (int i = 0; i < counts.size(); ++i) { if (counts[i] == max_counts && (f[i] - s[i]) < len) { res = i; len = f[i] - s[i]; } } cout << s[res] + 1 << " " << f[res] + 1 << "\n"; return 0; } ```
#include <bits/stdc++.h> using namespace std; long long fastmul(long long a, long long b) { if (b == 0) return 0; if (b == 1) return a; long long tmp = fastmul(a, b >> 1); return (b & 1) ? ((tmp << 1) + a) % 1000000007LL : (tmp << 1) % 1000000007LL; } struct Point { double x, y; Point() {} Point(double A, double B) : x(A), y(B) {} }; struct Vec { double x, y; Vec() {} Vec(double A, double B) : x(A), y(B) {} }; struct line { double a, b, c; line() {} line(double A, double B, double C) : a(A), b(B), c(C) {} line(Point A, Point B) { a = A.y - B.y; b = B.x - A.x; c = -a * A.x - b * A.y; } }; Point intersect(line AB, line CD) { AB.c = -AB.c; CD.c = -CD.c; double D = (AB.a * CD.b - AB.b * CD.a); double Dx = (AB.c * CD.b - AB.b * CD.c); double Dy = (AB.a * CD.c - AB.c * CD.a); if (D == 0.0) return Point(1e9, 1e9); else return Point(Dx / D, Dy / D); } struct matrix { long long a[2 + 5][2 + 5]; matrix() {} }; matrix matmul(matrix A, matrix B) { matrix c; for (int i = 1; i <= 2; i++) for (int j = 1; j <= 2; j++) { c.a[i][j] = 0; for (int k = 1; k <= 2; k++) c.a[i][j] = (c.a[i][j] + fastmul(A.a[i][k], B.a[k][j])) % 1000000007LL; } return c; } int n, x, res = 0, cnt[1000005], Start[1000005], Stop[1000005], luui = 0, luuj = 0; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> n; for (int i = 1; i <= 1000000; i++) Start[i] = long(1e9); for (int i = 1; i <= n; i++) { cin >> x; cnt[x]++; Start[x] = min(Start[x], i); Stop[x] = max(Stop[x], i); res = max(res, cnt[x]); } int RES = long(1e9); for (int i = 1; i <= 1000000; i++) { if (cnt[i] == res) { if (Stop[i] - Start[i] + 1 < RES) { RES = Stop[i] - Start[i] + 1; luui = Start[i]; luuj = Stop[i]; } } } cout << luui << ' ' << luuj; return 0; }
### Prompt Please create a solution in Cpp to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long fastmul(long long a, long long b) { if (b == 0) return 0; if (b == 1) return a; long long tmp = fastmul(a, b >> 1); return (b & 1) ? ((tmp << 1) + a) % 1000000007LL : (tmp << 1) % 1000000007LL; } struct Point { double x, y; Point() {} Point(double A, double B) : x(A), y(B) {} }; struct Vec { double x, y; Vec() {} Vec(double A, double B) : x(A), y(B) {} }; struct line { double a, b, c; line() {} line(double A, double B, double C) : a(A), b(B), c(C) {} line(Point A, Point B) { a = A.y - B.y; b = B.x - A.x; c = -a * A.x - b * A.y; } }; Point intersect(line AB, line CD) { AB.c = -AB.c; CD.c = -CD.c; double D = (AB.a * CD.b - AB.b * CD.a); double Dx = (AB.c * CD.b - AB.b * CD.c); double Dy = (AB.a * CD.c - AB.c * CD.a); if (D == 0.0) return Point(1e9, 1e9); else return Point(Dx / D, Dy / D); } struct matrix { long long a[2 + 5][2 + 5]; matrix() {} }; matrix matmul(matrix A, matrix B) { matrix c; for (int i = 1; i <= 2; i++) for (int j = 1; j <= 2; j++) { c.a[i][j] = 0; for (int k = 1; k <= 2; k++) c.a[i][j] = (c.a[i][j] + fastmul(A.a[i][k], B.a[k][j])) % 1000000007LL; } return c; } int n, x, res = 0, cnt[1000005], Start[1000005], Stop[1000005], luui = 0, luuj = 0; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> n; for (int i = 1; i <= 1000000; i++) Start[i] = long(1e9); for (int i = 1; i <= n; i++) { cin >> x; cnt[x]++; Start[x] = min(Start[x], i); Stop[x] = max(Stop[x], i); res = max(res, cnt[x]); } int RES = long(1e9); for (int i = 1; i <= 1000000; i++) { if (cnt[i] == res) { if (Stop[i] - Start[i] + 1 < RES) { RES = Stop[i] - Start[i] + 1; luui = Start[i]; luuj = Stop[i]; } } } cout << luui << ' ' << luuj; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1000000 + 10; const int INF = 10000000; int n; struct Number { int L, R, num; } a[N]; const bool comp(const Number &A, const Number &B) { return A.num > B.num; } int main() { scanf("%d", &n); int pos; for (int i = 1; i <= N; ++i) a[i - 1].L = INF, a[i - 1].R = -INF, a[i - 1].num = 0; for (int i = 1; i <= n; ++i) { scanf("%d", &pos); ++a[pos].num; a[pos].L = min(i, a[pos].L); a[pos].R = max(i, a[pos].R); } sort(a + 1, a + N, comp); int Max = a[1].num, tmp = 1; int ans1, ans2, ans3 = INF; while (a[tmp].num == Max) { if (a[tmp].R - a[tmp].L + 1 < ans3) { ans3 = a[tmp].R - a[tmp].L + 1; ans1 = a[tmp].L, ans2 = a[tmp].R; } ++tmp; } printf("%d %d\n", ans1, ans2); return 0; }
### Prompt Create a solution in CPP for the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1000000 + 10; const int INF = 10000000; int n; struct Number { int L, R, num; } a[N]; const bool comp(const Number &A, const Number &B) { return A.num > B.num; } int main() { scanf("%d", &n); int pos; for (int i = 1; i <= N; ++i) a[i - 1].L = INF, a[i - 1].R = -INF, a[i - 1].num = 0; for (int i = 1; i <= n; ++i) { scanf("%d", &pos); ++a[pos].num; a[pos].L = min(i, a[pos].L); a[pos].R = max(i, a[pos].R); } sort(a + 1, a + N, comp); int Max = a[1].num, tmp = 1; int ans1, ans2, ans3 = INF; while (a[tmp].num == Max) { if (a[tmp].R - a[tmp].L + 1 < ans3) { ans3 = a[tmp].R - a[tmp].L + 1; ans1 = a[tmp].L, ans2 = a[tmp].R; } ++tmp; } printf("%d %d\n", ans1, ans2); return 0; } ```
#include <bits/stdc++.h> using namespace std; long long n, i, mx, mn = 9999999, j; vector<long long> v[9999999]; int main() { cin >> n; map<long long, long long> m; long long a[n + 1]; for (i = 1; i <= n; i++) { cin >> a[i]; m[a[i]]++; v[a[i]].push_back(i); mx = max(mx, m[a[i]]); } long long l, r; for (i = 1; i <= n; i++) { if (mx == m[a[i]]) { if (mn > v[a[i]][v[a[i]].size() - 1] - v[a[i]][0]) { mn = v[a[i]][v[a[i]].size() - 1] - v[a[i]][0]; l = v[a[i]][0]; r = v[a[i]][v[a[i]].size() - 1]; } } } cout << l << " " << r; return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n, i, mx, mn = 9999999, j; vector<long long> v[9999999]; int main() { cin >> n; map<long long, long long> m; long long a[n + 1]; for (i = 1; i <= n; i++) { cin >> a[i]; m[a[i]]++; v[a[i]].push_back(i); mx = max(mx, m[a[i]]); } long long l, r; for (i = 1; i <= n; i++) { if (mx == m[a[i]]) { if (mn > v[a[i]][v[a[i]].size() - 1] - v[a[i]][0]) { mn = v[a[i]][v[a[i]].size() - 1] - v[a[i]][0]; l = v[a[i]][0]; r = v[a[i]][v[a[i]].size() - 1]; } } } cout << l << " " << r; return 0; } ```
#include <bits/stdc++.h> using namespace std; int li[1000005], ri[1000005], c[1000005]; vector<int> v; vector<pair<int, int> > te; int main() { int n, i; cin >> n; int A[n + 1]; for (i = 0; i < n; i++) { cin >> A[i]; if (!c[A[i]]) { c[A[i]]++; v.push_back(A[i]); li[A[i]] = i; ri[A[i]] = i; } else { c[A[i]]++; ri[A[i]] = i; } } for (i = 0; i < v.size(); i++) { te.push_back(make_pair(c[v[i]], v[i])); } sort(te.rbegin(), te.rend()); int ansl, ansr, templ, tempr, si, ti; ansl = li[te[0].second]; ansr = ri[te[0].second]; si = ansr - ansl + 1; i = 1; while (i < te.size()) { if (te[i].first < te[0].first) break; templ = li[te[i].second]; tempr = ri[te[i].second]; ti = tempr - templ + 1; if (ti < si) { ansl = templ; ansr = tempr; si = ti; } i++; } cout << ansl + 1 << " " << ansr + 1 << endl; return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int li[1000005], ri[1000005], c[1000005]; vector<int> v; vector<pair<int, int> > te; int main() { int n, i; cin >> n; int A[n + 1]; for (i = 0; i < n; i++) { cin >> A[i]; if (!c[A[i]]) { c[A[i]]++; v.push_back(A[i]); li[A[i]] = i; ri[A[i]] = i; } else { c[A[i]]++; ri[A[i]] = i; } } for (i = 0; i < v.size(); i++) { te.push_back(make_pair(c[v[i]], v[i])); } sort(te.rbegin(), te.rend()); int ansl, ansr, templ, tempr, si, ti; ansl = li[te[0].second]; ansr = ri[te[0].second]; si = ansr - ansl + 1; i = 1; while (i < te.size()) { if (te[i].first < te[0].first) break; templ = li[te[i].second]; tempr = ri[te[i].second]; ti = tempr - templ + 1; if (ti < si) { ansl = templ; ansr = tempr; si = ti; } i++; } cout << ansl + 1 << " " << ansr + 1 << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; long long i, j, k, l, fx[1000000], m, n, c, e, r, t, h, mx, mn, mx1, mx2, mn1, mn2, x, y, x2, y2, i1, j2, i2; vector<int> v[10000]; set<int> sas; set<int>::iterator it; long long a[10000000]; pair<int, int> p[1000000]; pair<int, int> pp; long long s[100000]; int main() { cin >> n; for (i = 0; i < n; i++) { cin >> k; p[i] = {k, i}; } sort(p, p + n); mx = 0; long long mxl = 0; long long mxr = n - 1; for (i = 0; i < n; i++) { long long st = p[i].second; long long sti = i; while (i < n && p[i].first == p[i + 1].first) i++; if (mx < i - sti + 1) { mx = i - sti + 1; mxl = st; mxr = p[i].second; } else if (mx == i - sti + 1) { if (mxr - mxl > p[i].second - st) { mxl = st; mxr = p[i].second; } } } cout << mxl + 1 << " " << mxr + 1; return 0; }
### Prompt Please create a solution in Cpp to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long i, j, k, l, fx[1000000], m, n, c, e, r, t, h, mx, mn, mx1, mx2, mn1, mn2, x, y, x2, y2, i1, j2, i2; vector<int> v[10000]; set<int> sas; set<int>::iterator it; long long a[10000000]; pair<int, int> p[1000000]; pair<int, int> pp; long long s[100000]; int main() { cin >> n; for (i = 0; i < n; i++) { cin >> k; p[i] = {k, i}; } sort(p, p + n); mx = 0; long long mxl = 0; long long mxr = n - 1; for (i = 0; i < n; i++) { long long st = p[i].second; long long sti = i; while (i < n && p[i].first == p[i + 1].first) i++; if (mx < i - sti + 1) { mx = i - sti + 1; mxl = st; mxr = p[i].second; } else if (mx == i - sti + 1) { if (mxr - mxl > p[i].second - st) { mxl = st; mxr = p[i].second; } } } cout << mxl + 1 << " " << mxr + 1; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; int open[N], close[N], k[N], n, a[N], MAX, len = 1e9; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; k[a[i]]++; if (!open[a[i]]) open[a[i]] = i; } for (int i = n; i > 0; i--) { if (!close[a[i]]) { close[a[i]] = i; if (k[a[i]] > MAX) MAX = k[a[i]]; } } int r, l; for (int i = 1; i <= n; i++) { int x = a[i]; if (k[x] == MAX && len > close[x] - open[x]) { len = close[x] - open[x]; l = open[x]; r = close[x]; } } cout << l << " " << r << '\n'; return 0; }
### Prompt Develop a solution in CPP to the problem described below: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; int open[N], close[N], k[N], n, a[N], MAX, len = 1e9; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; k[a[i]]++; if (!open[a[i]]) open[a[i]] = i; } for (int i = n; i > 0; i--) { if (!close[a[i]]) { close[a[i]] = i; if (k[a[i]] > MAX) MAX = k[a[i]]; } } int r, l; for (int i = 1; i <= n; i++) { int x = a[i]; if (k[x] == MAX && len > close[x] - open[x]) { len = close[x] - open[x]; l = open[x]; r = close[x]; } } cout << l << " " << r << '\n'; return 0; } ```
#include <bits/stdc++.h> using namespace std; class num { public: int i1, i2, s; num() { i1 = i2 = s = 0; } bool operator>(num n) { if (n.s < this->s) return 1; else if (n.s == this->s) if (i2 - i1 < n.i2 - n.i1) return 1; return 0; } }; int a[100005], n, i; num h[1000005], ans; int main() { cin >> n; for (i = 0; i < n; i++) { scanf("%d", a + i); if (!h[a[i]].s) h[a[i]].i1 = i + 1; h[a[i]].i2 = i + 1; h[a[i]].s++; } for (i = 0; i < 100001; i++) { if (h[a[i]] > ans) ans = h[a[i]]; } printf("%d %d", ans.i1, ans.i2); }
### Prompt Your task is to create a cpp solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; class num { public: int i1, i2, s; num() { i1 = i2 = s = 0; } bool operator>(num n) { if (n.s < this->s) return 1; else if (n.s == this->s) if (i2 - i1 < n.i2 - n.i1) return 1; return 0; } }; int a[100005], n, i; num h[1000005], ans; int main() { cin >> n; for (i = 0; i < n; i++) { scanf("%d", a + i); if (!h[a[i]].s) h[a[i]].i1 = i + 1; h[a[i]].i2 = i + 1; h[a[i]].s++; } for (i = 0; i < 100001; i++) { if (h[a[i]] > ans) ans = h[a[i]]; } printf("%d %d", ans.i1, ans.i2); } ```
#include <bits/stdc++.h> using namespace std; struct data { int num, st, ed; }; data ar[1000002]; int a[100002]; bool vis[1000002]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, maxi = 0, s = 0, e = 0, cnt = 100000000, x, p, i, c, b; cin >> n; for (i = 0; i < n; i++) { cin >> a[i]; x = a[i]; ar[x].num++; if (!vis[x]) { vis[x] = 1; ar[x].st = i + 1; } ar[x].ed = i + 1; } for (i = 0; i < n; i++) { x = a[i]; p = ar[x].num; s = ar[x].st; e = ar[x].ed; if (p == maxi) { if (e - s < cnt) cnt = e - s, c = s, b = e; } else if (p > maxi) cnt = e - s, maxi = p, c = s, b = e; } cout << c << " " << b << endl; }
### Prompt In cpp, your task is to solve the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct data { int num, st, ed; }; data ar[1000002]; int a[100002]; bool vis[1000002]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, maxi = 0, s = 0, e = 0, cnt = 100000000, x, p, i, c, b; cin >> n; for (i = 0; i < n; i++) { cin >> a[i]; x = a[i]; ar[x].num++; if (!vis[x]) { vis[x] = 1; ar[x].st = i + 1; } ar[x].ed = i + 1; } for (i = 0; i < n; i++) { x = a[i]; p = ar[x].num; s = ar[x].st; e = ar[x].ed; if (p == maxi) { if (e - s < cnt) cnt = e - s, c = s, b = e; } else if (p > maxi) cnt = e - s, maxi = p, c = s, b = e; } cout << c << " " << b << endl; } ```
#include <bits/stdc++.h> using namespace std; int N, t1; map<int, pair<int, int> > num; int count_sort[1000005] = {0}; int main() { cin >> N; int max_ = 0, max_num = 0; for (int i = 0; i < N; i++) { cin >> t1; count_sort[t1]++; if (num.find(t1) != num.end()) { num[t1].second = i; } else { num[t1] = pair<int, int>(i, i); } if (count_sort[t1] > max_) max_ = count_sort[t1]; max_num = max(max_num, t1); } int distance = 1e9, max_id = 0; for (int i = 1; i <= max_num; i++) { if (count_sort[i] != max_) continue; int tmp = num[i].second - num[i].first + 1; if (distance > tmp) { distance = tmp; max_id = i; } } cout << num[max_id].first + 1 << " "; cout << num[max_id].second + 1 << endl; return 0; }
### Prompt Create a solution in Cpp for the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int N, t1; map<int, pair<int, int> > num; int count_sort[1000005] = {0}; int main() { cin >> N; int max_ = 0, max_num = 0; for (int i = 0; i < N; i++) { cin >> t1; count_sort[t1]++; if (num.find(t1) != num.end()) { num[t1].second = i; } else { num[t1] = pair<int, int>(i, i); } if (count_sort[t1] > max_) max_ = count_sort[t1]; max_num = max(max_num, t1); } int distance = 1e9, max_id = 0; for (int i = 1; i <= max_num; i++) { if (count_sort[i] != max_) continue; int tmp = num[i].second - num[i].first + 1; if (distance > tmp) { distance = tmp; max_id = i; } } cout << num[max_id].first + 1 << " "; cout << num[max_id].second + 1 << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e6 + 5; int cnt[MAXN]; int start[MAXN]; int main() { int n, a, l = 0, r = 0, maxCount = 0; memset(cnt, 0, MAXN); memset(start, 0, MAXN); int minDistance = n; cin >> n; for (int i = 0; i < n; ++i) { cin >> a; if (!cnt[a]) { start[a] = i; } ++cnt[a]; if (maxCount < cnt[a]) { maxCount = cnt[a]; l = start[a]; r = i; minDistance = r - l + 1; } else if (maxCount == cnt[a] && i - start[a] + 1 < minDistance) { l = start[a]; r = i; minDistance = r - l + 1; } } cout << l + 1 << ' ' << r + 1 << endl; }
### Prompt Your task is to create a Cpp solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 1e6 + 5; int cnt[MAXN]; int start[MAXN]; int main() { int n, a, l = 0, r = 0, maxCount = 0; memset(cnt, 0, MAXN); memset(start, 0, MAXN); int minDistance = n; cin >> n; for (int i = 0; i < n; ++i) { cin >> a; if (!cnt[a]) { start[a] = i; } ++cnt[a]; if (maxCount < cnt[a]) { maxCount = cnt[a]; l = start[a]; r = i; minDistance = r - l + 1; } else if (maxCount == cnt[a] && i - start[a] + 1 < minDistance) { l = start[a]; r = i; minDistance = r - l + 1; } } cout << l + 1 << ' ' << r + 1 << endl; } ```
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int a, n; map<int, int> freq; map<int, pair<int, int> > idx; cin >> n; for (int i = 1; i <= n; ++i) { cin >> a; if (!idx[a].first) idx[a].first = i; else idx[a].second = i; freq[a]++; } vector<pair<int, int> > nums; vector<int> lol; map<int, bool> vis; int mx = INT_MIN, number; for (map<int, int>::iterator i = freq.begin(); i != freq.end(); ++i) { if (mx < i->second) { mx = i->second; } } for (map<int, int>::iterator i = freq.begin(); i != freq.end(); ++i) { if (mx == i->second) { lol.push_back(i->first); vis[i->first] = 1; } } if (mx == 1) return cout << 1 << " " << 1 << '\n', 0; for (map<int, pair<int, int> >::iterator i = idx.begin(); i != idx.end(); ++i) { if (vis[i->first]) { nums.push_back({i->second.first, i->second.second}); } } pair<int, int> ans; int diff = INT_MAX; for (int i = 0; i < nums.size(); ++i) { if (nums[i].second - nums[i].first < diff) { ans = {nums[i].first, nums[i].second}; diff = nums[i].second - nums[i].first; } } cout << ans.first << " " << ans.second << '\n'; return 0; }
### Prompt Please formulate a cpp solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int a, n; map<int, int> freq; map<int, pair<int, int> > idx; cin >> n; for (int i = 1; i <= n; ++i) { cin >> a; if (!idx[a].first) idx[a].first = i; else idx[a].second = i; freq[a]++; } vector<pair<int, int> > nums; vector<int> lol; map<int, bool> vis; int mx = INT_MIN, number; for (map<int, int>::iterator i = freq.begin(); i != freq.end(); ++i) { if (mx < i->second) { mx = i->second; } } for (map<int, int>::iterator i = freq.begin(); i != freq.end(); ++i) { if (mx == i->second) { lol.push_back(i->first); vis[i->first] = 1; } } if (mx == 1) return cout << 1 << " " << 1 << '\n', 0; for (map<int, pair<int, int> >::iterator i = idx.begin(); i != idx.end(); ++i) { if (vis[i->first]) { nums.push_back({i->second.first, i->second.second}); } } pair<int, int> ans; int diff = INT_MAX; for (int i = 0; i < nums.size(); ++i) { if (nums[i].second - nums[i].first < diff) { ans = {nums[i].first, nums[i].second}; diff = nums[i].second - nums[i].first; } } cout << ans.first << " " << ans.second << '\n'; return 0; } ```
#include <bits/stdc++.h> using namespace std; int Count[1000000]; bool used[1000000]; int main() { int n, checkStart, checkEnd, ansBegin, ansEnd, mx = 0, mx2 = 0, mnSize = 1 << 30; int begin[1000000 + 1], end[1000000 + 1]; scanf("%d", &n); int list[n]; for (int i = 0; i < n; i++) { scanf("%d", &list[i]); if (!used[list[i]]) { used[list[i]] = true; begin[list[i]] = i + 1; } end[list[i]] = i + 1; Count[list[i]]++; mx2 = max(mx2, list[i]); mx = max(mx, Count[list[i]]); } for (int i = 1; i <= mx2; i++) { if (Count[i] == mx) { checkEnd = end[i]; checkStart = begin[i]; if (checkEnd - checkStart + 1 < mnSize) { mnSize = checkEnd - checkStart + 1; ansBegin = checkStart; ansEnd = checkEnd; } } } printf("%d %d\n", ansBegin, ansEnd); }
### Prompt Generate a CPP solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int Count[1000000]; bool used[1000000]; int main() { int n, checkStart, checkEnd, ansBegin, ansEnd, mx = 0, mx2 = 0, mnSize = 1 << 30; int begin[1000000 + 1], end[1000000 + 1]; scanf("%d", &n); int list[n]; for (int i = 0; i < n; i++) { scanf("%d", &list[i]); if (!used[list[i]]) { used[list[i]] = true; begin[list[i]] = i + 1; } end[list[i]] = i + 1; Count[list[i]]++; mx2 = max(mx2, list[i]); mx = max(mx, Count[list[i]]); } for (int i = 1; i <= mx2; i++) { if (Count[i] == mx) { checkEnd = end[i]; checkStart = begin[i]; if (checkEnd - checkStart + 1 < mnSize) { mnSize = checkEnd - checkStart + 1; ansBegin = checkStart; ansEnd = checkEnd; } } } printf("%d %d\n", ansBegin, ansEnd); } ```
#include <bits/stdc++.h> using namespace std; typedef struct interv { int left, right, sum; interv() { left = 0; right = 0; sum = 0; } interv(int l, int r, int s) { left = l; right = r; sum = s; } } interv; int main() { int n; cin >> n; map<int, interv> m; for (int i = 0; i < n; i++) { int k; cin >> k; if (m.find(k) == m.end()) { m[k] = interv(i, i, 1); } else { m[k].right = i; m[k].sum += 1; } } int max_sum = -1; int l = 0, r = 0; for (map<int, interv>::iterator it = m.begin(); it != m.end(); it++) { if (it->second.sum > max_sum) { max_sum = it->second.sum; l = it->second.left; r = it->second.right; } else if (it->second.sum == max_sum) { if (abs(it->second.left - it->second.right) < abs(l - r)) { l = it->second.left; r = it->second.right; } } } cout << l + 1 << ' ' << r + 1 << endl; return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; typedef struct interv { int left, right, sum; interv() { left = 0; right = 0; sum = 0; } interv(int l, int r, int s) { left = l; right = r; sum = s; } } interv; int main() { int n; cin >> n; map<int, interv> m; for (int i = 0; i < n; i++) { int k; cin >> k; if (m.find(k) == m.end()) { m[k] = interv(i, i, 1); } else { m[k].right = i; m[k].sum += 1; } } int max_sum = -1; int l = 0, r = 0; for (map<int, interv>::iterator it = m.begin(); it != m.end(); it++) { if (it->second.sum > max_sum) { max_sum = it->second.sum; l = it->second.left; r = it->second.right; } else if (it->second.sum == max_sum) { if (abs(it->second.left - it->second.right) < abs(l - r)) { l = it->second.left; r = it->second.right; } } } cout << l + 1 << ' ' << r + 1 << endl; return 0; } ```
#include <bits/stdc++.h> struct cont { long start; long end; long count; }; int main() { long n; scanf("%ld\n", &n); long ansStart(0), ansEnd(0), maxCount(1), minDist(1); std::map<long, cont> dets; for (int p = 0; p < n; p++) { long t; scanf("%ld", &t); if (dets.count(t) > 0) { dets[t].end = p; ++dets[t].count; if (dets[t].count > maxCount) { maxCount = dets[t].count; ansStart = dets[t].start; ansEnd = dets[t].end; minDist = dets[t].end - dets[t].start; } else if (dets[t].count == maxCount && minDist > dets[t].end - dets[t].start) { ansStart = dets[t].start; ansEnd = dets[t].end; minDist = dets[t].end - dets[t].start; } } else { cont c; c.start = p; c.end = p; c.count = 1; dets.insert(std::pair<long, cont>(t, c)); } } if (maxCount > 1) { printf("%ld %ld\n", 1 + ansStart, 1 + ansEnd); } else { puts("1 1"); } return 0; }
### Prompt Create a solution in cpp for the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> struct cont { long start; long end; long count; }; int main() { long n; scanf("%ld\n", &n); long ansStart(0), ansEnd(0), maxCount(1), minDist(1); std::map<long, cont> dets; for (int p = 0; p < n; p++) { long t; scanf("%ld", &t); if (dets.count(t) > 0) { dets[t].end = p; ++dets[t].count; if (dets[t].count > maxCount) { maxCount = dets[t].count; ansStart = dets[t].start; ansEnd = dets[t].end; minDist = dets[t].end - dets[t].start; } else if (dets[t].count == maxCount && minDist > dets[t].end - dets[t].start) { ansStart = dets[t].start; ansEnd = dets[t].end; minDist = dets[t].end - dets[t].start; } } else { cont c; c.start = p; c.end = p; c.count = 1; dets.insert(std::pair<long, cont>(t, c)); } } if (maxCount > 1) { printf("%ld %ld\n", 1 + ansStart, 1 + ansEnd); } else { puts("1 1"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1E6 + 7; struct Q { int cnt, s, len; } q[N]; int a[N]; bool cmp(Q a, Q b) { if (a.cnt > b.cnt) return true; if (a.cnt == b.cnt && a.len < b.len) return true; return false; } int main() { int n; int mx = -1; cin >> n; memset(q, 0, sizeof(q)); for (int i = 1; i <= n; i++) { cin >> a[i]; if (a[i] > mx) { mx = a[i]; } if (q[a[i]].cnt == 0) { q[a[i]].s = i; } else { q[a[i]].len = i - q[a[i]].s; } q[a[i]].cnt++; } sort(q + 1, q + 1 + mx, cmp); cout << q[1].s << " " << q[1].s + q[1].len << endl; return 0; }
### Prompt Develop a solution in cpp to the problem described below: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1E6 + 7; struct Q { int cnt, s, len; } q[N]; int a[N]; bool cmp(Q a, Q b) { if (a.cnt > b.cnt) return true; if (a.cnt == b.cnt && a.len < b.len) return true; return false; } int main() { int n; int mx = -1; cin >> n; memset(q, 0, sizeof(q)); for (int i = 1; i <= n; i++) { cin >> a[i]; if (a[i] > mx) { mx = a[i]; } if (q[a[i]].cnt == 0) { q[a[i]].s = i; } else { q[a[i]].len = i - q[a[i]].s; } q[a[i]].cnt++; } sort(q + 1, q + 1 + mx, cmp); cout << q[1].s << " " << q[1].s + q[1].len << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; static int arr[1111111][3]; int first; cin >> n; for (int i = 0; i < n; i++) { int tmp; cin >> tmp; arr[tmp][0]++; if (i == 0) first = tmp; else if (arr[tmp][1] == 0 && tmp != first) { arr[tmp][1] = i; arr[tmp][2] = i; } else arr[tmp][2] = i; } int val, M = 0; for (int i = 0; i < 1111111; i++) { if (arr[i][0] > M) { M = arr[i][0]; val = i; } } int Min = 1111111; int key = 0; for (int i = 0; i < 1111111; i++) { if (arr[i][0] == M) { if (Min > arr[i][2] - arr[i][1]) { key = i; Min = arr[i][2] - arr[i][1]; } } } cout << arr[key][1] + 1 << " " << arr[key][2] + 1 << endl; return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; static int arr[1111111][3]; int first; cin >> n; for (int i = 0; i < n; i++) { int tmp; cin >> tmp; arr[tmp][0]++; if (i == 0) first = tmp; else if (arr[tmp][1] == 0 && tmp != first) { arr[tmp][1] = i; arr[tmp][2] = i; } else arr[tmp][2] = i; } int val, M = 0; for (int i = 0; i < 1111111; i++) { if (arr[i][0] > M) { M = arr[i][0]; val = i; } } int Min = 1111111; int key = 0; for (int i = 0; i < 1111111; i++) { if (arr[i][0] == M) { if (Min > arr[i][2] - arr[i][1]) { key = i; Min = arr[i][2] - arr[i][1]; } } } cout << arr[key][1] + 1 << " " << arr[key][2] + 1 << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; map<long int, long int> freq; long int lower[1000006]; long int upper[1000006]; int main() { int N; long int n; scanf("%d", &N); memset(lower, -1, sizeof(lower)); memset(upper, -1, sizeof(upper)); for (int i = 0; i < N; ++i) { scanf("%ld", &n); freq[n]++; if (lower[n] == -1) { lower[n] = i; } if (i > upper[n]) upper[n] = i; } int maxi = INT_MIN; int num; for (auto it = freq.begin(); it != freq.end(); ++it) { if (maxi < it->second) { num = it->first; maxi = it->second; } } int i, j; i = 0; j = N - 1; for (auto it = freq.begin(); it != freq.end(); ++it) { if (it->second == maxi) { int diff = upper[it->first] - lower[it->first]; if (diff < (j - i)) { i = lower[it->first]; j = upper[it->first]; } } } printf("%d %d\n", i + 1, j + 1); }
### Prompt Develop a solution in cpp to the problem described below: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; map<long int, long int> freq; long int lower[1000006]; long int upper[1000006]; int main() { int N; long int n; scanf("%d", &N); memset(lower, -1, sizeof(lower)); memset(upper, -1, sizeof(upper)); for (int i = 0; i < N; ++i) { scanf("%ld", &n); freq[n]++; if (lower[n] == -1) { lower[n] = i; } if (i > upper[n]) upper[n] = i; } int maxi = INT_MIN; int num; for (auto it = freq.begin(); it != freq.end(); ++it) { if (maxi < it->second) { num = it->first; maxi = it->second; } } int i, j; i = 0; j = N - 1; for (auto it = freq.begin(); it != freq.end(); ++it) { if (it->second == maxi) { int diff = upper[it->first] - lower[it->first]; if (diff < (j - i)) { i = lower[it->first]; j = upper[it->first]; } } } printf("%d %d\n", i + 1, j + 1); } ```
#include <bits/stdc++.h> using namespace std; vector<int> v; int sum[1000500] = {0}; int visit[1000500] = {0}; int main() { int n; int maior = -1; int fim = -1; int beg = -1; int value = -1; cin >> n; for (int i = 0; i < n; i++) { int x; cin >> x; v.push_back(x); sum[x] = sum[x] + 1; if (sum[x] == 1) { visit[x] = i; } if (sum[x] == maior) { if (i - visit[x] < fim - visit[value]) { maior = sum[x]; fim = i; beg = i; value = x; } } if (sum[x] > maior) { maior = sum[x]; fim = i; beg = i; value = x; } } for (int i = 0; i < v.size(); i++) { if (v[i] == value) { beg = i; break; } } cout << (beg + 1) << " " << (fim + 1) << endl; return 0; }
### Prompt In Cpp, your task is to solve the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<int> v; int sum[1000500] = {0}; int visit[1000500] = {0}; int main() { int n; int maior = -1; int fim = -1; int beg = -1; int value = -1; cin >> n; for (int i = 0; i < n; i++) { int x; cin >> x; v.push_back(x); sum[x] = sum[x] + 1; if (sum[x] == 1) { visit[x] = i; } if (sum[x] == maior) { if (i - visit[x] < fim - visit[value]) { maior = sum[x]; fim = i; beg = i; value = x; } } if (sum[x] > maior) { maior = sum[x]; fim = i; beg = i; value = x; } } for (int i = 0; i < v.size(); i++) { if (v[i] == value) { beg = i; break; } } cout << (beg + 1) << " " << (fim + 1) << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> jmlr(n); map<int, int> jmlrs; map<int, int> last; map<int, int> first; for (int i = 0; i < n; i++) { cin >> jmlr[i]; if (jmlrs.find(jmlr[i]) == jmlrs.end()) { jmlrs[jmlr[i]] = 0; first[jmlr[i]] = i; } jmlrs[jmlr[i]] += 1; last[jmlr[i]] = i; } int chip = first[jmlr[0]]; int munk = last[jmlr[0]]; int maxim = jmlr[0]; for (int i = 0; i < n; i++) { if (jmlrs[jmlr[i]] > jmlrs[maxim]) { maxim = jmlr[i]; chip = first[jmlr[i]]; munk = last[jmlr[i]]; } else if (jmlrs[jmlr[i]] == jmlrs[maxim]) { if (munk - chip > last[jmlr[i]] - first[jmlr[i]]) { chip = first[jmlr[i]]; munk = last[jmlr[i]]; } } } cout << (chip + 1) << " " << (munk + 1) << endl; return 0; }
### Prompt In CPP, your task is to solve the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> jmlr(n); map<int, int> jmlrs; map<int, int> last; map<int, int> first; for (int i = 0; i < n; i++) { cin >> jmlr[i]; if (jmlrs.find(jmlr[i]) == jmlrs.end()) { jmlrs[jmlr[i]] = 0; first[jmlr[i]] = i; } jmlrs[jmlr[i]] += 1; last[jmlr[i]] = i; } int chip = first[jmlr[0]]; int munk = last[jmlr[0]]; int maxim = jmlr[0]; for (int i = 0; i < n; i++) { if (jmlrs[jmlr[i]] > jmlrs[maxim]) { maxim = jmlr[i]; chip = first[jmlr[i]]; munk = last[jmlr[i]]; } else if (jmlrs[jmlr[i]] == jmlrs[maxim]) { if (munk - chip > last[jmlr[i]] - first[jmlr[i]]) { chip = first[jmlr[i]]; munk = last[jmlr[i]]; } } } cout << (chip + 1) << " " << (munk + 1) << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, a[100005]; map<int, int> cnt, fi, la; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; cnt[a[i]]++; la[a[i]] = i; if (fi.count(a[i]) == 0) fi[a[i]] = i; } int beauty = 0; for (int i = 0; i < n; i++) { if (cnt[a[i]] > beauty) beauty = cnt[a[i]]; } int diff = n, l = 1, r = n; for (int i = 0; i < n; i++) { if (cnt[a[i]] == beauty && la[a[i]] - fi[a[i]] + 1 < diff) { l = fi[a[i]] + 1; r = la[a[i]] + 1; diff = la[a[i]] - fi[a[i]] + 1; } } cout << l << " " << r << endl; return 0; }
### Prompt Generate a cpp solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, a[100005]; map<int, int> cnt, fi, la; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; cnt[a[i]]++; la[a[i]] = i; if (fi.count(a[i]) == 0) fi[a[i]] = i; } int beauty = 0; for (int i = 0; i < n; i++) { if (cnt[a[i]] > beauty) beauty = cnt[a[i]]; } int diff = n, l = 1, r = n; for (int i = 0; i < n; i++) { if (cnt[a[i]] == beauty && la[a[i]] - fi[a[i]] + 1 < diff) { l = fi[a[i]] + 1; r = la[a[i]] + 1; diff = la[a[i]] - fi[a[i]] + 1; } } cout << l << " " << r << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1e6; int lft[N + 10], rght[N + 10], f[N + 10], ind[N + 10], l, r, n, a, mn, mx; bool cmp(int i, int j) { return f[i] > f[j]; } int main() { std::ios::sync_with_stdio(0); cin.tie(0); for (int i = 1; i <= N; i++) { lft[i] = -1; rght[i] = -1; f[i] = 0; ind[i] = i; } cin >> n; for (int i = 1; i <= n; i++) { cin >> a; if (lft[a] == -1) lft[a] = i; rght[a] = i; f[a]++; } sort(ind + 1, ind + N + 1, cmp); mn = n + 10; mx = f[ind[1]]; for (int i = 1, j; i <= N; i++) { j = ind[i]; if (f[j] < mx) break; if (rght[j] - lft[j] < mn) { mn = rght[j] - lft[j]; r = rght[j]; l = lft[j]; } } cout << l << " " << r << "\n"; return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1e6; int lft[N + 10], rght[N + 10], f[N + 10], ind[N + 10], l, r, n, a, mn, mx; bool cmp(int i, int j) { return f[i] > f[j]; } int main() { std::ios::sync_with_stdio(0); cin.tie(0); for (int i = 1; i <= N; i++) { lft[i] = -1; rght[i] = -1; f[i] = 0; ind[i] = i; } cin >> n; for (int i = 1; i <= n; i++) { cin >> a; if (lft[a] == -1) lft[a] = i; rght[a] = i; f[a]++; } sort(ind + 1, ind + N + 1, cmp); mn = n + 10; mx = f[ind[1]]; for (int i = 1, j; i <= N; i++) { j = ind[i]; if (f[j] < mx) break; if (rght[j] - lft[j] < mn) { mn = rght[j] - lft[j]; r = rght[j]; l = lft[j]; } } cout << l << " " << r << "\n"; return 0; } ```
#include <bits/stdc++.h> using namespace std; void FastIO() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); } const int N = 1e6; vector<int> v[N + 1]; int main() { FastIO(); int tc, ca = 0; int n, el; cin >> n; int mx = 0; for (int i = 1; i <= n; i++) { cin >> el; v[el].push_back(i); mx = max(mx, int(v[el].size())); } int L = 1; int R = n + 1; for (int i = 1; i <= N; i++) { if (mx == v[i].size()) { if (R - L > v[i][v[i].size() - 1] - v[i][0]) { L = v[i][0]; R = v[i][v[i].size() - 1]; mx = v[i].size(); } } } cout << L << ' ' << R << '\n'; return 0; }
### Prompt Generate a Cpp solution to the following problem: Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array. Help Amr by choosing the smallest subsegment possible. Input The first line contains one number n (1 ≀ n ≀ 105), the size of the array. The second line contains n integers ai (1 ≀ ai ≀ 106), representing elements of the array. Output Output two integers l, r (1 ≀ l ≀ r ≀ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≀ i ≀ r - l + 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; void FastIO() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); } const int N = 1e6; vector<int> v[N + 1]; int main() { FastIO(); int tc, ca = 0; int n, el; cin >> n; int mx = 0; for (int i = 1; i <= n; i++) { cin >> el; v[el].push_back(i); mx = max(mx, int(v[el].size())); } int L = 1; int R = n + 1; for (int i = 1; i <= N; i++) { if (mx == v[i].size()) { if (R - L > v[i][v[i].size() - 1] - v[i][0]) { L = v[i][0]; R = v[i][v[i].size() - 1]; mx = v[i].size(); } } } cout << L << ' ' << R << '\n'; return 0; } ```