exec_outcome stringclasses 1 value | code_uid stringlengths 32 32 | file_name stringclasses 111 values | prob_desc_created_at stringlengths 10 10 | prob_desc_description stringlengths 63 3.8k | prob_desc_memory_limit stringclasses 18 values | source_code stringlengths 117 65.5k | lang_cluster stringclasses 1 value | prob_desc_sample_inputs stringlengths 2 802 | prob_desc_time_limit stringclasses 27 values | prob_desc_sample_outputs stringlengths 2 796 | prob_desc_notes stringlengths 4 3k ⌀ | lang stringclasses 5 values | prob_desc_input_from stringclasses 3 values | tags listlengths 0 11 | src_uid stringlengths 32 32 | prob_desc_input_spec stringlengths 28 2.37k ⌀ | difficulty int64 -1 3.5k ⌀ | prob_desc_output_spec stringlengths 17 1.47k ⌀ | prob_desc_output_to stringclasses 3 values | hidden_unit_tests stringclasses 1 value |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
PASSED | a80d536938afcee48413246611a73198 | train_001.jsonl | 1553267100 | For a given set of two-dimensional points $$$S$$$, let's denote its extension $$$E(S)$$$ as the result of the following algorithm:Create another set of two-dimensional points $$$R$$$, which is initially equal to $$$S$$$. Then, while there exist four numbers $$$x_1$$$, $$$y_1$$$, $$$x_2$$$ and $$$y_2$$$ such that $$$(x_1, y_1) \in R$$$, $$$(x_1, y_2) \in R$$$, $$$(x_2, y_1) \in R$$$ and $$$(x_2, y_2) \notin R$$$, add $$$(x_2, y_2)$$$ to $$$R$$$. When it is impossible to find such four integers, let $$$R$$$ be the result of the algorithm.Now for the problem itself. You are given a set of two-dimensional points $$$S$$$, which is initially empty. You have to process two types of queries: add some point to $$$S$$$, or remove some point from it. After each query you have to compute the size of $$$E(S)$$$. | 1024 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;
import static java.lang.Math.*;
public class Main implements Runnable {
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars==-1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
}
catch (IOException e) {
throw new InputMismatchException();
}
if(numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
int c = read();
while(isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if(c<'0'||c>'9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
public static void main(String args[]) throws Exception {
new Thread(null, new Main(),"Main",1<<27).start();
}
class DynamicBiconnectivity {
class Move {
int child, par, prevRank;
Move(int a, int b, int c) {
child = a;
par = b;
prevRank = c;
}
}
class Edge {
int u, v;
Edge(int a, int b) {
u = a;
v = b;
}
}
void rollback(LinkedList<Move> list) {
while(list.size() > 0) {
Move m = list.getLast();
int child = m.child;
int parent = m.par;
unrollOperation(child, parent);
rank[parent] = m.prevRank;
par[child] = child;
list.removeLast();
}
}
void combine(int u, int v, LinkedList<Move> list) {
if(rank[u] > rank[v]) {
list.add(new Move(v, u, rank[u]));
combineOperation(v, u);
par[v] = u;
}
else {
list.add(new Move(u, v, rank[v]));
combineOperation(u, v);
if(rank[v] == rank[u])
rank[v]++;
par[u] = v;
}
}
int find(int i) {
if(par[i] == i)
return i;
return find(par[i]);
}
void dfsSolve(int low, int high, int pos) {
LinkedList<Move> list = new LinkedList<>();
for(Edge e : segTree[pos]) {
int u = find(e.u);
int v = find(e.v);
if(u != v) {
combine(u, v, list);
}
}
if(low != high) {
int mid = (low + high) >> 1;
dfsSolve(low, mid, 2 * pos + 1);
dfsSolve(mid + 1, high, 2 * pos + 2);
}
else
ans[low] = getAns(low);
rollback(list);
}
void dfsAddEdge(int low, int high, int pos, int l, int r, Edge e) {
if(low > r || high < l)
return;
if(low >= l && high <= r) {
segTree[pos].add(e);
return;
}
int mid = (low + high) >> 1;
dfsAddEdge(low, mid, 2 * pos + 1, l, r, e);
dfsAddEdge(mid + 1, high, 2 * pos + 2, l, r, e);
}
int n, q;
ArrayList<Edge> segTree[];
int par[], rank[];
DynamicBiconnectivity(int n, int q) {
this.n = n;
this.q = q;
segTree = new ArrayList[4 * q];
for(int i = 0; i < 4 * q; ++i)
segTree[i] = new ArrayList<>();
par = new int[n];
rank = new int[n];
for(int i = 0; i < n; ++i) {
par[i] = i;
rank[i] = 0;
}
initialize();
}
void addEdge(int l, int r, int u, int v) {
Edge e = new Edge(u, v);
dfsAddEdge(0, q - 1, 0, l, r, e);
}
void solve() {
dfsSolve(0, q - 1, 0);
}
// Provide the necessary changes below
long ans[];
long cntX[], cntY[];
long cans = 0;
void initialize() {
ans = new long[q];
cntX = new long[n];
cntY = new long[n];
for(int i = 0; i < n; ++i) {
if(i < 300000)
cntX[i] = 1;
else
cntY[i] = 1;
}
}
long getAns(int ind) {
return cans;
}
void unrollOperation(int child, int parent) {
// Reverse steps of combine Opr, nullifying the changes made
cntX[parent] -= cntX[child];
cntY[parent] -= cntY[child];
cans -= cntX[parent] * cntY[child] + cntX[child] * cntY[parent];
}
void combineOperation(int child, int parent) {
cans += cntX[parent] * cntY[child] + cntX[child] * cntY[parent];
cntX[parent] += cntX[child];
cntY[parent] += cntY[child];
}
// DynamicBiconnectivity(int n, int q) - Number of nodes and queries
// addEdge(l, r, u, v) - to add edge from u to v in the range l to r
// solve() - to solve and find ans for each query
}
public void run() {
InputReader sc = new InputReader(System.in);
PrintWriter w = new PrintWriter(System.out);
int q = sc.nextInt();
DynamicBiconnectivity db = new DynamicBiconnectivity(600000, q);
TreeMap<Integer, Integer> map[] = new TreeMap[300000];
for(int i = 0; i < 300000; ++i)
map[i] = new TreeMap<>();
for(int x = 0; x < q; ++x) {
int indx = sc.nextInt() - 1;
int indy = sc.nextInt() + 299999;
if(map[indx].get(indy) != null) {
db.addEdge(map[indx].get(indy), x - 1, indx, indy);
map[indx].remove(indy);
}
else
map[indx].put(indy, x);
}
for(int i = 0; i < 300000; ++i) {
for(int j : map[i].keySet())
db.addEdge(map[i].get(j), q - 1, i, j);
}
db.solve();
for(long i : db.ans)
w.print(i + " ");
w.close();
}
} | Java | ["7\n1 1\n1 2\n2 1\n2 2\n1 2\n1 3\n2 1"] | 3.5 seconds | ["1 2 4 4 4 6 3"] | null | Java 8 | standard input | [
"data structures",
"dsu",
"divide and conquer"
] | feff009596baf8e2d7f1aac36b2f077e | The first line contains one integer $$$q$$$ ($$$1 \le q \le 3 \cdot 10^5$$$) — the number of queries. Then $$$q$$$ lines follow, each containing two integers $$$x_i$$$, $$$y_i$$$ ($$$1 \le x_i, y_i \le 3 \cdot 10^5$$$), denoting $$$i$$$-th query as follows: if $$$(x_i, y_i) \in S$$$, erase it from $$$S$$$, otherwise insert $$$(x_i, y_i)$$$ into $$$S$$$. | 2,600 | Print $$$q$$$ integers. $$$i$$$-th integer should be equal to the size of $$$E(S)$$$ after processing first $$$i$$$ queries. | standard output | |
PASSED | 9ada8324c6e45445fe80147406208543 | train_001.jsonl | 1553267100 | For a given set of two-dimensional points $$$S$$$, let's denote its extension $$$E(S)$$$ as the result of the following algorithm:Create another set of two-dimensional points $$$R$$$, which is initially equal to $$$S$$$. Then, while there exist four numbers $$$x_1$$$, $$$y_1$$$, $$$x_2$$$ and $$$y_2$$$ such that $$$(x_1, y_1) \in R$$$, $$$(x_1, y_2) \in R$$$, $$$(x_2, y_1) \in R$$$ and $$$(x_2, y_2) \notin R$$$, add $$$(x_2, y_2)$$$ to $$$R$$$. When it is impossible to find such four integers, let $$$R$$$ be the result of the algorithm.Now for the problem itself. You are given a set of two-dimensional points $$$S$$$, which is initially empty. You have to process two types of queries: add some point to $$$S$$$, or remove some point from it. After each query you have to compute the size of $$$E(S)$$$. | 1024 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;
import static java.lang.Math.*;
public class Main implements Runnable {
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars==-1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
}
catch (IOException e) {
throw new InputMismatchException();
}
if(numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
int c = read();
while(isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if(c<'0'||c>'9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
public static void main(String args[]) throws Exception {
new Thread(null, new Main(),"Main",1<<27).start();
}
class Move {
int child, par, prevRank;
Move(int a, int b, int c) {
child = a;
par = b;
prevRank = c;
}
}
class Edge {
int u, v;
Edge(int a, int b) {
u = a;
v = b;
}
}
int find(int i) {
if(par[i] == i)
return i;
return find(par[i]);
}
void rollback(LinkedList<Move> list) {
while(list.size() > 0) {
Move m = list.getLast();
int child = m.child;
int parent = m.par;
unrollOperation(child, parent);
rank[parent] = m.prevRank;
par[child] = child;
list.removeLast();
}
}
void combine(int u, int v, LinkedList<Move> list) {
if(rank[u] > rank[v]) {
list.add(new Move(v, u, rank[u]));
combineOperation(v, u);
par[v] = u;
}
else {
list.add(new Move(u, v, rank[v]));
combineOperation(u, v);
if(rank[v] == rank[u])
rank[v]++;
par[u] = v;
}
}
void solve(int low, int high, int pos) {
LinkedList<Move> list = new LinkedList<>();
for(Edge e : segTree[pos]) {
int u = find(e.u);
int v = find(e.v);
if(u != v) {
combine(u, v, list);
}
}
if(low != high) {
int mid = (low + high) >> 1;
solve(low, mid, 2 * pos + 1);
solve(mid + 1, high, 2 * pos + 2);
}
else
ans[low] = getAns(low);
rollback(list);
}
void addEdge(int low, int high, int pos, int l, int r, Edge e) {
if(low > r || high < l)
return;
if(low >= l && high <= r) {
segTree[pos].add(e);
return;
}
int mid = (low + high) >> 1;
addEdge(low, mid, 2 * pos + 1, l, r, e);
addEdge(mid + 1, high, 2 * pos + 2, l, r, e);
}
ArrayList<Edge> segTree[];
int par[], rank[];
// Provide the necessary changes below
long ans[];
long cntX[], cntY[];
long cans = 0;
long getAns(int ind) {
return cans;
}
void unrollOperation(int child, int parent) {
// Reverse steps of combine Opr, nullifying the changes made
cntX[parent] -= cntX[child];
cntY[parent] -= cntY[child];
cans -= cntX[parent] * cntY[child] + cntX[child] * cntY[parent];
}
void combineOperation(int child, int parent) {
cans += cntX[parent] * cntY[child] + cntX[child] * cntY[parent];
cntX[parent] += cntX[child];
cntY[parent] += cntY[child];
}
// addEdge(0, n - 1, 0, l, r, edge) - to add Edge edge in the range l to r
// solve(0, n - 1, 0) - to solve the segTree
public void run() {
InputReader sc = new InputReader(System.in);
PrintWriter w = new PrintWriter(System.out);
int q = sc.nextInt();
segTree = new ArrayList[4 * q];
for(int i = 0; i < 4 * q; ++i) {
segTree[i] = new ArrayList<>();
}
TreeMap<Integer, Integer> map[] = new TreeMap[300000];
for(int i = 0; i < 300000; ++i)
map[i] = new TreeMap<>();
for(int x = 0; x < q; ++x) {
int indx = sc.nextInt() - 1;
int indy = sc.nextInt() + 299999;
if(map[indx].get(indy) != null) {
addEdge(0, q - 1, 0, map[indx].get(indy), x - 1, new Edge(indx, indy));
map[indx].remove(indy);
}
else
map[indx].put(indy, x);
}
for(int i = 0; i < 300000; ++i) {
for(int j : map[i].keySet())
addEdge(0, q - 1, 0, map[i].get(j), q - 1, new Edge(i, j));
}
ans = new long[q];
cntX = new long[600000];
cntY = new long[600000];
par = new int[600000];
rank = new int[600000];
for(int i = 0; i < 600000; ++i) {
rank[i] = 0;
par[i] = i;
if(i < 300000)
cntX[i] = 1;
else
cntY[i] = 1;
}
solve(0, q - 1, 0);
for(long i : ans)
w.print(i + " ");
w.close();
}
} | Java | ["7\n1 1\n1 2\n2 1\n2 2\n1 2\n1 3\n2 1"] | 3.5 seconds | ["1 2 4 4 4 6 3"] | null | Java 8 | standard input | [
"data structures",
"dsu",
"divide and conquer"
] | feff009596baf8e2d7f1aac36b2f077e | The first line contains one integer $$$q$$$ ($$$1 \le q \le 3 \cdot 10^5$$$) — the number of queries. Then $$$q$$$ lines follow, each containing two integers $$$x_i$$$, $$$y_i$$$ ($$$1 \le x_i, y_i \le 3 \cdot 10^5$$$), denoting $$$i$$$-th query as follows: if $$$(x_i, y_i) \in S$$$, erase it from $$$S$$$, otherwise insert $$$(x_i, y_i)$$$ into $$$S$$$. | 2,600 | Print $$$q$$$ integers. $$$i$$$-th integer should be equal to the size of $$$E(S)$$$ after processing first $$$i$$$ queries. | standard output | |
PASSED | dd6e2b3b5f3f6d175a39efd4a2754d69 | train_001.jsonl | 1553267100 | For a given set of two-dimensional points $$$S$$$, let's denote its extension $$$E(S)$$$ as the result of the following algorithm:Create another set of two-dimensional points $$$R$$$, which is initially equal to $$$S$$$. Then, while there exist four numbers $$$x_1$$$, $$$y_1$$$, $$$x_2$$$ and $$$y_2$$$ such that $$$(x_1, y_1) \in R$$$, $$$(x_1, y_2) \in R$$$, $$$(x_2, y_1) \in R$$$ and $$$(x_2, y_2) \notin R$$$, add $$$(x_2, y_2)$$$ to $$$R$$$. When it is impossible to find such four integers, let $$$R$$$ be the result of the algorithm.Now for the problem itself. You are given a set of two-dimensional points $$$S$$$, which is initially empty. You have to process two types of queries: add some point to $$$S$$$, or remove some point from it. After each query you have to compute the size of $$$E(S)$$$. | 1024 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;
import static java.lang.Math.*;
public class Main implements Runnable {
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars==-1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
}
catch (IOException e) {
throw new InputMismatchException();
}
if(numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
int c = read();
while(isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if(c<'0'||c>'9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
public static void main(String args[]) throws Exception {
new Thread(null, new Main(),"Main",1<<27).start();
}
class Move {
int child, par, prevRank;
Move(int a, int b, int c) {
child = a;
par = b;
prevRank = c;
}
}
class Edge {
int u, v;
Edge(int a, int b) {
u = a;
v = b;
}
}
int find(int i) {
if(par[i] == i)
return i;
return find(par[i]);
}
void rollback(LinkedList<Move> list) {
while(list.size() > 0) {
Move m = list.getLast();
int child = m.child;
int parent = m.par;
unrollOperation(child, parent);
rank[parent] = m.prevRank;
par[child] = child;
list.removeLast();
}
}
void combine(int u, int v, LinkedList<Move> list) {
if(rank[u] > rank[v]) {
list.add(new Move(v, u, rank[u]));
combineOperation(v, u);
par[v] = u;
}
else {
list.add(new Move(u, v, rank[v]));
combineOperation(u, v);
if(rank[v] == rank[u])
rank[v]++;
par[u] = v;
}
}
void solve(int low, int high, int pos) {
LinkedList<Move> list = new LinkedList<>();
for(Edge e : segTree[pos]) {
int u = find(e.u);
int v = find(e.v);
if(u != v) {
combine(u, v, list);
}
}
if(low != high) {
int mid = (low + high) >> 1;
solve(low, mid, 2 * pos + 1);
solve(mid + 1, high, 2 * pos + 2);
}
else
ans[low] = getAns(low);
rollback(list);
}
void addEdge(int low, int high, int pos, int l, int r, Edge e) {
if(low > r || high < l)
return;
if(low >= l && high <= r) {
segTree[pos].add(e);
return;
}
int mid = (low + high) >> 1;
addEdge(low, mid, 2 * pos + 1, l, r, e);
addEdge(mid + 1, high, 2 * pos + 2, l, r, e);
}
LinkedList<Edge> segTree[];
int par[], rank[];
// Provide the necessary changes below
long ans[];
long cntX[], cntY[];
long cans = 0;
long getAns(int ind) {
return cans;
}
void unrollOperation(int child, int parent) {
// Reverse steps of combine Opr, nullifying the changes made
cntX[parent] -= cntX[child];
cntY[parent] -= cntY[child];
cans -= cntX[parent] * cntY[child] + cntX[child] * cntY[parent];
}
void combineOperation(int child, int parent) {
cans += cntX[parent] * cntY[child] + cntX[child] * cntY[parent];
cntX[parent] += cntX[child];
cntY[parent] += cntY[child];
}
// addEdge(0, n - 1, 0, l, r, edge) - to add Edge edge in the range l to r
// solve(0, n - 1, 0) - to solve the segTree
public void run() {
InputReader sc = new InputReader(System.in);
PrintWriter w = new PrintWriter(System.out);
int q = sc.nextInt();
segTree = new LinkedList[4 * q];
for(int i = 0; i < 4 * q; ++i) {
segTree[i] = new LinkedList<>();
}
TreeMap<Integer, Integer> map[] = new TreeMap[300000];
for(int i = 0; i < 300000; ++i)
map[i] = new TreeMap<>();
for(int x = 0; x < q; ++x) {
int indx = sc.nextInt() - 1;
int indy = sc.nextInt() + 299999;
if(map[indx].get(indy) != null) {
addEdge(0, q - 1, 0, map[indx].get(indy), x - 1, new Edge(indx, indy));
map[indx].remove(indy);
}
else
map[indx].put(indy, x);
}
for(int i = 0; i < 300000; ++i) {
for(int j : map[i].keySet())
addEdge(0, q - 1, 0, map[i].get(j), q - 1, new Edge(i, j));
}
ans = new long[q];
cntX = new long[600000];
cntY = new long[600000];
par = new int[600000];
rank = new int[600000];
for(int i = 0; i < 600000; ++i) {
rank[i] = 0;
par[i] = i;
if(i < 300000)
cntX[i] = 1;
else
cntY[i] = 1;
}
solve(0, q - 1, 0);
for(long i : ans)
w.print(i + " ");
w.close();
}
} | Java | ["7\n1 1\n1 2\n2 1\n2 2\n1 2\n1 3\n2 1"] | 3.5 seconds | ["1 2 4 4 4 6 3"] | null | Java 8 | standard input | [
"data structures",
"dsu",
"divide and conquer"
] | feff009596baf8e2d7f1aac36b2f077e | The first line contains one integer $$$q$$$ ($$$1 \le q \le 3 \cdot 10^5$$$) — the number of queries. Then $$$q$$$ lines follow, each containing two integers $$$x_i$$$, $$$y_i$$$ ($$$1 \le x_i, y_i \le 3 \cdot 10^5$$$), denoting $$$i$$$-th query as follows: if $$$(x_i, y_i) \in S$$$, erase it from $$$S$$$, otherwise insert $$$(x_i, y_i)$$$ into $$$S$$$. | 2,600 | Print $$$q$$$ integers. $$$i$$$-th integer should be equal to the size of $$$E(S)$$$ after processing first $$$i$$$ queries. | standard output | |
PASSED | 7423d0ef1dca35ccbb6fc016d354a478 | train_001.jsonl | 1553267100 | For a given set of two-dimensional points $$$S$$$, let's denote its extension $$$E(S)$$$ as the result of the following algorithm:Create another set of two-dimensional points $$$R$$$, which is initially equal to $$$S$$$. Then, while there exist four numbers $$$x_1$$$, $$$y_1$$$, $$$x_2$$$ and $$$y_2$$$ such that $$$(x_1, y_1) \in R$$$, $$$(x_1, y_2) \in R$$$, $$$(x_2, y_1) \in R$$$ and $$$(x_2, y_2) \notin R$$$, add $$$(x_2, y_2)$$$ to $$$R$$$. When it is impossible to find such four integers, let $$$R$$$ be the result of the algorithm.Now for the problem itself. You are given a set of two-dimensional points $$$S$$$, which is initially empty. You have to process two types of queries: add some point to $$$S$$$, or remove some point from it. After each query you have to compute the size of $$$E(S)$$$. | 1024 megabytes | import java.lang.*;
import java.math.*;
import java.util.*;
import java.io.*;
public class Main {
class Node {
int x;
int y;
public Node(int x,int y){
this.x=x;
this.y=y;
}
public boolean equals(Object o){
Node c=(Node)o;
return x==c.x && y==c.y;
}
public int hashCode(){
return x+y;
}
}
class Node2 {
int v,u,szxv,szyv,szxu,szyu;
public Node2(int v,int u,int szxv,int szyv,int szxu,int szyu){
this.v=v;
this.u=u;
this.szxv=szxv;
this.szyv=szyv;
this.szxu=szxu;
this.szyu=szyu;
}
}
void solve() {
int q=ni();
HashMap<Node,Integer> mp=new HashMap<>();
tree=new ArrayList[4*q+1];
for(int i=1;i<=4*q;i++) tree[i]=new ArrayList<>();
for(int i=0;i<q;i++){
int x=ni(),y=ni();
if(!mp.containsKey(new Node(x,y))) {
mp.put(new Node(x, y), i + 1);
}else {
int st = mp.get(new Node(x, y));
mp.remove(new Node(x, y));
update(1, st, i, 1, q, new Node(x, y));
// pw.println(x+" "+y+" "+st+" "+i);
}
}
for(Map.Entry<Node,Integer> e : mp.entrySet()){
Node p=e.getKey(); int st=e.getValue();
update(1,st,q,1,q,new Node(p.x,p.y));
// pw.println(p.x+" "+p.y+" "+st+" "+q);
}
F=new int[2*MX+1];
szx=new int[2*MX+1];
szy=new int[2*MX+1];
for(int i=1;i<=MX;i++){
F[i]=i; F[i+MX]=i+MX;
szx[i]=1; szy[i+MX]=1;
}
ans2=new long[q+1];
go(1,1,q);
for(int i=1;i<=q;i++) pw.print(ans2[i]+" ");
pw.println("");
}
int MX=300000;
ArrayList<Node> tree[];
int F[];
int szx[],szy[];
long ans=0;
long ans2[];
boolean bl=false;
Stack<Node2>stk=new Stack<>();
void go(int id,int l,int r){
long prevans=ans;
for(Node p : tree[id]){
merge(p.x,p.y);
}
if(l==r){
ans2[l]=ans;
// pw.println(l);
}else {
int mid=(l+r)>>1;
go(2*id,l,mid);
go(2*id+1,mid+1,r);
}
ans=prevans;
int k=tree[id].size();
while(k-->0){
Node2 p=stk.pop();
F[p.v]=p.v;
F[p.u]=p.u;
szx[p.v]=p.szxv;
szy[p.v]=p.szyv;
szx[p.u]=p.szxu;
szy[p.u]=p.szyu;
// pw.println(p.v+" "+p.u+" rollback"+" "+szx[p.v]+" "+szy[p.v]+" "+szx[p.u]+" "+szy[p.u]);
}
}
int root(int a){
while(a!=F[a]){
a=F[a];
}
return a;
}
void merge(int x,int y){
int u=root(x),v=root(y+MX);
stk.push(new Node2(v,u,szx[v],szy[v],szx[u],szy[u]));
// pw.println(v+" "+u+" "+x+" "+y+" "+szx[v]+" "+szy[v]+" "+szx[u]+" "+szy[u]);
if(u==v){
return;
}
ans-=(szx[u]*1L*szy[u]);
ans-=(szx[v]*1L*szy[v]);
if(szx[u]+szy[u]>szx[v]+szy[v]){
int tmp=u; u=v; v=tmp;
}
szx[v]+=szx[u]; szy[v]+=szy[u];
F[u]=v;
ans+=(szx[v]*1L*szy[v]);
}
void update(int id,int x,int y,int l,int r,Node p){
if(r<x || l>y) return;
if(x==l && y==r) {
// pw.println(l+" "+r+" "+p.x+" "+p.y);
tree[id].add(new Node(p.x,p.y));
return;
}
int mid=(l+r)>>1;
if(y<=mid) update(2*id,x,y,l,mid,p);
else if(x>mid) update(2*id+1,x,y,mid+1,r,p);
else {
update(2*id,x,mid,l,mid,p);
update(2*id+1,mid+1,y,mid+1,r,p);
}
}
long M = (long)1e9+7;
InputStream is;
PrintWriter pw;
String INPUT = "";
void run() throws Exception {
is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
pw = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
pw.flush();
if (!INPUT.isEmpty()) tr(System.currentTimeMillis() - s + "ms");
}
public static void main(String[] args) throws Exception {
new Main().run();
}
private byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
private int readByte() {
if (lenbuf == -1) throw new InputMismatchException();
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0) return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b)) ;
return b;
}
private double nd() {
return Double.parseDouble(ns());
}
private char nc() {
return (char) skip();
}
private String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n) {
char[] buf = new char[n];
int b = skip(), p = 0;
while (p < n && !(isSpaceChar(b))) {
buf[p++] = (char) b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m) {
char[][] map = new char[n][];
for (int i = 0; i < n; i++) map[i] = ns(m);
return map;
}
private int[] na(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = ni();
return a;
}
private int ni() {
int num = 0, b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private long nl() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private void tr(Object... o) {
if (INPUT.length() > 0) System.out.println(Arrays.deepToString(o));
}
} | Java | ["7\n1 1\n1 2\n2 1\n2 2\n1 2\n1 3\n2 1"] | 3.5 seconds | ["1 2 4 4 4 6 3"] | null | Java 8 | standard input | [
"data structures",
"dsu",
"divide and conquer"
] | feff009596baf8e2d7f1aac36b2f077e | The first line contains one integer $$$q$$$ ($$$1 \le q \le 3 \cdot 10^5$$$) — the number of queries. Then $$$q$$$ lines follow, each containing two integers $$$x_i$$$, $$$y_i$$$ ($$$1 \le x_i, y_i \le 3 \cdot 10^5$$$), denoting $$$i$$$-th query as follows: if $$$(x_i, y_i) \in S$$$, erase it from $$$S$$$, otherwise insert $$$(x_i, y_i)$$$ into $$$S$$$. | 2,600 | Print $$$q$$$ integers. $$$i$$$-th integer should be equal to the size of $$$E(S)$$$ after processing first $$$i$$$ queries. | standard output | |
PASSED | 6abac3e758a739296f48fff1040d7906 | train_001.jsonl | 1449677100 | An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train? | 256 megabytes |
//package round_606;
import java.util.Scanner;
public class SortingRailwayCars {
static int n;
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
n = s.nextInt();
int[] loc = new int[n];
for (int i = 0; i < n; i++) {
loc[s.nextInt() - 1] = i;
}
System.out.println(n - miss(loc));
System.out.println();
s.close();
}
public static int miss(int[] loc) {
int max = 1;
int len;
for (int i = 0; i < n; i += len) {
len = 1;
while (i + len < n && loc[i + len] > loc[i + len - 1]) {
len++;
}
max = Math.max(max, len);
}
return max;
}
} | Java | ["5\n4 1 2 5 3", "4\n4 1 3 2"] | 2 seconds | ["2", "2"] | NoteIn the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | Java 8 | standard input | [
"constructive algorithms",
"greedy"
] | 277948a70c75840445e1826f2b23a897 | The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train. The second line contains n integers pi (1 ≤ pi ≤ n, pi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train. | 1,600 | Print a single integer — the minimum number of actions needed to sort the railway cars. | standard output | |
PASSED | 2a6bbd39fdde11510c7d9f458c4205e5 | train_001.jsonl | 1449677100 | An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train? | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author AlexFetisov
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC_Perm solver = new TaskC_Perm();
solver.solve(1, in, out);
out.close();
}
static class TaskC_Perm {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int[] a = new int[n];
int[] pos = new int[n];
for (int i = 0; i < n; ++i) {
a[i] = in.nextInt() - 1;
pos[a[i]] = i;
}
int res = 0;
int begin = 0;
for (int i = 1; i < n; ++i) {
if (pos[i] < pos[i - 1]) {
res = Math.max(res, i - begin);
begin = i;
}
}
res = Math.max(res, n - begin);
out.println(n - res);
}
}
static class InputReader {
private BufferedReader reader;
private StringTokenizer stt;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
}
public String nextLine() {
try {
return reader.readLine();
} catch (IOException e) {
return null;
}
}
public String nextString() {
while (stt == null || !stt.hasMoreTokens()) {
stt = new StringTokenizer(nextLine());
}
return stt.nextToken();
}
public int nextInt() {
return Integer.parseInt(nextString());
}
}
}
| Java | ["5\n4 1 2 5 3", "4\n4 1 3 2"] | 2 seconds | ["2", "2"] | NoteIn the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | Java 8 | standard input | [
"constructive algorithms",
"greedy"
] | 277948a70c75840445e1826f2b23a897 | The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train. The second line contains n integers pi (1 ≤ pi ≤ n, pi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train. | 1,600 | Print a single integer — the minimum number of actions needed to sort the railway cars. | standard output | |
PASSED | 419a130c5610d486a6ae19580c11aade | train_001.jsonl | 1449677100 | An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train? | 256 megabytes |
import java.util.Scanner;
//http://codeforces.com/contest/605/problem/A
public class Div1605A {
int arr[];
int pos[];
int n;
public static void main(String []args) {
Scanner sc = new Scanner(System.in);
Div1605A sol;
int n = sc.nextInt();
int temp[] = new int[n+1];
for(int i=1;i<=n;i++) {
temp[i]=sc.nextInt();
}
sol = new Div1605A(temp, n);
System.out.println(sol.solution());
sc.close();
}
public Div1605A(int []arr,int n) {
// TODO Auto-generated constructor stub
this.arr = arr;
this.n = n;
}
public int solution() {
pos = new int[n+1];
for(int i=1;i<=n;i++) {
pos[arr[i]] = i;
}
int len = maxIncreasingContiguousSequence();
return n - len;
}
private int maxIncreasingContiguousSequence() {
// TODO Auto-generated method stub
int curlen = 1;
int max = 1;
for(int i=2;i<=n;i++) {
if(pos[i-1]>pos[i]) {
if(curlen>max) {
max = curlen;
}
curlen=1;
continue;
}
curlen++;
}
return (curlen>max)?curlen:max;
}
}
| Java | ["5\n4 1 2 5 3", "4\n4 1 3 2"] | 2 seconds | ["2", "2"] | NoteIn the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | Java 8 | standard input | [
"constructive algorithms",
"greedy"
] | 277948a70c75840445e1826f2b23a897 | The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train. The second line contains n integers pi (1 ≤ pi ≤ n, pi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train. | 1,600 | Print a single integer — the minimum number of actions needed to sort the railway cars. | standard output | |
PASSED | a6f124ddc8c41fe1a7586b9e498b1b5a | train_001.jsonl | 1449677100 | An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train? | 256 megabytes | import java.util.Scanner;
public class sorting_railway_problem {
public static void main(String args[]){
Scanner s= new Scanner (System.in);
int n= s.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++){
a[i]=s.nextInt();
}
int ans=0;
int newa[] = new int[n+1];
for(int i=0;i<n;i++){
newa[a[i]]=newa[a[i]-1]+1;
ans=Math.max(ans, newa[a[i]]);
}
System.out.println(n-ans);
}
}
| Java | ["5\n4 1 2 5 3", "4\n4 1 3 2"] | 2 seconds | ["2", "2"] | NoteIn the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | Java 8 | standard input | [
"constructive algorithms",
"greedy"
] | 277948a70c75840445e1826f2b23a897 | The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train. The second line contains n integers pi (1 ≤ pi ≤ n, pi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train. | 1,600 | Print a single integer — the minimum number of actions needed to sort the railway cars. | standard output | |
PASSED | 688f230fa53ab1740149aa3b464a0f32 | train_001.jsonl | 1449677100 | An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train? | 256 megabytes | import java.util.*;
public class Main {
public static void main (String[]args)
{
int arr[]=new int[100100];
int n;
Scanner reader=new Scanner(System.in);
n=reader.nextInt ();
int x;
int ans=0;
for (int i=1;i<=n;i++)
{
x=reader.nextInt ();
arr[x]=i;
}
x=0;
int sum=0;
for (int i=1;i<=n;i++)
{
if (arr[i]<x) sum=0;
x=arr[i];
sum++;
if (ans<sum) ans=sum;
}
System.out.println(n-ans);
}
}
| Java | ["5\n4 1 2 5 3", "4\n4 1 3 2"] | 2 seconds | ["2", "2"] | NoteIn the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | Java 8 | standard input | [
"constructive algorithms",
"greedy"
] | 277948a70c75840445e1826f2b23a897 | The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train. The second line contains n integers pi (1 ≤ pi ≤ n, pi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train. | 1,600 | Print a single integer — the minimum number of actions needed to sort the railway cars. | standard output | |
PASSED | af9c587a3cab183db86eccf5c2454979 | train_001.jsonl | 1449677100 | An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train? | 256 megabytes | import java.util.*;
import java.io.*;
public class Q1 {
static ArrayList<Integer> adj[];
static char color[];
static TreeSet<Integer> ts[];
static boolean b[],visited[],possible;
static Map<Integer,HashSet<Integer>> s;
static int totalnodes,colored;
static int count[];
static long sum[];
public static void main(String[] args) throws IOException {
in=new InputReader(System.in);
w=new PrintWriter(System.out);
int n=in.nextInt();
int arr[]=new int[1000001];
int max=0;
for(int i=0;i<n;i++)
{
int a=in.nextInt();
arr[a]=arr[a-1]+1;
max=Math.max(max,arr[a]);
}
w.println(n-max);
w.close();
}
static InputReader in;
static PrintWriter w;
public static void seive(long n){
b = new boolean[(int) (n+1)];
Arrays.fill(b, true);
for(int i = 2;i*i<=n;i++){
if(b[i]){
sum[i]=count[i];
// System.out.println(sum[i]+" wf");
for(int p = 2*i;p<=n;p+=i){
b[p] = false;
sum[i]+=count[p];
//System.out.println(sum[i]);
}
}
}
}
static class Pair implements Comparable<Pair> {
int w;
int inc;
int index;
public Pair(){
}
public Pair(int u, int v,int index) {
this.w = u;
this.inc= v;
this.index=index;
}
public int compareTo(Pair other) {
if(this.w==other.w)
{
if(this.inc==other.inc)return 0;
return (this.inc<other.inc)?1:-1;
}
return (this.w<other.w)?-1:1;
}
/*public String toString() {
return "[u=" + u + ", v=" + v + "]";
}*/
}
static class Node2{
Node2 left = null;
Node2 right = null;
Node2 parent = null;
int data;
}
//binaryStree
static class BinarySearchTree{
Node2 root = null;
int height = 0;
int max = 0;
int cnt = 1;
ArrayList<Integer> parent = new ArrayList<>();
HashMap<Integer, Integer> hm = new HashMap<>();
public void insert(int x){
Node2 n = new Node2();
n.data = x;
if(root==null){
root = n;
}
else{
Node2 temp = root,temp2 = null;
while(temp!=null){
temp2 = temp;
if(x>temp.data) temp = temp.right;
else temp = temp.left;
}
if(x>temp2.data) temp2.right = n;
else temp2.left = n;
n.parent = temp2;
parent.add(temp2.data);
}
}
public Node2 getSomething(int x, int y, Node2 n){
if(n.data==x || n.data==y) return n;
else if(n.data>x && n.data<y) return n;
else if(n.data<x && n.data<y) return getSomething(x,y,n.right);
else return getSomething(x,y,n.left);
}
public Node2 search(int x,Node2 n){
if(x==n.data){
max = Math.max(max, n.data);
return n;
}
if(x>n.data){
max = Math.max(max, n.data);
return search(x,n.right);
}
else{
max = Math.max(max, n.data);
return search(x,n.left);
}
}
public int getHeight(Node2 n){
if(n==null) return 0;
height = 1+ Math.max(getHeight(n.left), getHeight(n.right));
return height;
}
}
public static void debug(Object... o) {
System.out.println(Arrays.deepToString(o));
}
public static String rev(String s)
{
StringBuilder sb=new StringBuilder(s);
sb.reverse();
return sb.toString();
}
static long lcm(long a, long b)
{
return a * (b / gcd(a, b));
}
static long gcd(long a, long b)
{
while (b > 0)
{
long temp = b;
b = a % b; // % is remainder
a = temp;
}
return a;
}
public static long max(long x, long y, long z){
if(x>=y && x>=z) return x;
if(y>=x && y>=z) return y;
return z;
}
static int[] sieve(int n,int[] arr)
{
for(int i=2;i*i<=n;i++)
{
if(arr[i]==0)
{
for(int j=i*2;j<=n;j+=i)
arr[j]=1;
}
}
return arr;
}
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
return nextIntArray(n, 0);
}
public int[] nextIntArray(int n, int off) {
int[] arr = new int[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
return nextLongArray(n, 0);
}
public long[] nextLongArray(int n, int off) {
long[] arr = new long[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextLong();
}
return arr;
}
public String readString() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| Java | ["5\n4 1 2 5 3", "4\n4 1 3 2"] | 2 seconds | ["2", "2"] | NoteIn the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | Java 8 | standard input | [
"constructive algorithms",
"greedy"
] | 277948a70c75840445e1826f2b23a897 | The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train. The second line contains n integers pi (1 ≤ pi ≤ n, pi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train. | 1,600 | Print a single integer — the minimum number of actions needed to sort the railway cars. | standard output | |
PASSED | 90e7c08474920fe1029bb530b6c58eeb | train_001.jsonl | 1449677100 | An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train? | 256 megabytes | import java.io.File;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;
public class p009 {
public static void main(String args[]) throws Exception {
StringTokenizer stok = new StringTokenizer(new Scanner(System.in).useDelimiter("\\A").next());
StringBuilder sb = new StringBuilder();
int n = Integer.parseInt(stok.nextToken());
int[] a = new int[n+2];
for(int i=0;i<n;i++) a[Integer.parseInt(stok.nextToken())]=i;
int[] dp = new int[n+2];
for(int i=1;i<=n;i++) dp[i]=1;
for(int i=n-1;i>0;i--) if(a[i]<a[i+1]) dp[i]+=dp[i+1];
System.out.println(n-Arrays.stream(dp).max().getAsInt());
}
} | Java | ["5\n4 1 2 5 3", "4\n4 1 3 2"] | 2 seconds | ["2", "2"] | NoteIn the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | Java 8 | standard input | [
"constructive algorithms",
"greedy"
] | 277948a70c75840445e1826f2b23a897 | The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train. The second line contains n integers pi (1 ≤ pi ≤ n, pi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train. | 1,600 | Print a single integer — the minimum number of actions needed to sort the railway cars. | standard output | |
PASSED | 702102f864f028f9eb99d38059027f1b | train_001.jsonl | 1449677100 | An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train? | 256 megabytes |
import java.util.*;
import java.io.*;
public class sortlis
{
static class InputReader {
private InputStream stream;
private byte[] inbuf = new byte[1024];
private int start= 0;
private int end = 0;
public InputReader(InputStream stream) {
this.stream = stream;
}
private int readByte() {
if (start == -1) throw new UnknownError();
if (end >= start) {
end= 0;
try {
start= stream.read(inbuf);
} catch (IOException e) {
throw new UnknownError();
}
if (start<= 0) return -1;
}
return inbuf[end++];
}
private boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b)) ;
return b;
}
public String next() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public int nextInt() {
int num = 0, b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
public long nextLong() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
}
public static void main(String args[])
{
InputReader sc=new InputReader(System.in);
int n=sc.nextInt();
int a[]=new int[n+1];
int b[]=new int[n+1];
for(int i=1;i<=n;i++)
{
a[i]=sc.nextInt();
b[a[i]]=i;
}
int l=1,best=1;
int prev=b[1];
for(int i=2;i<=n;i++)
{
if(b[i]>prev)
{
l++;
}
else
{
best=Math.max(best,l);
l=1;
}
prev=b[i];
}
best=Math.max(best,l);
System.out.println(n-best);
}
}
| Java | ["5\n4 1 2 5 3", "4\n4 1 3 2"] | 2 seconds | ["2", "2"] | NoteIn the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | Java 8 | standard input | [
"constructive algorithms",
"greedy"
] | 277948a70c75840445e1826f2b23a897 | The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train. The second line contains n integers pi (1 ≤ pi ≤ n, pi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train. | 1,600 | Print a single integer — the minimum number of actions needed to sort the railway cars. | standard output | |
PASSED | 3dcd94fcf8e350a154b57e5d660373ba | train_001.jsonl | 1449677100 | An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train? | 256 megabytes | import java.util.Map.Entry;
import java.util.Scanner;
import java.util.TreeMap;
public class CF_605A {
public static int[] arr;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
arr = new int[n];
TreeMap<Integer,Integer> tm = new TreeMap<>();
int maxChain = 0;
int[] chainLengths = new int[n];
for(int i=0;i<n;i++) {
arr[i] = scan.nextInt()-1;
if(arr[i] == 0) {
chainLengths[arr[i]] = 1;
} else {
chainLengths[arr[i]] = chainLengths[arr[i]-1] + 1;
}
if(chainLengths[arr[i]] > maxChain)
maxChain = chainLengths[arr[i]];
}
System.out.println(n-maxChain);
}
} | Java | ["5\n4 1 2 5 3", "4\n4 1 3 2"] | 2 seconds | ["2", "2"] | NoteIn the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | Java 8 | standard input | [
"constructive algorithms",
"greedy"
] | 277948a70c75840445e1826f2b23a897 | The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train. The second line contains n integers pi (1 ≤ pi ≤ n, pi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train. | 1,600 | Print a single integer — the minimum number of actions needed to sort the railway cars. | standard output | |
PASSED | 8fa113eb8214d96f81a820060a1cfa1e | train_001.jsonl | 1449677100 | An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train? | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class SortingRailwayCars
{
static int[] arr;
public static void main(String[] args)
{
MyScanner sc = new MyScanner();
int n = sc.nextInt();
arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = sc.nextInt();
int min = calcM();
System.out.println(min);
}
public static int calcM()
{
int cnt = 1;
int[] foo = new int[arr.length];
for (int i = 0; i < arr.length; i++)
{
foo[arr[i]-1] = i;
}
for (int i = 1; i < foo.length; i++)
{
int min = 1;
while(i < foo.length && foo[i] > foo[i-1])
{
min++;
i++;
}
cnt = Math.max(min, cnt);
}
return arr.length - cnt;
}
static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner(){
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["5\n4 1 2 5 3", "4\n4 1 3 2"] | 2 seconds | ["2", "2"] | NoteIn the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | Java 8 | standard input | [
"constructive algorithms",
"greedy"
] | 277948a70c75840445e1826f2b23a897 | The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train. The second line contains n integers pi (1 ≤ pi ≤ n, pi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train. | 1,600 | Print a single integer — the minimum number of actions needed to sort the railway cars. | standard output | |
PASSED | 227801793137cb75862790b1458e839f | train_001.jsonl | 1449677100 | An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train? | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author neuivn
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
static class TaskA {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int N = in.nextInt();
int[] elem = new int[N];
int[] pos = new int[N];
for (int i = 0; i < N; ++i) {
elem[i] = in.nextInt() - 1;
pos[elem[i]] = i;
}
int temp = 1;
int ret = 0;
for (int i = 1; i < N; ++i) {
if (pos[i] > pos[i - 1]) {
++temp;
} else {
ret = Math.max(ret, temp);
temp = 1;
}
}
ret = Math.max(ret, temp);
out.println((N - ret));
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["5\n4 1 2 5 3", "4\n4 1 3 2"] | 2 seconds | ["2", "2"] | NoteIn the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | Java 8 | standard input | [
"constructive algorithms",
"greedy"
] | 277948a70c75840445e1826f2b23a897 | The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train. The second line contains n integers pi (1 ≤ pi ≤ n, pi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train. | 1,600 | Print a single integer — the minimum number of actions needed to sort the railway cars. | standard output | |
PASSED | 913c04192709e5a858a78266749312cd | train_001.jsonl | 1449677100 | An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train? | 256 megabytes | import java.util.*;
import java.io.*;
public class A
{
public static void main(String ar[]) throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
String s1[]=br.readLine().split(" ");
int a[]=new int[n];
int b[]=new int[n+1];
int max=0;
for(int i=0;i<n;i++)
{
a[i]=Integer.parseInt(s1[i]);
b[a[i]]=b[a[i]-1]+1;
max=Math.max(max,b[a[i]]);
}
System.out.println(n-max);
}
} | Java | ["5\n4 1 2 5 3", "4\n4 1 3 2"] | 2 seconds | ["2", "2"] | NoteIn the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | Java 8 | standard input | [
"constructive algorithms",
"greedy"
] | 277948a70c75840445e1826f2b23a897 | The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train. The second line contains n integers pi (1 ≤ pi ≤ n, pi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train. | 1,600 | Print a single integer — the minimum number of actions needed to sort the railway cars. | standard output | |
PASSED | 38eb7b5f4bebc8f946cfb2a8c762997f | train_001.jsonl | 1449677100 | An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train? | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class A {
public static void main(String[] args) throws Exception {
new A().run();
out.close();
}
void run() throws Exception {
int n = nextInt();
int[] arr = nextIntArray(n);
int[] maxLen = new int[n + 1];
int maxV = -1;
for (int i = 0; i < n; ++i) {
maxLen[arr[i]] = maxLen[arr[i] - 1] + 1;
maxV = Math.max(maxV, maxLen[arr[i]]);
}
System.out.println(n - maxV);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~ template ~~~~~~~~~~~~~~~~~~~~~~~~~~~
int[] nextIntArray(int n) throws IOException {
int[] res = new int[n];
for (int i = 0; i < n; ++i) {
res[i] = nextInt();
}
return res;
}
int[][] nextIntArray(int nx, int ny) throws IOException {
int[][] res = new int[nx][ny];
for (int i = 0; i < nx; ++i) {
for (int j = 0; j < ny; ++j) {
res[i][j] = nextInt();
}
}
return res;
}
long[] nextLongArray(int n) throws IOException {
long[] res = new long[n];
for (int i = 0; i < n; ++i) {
res[i] = nextLong();
}
return res;
}
long[][] nextLongArray(int nx, int ny) throws IOException {
long[][] res = new long[nx][ny];
for (int i = 0; i < nx; ++i) {
for (int j = 0; j < ny; ++j) {
res[i][j] = nextLong();
}
}
return res;
}
void fill(int[][] arr, int val) {
for (int i = 0; i < arr.length; ++i) {
Arrays.fill(arr[i], val);
}
}
void fill(int[][][] arr, int val) {
for (int i = 0; i < arr.length; ++i) {
fill(arr[i], val);
}
}
int[][] newIntArray(int nx, int ny, int val) {
int[][] res = new int[nx][ny];
fill(res, val);
return res;
}
int[][][] newIntArray(int nx, int ny, int nz, int val) {
int[][][] res = new int[nx][ny][nz];
fill(res, val);
return res;
}
long[][] newLongArray(int nx, int ny, long val) {
long[][] res = new long[nx][ny];
for (int i = 0; i < nx; ++i) {
Arrays.fill(res[i], val);
}
return res;
}
long[][][] newLongArray(int nx, int ny, int nz, long val) {
long[][][] res = new long[nx][ny][nz];
for (int i = 0; i < nx; ++i) {
for (int j = 0; j < ny; ++j) {
Arrays.fill(res[i][j], val);
}
}
return res;
}
<T> ArrayList<T>[] newArrayListArray(int sz) {
ArrayList<T>[] res = new ArrayList[sz];
for (int i = 0; i < sz; ++i) {
res[i] = new ArrayList<>();
}
return res;
}
String nextToken() throws IOException {
while (strTok == null || !strTok.hasMoreTokens()) {
String line = in.readLine();
if (line == null) {
return null;
}
strTok = new StringTokenizer(line);
}
return strTok.nextToken();
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
static StringTokenizer strTok;
final static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
final static PrintWriter out = new PrintWriter(System.out);
}
| Java | ["5\n4 1 2 5 3", "4\n4 1 3 2"] | 2 seconds | ["2", "2"] | NoteIn the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | Java 8 | standard input | [
"constructive algorithms",
"greedy"
] | 277948a70c75840445e1826f2b23a897 | The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train. The second line contains n integers pi (1 ≤ pi ≤ n, pi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train. | 1,600 | Print a single integer — the minimum number of actions needed to sort the railway cars. | standard output | |
PASSED | 11aa8fc0935f715e18c8e3b62562a08b | train_001.jsonl | 1449677100 | An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train? | 256 megabytes | import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class CF605A {
public static void main(String []args) {
MyScanner in = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
//Start your solution from here.
int n = in.nextInt();
int [] ar = new int[n+1];
int ans = 0;
for(int i=1;i<=n;++i){
int d = in.nextInt();
ar[d] = ar[d-1] + 1;
ans = Math.max(ans, ar[d]);
}
out.println(n - ans);
//End your solution here.
out.close();
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
char nextChar() {
return next().charAt(0);
}
int [] nextIntAr(int [] ar){
for(int i=0;i<ar.length;++i){
ar[i] = Integer.parseInt(next());
}
return ar;
}
long [] nextLongAr(long [] ar){
for(int i=0;i<ar.length;++i){
ar[i] = Long.parseLong(next());
}
return ar;
}
double [] nextDoubleAr(double [] ar){
for(int i=0;i<ar.length;++i){
ar[i] = Double.parseDouble(next());
}
return ar;
}
String [] nextStringAr(String [] ar){
for(int i=0;i<ar.length;++i){
ar[i] = next();
}
return ar;
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["5\n4 1 2 5 3", "4\n4 1 3 2"] | 2 seconds | ["2", "2"] | NoteIn the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | Java 8 | standard input | [
"constructive algorithms",
"greedy"
] | 277948a70c75840445e1826f2b23a897 | The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train. The second line contains n integers pi (1 ≤ pi ≤ n, pi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train. | 1,600 | Print a single integer — the minimum number of actions needed to sort the railway cars. | standard output | |
PASSED | 79861744178ac198ee4abfb9af622761 | train_001.jsonl | 1449677100 | An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train? | 256 megabytes | //package codeforces.cfr335div1;
import java.io.*;
import java.util.StringTokenizer;
import java.util.function.Function;
/**
* Created by raggzy on 4/1/2016.
*/
public class A {
private static class Reader implements Closeable {
private BufferedReader br;
private StringTokenizer st;
public Reader(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
}
public void readLine() {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public String next() {
if (st == null || !st.hasMoreTokens()) {
readLine();
}
return st.nextToken();
}
public <T> T next(Function<String, T> parser) {
return parser.apply(next());
}
public int nextInt() {
return next(Integer::valueOf);
}
public long nextLong() {
return next(Long::valueOf);
}
public double nexDouble() {
return next(Double::valueOf);
}
@Override
public void close() throws IOException {
br.close();
}
}
public static void main(String[] args) {
Reader in = new Reader(System.in);
int N = in.nextInt();
int[] idx = new int[N + 1];
for (int i = 0; i < N; i++) {
idx[in.nextInt()] = i;
}
int connected = 1;
int maxConnected = 1;
for (int i = 2; i <= N; i++) {
connected = idx[i] > idx[i - 1] ? connected + 1 : 1;
maxConnected = Math.max(connected, maxConnected);
}
System.out.println(N - maxConnected);
}
}
| Java | ["5\n4 1 2 5 3", "4\n4 1 3 2"] | 2 seconds | ["2", "2"] | NoteIn the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | Java 8 | standard input | [
"constructive algorithms",
"greedy"
] | 277948a70c75840445e1826f2b23a897 | The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train. The second line contains n integers pi (1 ≤ pi ≤ n, pi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train. | 1,600 | Print a single integer — the minimum number of actions needed to sort the railway cars. | standard output | |
PASSED | fba56939f2c93277fcdb8f76c22e151a | train_001.jsonl | 1449677100 | An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train? | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.Closeable;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) throws Exception {
Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 29);
thread.start();
thread.join();
}
static class TaskAdapter implements Runnable {
@Override
public void run() {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastInput in = new FastInput(inputStream);
FastOutput out = new FastOutput(outputStream);
ASortingRailwayCars solver = new ASortingRailwayCars();
solver.solve(1, in, out);
out.close();
}
}
static class ASortingRailwayCars {
public void solve(int testNumber, FastInput in, FastOutput out) {
int n = in.readInt();
int[] p = new int[n];
int[] inv = new int[n];
for (int i = 0; i < n; i++) {
p[i] = in.readInt() - 1;
inv[p[i]] = i;
}
int ans = 1;
int last = 1;
for (int i = 1; i < n; i++) {
if (inv[i - 1] > inv[i]) {
last = 1;
} else {
last++;
}
ans = Math.max(ans, last);
}
out.println(n - ans);
}
}
static class FastInput {
private final InputStream is;
private byte[] buf = new byte[1 << 13];
private int bufLen;
private int bufOffset;
private int next;
public FastInput(InputStream is) {
this.is = is;
}
private int read() {
while (bufLen == bufOffset) {
bufOffset = 0;
try {
bufLen = is.read(buf);
} catch (IOException e) {
bufLen = -1;
}
if (bufLen == -1) {
return -1;
}
}
return buf[bufOffset++];
}
public void skipBlank() {
while (next >= 0 && next <= 32) {
next = read();
}
}
public int readInt() {
int sign = 1;
skipBlank();
if (next == '+' || next == '-') {
sign = next == '+' ? 1 : -1;
next = read();
}
int val = 0;
if (sign == 1) {
while (next >= '0' && next <= '9') {
val = val * 10 + next - '0';
next = read();
}
} else {
while (next >= '0' && next <= '9') {
val = val * 10 - next + '0';
next = read();
}
}
return val;
}
}
static class FastOutput implements AutoCloseable, Closeable, Appendable {
private static final int THRESHOLD = 1 << 13;
private final Writer os;
private StringBuilder cache = new StringBuilder(THRESHOLD * 2);
public FastOutput append(CharSequence csq) {
cache.append(csq);
return this;
}
public FastOutput append(CharSequence csq, int start, int end) {
cache.append(csq, start, end);
return this;
}
private void afterWrite() {
if (cache.length() < THRESHOLD) {
return;
}
flush();
}
public FastOutput(Writer os) {
this.os = os;
}
public FastOutput(OutputStream os) {
this(new OutputStreamWriter(os));
}
public FastOutput append(char c) {
cache.append(c);
afterWrite();
return this;
}
public FastOutput append(int c) {
cache.append(c);
afterWrite();
return this;
}
public FastOutput append(String c) {
cache.append(c);
afterWrite();
return this;
}
public FastOutput println(int c) {
return append(c).println();
}
public FastOutput println() {
return append(System.lineSeparator());
}
public FastOutput flush() {
try {
os.append(cache);
os.flush();
cache.setLength(0);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return this;
}
public void close() {
flush();
try {
os.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public String toString() {
return cache.toString();
}
}
}
| Java | ["5\n4 1 2 5 3", "4\n4 1 3 2"] | 2 seconds | ["2", "2"] | NoteIn the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | Java 8 | standard input | [
"constructive algorithms",
"greedy"
] | 277948a70c75840445e1826f2b23a897 | The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train. The second line contains n integers pi (1 ≤ pi ≤ n, pi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train. | 1,600 | Print a single integer — the minimum number of actions needed to sort the railway cars. | standard output | |
PASSED | e885a36ca168f63d591f861646046e68 | train_001.jsonl | 1449677100 | An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train? | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
static class TaskA {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.ni();
int[] arr = new int[n + 1];
for (int i = 1; i <= n; ++i)
arr[in.ni()] = i;
int max = 0;
int count = 1;
for (int i = 2; i <= n; ++i) {
if (arr[i] > arr[i - 1])
++count;
else {
max = Math.max(max, count);
count = 1;
}
}
max = Math.max(max, count);
out.println(n - max);
}
}
static class InputReader {
BufferedReader br;
StringTokenizer st;
public InputReader(InputStream inputStream) {
br = new BufferedReader(new InputStreamReader(inputStream));
}
public String n() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
throw new RuntimeException();
}
}
return st.nextToken();
}
public int ni() {
return Integer.parseInt(n());
}
}
}
| Java | ["5\n4 1 2 5 3", "4\n4 1 3 2"] | 2 seconds | ["2", "2"] | NoteIn the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | Java 8 | standard input | [
"constructive algorithms",
"greedy"
] | 277948a70c75840445e1826f2b23a897 | The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train. The second line contains n integers pi (1 ≤ pi ≤ n, pi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train. | 1,600 | Print a single integer — the minimum number of actions needed to sort the railway cars. | standard output | |
PASSED | c716f28fd3dc576061ccfcfe4382fe91 | train_001.jsonl | 1449677100 | An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train? | 256 megabytes | import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class Main {
FastScanner in;
PrintWriter out;
static final String FILE = "";
public void solve() {
int n = in.nextInt();
ArrayList<Integer> p = readIntList(n);
int pos[] = new int[n + 1];
for (int i = 0; i < n; i++) {
pos[p.get(i)] = i;
}
int curr = 1, maxi = 0;
for (int i = 2; i <= n; i++) {
if (pos[i] > pos[i - 1]) {
curr++;
} else {
maxi = max(maxi, curr);
curr = 1;
}
}
maxi = max(maxi, curr);
out.print(n - maxi);
}
public void run() {
if (FILE.equals("")) {
in = new FastScanner(System.in);
out = new PrintWriter(System.out);
} else {
try {
in = new FastScanner(new FileInputStream(FILE + ".in"));
out = new PrintWriter(new FileOutputStream(FILE + ".out"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
solve();
out.close();
}
public static void main(String[] args) {
(new Main()).run();
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public String nextLine() {
st = null;
try {
return br.readLine();
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
public ArrayList<Integer> readIntList(int n) {
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(in.nextInt());
return list;
}
public ArrayList<String> readStringList(int n) {
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(in.next());
return list;
}
class Pair<A extends Comparable<A>, B extends Comparable<B>> implements Comparable<Pair<A, B>> {
public A a;
public B b;
public Pair(A a, B b) {
this.a = a;
this.b = b;
}
@Override
public int compareTo(Pair<A, B> o) {
if (o == null || o.getClass() != getClass())
return 1;
int cmp = a.compareTo(o.a);
if (cmp == 0)
return b.compareTo(o.b);
return cmp;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair<?, ?> pair = (Pair<?, ?>) o;
if (a != null ? !a.equals(pair.a) : pair.a != null) return false;
return !(b != null ? !b.equals(pair.b) : pair.b != null);
}
}
class PairInt extends Pair<Integer, Integer> {
public PairInt(Integer u, Integer v) {
super(u, v);
}
}
}
| Java | ["5\n4 1 2 5 3", "4\n4 1 3 2"] | 2 seconds | ["2", "2"] | NoteIn the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | Java 8 | standard input | [
"constructive algorithms",
"greedy"
] | 277948a70c75840445e1826f2b23a897 | The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train. The second line contains n integers pi (1 ≤ pi ≤ n, pi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train. | 1,600 | Print a single integer — the minimum number of actions needed to sort the railway cars. | standard output | |
PASSED | c0a0142204847ba9351bb9c9f53d1be9 | train_001.jsonl | 1449677100 | An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train? | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class A605 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static int n, cur, max;
static int[] a;
static StringTokenizer st;
public static void main(String[] args) throws NumberFormatException, IOException {
n = Integer.parseInt(br.readLine());
st = new StringTokenizer(br.readLine());
a = new int[100001];
for (int i = 0; i < n; i++) {
a[Integer.parseInt(st.nextToken())] = i;
}
cur = 1;
for (int i = 2; i <= n; i++) {
if (a[i] > a[i - 1]) {
cur++;
} else {
max = Math.max(cur, max);
cur = 1;
}
}
max = Math.max(cur, max);
System.out.println(n - max);
}
}
| Java | ["5\n4 1 2 5 3", "4\n4 1 3 2"] | 2 seconds | ["2", "2"] | NoteIn the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | Java 8 | standard input | [
"constructive algorithms",
"greedy"
] | 277948a70c75840445e1826f2b23a897 | The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train. The second line contains n integers pi (1 ≤ pi ≤ n, pi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train. | 1,600 | Print a single integer — the minimum number of actions needed to sort the railway cars. | standard output | |
PASSED | edfd6f395f4f3477646ecef580d4a9a6 | train_001.jsonl | 1449677100 | An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train? | 256 megabytes | import java.util.*;
import java.io.*;
import java.lang.*;
import java.math.*;
public class A {
public static void main(String[] args) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(bf.readLine());
StringTokenizer st = new StringTokenizer(bf.readLine());
int[] a = new int[n]; for(int i=0; i<n; i++) a[i] = Integer.parseInt(st.nextToken())-1;
int[] b = new int[n];
for(int i=0; i<n; i++) b[a[i]] = i;
int max = -1;
int cur = 1;
for(int i=1; i<n; i++) {
if(b[i] > b[i-1]) cur++;
else {
if(cur > max) max=cur;
cur = 1;
}
}
if(cur>max) max=cur;
out.println((n-max));
// int n = Integer.parseInt(st.nextToken());
out.close(); System.exit(0);
}
}
| Java | ["5\n4 1 2 5 3", "4\n4 1 3 2"] | 2 seconds | ["2", "2"] | NoteIn the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | Java 8 | standard input | [
"constructive algorithms",
"greedy"
] | 277948a70c75840445e1826f2b23a897 | The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train. The second line contains n integers pi (1 ≤ pi ≤ n, pi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train. | 1,600 | Print a single integer — the minimum number of actions needed to sort the railway cars. | standard output | |
PASSED | b65d9982a9dab5ef12ecd6344304b7b0 | train_001.jsonl | 1449677100 | An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train? | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
/**
* @author Don Li
*/
public class RailwayCars {
void solve() {
int n = in.nextInt();
int[] A = new int[n];
for (int i = 0; i < n; i++) A[i] = in.nextInt();
int[] dp = new int[n + 1];
for (int i = 0; i < n; i++) {
dp[A[i]] = 1;
if (A[i] - 1 >= 0) dp[A[i]] = Math.max(dp[A[i]], dp[A[i] - 1] + 1);
}
int max = 0, idx = -1;
for (int i = 0; i < n; i++) {
if (max < dp[A[i]]) {
max = dp[A[i]];
idx = i;
}
}
int p = idx - max + 1, q = idx;
out.println(p + n - q - 1);
}
public static void main(String[] args) {
in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
out = new PrintWriter(System.out);
new RailwayCars().solve();
out.close();
}
static FastScanner in;
static PrintWriter out;
static class FastScanner {
BufferedReader in;
StringTokenizer st;
public FastScanner(BufferedReader in) {
this.in = in;
}
public String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(in.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(nextToken());
}
public long nextLong() {
return Long.parseLong(nextToken());
}
public double nextDouble() {
return Double.parseDouble(nextToken());
}
}
}
| Java | ["5\n4 1 2 5 3", "4\n4 1 3 2"] | 2 seconds | ["2", "2"] | NoteIn the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | Java 8 | standard input | [
"constructive algorithms",
"greedy"
] | 277948a70c75840445e1826f2b23a897 | The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train. The second line contains n integers pi (1 ≤ pi ≤ n, pi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train. | 1,600 | Print a single integer — the minimum number of actions needed to sort the railway cars. | standard output | |
PASSED | 664f1daa3fd13f49b9f7127aa03dc3d3 | train_001.jsonl | 1449677100 | An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train? | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class SortingRailwayCars605A2 {
public static void main(String[] args) throws IOException{
BufferedReader r=new BufferedReader(new InputStreamReader(System.in));
String a=r.readLine();
int b=Integer.parseInt(a);
int[] pos=new int[b+1];
a=r.readLine();
String[] info=a.split(" ");
int max=1;
for(int z=0; z<info.length; z++){
int current=Integer.parseInt(info[z]);
pos[current]=pos[current-1]+1;
max=Math.max(pos[current], max);
}
System.out.println(b-max);
}
}
| Java | ["5\n4 1 2 5 3", "4\n4 1 3 2"] | 2 seconds | ["2", "2"] | NoteIn the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | Java 8 | standard input | [
"constructive algorithms",
"greedy"
] | 277948a70c75840445e1826f2b23a897 | The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train. The second line contains n integers pi (1 ≤ pi ≤ n, pi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train. | 1,600 | Print a single integer — the minimum number of actions needed to sort the railway cars. | standard output | |
PASSED | f830aab026e110a2c26491487e8d7b7c | train_001.jsonl | 1449677100 | An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train? | 256 megabytes | import java.util.*;
public class A {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int elm, res = 1;
int a[] = new int[100010], cache[] = new int[100010];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
elm = a[i] - 1;
if (cache[elm] == -1)
cache[a[i]] = 1;
else
cache[a[i]] = 1 + cache[elm];
res = Math.max(res, cache[a[i]]);
}
long ans = n - res;
System.out.println(ans);
}
} | Java | ["5\n4 1 2 5 3", "4\n4 1 3 2"] | 2 seconds | ["2", "2"] | NoteIn the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | Java 8 | standard input | [
"constructive algorithms",
"greedy"
] | 277948a70c75840445e1826f2b23a897 | The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train. The second line contains n integers pi (1 ≤ pi ≤ n, pi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train. | 1,600 | Print a single integer — the minimum number of actions needed to sort the railway cars. | standard output | |
PASSED | 7b413ab4c1cac3c886b0e4a1d92a26f8 | train_001.jsonl | 1449677100 | An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train? | 256 megabytes | import java.util.Scanner;
public class main{
public static void main(String[] args) {
int[] a=new int [100005];
int n,q,maxs=-1;
Scanner reader=new Scanner(System.in);
n=reader.nextInt();
for(int i=0;i<n;i++){
q=reader.nextInt();
a[q]=a[q-1]+1;
if(a[q]>maxs) maxs=a[q];
}
System.out.println(n-maxs);
}
}
| Java | ["5\n4 1 2 5 3", "4\n4 1 3 2"] | 2 seconds | ["2", "2"] | NoteIn the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | Java 8 | standard input | [
"constructive algorithms",
"greedy"
] | 277948a70c75840445e1826f2b23a897 | The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train. The second line contains n integers pi (1 ≤ pi ≤ n, pi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train. | 1,600 | Print a single integer — the minimum number of actions needed to sort the railway cars. | standard output | |
PASSED | 75a1f59d1c1a3a5411ff8603a8d3d44a | train_001.jsonl | 1449677100 | An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train? | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static void solve(int test_number, InputReader in, PrintWriter out) {
int n = in.nextInt();
int[] pos = new int[n + 1];
for (int i = 0; i < n; i++) {
int x = in.nextInt();
pos[x] = i;
}
if (n == 1) {
out.println(0);
return;
}
int c = 1, ans = 0;
for (int i = 2; i <= n; i++) {
if (pos[i] > pos[i - 1]) c++;
else c = 1;
ans = Math.max(ans, c);
}
out.println(n - ans);
}
public static void main(String[] args) {
InputReader in = new InputReader();
PrintWriter out = new PrintWriter(System.out);
int t = 1;
for (int i = 1; i <= t; i++) {
solve(i, in, out);
}
out.close();
}
static class InputReader {
BufferedReader br;
StringTokenizer st;
public InputReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["5\n4 1 2 5 3", "4\n4 1 3 2"] | 2 seconds | ["2", "2"] | NoteIn the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | Java 8 | standard input | [
"constructive algorithms",
"greedy"
] | 277948a70c75840445e1826f2b23a897 | The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train. The second line contains n integers pi (1 ≤ pi ≤ n, pi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train. | 1,600 | Print a single integer — the minimum number of actions needed to sort the railway cars. | standard output | |
PASSED | 9825d65743f0a5aa020118c377f1ae88 | train_001.jsonl | 1449677100 | An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train? | 256 megabytes |
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.lang.*;
public class me {
public static void main(String[] args)
{
Scanner br= new Scanner(System.in);
int n=br.nextInt();
int a[]=new int[n+1];
for(int i=0;i<n;i++)
{
int x=br.nextInt();
a[x]=i;
}
int c=1;
int max=999999999;
for(int i=n-1;i>=1;i--)
{
if(a[i+1]>a[i])
c++;
else
{
max=min(max,n-c);
c=1;
}
}
max=min(max,n-c);
System.out.print(max);
}
public static long ways(int n, int r, long MOD)
{
// vector<long long> f(n + 1,1);
long f[]=new long[n+1];
Arrays.fill(f,1);
for (int i=2; i<=n;i++)
f[i]= (f[i-1]*i) % MOD;
return (f[n]*((InverseEuler(f[r], MOD) * InverseEuler(f[n-r], MOD)) % MOD)) % MOD;
}
/*THIS IS FOR THE INVERSE EULER*/
public static long InverseEuler(long n, long MOD)
{
return pow(n,MOD-2,MOD);
}
/*THIS ID THE CHECK FOR THE PRIME*/
static int isPrime(int n) {
for(int i=2;i<n;i++) {
if(n%i==0)
return 0;
}
return 1;
}
/*TO CHECK FOR THE GCD IN LONG*/
public static long gcd(long x,long y)
{
if(x%y==0)
return y;
else
return gcd(y,x%y);
}
/* CHECK FOR THE GCD IN INT*/
public static int gcd(int x,int y)
{
if(x%y==0)
return y;
else
return gcd(y,x%y);
}
/*CHECK FOR THE ABSOLUTE IN INT*/
public static int abs(int a,int b)
{
return (int)Math.abs(a-b);
}
/*CHECK FOR THE ABSOLUTE IN LONG*/
public static long abs(long a,long b)
{
return (long)Math.abs(a-b);
}
/*CHECK FOR MAX BETWEEN TWO NUMBERS IN INT*/
public static int max(int a,int b)
{
if(a>b)
return a;
else
return b;
}
/*CHECK FOR MIN BETWEEN TWO NUMBERS IN INT*/
public static int min(int a,int b)
{
if(a>b)
return b;
else
return a;
}
/*CHECK FOR MAX BETWEEN TWO NUMBERS IN LONG*/
public static long max(long a,long b)
{
if(a>b)
return a;
else
return b;
}
/*CHECK FOR MIN BETWEEN TWO NUMBERS IN LONG*/
public static long min(long a,long b)
{
if(a>b)
return b;
else
return a;
}
/*check for power of two of any num*/
static boolean isPowerOfTwo (long v) {
return (v & (v - 1)) == 0;
}
public static long pow(long n,long p,long m)
{
long result = 1;
if(p==0)
return 1;
if (p==1)
return n;
while(p!=0)
{
if(p%2==1)
result *= n;
if(result>=m)
result%=m;
p >>=1;
n*=n;
if(n>=m)
n%=m;
}
return result;
}
/*SORT GIVEN ARRAY*/
static long sort(int a[],int n)
{ int b[]=new int[n];
return mergeSort(a,b,0,n-1);}
static long mergeSort(int a[],int b[],long left,long right)
{ long c=0;if(left<right)
{ long mid=left+(right-left)/2;
c= mergeSort(a,b,left,mid);
c+=mergeSort(a,b,mid+1,right);
c+=merge(a,b,left,mid+1,right); }
return c; }
static long merge(int a[],int b[],long left,long mid,long right)
{long c=0;int i=(int)left;int j=(int)mid; int k=(int)left;
while(i<=(int)mid-1&&j<=(int)right)
{ if(a[i]<=a[j]) {b[k++]=a[i++]; }
else { b[k++]=a[j++];c+=mid-i;}}
while (i <= (int)mid - 1) b[k++] = a[i++];
while (j <= (int)right) b[k++] = a[j++];
for (i=(int)left; i <= (int)right; i++)
a[i] = b[i]; return c; }
/*REMOVE ZERO FROM GIVEN NUM*/
static int removeZero(int num) {
int ret = 0;
int ten = 1;
while (num>0) {
int dig = num % 10;
num /= 10;
if (dig>0) {
ret += dig * ten;
ten *= 10;
}
}
return ret;
}
/*CHECK FOR LCM*/
static int lcm(int x, int y)
{
int a;
a = (x > y) ? x : y; // a is greater number
while(true)
{
if(a % x == 0 && a % y == 0)
return a;
++a;
}
}
} | Java | ["5\n4 1 2 5 3", "4\n4 1 3 2"] | 2 seconds | ["2", "2"] | NoteIn the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | Java 8 | standard input | [
"constructive algorithms",
"greedy"
] | 277948a70c75840445e1826f2b23a897 | The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train. The second line contains n integers pi (1 ≤ pi ≤ n, pi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train. | 1,600 | Print a single integer — the minimum number of actions needed to sort the railway cars. | standard output | |
PASSED | b01ec5b8830bfdcce0cf81a433a24851 | train_001.jsonl | 1449677100 | An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train? | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
ProblemASortingRailwayCars solver = new ProblemASortingRailwayCars();
solver.solve(1, in, out);
out.close();
}
static class ProblemASortingRailwayCars {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.readInt();
int[] a = in.readIntArray(n);
int[] pos = new int[n + 1];
for (int i = 0; i < n; i++) pos[a[i]] = i + 1;
int cnt = 0, last = 0, max = 0;
for (int i = 1; i <= n; i++) {
if (pos[i] < last) {
max = Math.max(cnt, max);
cnt = 0;
}
cnt++;
last = pos[i];
}
max = Math.max(cnt, max);
out.println(n - max);
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public int[] readIntArray(int size) {
int[] ans = new int[size];
for (int i = 0; i < size; i++) ans[i] = readInt();
return ans;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| Java | ["5\n4 1 2 5 3", "4\n4 1 3 2"] | 2 seconds | ["2", "2"] | NoteIn the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | Java 8 | standard input | [
"constructive algorithms",
"greedy"
] | 277948a70c75840445e1826f2b23a897 | The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train. The second line contains n integers pi (1 ≤ pi ≤ n, pi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train. | 1,600 | Print a single integer — the minimum number of actions needed to sort the railway cars. | standard output | |
PASSED | 7bf57cf6b7dd6b7696a79f047fc5b759 | train_001.jsonl | 1449677100 | An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train? | 256 megabytes | import java.io.*;
import java.util.*;
public class R335qB {
public static void main(String args[]) {
InputReader in = new InputReader(System.in);
PrintWriter w = new PrintWriter(System.out);
int n = in.nextInt();
int p[] = new int[n + 1];
int r[] = new int[n + 1];
for (int i = 1; i <= n; i++){
p[i] = in.nextInt();
r[p[i]] = i;
}
int max[] = new int[n + 1];
max[n] = 1;
int ans = n - 1;
for(int i= n - 1; i >= 1; i--){
max[i] = 1;
if(r[i+1] > r[i])
max[i] += max[i+1];
ans = Math.min(ans, n - max[i]);
}
w.println(ans);
w.close();
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[8192];
private int curChar;
private int snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = snext();
while (isSpaceChar(c))
c = snext();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = snext();
while (isSpaceChar(c))
c = snext();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| Java | ["5\n4 1 2 5 3", "4\n4 1 3 2"] | 2 seconds | ["2", "2"] | NoteIn the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | Java 8 | standard input | [
"constructive algorithms",
"greedy"
] | 277948a70c75840445e1826f2b23a897 | The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train. The second line contains n integers pi (1 ≤ pi ≤ n, pi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train. | 1,600 | Print a single integer — the minimum number of actions needed to sort the railway cars. | standard output | |
PASSED | 09ba3da758cdd93bf543bc0e0d1390f8 | train_001.jsonl | 1449677100 | An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train? | 256 megabytes | import java.util.ArrayList;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.StringTokenizer;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Reader in = new Reader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
}
class TaskA {
public void solve(int testNumber, Reader in, PrintWriter out) {
int n = in.nextInt();
int[] p = in.nextIntArray(n, 1);
int[] op = new int[n + 1];
for (int i = 1; i <= n; i++) op[p[i]] = i;
int[] q = new int[n + 1];
q[n] = 0;
for (int i = n - 1; i >= 1; i--) {
if (op[i] < op[i + 1]) q[i] = q[i + 1] + 1; else q[i] = 0;
}
int ans = n;
for (int i = 1; i <= n; i++) {
int q1 = p[i];
int q2 = p[i] + q[p[i]];
ans = Math.min(ans, q1 - 1 + n - q2);
}
out.println(ans);
}
}
class Reader {
private BufferedReader in;
private StringTokenizer st;
public Reader(InputStream is) {
in = new BufferedReader(new InputStreamReader(is));
}
public String next() {
try {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public int nextInt() {
return Integer.parseInt(next());
}
public int[] nextIntArray(int n, int offset) {
int[] res = new int[n + offset];
for (int i = 0; i < n; i++) res[i + offset] = nextInt();
return res;
}
}
| Java | ["5\n4 1 2 5 3", "4\n4 1 3 2"] | 2 seconds | ["2", "2"] | NoteIn the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | Java 8 | standard input | [
"constructive algorithms",
"greedy"
] | 277948a70c75840445e1826f2b23a897 | The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train. The second line contains n integers pi (1 ≤ pi ≤ n, pi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train. | 1,600 | Print a single integer — the minimum number of actions needed to sort the railway cars. | standard output | |
PASSED | 9edf801837ae93f53e8b5a8f9f2b5ffd | train_001.jsonl | 1449677100 | An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train? | 256 megabytes |
import java.util.*;
import java.io.*;
public class marte{
private int contor=0;
public static void main (String args[]) throws Exception{
Scanner input=new Scanner(System.in);
HashMap<Integer, Integer> map= new HashMap<Integer, Integer>();
HashMap<String, Integer> map2= new HashMap<String, Integer>();
HashMap<Integer, Integer> finish= new HashMap<Integer, Integer>();
HashMap<Integer, Integer> maxime= new HashMap<Integer, Integer>();
int n=input.nextInt();
int v[]=new int[n];
int contor=0;
for(int i=0;i<n;i++) {
v[i]=input.nextInt();
if(v[i]==1) map.put(1,1);
else {if (v[i]!=1 && map.containsKey(v[i]-1))
map.put(v[i],map.get(v[i]-1)+1);
else map.put(v[i],1);
}
if(map.get(v[i])>contor) contor=map.get(v[i]);
}
int result=n-contor;
System.out.println(result);
}
}
| Java | ["5\n4 1 2 5 3", "4\n4 1 3 2"] | 2 seconds | ["2", "2"] | NoteIn the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | Java 8 | standard input | [
"constructive algorithms",
"greedy"
] | 277948a70c75840445e1826f2b23a897 | The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train. The second line contains n integers pi (1 ≤ pi ≤ n, pi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train. | 1,600 | Print a single integer — the minimum number of actions needed to sort the railway cars. | standard output | |
PASSED | 4b5239901a8c8f243d3e3c37e0fd518d | train_001.jsonl | 1449677100 | An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train? | 256 megabytes | import java.util.*;
import java.io.*;
import java.awt.geom.*;
import java.math.*;
public class CF605A {
static final Scanner in = new Scanner(System.in);
static final PrintWriter out = new PrintWriter(System.out,false);
static void solve() {
int n = in.nextInt();
int[] p = new int[n];
int[] pos = new int[n];
for (int i=0; i<n; i++) {
p[i] = in.nextInt() - 1;
pos[p[i]] = i;
}
int[] dp = new int[n];
for (int i=0; i<n; i++) {
dp[i] = i;
}
for (int i=n-2; i>=0; i--) {
if (pos[i] < pos[i+1]) dp[i] = dp[i+1];
}
int ans = n;
for (int i=0; i<n; i++) {
ans = Math.min(ans, n-(dp[i]-i+1));
}
out.println(ans);
}
public static void main(String[] args) {
long start = System.currentTimeMillis();
solve();
out.flush();
long end = System.currentTimeMillis();
//trace(end-start + "ms");
in.close();
out.close();
}
static void trace(Object... o) { System.out.println(Arrays.deepToString(o));}
} | Java | ["5\n4 1 2 5 3", "4\n4 1 3 2"] | 2 seconds | ["2", "2"] | NoteIn the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train. | Java 8 | standard input | [
"constructive algorithms",
"greedy"
] | 277948a70c75840445e1826f2b23a897 | The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train. The second line contains n integers pi (1 ≤ pi ≤ n, pi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train. | 1,600 | Print a single integer — the minimum number of actions needed to sort the railway cars. | standard output | |
PASSED | a1ae1e2941828137202d787de81fa3a6 | train_001.jsonl | 1584628500 | You are given a integer $$$n$$$ ($$$n > 0$$$). Find any integer $$$s$$$ which satisfies these conditions, or report that there are no such numbers:In the decimal representation of $$$s$$$: $$$s > 0$$$, $$$s$$$ consists of $$$n$$$ digits, no digit in $$$s$$$ equals $$$0$$$, $$$s$$$ is not divisible by any of it's digits. | 256 megabytes | //package com.company;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int t =input.nextInt();
for (int i =0;i<t;i++){
int n =input.nextInt();
if (n<2){
System.out.println(-1);
}else{
System.out.print(2);
for(int j =0;j<n-2;j++){
System.out.print(3);
}
System.out.println(3);
}
}
}
}
| Java | ["4\n1\n2\n3\n4"] | 1 second | ["-1\n57\n239\n6789"] | NoteIn the first test case, there are no possible solutions for $$$s$$$ consisting of one digit, because any such solution is divisible by itself.For the second test case, the possible solutions are: $$$23$$$, $$$27$$$, $$$29$$$, $$$34$$$, $$$37$$$, $$$38$$$, $$$43$$$, $$$46$$$, $$$47$$$, $$$49$$$, $$$53$$$, $$$54$$$, $$$56$$$, $$$57$$$, $$$58$$$, $$$59$$$, $$$67$$$, $$$68$$$, $$$69$$$, $$$73$$$, $$$74$$$, $$$76$$$, $$$78$$$, $$$79$$$, $$$83$$$, $$$86$$$, $$$87$$$, $$$89$$$, $$$94$$$, $$$97$$$, and $$$98$$$.For the third test case, one possible solution is $$$239$$$ because $$$239$$$ is not divisible by $$$2$$$, $$$3$$$ or $$$9$$$ and has three digits (none of which equals zero). | Java 11 | standard input | [
"constructive algorithms",
"number theory"
] | 43996d7e052aa628a46d03086f9c5436 | The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 400$$$), the number of test cases. The next $$$t$$$ lines each describe a test case. Each test case contains one positive integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$. | 1,000 | For each test case, print an integer $$$s$$$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $$$s$$$, print any solution. | standard output | |
PASSED | b28ef64dcf34fe01bc68c6e895f9a111 | train_001.jsonl | 1584628500 | You are given a integer $$$n$$$ ($$$n > 0$$$). Find any integer $$$s$$$ which satisfies these conditions, or report that there are no such numbers:In the decimal representation of $$$s$$$: $$$s > 0$$$, $$$s$$$ consists of $$$n$$$ digits, no digit in $$$s$$$ equals $$$0$$$, $$$s$$$ is not divisible by any of it's digits. | 256 megabytes | import java.util.*;
public class MyClass {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t--!=0)
{
int n = sc.nextInt();
if(n==1)
System.out.println("-1");
else
{
System.out.print("2");
n--;
while(n>0)
{
System.out.print("3");
n--;
}
}
System.out.println();
}
}
} | Java | ["4\n1\n2\n3\n4"] | 1 second | ["-1\n57\n239\n6789"] | NoteIn the first test case, there are no possible solutions for $$$s$$$ consisting of one digit, because any such solution is divisible by itself.For the second test case, the possible solutions are: $$$23$$$, $$$27$$$, $$$29$$$, $$$34$$$, $$$37$$$, $$$38$$$, $$$43$$$, $$$46$$$, $$$47$$$, $$$49$$$, $$$53$$$, $$$54$$$, $$$56$$$, $$$57$$$, $$$58$$$, $$$59$$$, $$$67$$$, $$$68$$$, $$$69$$$, $$$73$$$, $$$74$$$, $$$76$$$, $$$78$$$, $$$79$$$, $$$83$$$, $$$86$$$, $$$87$$$, $$$89$$$, $$$94$$$, $$$97$$$, and $$$98$$$.For the third test case, one possible solution is $$$239$$$ because $$$239$$$ is not divisible by $$$2$$$, $$$3$$$ or $$$9$$$ and has three digits (none of which equals zero). | Java 11 | standard input | [
"constructive algorithms",
"number theory"
] | 43996d7e052aa628a46d03086f9c5436 | The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 400$$$), the number of test cases. The next $$$t$$$ lines each describe a test case. Each test case contains one positive integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$. | 1,000 | For each test case, print an integer $$$s$$$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $$$s$$$, print any solution. | standard output | |
PASSED | 82140272e38c0ae7b999fe3a18855b92 | train_001.jsonl | 1584628500 | You are given a integer $$$n$$$ ($$$n > 0$$$). Find any integer $$$s$$$ which satisfies these conditions, or report that there are no such numbers:In the decimal representation of $$$s$$$: $$$s > 0$$$, $$$s$$$ consists of $$$n$$$ digits, no digit in $$$s$$$ equals $$$0$$$, $$$s$$$ is not divisible by any of it's digits. | 256 megabytes | import java.util.*;
public class MyClass {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t--!=0)
{
int n = sc.nextInt();
if(n==1)
System.out.println("-1");
else
{
char[] c = new char[n];
Arrays.fill(c,'3');
c[0] = '2';
System.out.println(new String(c));
}
}
}
} | Java | ["4\n1\n2\n3\n4"] | 1 second | ["-1\n57\n239\n6789"] | NoteIn the first test case, there are no possible solutions for $$$s$$$ consisting of one digit, because any such solution is divisible by itself.For the second test case, the possible solutions are: $$$23$$$, $$$27$$$, $$$29$$$, $$$34$$$, $$$37$$$, $$$38$$$, $$$43$$$, $$$46$$$, $$$47$$$, $$$49$$$, $$$53$$$, $$$54$$$, $$$56$$$, $$$57$$$, $$$58$$$, $$$59$$$, $$$67$$$, $$$68$$$, $$$69$$$, $$$73$$$, $$$74$$$, $$$76$$$, $$$78$$$, $$$79$$$, $$$83$$$, $$$86$$$, $$$87$$$, $$$89$$$, $$$94$$$, $$$97$$$, and $$$98$$$.For the third test case, one possible solution is $$$239$$$ because $$$239$$$ is not divisible by $$$2$$$, $$$3$$$ or $$$9$$$ and has three digits (none of which equals zero). | Java 11 | standard input | [
"constructive algorithms",
"number theory"
] | 43996d7e052aa628a46d03086f9c5436 | The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 400$$$), the number of test cases. The next $$$t$$$ lines each describe a test case. Each test case contains one positive integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$. | 1,000 | For each test case, print an integer $$$s$$$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $$$s$$$, print any solution. | standard output | |
PASSED | a72b91543b237f97071c341c67a874f4 | train_001.jsonl | 1584628500 | You are given a integer $$$n$$$ ($$$n > 0$$$). Find any integer $$$s$$$ which satisfies these conditions, or report that there are no such numbers:In the decimal representation of $$$s$$$: $$$s > 0$$$, $$$s$$$ consists of $$$n$$$ digits, no digit in $$$s$$$ equals $$$0$$$, $$$s$$$ is not divisible by any of it's digits. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
for(; t >= 1; --t){
int n = scan.nextInt();
if (n == 1) {
System.out.println("-1");
continue;
}
if (--n >= 1){
System.out.print("2");
for (; n >= 1; --n)
System.out.print("3");
System.out.println("");
continue;
}
}
}
}
| Java | ["4\n1\n2\n3\n4"] | 1 second | ["-1\n57\n239\n6789"] | NoteIn the first test case, there are no possible solutions for $$$s$$$ consisting of one digit, because any such solution is divisible by itself.For the second test case, the possible solutions are: $$$23$$$, $$$27$$$, $$$29$$$, $$$34$$$, $$$37$$$, $$$38$$$, $$$43$$$, $$$46$$$, $$$47$$$, $$$49$$$, $$$53$$$, $$$54$$$, $$$56$$$, $$$57$$$, $$$58$$$, $$$59$$$, $$$67$$$, $$$68$$$, $$$69$$$, $$$73$$$, $$$74$$$, $$$76$$$, $$$78$$$, $$$79$$$, $$$83$$$, $$$86$$$, $$$87$$$, $$$89$$$, $$$94$$$, $$$97$$$, and $$$98$$$.For the third test case, one possible solution is $$$239$$$ because $$$239$$$ is not divisible by $$$2$$$, $$$3$$$ or $$$9$$$ and has three digits (none of which equals zero). | Java 11 | standard input | [
"constructive algorithms",
"number theory"
] | 43996d7e052aa628a46d03086f9c5436 | The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 400$$$), the number of test cases. The next $$$t$$$ lines each describe a test case. Each test case contains one positive integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$. | 1,000 | For each test case, print an integer $$$s$$$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $$$s$$$, print any solution. | standard output | |
PASSED | 8dadf6ce0e661c17ec68f51fc269eac3 | train_001.jsonl | 1584628500 | You are given a integer $$$n$$$ ($$$n > 0$$$). Find any integer $$$s$$$ which satisfies these conditions, or report that there are no such numbers:In the decimal representation of $$$s$$$: $$$s > 0$$$, $$$s$$$ consists of $$$n$$$ digits, no digit in $$$s$$$ equals $$$0$$$, $$$s$$$ is not divisible by any of it's digits. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
for(; t >= 1; --t){
int n = scan.nextInt();
if (n == 1) {
System.out.println("-1");
continue;
}
if (--n >= 1) {
System.out.print("2");
for (; n >= 1; --n)
System.out.print("3");
System.out.println("");
continue;
}
}
}
}
| Java | ["4\n1\n2\n3\n4"] | 1 second | ["-1\n57\n239\n6789"] | NoteIn the first test case, there are no possible solutions for $$$s$$$ consisting of one digit, because any such solution is divisible by itself.For the second test case, the possible solutions are: $$$23$$$, $$$27$$$, $$$29$$$, $$$34$$$, $$$37$$$, $$$38$$$, $$$43$$$, $$$46$$$, $$$47$$$, $$$49$$$, $$$53$$$, $$$54$$$, $$$56$$$, $$$57$$$, $$$58$$$, $$$59$$$, $$$67$$$, $$$68$$$, $$$69$$$, $$$73$$$, $$$74$$$, $$$76$$$, $$$78$$$, $$$79$$$, $$$83$$$, $$$86$$$, $$$87$$$, $$$89$$$, $$$94$$$, $$$97$$$, and $$$98$$$.For the third test case, one possible solution is $$$239$$$ because $$$239$$$ is not divisible by $$$2$$$, $$$3$$$ or $$$9$$$ and has three digits (none of which equals zero). | Java 11 | standard input | [
"constructive algorithms",
"number theory"
] | 43996d7e052aa628a46d03086f9c5436 | The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 400$$$), the number of test cases. The next $$$t$$$ lines each describe a test case. Each test case contains one positive integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$. | 1,000 | For each test case, print an integer $$$s$$$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $$$s$$$, print any solution. | standard output | |
PASSED | a66d98e93c71f4117e94bd02bcfc2565 | train_001.jsonl | 1584628500 | You are given a integer $$$n$$$ ($$$n > 0$$$). Find any integer $$$s$$$ which satisfies these conditions, or report that there are no such numbers:In the decimal representation of $$$s$$$: $$$s > 0$$$, $$$s$$$ consists of $$$n$$$ digits, no digit in $$$s$$$ equals $$$0$$$, $$$s$$$ is not divisible by any of it's digits. | 256 megabytes | import java.util.Scanner;
public class BadUgly {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int cases = scan.nextInt();
for (int i = 0; i < cases; i++) {
int needed = scan.nextInt();
String x1 = "2";
String x = "3";
x = x.repeat(needed - 1);
System.out.println(needed == 1 ? -1 : x1.concat(x));
}
}
} | Java | ["4\n1\n2\n3\n4"] | 1 second | ["-1\n57\n239\n6789"] | NoteIn the first test case, there are no possible solutions for $$$s$$$ consisting of one digit, because any such solution is divisible by itself.For the second test case, the possible solutions are: $$$23$$$, $$$27$$$, $$$29$$$, $$$34$$$, $$$37$$$, $$$38$$$, $$$43$$$, $$$46$$$, $$$47$$$, $$$49$$$, $$$53$$$, $$$54$$$, $$$56$$$, $$$57$$$, $$$58$$$, $$$59$$$, $$$67$$$, $$$68$$$, $$$69$$$, $$$73$$$, $$$74$$$, $$$76$$$, $$$78$$$, $$$79$$$, $$$83$$$, $$$86$$$, $$$87$$$, $$$89$$$, $$$94$$$, $$$97$$$, and $$$98$$$.For the third test case, one possible solution is $$$239$$$ because $$$239$$$ is not divisible by $$$2$$$, $$$3$$$ or $$$9$$$ and has three digits (none of which equals zero). | Java 11 | standard input | [
"constructive algorithms",
"number theory"
] | 43996d7e052aa628a46d03086f9c5436 | The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 400$$$), the number of test cases. The next $$$t$$$ lines each describe a test case. Each test case contains one positive integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$. | 1,000 | For each test case, print an integer $$$s$$$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $$$s$$$, print any solution. | standard output | |
PASSED | 7b55ab655b7b2ce526d6c66b7e26d027 | train_001.jsonl | 1584628500 | You are given a integer $$$n$$$ ($$$n > 0$$$). Find any integer $$$s$$$ which satisfies these conditions, or report that there are no such numbers:In the decimal representation of $$$s$$$: $$$s > 0$$$, $$$s$$$ consists of $$$n$$$ digits, no digit in $$$s$$$ equals $$$0$$$, $$$s$$$ is not divisible by any of it's digits. | 256 megabytes | import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.InputStreamReader;
public class Main {
public static void Solve(PrintWriter out,Cin in) {
int n = in.nextInt();
if(n == 1) {
out.print("-1\n");
} else {
StringBuilder strb = new StringBuilder();
strb.append("2");
for(int i = 1; i < n; i++) {
strb.append("3");
}
out.print(strb.toString()+"\n");
}
}
public static void main(String[] args) {
PrintWriter out = new PrintWriter(System.out);
Cin in = new Cin();
for(int i = in.nextInt(); i > 0; i--)
Solve(out,in);
out.close();
}
static class Cin {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while(!st.hasMoreTokens()) {
try {
st = new StringTokenizer(reader.readLine());
} catch (Exception e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {return Integer.parseInt(next());}
long nextLong() {return Long.parseLong(next());}
int[] nextArrInt(int n) {
int[] r = new int[n];
for(int i = 0; i < n; i++) r[i]=nextInt();
return r;
}
}
} | Java | ["4\n1\n2\n3\n4"] | 1 second | ["-1\n57\n239\n6789"] | NoteIn the first test case, there are no possible solutions for $$$s$$$ consisting of one digit, because any such solution is divisible by itself.For the second test case, the possible solutions are: $$$23$$$, $$$27$$$, $$$29$$$, $$$34$$$, $$$37$$$, $$$38$$$, $$$43$$$, $$$46$$$, $$$47$$$, $$$49$$$, $$$53$$$, $$$54$$$, $$$56$$$, $$$57$$$, $$$58$$$, $$$59$$$, $$$67$$$, $$$68$$$, $$$69$$$, $$$73$$$, $$$74$$$, $$$76$$$, $$$78$$$, $$$79$$$, $$$83$$$, $$$86$$$, $$$87$$$, $$$89$$$, $$$94$$$, $$$97$$$, and $$$98$$$.For the third test case, one possible solution is $$$239$$$ because $$$239$$$ is not divisible by $$$2$$$, $$$3$$$ or $$$9$$$ and has three digits (none of which equals zero). | Java 11 | standard input | [
"constructive algorithms",
"number theory"
] | 43996d7e052aa628a46d03086f9c5436 | The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 400$$$), the number of test cases. The next $$$t$$$ lines each describe a test case. Each test case contains one positive integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$. | 1,000 | For each test case, print an integer $$$s$$$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $$$s$$$, print any solution. | standard output | |
PASSED | cb12d42b0aba52bf4cec523a0567e75d | train_001.jsonl | 1584628500 | You are given a integer $$$n$$$ ($$$n > 0$$$). Find any integer $$$s$$$ which satisfies these conditions, or report that there are no such numbers:In the decimal representation of $$$s$$$: $$$s > 0$$$, $$$s$$$ consists of $$$n$$$ digits, no digit in $$$s$$$ equals $$$0$$$, $$$s$$$ is not divisible by any of it's digits. | 256 megabytes | import java.util.*;
public class HelloWorld{
public static void main(String []args){
Scanner sc = new Scanner (System.in);
int test = sc.nextInt();
sc.nextLine();
while(test>0){
int n = sc.nextInt();
if(n==1){
System.out.println(-1);
}else{
String s = "23";
String y ="3";
if (n==2){
System.out.println(s);
}else{
String ans = s+y.repeat(n-2);
System.out.println(ans);
}
}
test--;
}
}
} | Java | ["4\n1\n2\n3\n4"] | 1 second | ["-1\n57\n239\n6789"] | NoteIn the first test case, there are no possible solutions for $$$s$$$ consisting of one digit, because any such solution is divisible by itself.For the second test case, the possible solutions are: $$$23$$$, $$$27$$$, $$$29$$$, $$$34$$$, $$$37$$$, $$$38$$$, $$$43$$$, $$$46$$$, $$$47$$$, $$$49$$$, $$$53$$$, $$$54$$$, $$$56$$$, $$$57$$$, $$$58$$$, $$$59$$$, $$$67$$$, $$$68$$$, $$$69$$$, $$$73$$$, $$$74$$$, $$$76$$$, $$$78$$$, $$$79$$$, $$$83$$$, $$$86$$$, $$$87$$$, $$$89$$$, $$$94$$$, $$$97$$$, and $$$98$$$.For the third test case, one possible solution is $$$239$$$ because $$$239$$$ is not divisible by $$$2$$$, $$$3$$$ or $$$9$$$ and has three digits (none of which equals zero). | Java 11 | standard input | [
"constructive algorithms",
"number theory"
] | 43996d7e052aa628a46d03086f9c5436 | The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 400$$$), the number of test cases. The next $$$t$$$ lines each describe a test case. Each test case contains one positive integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$. | 1,000 | For each test case, print an integer $$$s$$$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $$$s$$$, print any solution. | standard output | |
PASSED | fb66cb5ceb1c1b92ddfd85bcf6264eaa | train_001.jsonl | 1584628500 | You are given a integer $$$n$$$ ($$$n > 0$$$). Find any integer $$$s$$$ which satisfies these conditions, or report that there are no such numbers:In the decimal representation of $$$s$$$: $$$s > 0$$$, $$$s$$$ consists of $$$n$$$ digits, no digit in $$$s$$$ equals $$$0$$$, $$$s$$$ is not divisible by any of it's digits. | 256 megabytes | import java.io.*;
import java.io.IOException;
public class Main
{
public static void main(String[]args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int tcs=Integer.parseInt(br.readLine());
for(int j=0;j<tcs;j++)
{
int digit=Integer.parseInt(br.readLine());
if(digit==1)
{
System.out.println(-1);
}
else
{
for(int i=0;i<digit-1;i++)
{
System.out.print(7);
}
System.out.print(8);
System.out.println();
}
}
}
} | Java | ["4\n1\n2\n3\n4"] | 1 second | ["-1\n57\n239\n6789"] | NoteIn the first test case, there are no possible solutions for $$$s$$$ consisting of one digit, because any such solution is divisible by itself.For the second test case, the possible solutions are: $$$23$$$, $$$27$$$, $$$29$$$, $$$34$$$, $$$37$$$, $$$38$$$, $$$43$$$, $$$46$$$, $$$47$$$, $$$49$$$, $$$53$$$, $$$54$$$, $$$56$$$, $$$57$$$, $$$58$$$, $$$59$$$, $$$67$$$, $$$68$$$, $$$69$$$, $$$73$$$, $$$74$$$, $$$76$$$, $$$78$$$, $$$79$$$, $$$83$$$, $$$86$$$, $$$87$$$, $$$89$$$, $$$94$$$, $$$97$$$, and $$$98$$$.For the third test case, one possible solution is $$$239$$$ because $$$239$$$ is not divisible by $$$2$$$, $$$3$$$ or $$$9$$$ and has three digits (none of which equals zero). | Java 11 | standard input | [
"constructive algorithms",
"number theory"
] | 43996d7e052aa628a46d03086f9c5436 | The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 400$$$), the number of test cases. The next $$$t$$$ lines each describe a test case. Each test case contains one positive integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$. | 1,000 | For each test case, print an integer $$$s$$$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $$$s$$$, print any solution. | standard output | |
PASSED | d0cd3e1e1339eddec0a6a6657e771638 | train_001.jsonl | 1584628500 | You are given a integer $$$n$$$ ($$$n > 0$$$). Find any integer $$$s$$$ which satisfies these conditions, or report that there are no such numbers:In the decimal representation of $$$s$$$: $$$s > 0$$$, $$$s$$$ consists of $$$n$$$ digits, no digit in $$$s$$$ equals $$$0$$$, $$$s$$$ is not divisible by any of it's digits. | 256 megabytes |
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;
import java.io.*;
import java.math.*;
public class CC
{
static class point implements Comparable<point>
{
long x,y;
point(long x,long y)
{
this.x=x;
this.y=y;
}
public int compareTo(point o)
{
return (int)(x-o.x);
}
}
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
int t=i();
while (t-->0)
{
int n=i();
String s="";
s+=2;
for(int i=0;i<n-1;i++)
{
s+=3;
}
if(n!=1)
sb.append(s+"\n");
else
sb.append(-1+"\n");
}
System.out.println(sb.toString());
}
///******************************SYSTEM PART*****************************************************************************************************************************************************************************************************************************************************////
static InputReader in=new InputReader(System.in);
static OutputWriter out=new OutputWriter(System.out);
public static long pow(long a, long b)
{
long result=1;
while(b>0){
if (b % 2 != 0){
result=(result*a);
b--;
}
a=(a*a);
b /= 2;
}
return result;
}
public static long gcd(long a, long b){
if (a == 0){
return b;
}
return gcd(b%a, a);
}
public static long lcm(long a, long b){
return a*(b/gcd(a,b));
}
public static long l(){
String s=in.String();
return Long.parseLong(s);
}
public static void pln(String value){
System.out.println(value);
}
public static int i(){
return in.Int();
}
public static String s(){
return in.String();
}
}
///******************input Reader//***************************************************************************************************************************************************************************************
class InputReader
{
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars== -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int Int() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String String() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return String();
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
}
//*******************OUTPUT WRITER********************************************************************************************************************************************************************************************************************************************************//
class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object...objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object...objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
| Java | ["4\n1\n2\n3\n4"] | 1 second | ["-1\n57\n239\n6789"] | NoteIn the first test case, there are no possible solutions for $$$s$$$ consisting of one digit, because any such solution is divisible by itself.For the second test case, the possible solutions are: $$$23$$$, $$$27$$$, $$$29$$$, $$$34$$$, $$$37$$$, $$$38$$$, $$$43$$$, $$$46$$$, $$$47$$$, $$$49$$$, $$$53$$$, $$$54$$$, $$$56$$$, $$$57$$$, $$$58$$$, $$$59$$$, $$$67$$$, $$$68$$$, $$$69$$$, $$$73$$$, $$$74$$$, $$$76$$$, $$$78$$$, $$$79$$$, $$$83$$$, $$$86$$$, $$$87$$$, $$$89$$$, $$$94$$$, $$$97$$$, and $$$98$$$.For the third test case, one possible solution is $$$239$$$ because $$$239$$$ is not divisible by $$$2$$$, $$$3$$$ or $$$9$$$ and has three digits (none of which equals zero). | Java 11 | standard input | [
"constructive algorithms",
"number theory"
] | 43996d7e052aa628a46d03086f9c5436 | The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 400$$$), the number of test cases. The next $$$t$$$ lines each describe a test case. Each test case contains one positive integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$. | 1,000 | For each test case, print an integer $$$s$$$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $$$s$$$, print any solution. | standard output | |
PASSED | 6f7c766e822a1ba833b6e1d60c992c29 | train_001.jsonl | 1584628500 | You are given a integer $$$n$$$ ($$$n > 0$$$). Find any integer $$$s$$$ which satisfies these conditions, or report that there are no such numbers:In the decimal representation of $$$s$$$: $$$s > 0$$$, $$$s$$$ consists of $$$n$$$ digits, no digit in $$$s$$$ equals $$$0$$$, $$$s$$$ is not divisible by any of it's digits. | 256 megabytes | import java.util.*;
public class Main
{
public static void main(String[]args)
{
Scanner sc=new Scanner( System.in);
int t= sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
if(n==1)
{System.out.println("-1");
}
else{
System.out.print("2");
for(int i=0;i<n-1;i++)
{
System.out.print("3");
}
System.out.println("");}
}
}
} | Java | ["4\n1\n2\n3\n4"] | 1 second | ["-1\n57\n239\n6789"] | NoteIn the first test case, there are no possible solutions for $$$s$$$ consisting of one digit, because any such solution is divisible by itself.For the second test case, the possible solutions are: $$$23$$$, $$$27$$$, $$$29$$$, $$$34$$$, $$$37$$$, $$$38$$$, $$$43$$$, $$$46$$$, $$$47$$$, $$$49$$$, $$$53$$$, $$$54$$$, $$$56$$$, $$$57$$$, $$$58$$$, $$$59$$$, $$$67$$$, $$$68$$$, $$$69$$$, $$$73$$$, $$$74$$$, $$$76$$$, $$$78$$$, $$$79$$$, $$$83$$$, $$$86$$$, $$$87$$$, $$$89$$$, $$$94$$$, $$$97$$$, and $$$98$$$.For the third test case, one possible solution is $$$239$$$ because $$$239$$$ is not divisible by $$$2$$$, $$$3$$$ or $$$9$$$ and has three digits (none of which equals zero). | Java 11 | standard input | [
"constructive algorithms",
"number theory"
] | 43996d7e052aa628a46d03086f9c5436 | The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 400$$$), the number of test cases. The next $$$t$$$ lines each describe a test case. Each test case contains one positive integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$. | 1,000 | For each test case, print an integer $$$s$$$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $$$s$$$, print any solution. | standard output | |
PASSED | f08128a04d0f05cd0ec7f89ad2a8be2c | train_001.jsonl | 1584628500 | You are given a integer $$$n$$$ ($$$n > 0$$$). Find any integer $$$s$$$ which satisfies these conditions, or report that there are no such numbers:In the decimal representation of $$$s$$$: $$$s > 0$$$, $$$s$$$ consists of $$$n$$$ digits, no digit in $$$s$$$ equals $$$0$$$, $$$s$$$ is not divisible by any of it's digits. | 256 megabytes | import java.util.*;
public class Test
{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t>0)
{
int n=sc.nextInt();
if(n==1)
{
System.out.println(-1);
}
else
{
System.out.print(2);
for(int i=1;i<n;i++)
{
System.out.print(3);
}
System.out.println();
}
t--;
}
// return -1;
}
} | Java | ["4\n1\n2\n3\n4"] | 1 second | ["-1\n57\n239\n6789"] | NoteIn the first test case, there are no possible solutions for $$$s$$$ consisting of one digit, because any such solution is divisible by itself.For the second test case, the possible solutions are: $$$23$$$, $$$27$$$, $$$29$$$, $$$34$$$, $$$37$$$, $$$38$$$, $$$43$$$, $$$46$$$, $$$47$$$, $$$49$$$, $$$53$$$, $$$54$$$, $$$56$$$, $$$57$$$, $$$58$$$, $$$59$$$, $$$67$$$, $$$68$$$, $$$69$$$, $$$73$$$, $$$74$$$, $$$76$$$, $$$78$$$, $$$79$$$, $$$83$$$, $$$86$$$, $$$87$$$, $$$89$$$, $$$94$$$, $$$97$$$, and $$$98$$$.For the third test case, one possible solution is $$$239$$$ because $$$239$$$ is not divisible by $$$2$$$, $$$3$$$ or $$$9$$$ and has three digits (none of which equals zero). | Java 11 | standard input | [
"constructive algorithms",
"number theory"
] | 43996d7e052aa628a46d03086f9c5436 | The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 400$$$), the number of test cases. The next $$$t$$$ lines each describe a test case. Each test case contains one positive integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$. | 1,000 | For each test case, print an integer $$$s$$$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $$$s$$$, print any solution. | standard output | |
PASSED | b8c22ff635a8868114a5ec9061cdb033 | train_001.jsonl | 1584628500 | You are given a integer $$$n$$$ ($$$n > 0$$$). Find any integer $$$s$$$ which satisfies these conditions, or report that there are no such numbers:In the decimal representation of $$$s$$$: $$$s > 0$$$, $$$s$$$ consists of $$$n$$$ digits, no digit in $$$s$$$ equals $$$0$$$, $$$s$$$ is not divisible by any of it's digits. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class p2 {
public static void main(String[]args) throws IOException {
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
int l=Integer.parseInt(r.readLine());
for(int i=0;i<l;i++){
// BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(r.readLine());
if(n==1){
System.out.println("-1");
}
else{
n--;
System.out.print("2");
for(int j=0;j<n;j++){
System.out.print("3");
}
System.out.println();
}
}
}
} | Java | ["4\n1\n2\n3\n4"] | 1 second | ["-1\n57\n239\n6789"] | NoteIn the first test case, there are no possible solutions for $$$s$$$ consisting of one digit, because any such solution is divisible by itself.For the second test case, the possible solutions are: $$$23$$$, $$$27$$$, $$$29$$$, $$$34$$$, $$$37$$$, $$$38$$$, $$$43$$$, $$$46$$$, $$$47$$$, $$$49$$$, $$$53$$$, $$$54$$$, $$$56$$$, $$$57$$$, $$$58$$$, $$$59$$$, $$$67$$$, $$$68$$$, $$$69$$$, $$$73$$$, $$$74$$$, $$$76$$$, $$$78$$$, $$$79$$$, $$$83$$$, $$$86$$$, $$$87$$$, $$$89$$$, $$$94$$$, $$$97$$$, and $$$98$$$.For the third test case, one possible solution is $$$239$$$ because $$$239$$$ is not divisible by $$$2$$$, $$$3$$$ or $$$9$$$ and has three digits (none of which equals zero). | Java 11 | standard input | [
"constructive algorithms",
"number theory"
] | 43996d7e052aa628a46d03086f9c5436 | The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 400$$$), the number of test cases. The next $$$t$$$ lines each describe a test case. Each test case contains one positive integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$. | 1,000 | For each test case, print an integer $$$s$$$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $$$s$$$, print any solution. | standard output | |
PASSED | c60f4c2ff8872b286f4561cdba56773e | train_001.jsonl | 1584628500 | You are given a integer $$$n$$$ ($$$n > 0$$$). Find any integer $$$s$$$ which satisfies these conditions, or report that there are no such numbers:In the decimal representation of $$$s$$$: $$$s > 0$$$, $$$s$$$ consists of $$$n$$$ digits, no digit in $$$s$$$ equals $$$0$$$, $$$s$$$ is not divisible by any of it's digits. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
static class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
String next() { // reads in the next string
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() { // reads in the next int
return Integer.parseInt(next());
}
public long nextLong() { // reads in the next long
return Long.parseLong(next());
}
public double nextDouble() { // reads in the next double
return Double.parseDouble(next());
}
}
static InputReader r = new InputReader(System.in);
static PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args) {
int t=r.nextInt();
for (int q=0; q<t; q++) {
int n=r.nextInt();
if (n==1) {
pw.println(-1);
}
else {
String s="";
for (int i=0; i<n-1; i++) {
s+="3";
}
s+="4";
pw.println(s);
}
}
pw.close();
}
} | Java | ["4\n1\n2\n3\n4"] | 1 second | ["-1\n57\n239\n6789"] | NoteIn the first test case, there are no possible solutions for $$$s$$$ consisting of one digit, because any such solution is divisible by itself.For the second test case, the possible solutions are: $$$23$$$, $$$27$$$, $$$29$$$, $$$34$$$, $$$37$$$, $$$38$$$, $$$43$$$, $$$46$$$, $$$47$$$, $$$49$$$, $$$53$$$, $$$54$$$, $$$56$$$, $$$57$$$, $$$58$$$, $$$59$$$, $$$67$$$, $$$68$$$, $$$69$$$, $$$73$$$, $$$74$$$, $$$76$$$, $$$78$$$, $$$79$$$, $$$83$$$, $$$86$$$, $$$87$$$, $$$89$$$, $$$94$$$, $$$97$$$, and $$$98$$$.For the third test case, one possible solution is $$$239$$$ because $$$239$$$ is not divisible by $$$2$$$, $$$3$$$ or $$$9$$$ and has three digits (none of which equals zero). | Java 11 | standard input | [
"constructive algorithms",
"number theory"
] | 43996d7e052aa628a46d03086f9c5436 | The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 400$$$), the number of test cases. The next $$$t$$$ lines each describe a test case. Each test case contains one positive integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$. | 1,000 | For each test case, print an integer $$$s$$$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $$$s$$$, print any solution. | standard output | |
PASSED | b0d2c26ceca1b8f71f481ec3e4764302 | train_001.jsonl | 1584628500 | You are given a integer $$$n$$$ ($$$n > 0$$$). Find any integer $$$s$$$ which satisfies these conditions, or report that there are no such numbers:In the decimal representation of $$$s$$$: $$$s > 0$$$, $$$s$$$ consists of $$$n$$$ digits, no digit in $$$s$$$ equals $$$0$$$, $$$s$$$ is not divisible by any of it's digits. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;
public class A implements Runnable {
public void run() {
InputReader sc = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
int t=sc.nextInt();
while(t--!=0)
{
int n=sc.nextInt();
if(n==1)
{
System.out.println("-1");
}
else
{
System.out.print("2");
for(int i=0;i<n-1;i++)
{
System.out.print("9");
}
}
System.out.println();
}
out.close();
}
//========================================================
static class Pair
{
int a,b;
Pair(int aa,int bb)
{
a=aa;
b=bb;
}
}
static void sa(int a[],InputReader sc)
{
for(int i=0;i<a.length;i++)a[i]=sc.nextInt();
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars==-1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
}
catch (IOException e) {
throw new InputMismatchException();
}
if(numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
int c = read();
while(isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if(c<'0'||c>'9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
public static void main(String args[]) throws Exception {
new Thread(null, new A(),"Main",1<<27).start();
}
} | Java | ["4\n1\n2\n3\n4"] | 1 second | ["-1\n57\n239\n6789"] | NoteIn the first test case, there are no possible solutions for $$$s$$$ consisting of one digit, because any such solution is divisible by itself.For the second test case, the possible solutions are: $$$23$$$, $$$27$$$, $$$29$$$, $$$34$$$, $$$37$$$, $$$38$$$, $$$43$$$, $$$46$$$, $$$47$$$, $$$49$$$, $$$53$$$, $$$54$$$, $$$56$$$, $$$57$$$, $$$58$$$, $$$59$$$, $$$67$$$, $$$68$$$, $$$69$$$, $$$73$$$, $$$74$$$, $$$76$$$, $$$78$$$, $$$79$$$, $$$83$$$, $$$86$$$, $$$87$$$, $$$89$$$, $$$94$$$, $$$97$$$, and $$$98$$$.For the third test case, one possible solution is $$$239$$$ because $$$239$$$ is not divisible by $$$2$$$, $$$3$$$ or $$$9$$$ and has three digits (none of which equals zero). | Java 11 | standard input | [
"constructive algorithms",
"number theory"
] | 43996d7e052aa628a46d03086f9c5436 | The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 400$$$), the number of test cases. The next $$$t$$$ lines each describe a test case. Each test case contains one positive integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$. | 1,000 | For each test case, print an integer $$$s$$$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $$$s$$$, print any solution. | standard output | |
PASSED | 5168b362038e8df27912b95b4388fa68 | train_001.jsonl | 1584628500 | You are given a integer $$$n$$$ ($$$n > 0$$$). Find any integer $$$s$$$ which satisfies these conditions, or report that there are no such numbers:In the decimal representation of $$$s$$$: $$$s > 0$$$, $$$s$$$ consists of $$$n$$$ digits, no digit in $$$s$$$ equals $$$0$$$, $$$s$$$ is not divisible by any of it's digits. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;
public class A implements Runnable {
public void run() {
InputReader sc = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
int t=sc.nextInt();
while(t--!=0)
{
int n=sc.nextInt();
if(n==1)
{
System.out.println("-1");
}
else
{
System.out.print("2");
for(int i=0;i<n-1;i++)
{
System.out.print("3");
}
}
System.out.println();
}
out.close();
}
//========================================================
static class Pair
{
int a,b;
Pair(int aa,int bb)
{
a=aa;
b=bb;
}
}
static void sa(int a[],InputReader sc)
{
for(int i=0;i<a.length;i++)a[i]=sc.nextInt();
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars==-1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
}
catch (IOException e) {
throw new InputMismatchException();
}
if(numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
int c = read();
while(isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if(c<'0'||c>'9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
public static void main(String args[]) throws Exception {
new Thread(null, new A(),"Main",1<<27).start();
}
} | Java | ["4\n1\n2\n3\n4"] | 1 second | ["-1\n57\n239\n6789"] | NoteIn the first test case, there are no possible solutions for $$$s$$$ consisting of one digit, because any such solution is divisible by itself.For the second test case, the possible solutions are: $$$23$$$, $$$27$$$, $$$29$$$, $$$34$$$, $$$37$$$, $$$38$$$, $$$43$$$, $$$46$$$, $$$47$$$, $$$49$$$, $$$53$$$, $$$54$$$, $$$56$$$, $$$57$$$, $$$58$$$, $$$59$$$, $$$67$$$, $$$68$$$, $$$69$$$, $$$73$$$, $$$74$$$, $$$76$$$, $$$78$$$, $$$79$$$, $$$83$$$, $$$86$$$, $$$87$$$, $$$89$$$, $$$94$$$, $$$97$$$, and $$$98$$$.For the third test case, one possible solution is $$$239$$$ because $$$239$$$ is not divisible by $$$2$$$, $$$3$$$ or $$$9$$$ and has three digits (none of which equals zero). | Java 11 | standard input | [
"constructive algorithms",
"number theory"
] | 43996d7e052aa628a46d03086f9c5436 | The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 400$$$), the number of test cases. The next $$$t$$$ lines each describe a test case. Each test case contains one positive integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$. | 1,000 | For each test case, print an integer $$$s$$$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $$$s$$$, print any solution. | standard output | |
PASSED | 6be40e15c3c7535771e44a0e7678b801 | train_001.jsonl | 1584628500 | You are given a integer $$$n$$$ ($$$n > 0$$$). Find any integer $$$s$$$ which satisfies these conditions, or report that there are no such numbers:In the decimal representation of $$$s$$$: $$$s > 0$$$, $$$s$$$ consists of $$$n$$$ digits, no digit in $$$s$$$ equals $$$0$$$, $$$s$$$ is not divisible by any of it's digits. | 256 megabytes | import java.util.*;
public class BadUglyNumbers{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t>0)
{
long n=sc.nextLong();
String s = "";
if(n>1)
{
s=s+"2";
for(long i=1;i<n;i++)
s=s+"3";
System.out.println(s);
}
else
System.out.println("-1");
t--;
}
}
} | Java | ["4\n1\n2\n3\n4"] | 1 second | ["-1\n57\n239\n6789"] | NoteIn the first test case, there are no possible solutions for $$$s$$$ consisting of one digit, because any such solution is divisible by itself.For the second test case, the possible solutions are: $$$23$$$, $$$27$$$, $$$29$$$, $$$34$$$, $$$37$$$, $$$38$$$, $$$43$$$, $$$46$$$, $$$47$$$, $$$49$$$, $$$53$$$, $$$54$$$, $$$56$$$, $$$57$$$, $$$58$$$, $$$59$$$, $$$67$$$, $$$68$$$, $$$69$$$, $$$73$$$, $$$74$$$, $$$76$$$, $$$78$$$, $$$79$$$, $$$83$$$, $$$86$$$, $$$87$$$, $$$89$$$, $$$94$$$, $$$97$$$, and $$$98$$$.For the third test case, one possible solution is $$$239$$$ because $$$239$$$ is not divisible by $$$2$$$, $$$3$$$ or $$$9$$$ and has three digits (none of which equals zero). | Java 11 | standard input | [
"constructive algorithms",
"number theory"
] | 43996d7e052aa628a46d03086f9c5436 | The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 400$$$), the number of test cases. The next $$$t$$$ lines each describe a test case. Each test case contains one positive integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$. | 1,000 | For each test case, print an integer $$$s$$$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $$$s$$$, print any solution. | standard output | |
PASSED | 6ed87e23312058180c86a3bda65cfa16 | train_001.jsonl | 1584628500 | You are given a integer $$$n$$$ ($$$n > 0$$$). Find any integer $$$s$$$ which satisfies these conditions, or report that there are no such numbers:In the decimal representation of $$$s$$$: $$$s > 0$$$, $$$s$$$ consists of $$$n$$$ digits, no digit in $$$s$$$ equals $$$0$$$, $$$s$$$ is not divisible by any of it's digits. | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.math.*;
//Mann Shah [ DAIICT ].
//fast io
public class Main {
static long mod = (long) (1e9+7);
static long mm = (long)(1e9+6);
static long N = (long)(2*1e5);
static InputReader in;
static PrintWriter out;
static Debugger deb;
public static int lower_bound(long[] a,int k) { // no. of elements less than k in array.
int first = 0,last = a.length,mid;
while (first < last) {
mid = first + ((last - first) >> 1);
if (a[mid] < k) //lower bound. for upper use <= ( n - first)
first = mid + 1;
else
last = mid;
}
return first;
}
public static int gcd(int a, int b){
if (a == 0)
return b;
return gcd(b % a, a);
}
public static long power(long x, long y, long p)
{
long res = 1;
x = x % p;
if (x == 0) return 0;
while (y > 0) {
if((y & 1)==1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
public static void main(String args[] ) throws NumberFormatException, IOException {
in = new InputReader(System.in);
out = new PrintWriter(System.out);
deb = new Debugger();
int T = in.nextInt();
while(T-->0) {
int n = in.nextInt();
if(n==1) {
out.println("-1");
}
else {
out.print("2");
for(int i=0;i<n-1;i++) {
out.print("3");
}
out.println("");
}
}
out.close();
}
/* TC space
*/
static class InputReader
{
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream)
{
this.stream = stream;
}
public int snext()
{
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars)
{
curChar = 0;
try
{
snumChars = stream.read(buf);
} catch (IOException e)
{
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = snext();
}
int res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = snext();
}
long res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n)
{
int a[] = new int[n];
for (int i = 0; i < n; i++)
{
a[i] = nextInt();
}
return a;
}
public long[] nextLongArray(int n)
{
long a[] = new long[n];
for (int i = 0; i < n; i++)
{
a[i] = nextLong();
}
return a;
}
public ArrayList<Integer> nextArrayList(int n){
ArrayList<Integer> x = new ArrayList<Integer>();
for(int i=0;i<n;i++) {
int v = in.nextInt();
x.add(v);
}
return x;
}
public String readString()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine()
{
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = snext();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c)
{
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c)
{
return c == '\n' || c == '\r' || c == -1;
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
}
static class Debugger{
public void n(int x) {
out.println(x);
}
public void a(int[] a) {
StringBuilder sb = new StringBuilder();
for(int i=0;i<a.length;i++) {
sb.append(a[i]+" ");
}
out.println(sb.toString());
}
}
}
class Pair {
int x;
int y;
// Constructor
public Pair(int x, int y)
{
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Pair)) return false;
Pair p = (Pair) o;
return x == p.x && y == p.y;
}
@Override
public int hashCode() {
return (""+x+"-"+y).hashCode();
}
}
// (p1,p2) -> p1.x-p2.x Using lembda function...
// Arrays.sort(arr, new Comparator<Pair>() {
// @Override public int compare(Pair p1, Pair p2)
// {
// return p1.x - p2.x;
// }
// });
| Java | ["4\n1\n2\n3\n4"] | 1 second | ["-1\n57\n239\n6789"] | NoteIn the first test case, there are no possible solutions for $$$s$$$ consisting of one digit, because any such solution is divisible by itself.For the second test case, the possible solutions are: $$$23$$$, $$$27$$$, $$$29$$$, $$$34$$$, $$$37$$$, $$$38$$$, $$$43$$$, $$$46$$$, $$$47$$$, $$$49$$$, $$$53$$$, $$$54$$$, $$$56$$$, $$$57$$$, $$$58$$$, $$$59$$$, $$$67$$$, $$$68$$$, $$$69$$$, $$$73$$$, $$$74$$$, $$$76$$$, $$$78$$$, $$$79$$$, $$$83$$$, $$$86$$$, $$$87$$$, $$$89$$$, $$$94$$$, $$$97$$$, and $$$98$$$.For the third test case, one possible solution is $$$239$$$ because $$$239$$$ is not divisible by $$$2$$$, $$$3$$$ or $$$9$$$ and has three digits (none of which equals zero). | Java 11 | standard input | [
"constructive algorithms",
"number theory"
] | 43996d7e052aa628a46d03086f9c5436 | The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 400$$$), the number of test cases. The next $$$t$$$ lines each describe a test case. Each test case contains one positive integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$. | 1,000 | For each test case, print an integer $$$s$$$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $$$s$$$, print any solution. | standard output | |
PASSED | 9100538fa8212a2912226dfe61923397 | train_001.jsonl | 1584628500 | You are given a integer $$$n$$$ ($$$n > 0$$$). Find any integer $$$s$$$ which satisfies these conditions, or report that there are no such numbers:In the decimal representation of $$$s$$$: $$$s > 0$$$, $$$s$$$ consists of $$$n$$$ digits, no digit in $$$s$$$ equals $$$0$$$, $$$s$$$ is not divisible by any of it's digits. | 256 megabytes |
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
FastReader scan = new FastReader();
int t = scan.nextInt();
boolean[] prime = sieveOfEratosthenes(100000);
while(t--> 0) {
int n = scan.nextInt();
if(n == 1) System.out.print(-1);
else {
System.out.print(2);
for(int i=1; i<n; i++) System.out.print(3);
}
System.out.println();
}
}
// sieveOfEratosthenes
public static boolean[] sieveOfEratosthenes(int n) {
boolean[] prime = new boolean[n+1];
Arrays.fill(prime, true);
prime[0] = false;
prime[1] = false;
for(int i=2; i*i<n; i++) {
if(prime[i] == true) {
for(int j=i*2; j<=n; j+=i) {
prime[j] = false;
}
}
}
return prime;
}
//smallestFactor function
public static long smallestFactor(long n) {
int i;
for(i=2; i<n; i++) {
if(n%i == 0)
break;
}
return i;
}
//FastReader Class
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
| Java | ["4\n1\n2\n3\n4"] | 1 second | ["-1\n57\n239\n6789"] | NoteIn the first test case, there are no possible solutions for $$$s$$$ consisting of one digit, because any such solution is divisible by itself.For the second test case, the possible solutions are: $$$23$$$, $$$27$$$, $$$29$$$, $$$34$$$, $$$37$$$, $$$38$$$, $$$43$$$, $$$46$$$, $$$47$$$, $$$49$$$, $$$53$$$, $$$54$$$, $$$56$$$, $$$57$$$, $$$58$$$, $$$59$$$, $$$67$$$, $$$68$$$, $$$69$$$, $$$73$$$, $$$74$$$, $$$76$$$, $$$78$$$, $$$79$$$, $$$83$$$, $$$86$$$, $$$87$$$, $$$89$$$, $$$94$$$, $$$97$$$, and $$$98$$$.For the third test case, one possible solution is $$$239$$$ because $$$239$$$ is not divisible by $$$2$$$, $$$3$$$ or $$$9$$$ and has three digits (none of which equals zero). | Java 11 | standard input | [
"constructive algorithms",
"number theory"
] | 43996d7e052aa628a46d03086f9c5436 | The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 400$$$), the number of test cases. The next $$$t$$$ lines each describe a test case. Each test case contains one positive integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$. | 1,000 | For each test case, print an integer $$$s$$$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $$$s$$$, print any solution. | standard output | |
PASSED | 63d45024d00783e585b7f28331955a49 | train_001.jsonl | 1584628500 | You are given a integer $$$n$$$ ($$$n > 0$$$). Find any integer $$$s$$$ which satisfies these conditions, or report that there are no such numbers:In the decimal representation of $$$s$$$: $$$s > 0$$$, $$$s$$$ consists of $$$n$$$ digits, no digit in $$$s$$$ equals $$$0$$$, $$$s$$$ is not divisible by any of it's digits. | 256 megabytes | import java.io.*;
import java.util.*;
public class badUglyNums3 {
public static void main(String[] args) throws IOException {
//get input separately
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
PrintWriter p = new PrintWriter(new BufferedOutputStream(System.out));
StringTokenizer s = new StringTokenizer(b.readLine());
int cases = Integer.parseInt(s.nextToken());
//store the answers in an int array
int[] arr = new int[100000];
for (int i = 0; i < cases; i++) {
long ans = 0;
s = new StringTokenizer(b.readLine());
int digits = Integer.parseInt(s.nextToken());
if (digits == 1) {
p.println(-1);
} else {
String temp = "23";
for (int j = 3; j <= digits; j++) {
temp += "" + 3;
}
p.println(temp);
}
}
p.close();
}
// you should actually read the stuff at the bottom
}
/* REMINDERS
* PLANNING!!!!!!!! Concrete plan before code
* DON'T MAKE ASSUMPTIONS
* DON'T OVERCOMPLICATE
* NAIVE SOL FIRST
* CHECK INT VS LONG, IF YOU NEED TO STORE LARGE NUMBERS
* CHECK CONSTRAINTS, C <= N <= F...
* CHECK SPECIAL CASES, N = 1...
* CHECK ARRAY BOUNDS, HOW BIG ARRAY HAS TO BE
* TO TEST TLE/MLE, PLUG IN MAX VALS ALLOWED AND SEE WHAT HAPPENS
* ALSO CALCULATE BIG-O, OVERALL TIME COMPLEXITY
*/
| Java | ["4\n1\n2\n3\n4"] | 1 second | ["-1\n57\n239\n6789"] | NoteIn the first test case, there are no possible solutions for $$$s$$$ consisting of one digit, because any such solution is divisible by itself.For the second test case, the possible solutions are: $$$23$$$, $$$27$$$, $$$29$$$, $$$34$$$, $$$37$$$, $$$38$$$, $$$43$$$, $$$46$$$, $$$47$$$, $$$49$$$, $$$53$$$, $$$54$$$, $$$56$$$, $$$57$$$, $$$58$$$, $$$59$$$, $$$67$$$, $$$68$$$, $$$69$$$, $$$73$$$, $$$74$$$, $$$76$$$, $$$78$$$, $$$79$$$, $$$83$$$, $$$86$$$, $$$87$$$, $$$89$$$, $$$94$$$, $$$97$$$, and $$$98$$$.For the third test case, one possible solution is $$$239$$$ because $$$239$$$ is not divisible by $$$2$$$, $$$3$$$ or $$$9$$$ and has three digits (none of which equals zero). | Java 11 | standard input | [
"constructive algorithms",
"number theory"
] | 43996d7e052aa628a46d03086f9c5436 | The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 400$$$), the number of test cases. The next $$$t$$$ lines each describe a test case. Each test case contains one positive integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$. | 1,000 | For each test case, print an integer $$$s$$$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $$$s$$$, print any solution. | standard output | |
PASSED | 24c1acc1ee773357213fbc5091263284 | train_001.jsonl | 1584628500 | You are given a integer $$$n$$$ ($$$n > 0$$$). Find any integer $$$s$$$ which satisfies these conditions, or report that there are no such numbers:In the decimal representation of $$$s$$$: $$$s > 0$$$, $$$s$$$ consists of $$$n$$$ digits, no digit in $$$s$$$ equals $$$0$$$, $$$s$$$ is not divisible by any of it's digits. | 256 megabytes | import java.util.Scanner;
public class BadUglyNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int test = scanner.nextInt();
while (test-- != 0){
int num = scanner.nextInt();
System.out.println(getNum(num));
}
}
private static String get(int val){
StringBuilder sb = new StringBuilder();
while (val > 2){
sb.append("6");
val -= 1;
}
sb.append("56");
return sb.reverse().toString();
}
private static String getNum(int val) {
StringBuilder sb = new StringBuilder();
if(val < 2) return "-1";
else if(val == 2) return "57";
else return get(val);
}
}
| Java | ["4\n1\n2\n3\n4"] | 1 second | ["-1\n57\n239\n6789"] | NoteIn the first test case, there are no possible solutions for $$$s$$$ consisting of one digit, because any such solution is divisible by itself.For the second test case, the possible solutions are: $$$23$$$, $$$27$$$, $$$29$$$, $$$34$$$, $$$37$$$, $$$38$$$, $$$43$$$, $$$46$$$, $$$47$$$, $$$49$$$, $$$53$$$, $$$54$$$, $$$56$$$, $$$57$$$, $$$58$$$, $$$59$$$, $$$67$$$, $$$68$$$, $$$69$$$, $$$73$$$, $$$74$$$, $$$76$$$, $$$78$$$, $$$79$$$, $$$83$$$, $$$86$$$, $$$87$$$, $$$89$$$, $$$94$$$, $$$97$$$, and $$$98$$$.For the third test case, one possible solution is $$$239$$$ because $$$239$$$ is not divisible by $$$2$$$, $$$3$$$ or $$$9$$$ and has three digits (none of which equals zero). | Java 11 | standard input | [
"constructive algorithms",
"number theory"
] | 43996d7e052aa628a46d03086f9c5436 | The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 400$$$), the number of test cases. The next $$$t$$$ lines each describe a test case. Each test case contains one positive integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$. | 1,000 | For each test case, print an integer $$$s$$$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $$$s$$$, print any solution. | standard output | |
PASSED | 3b9403073fea3974bc1de7b9f728fbf3 | train_001.jsonl | 1584628500 | You are given a integer $$$n$$$ ($$$n > 0$$$). Find any integer $$$s$$$ which satisfies these conditions, or report that there are no such numbers:In the decimal representation of $$$s$$$: $$$s > 0$$$, $$$s$$$ consists of $$$n$$$ digits, no digit in $$$s$$$ equals $$$0$$$, $$$s$$$ is not divisible by any of it's digits. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class NextRound {
String res;
void read() {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
try {
int t = Integer.parseInt(bufferedReader.readLine());
while (t-- != 0) {
int n = Integer.parseInt(bufferedReader.readLine());
if (n == 1)
System.out.println(-1);
else {
String s = 2+"3".repeat(Math.max(0, n - 1));
System.out.println(s);
}
}
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
public static void main(String[] args) {
NextRound n = new NextRound();
n.read();
// System.out.println(n.res);
}
} | Java | ["4\n1\n2\n3\n4"] | 1 second | ["-1\n57\n239\n6789"] | NoteIn the first test case, there are no possible solutions for $$$s$$$ consisting of one digit, because any such solution is divisible by itself.For the second test case, the possible solutions are: $$$23$$$, $$$27$$$, $$$29$$$, $$$34$$$, $$$37$$$, $$$38$$$, $$$43$$$, $$$46$$$, $$$47$$$, $$$49$$$, $$$53$$$, $$$54$$$, $$$56$$$, $$$57$$$, $$$58$$$, $$$59$$$, $$$67$$$, $$$68$$$, $$$69$$$, $$$73$$$, $$$74$$$, $$$76$$$, $$$78$$$, $$$79$$$, $$$83$$$, $$$86$$$, $$$87$$$, $$$89$$$, $$$94$$$, $$$97$$$, and $$$98$$$.For the third test case, one possible solution is $$$239$$$ because $$$239$$$ is not divisible by $$$2$$$, $$$3$$$ or $$$9$$$ and has three digits (none of which equals zero). | Java 11 | standard input | [
"constructive algorithms",
"number theory"
] | 43996d7e052aa628a46d03086f9c5436 | The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 400$$$), the number of test cases. The next $$$t$$$ lines each describe a test case. Each test case contains one positive integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$. | 1,000 | For each test case, print an integer $$$s$$$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $$$s$$$, print any solution. | standard output | |
PASSED | bdadb8317164d7c139e0bfc05ae4e7d8 | train_001.jsonl | 1584628500 | You are given a integer $$$n$$$ ($$$n > 0$$$). Find any integer $$$s$$$ which satisfies these conditions, or report that there are no such numbers:In the decimal representation of $$$s$$$: $$$s > 0$$$, $$$s$$$ consists of $$$n$$$ digits, no digit in $$$s$$$ equals $$$0$$$, $$$s$$$ is not divisible by any of it's digits. | 256 megabytes | /* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner in = new Scanner(System.in);
int t,n,i,j;
t=in.nextInt();
for(i=0;i<t;i++)
{
n=in.nextInt();
char a[]=new char[n];
a[0]='2';
for(j=1;j<n;j++)
a[j]='3';
if(n==1)
System.out.println("-1");
else
System.out.println(String.valueOf(a));
}
}
} | Java | ["4\n1\n2\n3\n4"] | 1 second | ["-1\n57\n239\n6789"] | NoteIn the first test case, there are no possible solutions for $$$s$$$ consisting of one digit, because any such solution is divisible by itself.For the second test case, the possible solutions are: $$$23$$$, $$$27$$$, $$$29$$$, $$$34$$$, $$$37$$$, $$$38$$$, $$$43$$$, $$$46$$$, $$$47$$$, $$$49$$$, $$$53$$$, $$$54$$$, $$$56$$$, $$$57$$$, $$$58$$$, $$$59$$$, $$$67$$$, $$$68$$$, $$$69$$$, $$$73$$$, $$$74$$$, $$$76$$$, $$$78$$$, $$$79$$$, $$$83$$$, $$$86$$$, $$$87$$$, $$$89$$$, $$$94$$$, $$$97$$$, and $$$98$$$.For the third test case, one possible solution is $$$239$$$ because $$$239$$$ is not divisible by $$$2$$$, $$$3$$$ or $$$9$$$ and has three digits (none of which equals zero). | Java 11 | standard input | [
"constructive algorithms",
"number theory"
] | 43996d7e052aa628a46d03086f9c5436 | The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 400$$$), the number of test cases. The next $$$t$$$ lines each describe a test case. Each test case contains one positive integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$. | 1,000 | For each test case, print an integer $$$s$$$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $$$s$$$, print any solution. | standard output | |
PASSED | 08bb710cdb6e5b89a9adfb553f1b030c | train_001.jsonl | 1584628500 | You are given a integer $$$n$$$ ($$$n > 0$$$). Find any integer $$$s$$$ which satisfies these conditions, or report that there are no such numbers:In the decimal representation of $$$s$$$: $$$s > 0$$$, $$$s$$$ consists of $$$n$$$ digits, no digit in $$$s$$$ equals $$$0$$$, $$$s$$$ is not divisible by any of it's digits. | 256 megabytes | import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
// Complete the maximumSum function below.
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
int a=scanner.nextInt();
for(int i=0;i<a;i++){
int b=scanner.nextInt();
if(b==1){
System.out.println(-1);
}
else {
StringBuilder x=new StringBuilder();
x.append("6");
b--;
for(int j=1;j<=b;j++){
x.append("7");
}
System.out.println(x);
}
}
}
}
| Java | ["4\n1\n2\n3\n4"] | 1 second | ["-1\n57\n239\n6789"] | NoteIn the first test case, there are no possible solutions for $$$s$$$ consisting of one digit, because any such solution is divisible by itself.For the second test case, the possible solutions are: $$$23$$$, $$$27$$$, $$$29$$$, $$$34$$$, $$$37$$$, $$$38$$$, $$$43$$$, $$$46$$$, $$$47$$$, $$$49$$$, $$$53$$$, $$$54$$$, $$$56$$$, $$$57$$$, $$$58$$$, $$$59$$$, $$$67$$$, $$$68$$$, $$$69$$$, $$$73$$$, $$$74$$$, $$$76$$$, $$$78$$$, $$$79$$$, $$$83$$$, $$$86$$$, $$$87$$$, $$$89$$$, $$$94$$$, $$$97$$$, and $$$98$$$.For the third test case, one possible solution is $$$239$$$ because $$$239$$$ is not divisible by $$$2$$$, $$$3$$$ or $$$9$$$ and has three digits (none of which equals zero). | Java 11 | standard input | [
"constructive algorithms",
"number theory"
] | 43996d7e052aa628a46d03086f9c5436 | The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 400$$$), the number of test cases. The next $$$t$$$ lines each describe a test case. Each test case contains one positive integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$. | 1,000 | For each test case, print an integer $$$s$$$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $$$s$$$, print any solution. | standard output | |
PASSED | 4b4e40f44b980f1283f955fedf10dde0 | train_001.jsonl | 1584628500 | You are given a integer $$$n$$$ ($$$n > 0$$$). Find any integer $$$s$$$ which satisfies these conditions, or report that there are no such numbers:In the decimal representation of $$$s$$$: $$$s > 0$$$, $$$s$$$ consists of $$$n$$$ digits, no digit in $$$s$$$ equals $$$0$$$, $$$s$$$ is not divisible by any of it's digits. | 256 megabytes | import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
import java.util.stream.*;
public class Solution {
/*get indexes
List<Integer> indexes = IntStream.range(0, a.length).boxed()
.filter(r -> a[r]==c).collect(Collectors.toList());
*/
/*
form string to array int
Integer[] a = Arrays.stream(scanner.nextLine().trim().split(" ")).map(e -> Integer.parseInt(e)).toArray(Integer[]::new);
*/
public static void main(String[] args) throws IOException {
// System.out.println("success");
Scanner scanner = new Scanner(System.in);
String tl = scanner.nextLine().trim();
int t = Integer.parseInt(tl);
for(int i=0;i<t;i++) {
String line=scanner.nextLine().trim();
if(line.equals("1")) {
System.out.println("-1");
}
else {
String p="2";
while(p.length()<Integer.parseInt(line)){
p+="3";
}
System.out.println(p);
}
}
}
}
| Java | ["4\n1\n2\n3\n4"] | 1 second | ["-1\n57\n239\n6789"] | NoteIn the first test case, there are no possible solutions for $$$s$$$ consisting of one digit, because any such solution is divisible by itself.For the second test case, the possible solutions are: $$$23$$$, $$$27$$$, $$$29$$$, $$$34$$$, $$$37$$$, $$$38$$$, $$$43$$$, $$$46$$$, $$$47$$$, $$$49$$$, $$$53$$$, $$$54$$$, $$$56$$$, $$$57$$$, $$$58$$$, $$$59$$$, $$$67$$$, $$$68$$$, $$$69$$$, $$$73$$$, $$$74$$$, $$$76$$$, $$$78$$$, $$$79$$$, $$$83$$$, $$$86$$$, $$$87$$$, $$$89$$$, $$$94$$$, $$$97$$$, and $$$98$$$.For the third test case, one possible solution is $$$239$$$ because $$$239$$$ is not divisible by $$$2$$$, $$$3$$$ or $$$9$$$ and has three digits (none of which equals zero). | Java 11 | standard input | [
"constructive algorithms",
"number theory"
] | 43996d7e052aa628a46d03086f9c5436 | The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 400$$$), the number of test cases. The next $$$t$$$ lines each describe a test case. Each test case contains one positive integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$. | 1,000 | For each test case, print an integer $$$s$$$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $$$s$$$, print any solution. | standard output | |
PASSED | 7b07d06c97351f9d95d5ff6c835cdbc1 | train_001.jsonl | 1584628500 | You are given a integer $$$n$$$ ($$$n > 0$$$). Find any integer $$$s$$$ which satisfies these conditions, or report that there are no such numbers:In the decimal representation of $$$s$$$: $$$s > 0$$$, $$$s$$$ consists of $$$n$$$ digits, no digit in $$$s$$$ equals $$$0$$$, $$$s$$$ is not divisible by any of it's digits. | 256 megabytes |
import java.util.Scanner;
public class NewClass2 {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
StringBuffer sb = new StringBuffer();
int t = scan.nextInt();
while (t-- > 0) {
int n = scan.nextInt();
if (n <= 2) {
if (n == 1) {
sb.append(-1 + "\n");
} else {
sb.append(34 + "\n");
}
} else {
String s = "3";
s = s.repeat(n - 2);
s = s + 2 + "" + 3;
if (n > 1) {
sb.append(s + "\n");
} else {
sb.append(-1 + "\n");
}
}
}
System.out.println(sb);
}
}
| Java | ["4\n1\n2\n3\n4"] | 1 second | ["-1\n57\n239\n6789"] | NoteIn the first test case, there are no possible solutions for $$$s$$$ consisting of one digit, because any such solution is divisible by itself.For the second test case, the possible solutions are: $$$23$$$, $$$27$$$, $$$29$$$, $$$34$$$, $$$37$$$, $$$38$$$, $$$43$$$, $$$46$$$, $$$47$$$, $$$49$$$, $$$53$$$, $$$54$$$, $$$56$$$, $$$57$$$, $$$58$$$, $$$59$$$, $$$67$$$, $$$68$$$, $$$69$$$, $$$73$$$, $$$74$$$, $$$76$$$, $$$78$$$, $$$79$$$, $$$83$$$, $$$86$$$, $$$87$$$, $$$89$$$, $$$94$$$, $$$97$$$, and $$$98$$$.For the third test case, one possible solution is $$$239$$$ because $$$239$$$ is not divisible by $$$2$$$, $$$3$$$ or $$$9$$$ and has three digits (none of which equals zero). | Java 11 | standard input | [
"constructive algorithms",
"number theory"
] | 43996d7e052aa628a46d03086f9c5436 | The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 400$$$), the number of test cases. The next $$$t$$$ lines each describe a test case. Each test case contains one positive integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$. | 1,000 | For each test case, print an integer $$$s$$$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $$$s$$$, print any solution. | standard output | |
PASSED | e120ae5f9f6bf441b37ba5650c90b244 | train_001.jsonl | 1584628500 | You are given a integer $$$n$$$ ($$$n > 0$$$). Find any integer $$$s$$$ which satisfies these conditions, or report that there are no such numbers:In the decimal representation of $$$s$$$: $$$s > 0$$$, $$$s$$$ consists of $$$n$$$ digits, no digit in $$$s$$$ equals $$$0$$$, $$$s$$$ is not divisible by any of it's digits. | 256 megabytes | import java.io.*;
import java.util.*;
/**
* Built using my Brain
* Actual solution is at the bottom
*
* @author Lenard Hoffstader
*/
public class cfjava
{
public static void main(String[] args)
{
OutputStream outputStream = System.out;
FastReader in = new FastReader();
PrintWriter out = new PrintWriter(outputStream);
PROBLEM solver = new PROBLEM();
int t=1;
t=in.nextInt();
for(int i=0;i<t;i++){
solver.solve(in, out);
}
out.close();
}
static class PROBLEM
{
public void solve(FastReader in,PrintWriter out)
{
int n=in.nextInt();
if(n==1)out.println(-1);
else {
out.print(2);
for(int i=1;i<=n-1;i++)out.print(3);
out.println();
}
}
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){return Integer.parseInt(next());}
long nextLong(){return Long.parseLong(next());}
double nextDouble(){return Double.parseDouble(next());}
boolean nextBoolean(){return Boolean.parseBoolean(next());}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e){e.printStackTrace();}
return str;
}
}
}
| Java | ["4\n1\n2\n3\n4"] | 1 second | ["-1\n57\n239\n6789"] | NoteIn the first test case, there are no possible solutions for $$$s$$$ consisting of one digit, because any such solution is divisible by itself.For the second test case, the possible solutions are: $$$23$$$, $$$27$$$, $$$29$$$, $$$34$$$, $$$37$$$, $$$38$$$, $$$43$$$, $$$46$$$, $$$47$$$, $$$49$$$, $$$53$$$, $$$54$$$, $$$56$$$, $$$57$$$, $$$58$$$, $$$59$$$, $$$67$$$, $$$68$$$, $$$69$$$, $$$73$$$, $$$74$$$, $$$76$$$, $$$78$$$, $$$79$$$, $$$83$$$, $$$86$$$, $$$87$$$, $$$89$$$, $$$94$$$, $$$97$$$, and $$$98$$$.For the third test case, one possible solution is $$$239$$$ because $$$239$$$ is not divisible by $$$2$$$, $$$3$$$ or $$$9$$$ and has three digits (none of which equals zero). | Java 11 | standard input | [
"constructive algorithms",
"number theory"
] | 43996d7e052aa628a46d03086f9c5436 | The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 400$$$), the number of test cases. The next $$$t$$$ lines each describe a test case. Each test case contains one positive integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$. | 1,000 | For each test case, print an integer $$$s$$$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $$$s$$$, print any solution. | standard output | |
PASSED | a33d44d35bd80a6588b2f7476246486f | train_001.jsonl | 1584628500 | You are given a integer $$$n$$$ ($$$n > 0$$$). Find any integer $$$s$$$ which satisfies these conditions, or report that there are no such numbers:In the decimal representation of $$$s$$$: $$$s > 0$$$, $$$s$$$ consists of $$$n$$$ digits, no digit in $$$s$$$ equals $$$0$$$, $$$s$$$ is not divisible by any of it's digits. | 256 megabytes | import java.math.BigInteger;
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Main{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
int t=in.nextInt();
while(t-->0)
{
int n=in.nextInt();
char[] s=new char[n];
// boolean flag=false;
if(n==1)System.out.println("-1");
else
{
s[0]='2';
for(int i=1;i<n;i++)
{
s[i]='3';
}
System.out.println(s);
}
}
}
} | Java | ["4\n1\n2\n3\n4"] | 1 second | ["-1\n57\n239\n6789"] | NoteIn the first test case, there are no possible solutions for $$$s$$$ consisting of one digit, because any such solution is divisible by itself.For the second test case, the possible solutions are: $$$23$$$, $$$27$$$, $$$29$$$, $$$34$$$, $$$37$$$, $$$38$$$, $$$43$$$, $$$46$$$, $$$47$$$, $$$49$$$, $$$53$$$, $$$54$$$, $$$56$$$, $$$57$$$, $$$58$$$, $$$59$$$, $$$67$$$, $$$68$$$, $$$69$$$, $$$73$$$, $$$74$$$, $$$76$$$, $$$78$$$, $$$79$$$, $$$83$$$, $$$86$$$, $$$87$$$, $$$89$$$, $$$94$$$, $$$97$$$, and $$$98$$$.For the third test case, one possible solution is $$$239$$$ because $$$239$$$ is not divisible by $$$2$$$, $$$3$$$ or $$$9$$$ and has three digits (none of which equals zero). | Java 11 | standard input | [
"constructive algorithms",
"number theory"
] | 43996d7e052aa628a46d03086f9c5436 | The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 400$$$), the number of test cases. The next $$$t$$$ lines each describe a test case. Each test case contains one positive integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$. | 1,000 | For each test case, print an integer $$$s$$$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $$$s$$$, print any solution. | standard output | |
PASSED | f5f6570aa077bc9a194a3085d1efb69e | train_001.jsonl | 1584628500 | You are given a integer $$$n$$$ ($$$n > 0$$$). Find any integer $$$s$$$ which satisfies these conditions, or report that there are no such numbers:In the decimal representation of $$$s$$$: $$$s > 0$$$, $$$s$$$ consists of $$$n$$$ digits, no digit in $$$s$$$ equals $$$0$$$, $$$s$$$ is not divisible by any of it's digits. | 256 megabytes | //package codeforcesa2;
import java.util.*;
public class BadUglyNumbers {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int t=in.nextInt();
for(int i=0;i<t;i++)
{
int n=in.nextInt();
if(n==1){
System.out.println("-1");
}else{
System.out.print("2");
for(int j=0;j<n-1;j++)
{
System.out.print("3");
}
System.out.println("");
}
}
}
}
| Java | ["4\n1\n2\n3\n4"] | 1 second | ["-1\n57\n239\n6789"] | NoteIn the first test case, there are no possible solutions for $$$s$$$ consisting of one digit, because any such solution is divisible by itself.For the second test case, the possible solutions are: $$$23$$$, $$$27$$$, $$$29$$$, $$$34$$$, $$$37$$$, $$$38$$$, $$$43$$$, $$$46$$$, $$$47$$$, $$$49$$$, $$$53$$$, $$$54$$$, $$$56$$$, $$$57$$$, $$$58$$$, $$$59$$$, $$$67$$$, $$$68$$$, $$$69$$$, $$$73$$$, $$$74$$$, $$$76$$$, $$$78$$$, $$$79$$$, $$$83$$$, $$$86$$$, $$$87$$$, $$$89$$$, $$$94$$$, $$$97$$$, and $$$98$$$.For the third test case, one possible solution is $$$239$$$ because $$$239$$$ is not divisible by $$$2$$$, $$$3$$$ or $$$9$$$ and has three digits (none of which equals zero). | Java 11 | standard input | [
"constructive algorithms",
"number theory"
] | 43996d7e052aa628a46d03086f9c5436 | The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 400$$$), the number of test cases. The next $$$t$$$ lines each describe a test case. Each test case contains one positive integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$. | 1,000 | For each test case, print an integer $$$s$$$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $$$s$$$, print any solution. | standard output | |
PASSED | dd1be0da9ab1cbd16598343c7c5452c4 | train_001.jsonl | 1584628500 | You are given a integer $$$n$$$ ($$$n > 0$$$). Find any integer $$$s$$$ which satisfies these conditions, or report that there are no such numbers:In the decimal representation of $$$s$$$: $$$s > 0$$$, $$$s$$$ consists of $$$n$$$ digits, no digit in $$$s$$$ equals $$$0$$$, $$$s$$$ is not divisible by any of it's digits. | 256 megabytes | import java.util.Scanner;
import java.util.Arrays;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int k=0;k<t;k++)
{
int n=sc.nextInt();
int result=1;
if(n==1)
{
System.out.println("-1");
continue;
}
else if(n==2)
{
System.out.println("23");
continue;
}
else
{
int num[]=new int[n];
System.out.print("23");
while(n>2)
{
System.out.print("9");
n--;
}
System.out.println("");
}
}
}
}
| Java | ["4\n1\n2\n3\n4"] | 1 second | ["-1\n57\n239\n6789"] | NoteIn the first test case, there are no possible solutions for $$$s$$$ consisting of one digit, because any such solution is divisible by itself.For the second test case, the possible solutions are: $$$23$$$, $$$27$$$, $$$29$$$, $$$34$$$, $$$37$$$, $$$38$$$, $$$43$$$, $$$46$$$, $$$47$$$, $$$49$$$, $$$53$$$, $$$54$$$, $$$56$$$, $$$57$$$, $$$58$$$, $$$59$$$, $$$67$$$, $$$68$$$, $$$69$$$, $$$73$$$, $$$74$$$, $$$76$$$, $$$78$$$, $$$79$$$, $$$83$$$, $$$86$$$, $$$87$$$, $$$89$$$, $$$94$$$, $$$97$$$, and $$$98$$$.For the third test case, one possible solution is $$$239$$$ because $$$239$$$ is not divisible by $$$2$$$, $$$3$$$ or $$$9$$$ and has three digits (none of which equals zero). | Java 11 | standard input | [
"constructive algorithms",
"number theory"
] | 43996d7e052aa628a46d03086f9c5436 | The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 400$$$), the number of test cases. The next $$$t$$$ lines each describe a test case. Each test case contains one positive integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$. | 1,000 | For each test case, print an integer $$$s$$$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $$$s$$$, print any solution. | standard output | |
PASSED | fbb64d24a9c1235a6d6fb3894ecd864e | train_001.jsonl | 1584628500 | You are given a integer $$$n$$$ ($$$n > 0$$$). Find any integer $$$s$$$ which satisfies these conditions, or report that there are no such numbers:In the decimal representation of $$$s$$$: $$$s > 0$$$, $$$s$$$ consists of $$$n$$$ digits, no digit in $$$s$$$ equals $$$0$$$, $$$s$$$ is not divisible by any of it's digits. | 256 megabytes | import java.util.*;
import java.io.*;
public class Solution{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
if(n == 1){
System.out.println(-1);
} else{
System.out.print(2);
for(int i = 1; i < n; i++){
System.out.print(3);
}
System.out.println();
}
}
}
} | Java | ["4\n1\n2\n3\n4"] | 1 second | ["-1\n57\n239\n6789"] | NoteIn the first test case, there are no possible solutions for $$$s$$$ consisting of one digit, because any such solution is divisible by itself.For the second test case, the possible solutions are: $$$23$$$, $$$27$$$, $$$29$$$, $$$34$$$, $$$37$$$, $$$38$$$, $$$43$$$, $$$46$$$, $$$47$$$, $$$49$$$, $$$53$$$, $$$54$$$, $$$56$$$, $$$57$$$, $$$58$$$, $$$59$$$, $$$67$$$, $$$68$$$, $$$69$$$, $$$73$$$, $$$74$$$, $$$76$$$, $$$78$$$, $$$79$$$, $$$83$$$, $$$86$$$, $$$87$$$, $$$89$$$, $$$94$$$, $$$97$$$, and $$$98$$$.For the third test case, one possible solution is $$$239$$$ because $$$239$$$ is not divisible by $$$2$$$, $$$3$$$ or $$$9$$$ and has three digits (none of which equals zero). | Java 11 | standard input | [
"constructive algorithms",
"number theory"
] | 43996d7e052aa628a46d03086f9c5436 | The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 400$$$), the number of test cases. The next $$$t$$$ lines each describe a test case. Each test case contains one positive integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$. | 1,000 | For each test case, print an integer $$$s$$$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $$$s$$$, print any solution. | standard output | |
PASSED | 84ed0f7fa6924a7c7b0adc178e5e91f4 | train_001.jsonl | 1584628500 | You are given a integer $$$n$$$ ($$$n > 0$$$). Find any integer $$$s$$$ which satisfies these conditions, or report that there are no such numbers:In the decimal representation of $$$s$$$: $$$s > 0$$$, $$$s$$$ consists of $$$n$$$ digits, no digit in $$$s$$$ equals $$$0$$$, $$$s$$$ is not divisible by any of it's digits. | 256 megabytes |
import com.sun.source.tree.Tree;
import javax.print.DocFlavor;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.management.MemoryType;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.security.UnrecoverableEntryException;
import java.security.cert.CollectionCertStoreParameters;
import java.util.*;
import java.util.function.Function;
public class Main {
static String reverseInParentheses(String inputString) {
char[]a=inputString.toCharArray();
StringBuilder ans= new StringBuilder(),stack=new StringBuilder();
int n=a.length;
Stack<Integer>temp= new Stack();
for(int i=0;i<a.length;i++){
if(a[i]=='('){
temp.push(i);
}else if(a[i]!=')'){
stack.append(a[i]);
}
else{
if(temp.size()==1){
ans.append(stack.reverse().toString());
System.out.println(stack);
stack=new StringBuilder();
temp.pop();
}else{
stack=stack.reverse();
temp.pop();
}
}
ans.append(stack);
}
return ans.toString();
}
public static void main (String[]args) throws IOException {
Scanner in = new Scanner(System.in);
try (PrintWriter or = new PrintWriter(System.out)) {
int t=in.nextInt();
while (t-->0){
int n = in.nextInt();
StringBuilder ans= new StringBuilder();
if (n==1)or.println(-1);
else {
while (n-->1){
ans.append(3);
}
or.println(ans+"8");
}
}
}
}
static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static int getlen(int r,int l,int a){
return (r-l+1+1)/(a+1);
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++) {
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec) {
f *= 10;
}
}
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public boolean ready() throws IOException {
return br.ready();
}
}
}
class Pair {
int first, second;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
}
class Tempo {
String first;
int second;
public Tempo(String first, int second) {
this.first = first;
this.second = second;
}
} | Java | ["4\n1\n2\n3\n4"] | 1 second | ["-1\n57\n239\n6789"] | NoteIn the first test case, there are no possible solutions for $$$s$$$ consisting of one digit, because any such solution is divisible by itself.For the second test case, the possible solutions are: $$$23$$$, $$$27$$$, $$$29$$$, $$$34$$$, $$$37$$$, $$$38$$$, $$$43$$$, $$$46$$$, $$$47$$$, $$$49$$$, $$$53$$$, $$$54$$$, $$$56$$$, $$$57$$$, $$$58$$$, $$$59$$$, $$$67$$$, $$$68$$$, $$$69$$$, $$$73$$$, $$$74$$$, $$$76$$$, $$$78$$$, $$$79$$$, $$$83$$$, $$$86$$$, $$$87$$$, $$$89$$$, $$$94$$$, $$$97$$$, and $$$98$$$.For the third test case, one possible solution is $$$239$$$ because $$$239$$$ is not divisible by $$$2$$$, $$$3$$$ or $$$9$$$ and has three digits (none of which equals zero). | Java 11 | standard input | [
"constructive algorithms",
"number theory"
] | 43996d7e052aa628a46d03086f9c5436 | The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 400$$$), the number of test cases. The next $$$t$$$ lines each describe a test case. Each test case contains one positive integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$. | 1,000 | For each test case, print an integer $$$s$$$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $$$s$$$, print any solution. | standard output | |
PASSED | f58069c813b57b0cbc8c05f642825782 | train_001.jsonl | 1584628500 | You are given a integer $$$n$$$ ($$$n > 0$$$). Find any integer $$$s$$$ which satisfies these conditions, or report that there are no such numbers:In the decimal representation of $$$s$$$: $$$s > 0$$$, $$$s$$$ consists of $$$n$$$ digits, no digit in $$$s$$$ equals $$$0$$$, $$$s$$$ is not divisible by any of it's digits. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
public class BadUglyNumber
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int i=0;i<t;i++)
{
int n=sc.nextInt();
String s="";
if(n==1)
{
System.out.println("-1");
continue;
}
else
{
for(int j=0;j<n-1;j++)
{
s=s+"9";
}
System.out.println(s+"8");
}
}
}
} | Java | ["4\n1\n2\n3\n4"] | 1 second | ["-1\n57\n239\n6789"] | NoteIn the first test case, there are no possible solutions for $$$s$$$ consisting of one digit, because any such solution is divisible by itself.For the second test case, the possible solutions are: $$$23$$$, $$$27$$$, $$$29$$$, $$$34$$$, $$$37$$$, $$$38$$$, $$$43$$$, $$$46$$$, $$$47$$$, $$$49$$$, $$$53$$$, $$$54$$$, $$$56$$$, $$$57$$$, $$$58$$$, $$$59$$$, $$$67$$$, $$$68$$$, $$$69$$$, $$$73$$$, $$$74$$$, $$$76$$$, $$$78$$$, $$$79$$$, $$$83$$$, $$$86$$$, $$$87$$$, $$$89$$$, $$$94$$$, $$$97$$$, and $$$98$$$.For the third test case, one possible solution is $$$239$$$ because $$$239$$$ is not divisible by $$$2$$$, $$$3$$$ or $$$9$$$ and has three digits (none of which equals zero). | Java 11 | standard input | [
"constructive algorithms",
"number theory"
] | 43996d7e052aa628a46d03086f9c5436 | The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 400$$$), the number of test cases. The next $$$t$$$ lines each describe a test case. Each test case contains one positive integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$. | 1,000 | For each test case, print an integer $$$s$$$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $$$s$$$, print any solution. | standard output | |
PASSED | b922e08369ebf3bc8acabf795bd1149c | train_001.jsonl | 1492266900 | Petya recieved a gift of a string s with length up to 105 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal.You should write a program that will help Petya win the game. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public Main() throws IOException {
}
static void solve() throws IOException {
// String s = input.readLine();
StringBuilder s = new StringBuilder(input.readLine());
int n = s.length();
int minPos[] = new int[n]; // store the position of "min character" for i to n
minPos[n - 1] = n - 1;
int i = n - 2;
while (i >= 0) {
if ((int)s.charAt(i) <= (int)s.charAt(minPos[i + 1])) {
minPos[i] = i;
} else {
minPos[i] = minPos[i + 1];
}
i--;
}
int pointer = minPos[0];
StringBuilder leftChars = new StringBuilder(s.substring(0, pointer));
ans.append(s.charAt(pointer));
while (pointer < n) {
if (pointer == n - 1) {
ans.append(leftChars.reverse());
break;
}
int leftLen = leftChars.length();
char leftChar = leftLen > 0 ? leftChars.charAt(leftLen - 1) : ('z' + 1);
int rightCharPos = minPos[pointer + 1];
char rightChar = s.charAt(rightCharPos);
if (leftChar <= rightChar) {
ans.append(leftChar);
leftChars.deleteCharAt(leftLen - 1);
} else {
leftChars.append(s.substring(pointer + 1, rightCharPos));
ans.append(s.charAt(rightCharPos));
pointer = rightCharPos;
}
}
output.write(ans.toString());
output.flush();
output.close();
}
public static void main(String[] args) throws IOException {
new Main().solve();
}
static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
static StringBuilder ans = new StringBuilder();
static StringTokenizer stringTokenizer;
static int ni1() throws IOException {
return Integer.parseInt(input.readLine());
}
static int ni2() throws IOException {
return Integer.parseInt(stringTokenizer.nextToken());
}
static long nl1() throws IOException {
return Long.parseLong(input.readLine());
}
static long nl2() {
return Long.parseLong(stringTokenizer.nextToken());
}
static String ns() {
return stringTokenizer.nextToken();
}
static void rl() throws IOException {
stringTokenizer = new StringTokenizer(input.readLine());
}
} | Java | ["cab", "acdb"] | 1 second | ["abc", "abdc"] | null | Java 8 | standard input | [
"data structures",
"greedy",
"strings"
] | e758ae072b8aed53038e4593a720276d | First line contains non-empty string s (1 ≤ |s| ≤ 105), consisting of lowercase English letters. | 1,700 | Print resulting string u. | standard output | |
PASSED | f97f0c9b4f3e664bd0f53996f8b35587 | train_001.jsonl | 1492266900 | Petya recieved a gift of a string s with length up to 105 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal.You should write a program that will help Petya win the game. | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String[] args) {
FastReader scr = new FastReader();
OutputStream outputStream = System.out;
PrintWriter out = new PrintWriter(outputStream);
// int t = scr.nextInt();
// while (t-- > 0)
solve(scr, out);
out.close();
}
static void solve(FastReader scr, PrintWriter out) {
String str = scr.next();
Deque<Character> t = new LinkedList<>();
Deque<Character> s = new LinkedList<>();
StringBuilder u = new StringBuilder("");
int[] a = new int[26];
for (int i = str.length() - 1; i > -1; i--) {
s.push(str.charAt(i));
a[str.charAt(i) - 'a']++;
}
while (!s.isEmpty()) {
for (int j = 0; j < 26; ) {
if (a[j] == 0) j++;
else {
while (!t.isEmpty() && !s.isEmpty() && t.peek() <= (char) (j + 'a')) {
u.append(t.pop());
}
if (!s.isEmpty() && (char) (j + 'a') == s.peek()) {
u.append(s.pop());
a[j]--;
} else if (!s.isEmpty() && (char) (j + 'a') != s.peek()) {
int te = s.peek() - 'a';
t.push(s.pop());
a[te]--;
}
}
}
}
while (!t.isEmpty()) {
u.append(t.pop());
}
out.println(u);
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["cab", "acdb"] | 1 second | ["abc", "abdc"] | null | Java 8 | standard input | [
"data structures",
"greedy",
"strings"
] | e758ae072b8aed53038e4593a720276d | First line contains non-empty string s (1 ≤ |s| ≤ 105), consisting of lowercase English letters. | 1,700 | Print resulting string u. | standard output | |
PASSED | 2996984c677b9b6d3f585a00638fe317 | train_001.jsonl | 1492266900 | Petya recieved a gift of a string s with length up to 105 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal.You should write a program that will help Petya win the game. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class C {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String s = reader.readLine();
int size = 0;
char[] st = new char[s.length()];
char[] res = new char[s.length()];
char[] b = new char[s.length()];
b[s.length() - 1] = s.charAt(s.length() - 1);
for (int i = s.length() - 2; i >= 0; i--){
if (s.charAt(i) < b[i + 1]){
b[i] = s.charAt(i);
} else {
b[i] = b[i + 1];
}
}
int ress = 0;
for (int i = 0; i < s.length(); i++) {
while (size != 0 && st[size - 1] <= b[i]){
res[ress++] = st[--size];
}
if (s.charAt(i) <= b[i]){
res[ress++] = s.charAt(i);
} else {
st[size++] = s.charAt(i);
}
}
while (size != 0){
res[ress++] = st[--size];
}
System.out.println(new String(res));
}
}
| Java | ["cab", "acdb"] | 1 second | ["abc", "abdc"] | null | Java 8 | standard input | [
"data structures",
"greedy",
"strings"
] | e758ae072b8aed53038e4593a720276d | First line contains non-empty string s (1 ≤ |s| ≤ 105), consisting of lowercase English letters. | 1,700 | Print resulting string u. | standard output | |
PASSED | a74637db57a7e4723c3623afd759bc76 | train_001.jsonl | 1492266900 | Petya recieved a gift of a string s with length up to 105 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal.You should write a program that will help Petya win the game. | 256 megabytes | import java.io.IOException;
import java.io.InputStream;
import java.util.InputMismatchException;
import java.util.Stack;
public class D {
public static void main(String[] args) {
FastScannerD sc = new FastScannerD(System.in);
String s = sc.next();
char[] arr = s.toCharArray();
int N = arr.length;
char[] dp = new char[N + 1];
dp[N] = (char)255;
for(int i = N - 1; i >= 0; i--) dp[i] = (char) Math.min(dp[i + 1], arr[i]);
Stack<Character> st = new Stack<>();
StringBuilder sb = new StringBuilder();
st.push((char) 255);
int current = 0;
while(current < N || st.size() > 1){
if(current == N){
sb.append(st.pop());
continue;
}
if(st.peek() <= dp[current]){
sb.append(st.pop());
}
else{
st.push(arr[current]);
current++;
}
}
System.out.println(sb.toString());
}
}
class FastScannerD{
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public FastScannerD(InputStream stream)
{
this.stream = stream;
}
public int read()
{
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars)
{
curChar = 0;
try
{
numChars = stream.read(buf);
} catch (IOException e)
{
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
int res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String next()
{
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine()
{
int c = read();
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = read();
} while (!isLineEndChar(c));
return res.toString();
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c)
{
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public boolean isLineEndChar(int c)
{
if (filter != null)
return filter.isSpaceChar(c);
return c == '\n' || c == '\r' || c == -1;
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
} | Java | ["cab", "acdb"] | 1 second | ["abc", "abdc"] | null | Java 8 | standard input | [
"data structures",
"greedy",
"strings"
] | e758ae072b8aed53038e4593a720276d | First line contains non-empty string s (1 ≤ |s| ≤ 105), consisting of lowercase English letters. | 1,700 | Print resulting string u. | standard output | |
PASSED | 8e98cc7cd136d830be8698b221efc747 | train_001.jsonl | 1492266900 | Petya recieved a gift of a string s with length up to 105 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal.You should write a program that will help Petya win the game. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
public class CF {
long mod = (long) 1e9 + 7;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
int n = s.length();
char[] arr = s.toCharArray();
char[] suffMin = new char[n];
suffMin[n - 1] = arr[n - 1];
for (int i = n - 2; i >= 0; i--) {
suffMin[i] = (char) Integer.min(suffMin[i + 1], arr[i]);
}
Stack<Character> t = new Stack<>();
Stack<Character> u = new Stack<>();
String ans = "";
for (int i = 0; i < n; i++) {
while (!t.empty() && t.peek() <= suffMin[i]) {
u.push(t.pop());
}
if (arr[i] == suffMin[i])
u.push(arr[i]);
else
t.push(arr[i]);
}
while (!t.empty())
u.push(t.pop());
for(int i=n-1;i>=0;--i)
arr[i]=u.pop();
System.out.println(new String(arr));
}
}
| Java | ["cab", "acdb"] | 1 second | ["abc", "abdc"] | null | Java 8 | standard input | [
"data structures",
"greedy",
"strings"
] | e758ae072b8aed53038e4593a720276d | First line contains non-empty string s (1 ≤ |s| ≤ 105), consisting of lowercase English letters. | 1,700 | Print resulting string u. | standard output | |
PASSED | 87eb1daae4c3d6a6886ffe05a3902610 | train_001.jsonl | 1492266900 | Petya recieved a gift of a string s with length up to 105 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal.You should write a program that will help Petya win the game. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class SSolution {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
SSolution s = new SSolution();
s.solve(in);
}
private void solve(BufferedReader in) throws IOException {
String s = in.readLine();
Deque<Character> d = new LinkedList<Character>();
TreeMap<Character, Integer> tm = new TreeMap<Character, Integer>();
for(Character c: s.toCharArray()){
if(tm.containsKey(c)){
tm.put(c, tm.get(c) + 1);
}
else{
tm.put(c, 1);
}
}
TreeMap<Character, Integer> tm_copy = (TreeMap<Character, Integer>)tm.clone();
ArrayList<Character> res = new ArrayList<Character>();
Iterator<Character> iterator = tm.navigableKeySet().iterator();
Character needed = iterator.next();
for (int i = 0; i < s.length(); ++i) {
Character curr = s.charAt(i);
tm.put(curr, tm.get(curr) - 1);
if (curr.equals(needed)) {
res.add(needed);
if (tm.get(needed) == 0) {
while (iterator.hasNext()) {
needed = iterator.next();
while (d.size() > 0 && d.getFirst().compareTo(needed) <=0) {
res.add(d.removeFirst());
}
if (tm.get(needed) > 0) break; // we have more in string
}
}
} else {
d.push(curr);
}
}
while(d.size() > 0){
res.add(d.remove());
}
for(Character c: res){
System.out.print(c);
}
System.out.println();
}
} | Java | ["cab", "acdb"] | 1 second | ["abc", "abdc"] | null | Java 8 | standard input | [
"data structures",
"greedy",
"strings"
] | e758ae072b8aed53038e4593a720276d | First line contains non-empty string s (1 ≤ |s| ≤ 105), consisting of lowercase English letters. | 1,700 | Print resulting string u. | standard output | |
PASSED | 5967793601417b0697e0dbce27aba48c | train_001.jsonl | 1492266900 | Petya recieved a gift of a string s with length up to 105 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal.You should write a program that will help Petya win the game. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC {
public void solve(int testNumber, InputReader in, PrintWriter out) {
char[] a = in.next().toCharArray();
int n = a.length;
char[] tail = new char[n];
char[] t = new char[n];
char[] res = new char[n];
tail[n - 1] = a[n - 1];
for (int i = n - 2; i >= 0; i--) {
tail[i] = (char) (Math.min(tail[i + 1], a[i]));
}
int lcnt = 0;
int rcnt = 0;
for (int i = 0; i < n; i++) {
while (lcnt > 0 && t[lcnt - 1] <= tail[i]) {
res[rcnt++] = t[--lcnt];
}
if (a[i] == tail[i]) {
res[rcnt++] = a[i];
} else {
t[lcnt++] = a[i];
}
}
while (lcnt > 0) res[rcnt++] = t[--lcnt];
out.println(new String(res));
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
}
}
| Java | ["cab", "acdb"] | 1 second | ["abc", "abdc"] | null | Java 8 | standard input | [
"data structures",
"greedy",
"strings"
] | e758ae072b8aed53038e4593a720276d | First line contains non-empty string s (1 ≤ |s| ≤ 105), consisting of lowercase English letters. | 1,700 | Print resulting string u. | standard output | |
PASSED | 60465454614cdfad1637a851ab6712c7 | train_001.jsonl | 1492266900 | Petya recieved a gift of a string s with length up to 105 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal.You should write a program that will help Petya win the game. | 256 megabytes |
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.SortedSet;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
/**
* #
* @author pttrung
*/
public class C_Edu_Round19 {
public static long MOD = 1000000007;
public static void main(String[] args) throws FileNotFoundException {
// PrintWriter out = new PrintWriter(new FileOutputStream(new File(
// "output.txt")));
PrintWriter out = new PrintWriter(System.out);
Scanner in = new Scanner();
String line = in.next();
StringBuilder result = new StringBuilder();
int[] count = new int[26];
for (int i = 0; i < line.length(); i++) {
int index = line.charAt(i) - 'a';
count[index]++;
}
Stack<Character> s = new Stack();
for (int i = 0; i < line.length(); i++) {
char c = line.charAt(i);
int index = c - 'a';
boolean have = !s.isEmpty() && s.peek() <= c;
for (int j = 0; j < index; j++) {
if (count[j] > 0) {
have = true;
break;
}
}
if (have) {
while (!s.isEmpty()) {
char top = s.peek();
have = false;
for (int j = 0; j < (top - 'a'); j++) {
if (count[j] > 0) {
have = true;
break;
}
}
if (!have) {
top = s.pop();
result.append(top);
} else {
break;
}
}
s.add(c);
} else {
result.append(c);
}
// System.out.println(result + " " + s);
count[index]--;
}
while (!s.isEmpty()) {
result.append(s.pop());
}
out.println(result.toString());
out.close();
}
public static int[] KMP(String val) {
int i = 0;
int j = -1;
int[] result = new int[val.length() + 1];
result[0] = -1;
while (i < val.length()) {
while (j >= 0 && val.charAt(j) != val.charAt(i)) {
j = result[j];
}
j++;
i++;
result[i] = j;
}
return result;
}
public static boolean nextPer(int[] data) {
int i = data.length - 1;
while (i > 0 && data[i] < data[i - 1]) {
i--;
}
if (i == 0) {
return false;
}
int j = data.length - 1;
while (data[j] < data[i - 1]) {
j--;
}
int temp = data[i - 1];
data[i - 1] = data[j];
data[j] = temp;
Arrays.sort(data, i, data.length);
return true;
}
public static int digit(long n) {
int result = 0;
while (n > 0) {
n /= 10;
result++;
}
return result;
}
public static double dist(long a, long b, long x, long y) {
double val = (b - a) * (b - a) + (x - y) * (x - y);
val = Math.sqrt(val);
double other = x * x + a * a;
other = Math.sqrt(other);
return val + other;
}
public static class Point implements Comparable<Point> {
int x, y;
public Point(int start, int end) {
this.x = start;
this.y = end;
}
@Override
public int hashCode() {
int hash = 5;
hash = 47 * hash + this.x;
hash = 47 * hash + this.y;
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Point other = (Point) obj;
if (this.x != other.x) {
return false;
}
if (this.y != other.y) {
return false;
}
return true;
}
@Override
public int compareTo(Point o) {
return Integer.compare(x, o.x);
}
}
public static class FT {
long[] data;
FT(int n) {
data = new long[n];
}
public void update(int index, long value) {
while (index < data.length) {
data[index] += value;
index += (index & (-index));
}
}
public long get(int index) {
long result = 0;
while (index > 0) {
result += data[index];
index -= (index & (-index));
}
return result;
}
}
public static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
public static long pow(long a, long b, long MOD) {
if (b == 0) {
return 1;
}
if (b == 1) {
return a;
}
long val = pow(a, b / 2, MOD);
if (b % 2 == 0) {
return val * val % MOD;
} else {
return val * (val * a % MOD) % MOD;
}
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner() throws FileNotFoundException {
// System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
br = new BufferedReader(new InputStreamReader(System.in));
// br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
throw new RuntimeException();
}
}
return st.nextToken();
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
st = null;
try {
return br.readLine();
} catch (Exception e) {
throw new RuntimeException();
}
}
public boolean endLine() {
try {
String next = br.readLine();
while (next != null && next.trim().isEmpty()) {
next = br.readLine();
}
if (next == null) {
return true;
}
st = new StringTokenizer(next);
return st.hasMoreTokens();
} catch (Exception e) {
throw new RuntimeException();
}
}
}
}
| Java | ["cab", "acdb"] | 1 second | ["abc", "abdc"] | null | Java 8 | standard input | [
"data structures",
"greedy",
"strings"
] | e758ae072b8aed53038e4593a720276d | First line contains non-empty string s (1 ≤ |s| ≤ 105), consisting of lowercase English letters. | 1,700 | Print resulting string u. | standard output | |
PASSED | 1dc251cffb1238f49aa066d81d98542d | train_001.jsonl | 1492266900 | Petya recieved a gift of a string s with length up to 105 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal.You should write a program that will help Petya win the game. | 256 megabytes | import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Stack;
import java.util.StringTokenizer;
public class C2
{
static PrintWriter out = new PrintWriter(
new BufferedOutputStream(System.out));
static MyScanner sc = new MyScanner(System.in);
static StringBuilder output = new StringBuilder();
public static void main(String... args)
{
String s = sc.next();
Stack<Character> t = new Stack<Character>();
int sIndex = 0;
for (char i = 'a'; i <= 'z'; i++)
{
while (!t.isEmpty() && t.peek() <= i)
{
output.append(t.pop());
}
int goal = s.indexOf(i, sIndex);
while (goal >= 0)
{
for (; sIndex < goal; sIndex++)
{
t.push(s.charAt(sIndex));
}
output.append(i);
sIndex++;
goal = s.indexOf(i, sIndex);
}
}
while (!t.isEmpty())
{
output.append(t.pop());
}
eOP();
}
private static void eOP()
{
System.out.println(output);
System.exit(0);
}
/**
* Flatfoot's Scanner with slight modifications.
*
* @author <a href="http://codeforces.com/profile/Flatfoot">Flatfoot</a>
* @see <a href="http://codeforces.com/blog/entry/7018">Source</a>
*/
private static class MyScanner
{
BufferedReader br;
StringTokenizer st;
MyScanner(InputStream in)
{
this.br = new BufferedReader(new InputStreamReader(in));
}
String next()
{
while (this.st == null || !this.st.hasMoreElements())
{
try
{
this.st = new StringTokenizer(this.br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return this.st.nextToken();
}
int nextInt()
{
return Integer.parseInt(this.next());
}
long nextLong()
{
return Long.parseLong(this.next());
}
double nextDouble()
{
return Double.parseDouble(this.next());
}
/**
* This method has been modified to be a bit more similar to Scanner's
*
* @author <a href=
* "http://codeforces.com/profile/Flatfoot">Flatfoot</a>,
* <a href=
* "http://codeforces.com/profile/JasonBaby">JasonBaby</a>
*/
String nextLine()
{
if (this.st == null || !this.st.hasMoreElements())
{
String str = "";
try
{
str = this.br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
else
{
StringBuilder str = new StringBuilder();
while (this.st.hasMoreElements())
{
str.append(this.st.nextToken());
str.append(" ");
}
str.deleteCharAt(str.length() - 1);
return str.toString();
}
}
}
}
| Java | ["cab", "acdb"] | 1 second | ["abc", "abdc"] | null | Java 8 | standard input | [
"data structures",
"greedy",
"strings"
] | e758ae072b8aed53038e4593a720276d | First line contains non-empty string s (1 ≤ |s| ≤ 105), consisting of lowercase English letters. | 1,700 | Print resulting string u. | standard output | |
PASSED | c34552323b35473af62e1333a1c2386b | train_001.jsonl | 1492266900 | Petya recieved a gift of a string s with length up to 105 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal.You should write a program that will help Petya win the game. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
import java.util.Stack;
public class CandidateCode {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int arr[] = new int[26];
String s = sc.next();
for (int i = 0; i < s.length(); i++) {
arr[s.charAt(i) - 'a']++;
}
Stack<Character> st = new Stack<>();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
boolean backCheck = false;
// System.out.println(backCheck+" "+s.charAt(i));
st.push(s.charAt(i));
arr[s.charAt(i)-'a']--;
for (int j = s.charAt(i) - 'a' - 1; j >= 0; j--) {
if (arr[j] > 0) {
backCheck = true;
break;
}
}
while (!st.isEmpty() && !backCheck) {
char popped = st.pop();
// System.out.println(popped);
sb.append(popped);
if (!st.isEmpty()) {
backCheck = false;
for (int j = st.peek() - 'a' - 1; j >= 0; j--) {
if (arr[j] > 0) {
backCheck = true;
break;
}
}
}
}
}
while (!st.isEmpty()) {
sb.append(st.pop());
}
System.out.println(sb);
}
}
| Java | ["cab", "acdb"] | 1 second | ["abc", "abdc"] | null | Java 8 | standard input | [
"data structures",
"greedy",
"strings"
] | e758ae072b8aed53038e4593a720276d | First line contains non-empty string s (1 ≤ |s| ≤ 105), consisting of lowercase English letters. | 1,700 | Print resulting string u. | standard output | |
PASSED | 32d52e2dc8227c8ba380d1ed0b9ac143 | train_001.jsonl | 1492266900 | Petya recieved a gift of a string s with length up to 105 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal.You should write a program that will help Petya win the game. | 256 megabytes | import java.io.*;
import java.util.*;
import java.text.*;
import java.lang.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.regex.*;
public class Myclass {
// public static ArrayList a[]=new ArrayList[300001];
public static void main(String[] args)
{
InputReader in = new InputReader(System.in);
PrintWriter pw = new PrintWriter(System.out);
char s[]=in.nextLine().toCharArray();
int dp[][]=new int[s.length][26];
for(int i=s.length-1;i>=0;i--) {
if(i==s.length-1) {
dp[i][s[i]-'a']++;
continue;
}
for(int j=0;j<26;j++) {
dp[i][j]=dp[i+1][j];
}
dp[i][s[i]-'a']++;
}
int i=0;
Stack<Character>st=new Stack<>();
Vector<Character>fi=new Vector<>();
for(int j=0;j<26 && i<s.length;j++) {
if(dp[i][j]>0)
{
while(!st.isEmpty() && i<s.length && st.peek()<=(char)(j+'a')) {
fi.add(st.pop());
}
while(i<s.length && dp[i][j]>0) {
if(s[i]!=(char)(j+'a')) {
st.add(s[i]);
i++;
}
else
{
fi.add(s[i]);
i++;
}
}
}
else
{
while( !st.isEmpty() && i<s.length && st.peek()<=(char)(j+'a')) {
fi.add(st.pop());
}
}
}
while(!st.isEmpty()) {
fi.add(st.pop());
}
for(int k=0;k<fi.size();k++) {
pw.print(fi.get(k));
}
pw.flush();
pw.close();
}
static class InputReader
{
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream)
{
this.stream = stream;
}
public int snext()
{
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars)
{
curChar = 0;
try
{
snumChars = stream.read(buf);
}
catch (IOException e)
{
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = snext();
}
int res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = snext();
}
long res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n)
{
int a[] = new int[n];
for (int i = 0; i < n; i++)
{
a[i] = nextInt();
}
return a;
}
static class tri implements Comparable<tri> {
int p, c, l;
tri(int p, int c, int l) {
this.p = p;
this.c = c;
this.l = l;
}
public int compareTo(tri o) {
return Integer.compare(l, o.l);
}
}
public String readString()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine()
{
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = snext();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c)
{
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c)
{
return c == '\n' || c == '\r' || c == -1;
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
}
public static long mod = 1000000007;
public static int d;
public static int p;
public static int q;
public static int[] suffle(int[] a,Random gen)
{
int n = a.length;
for(int i=0;i<n;i++)
{
int ind = gen.nextInt(n-i)+i;
int temp = a[ind];
a[ind] = a[i];
a[i] = temp;
}
return a;
}
public static void swap(int a, int b){
int temp = a;
a = b;
b = temp;
}
public static HashSet<Integer> primeFactorization(int n)
{
HashSet<Integer> a =new HashSet<Integer>();
for(int i=2;i*i<=n;i++)
{
while(n%i==0)
{
a.add(i);
n/=i;
}
}
if(n!=1)
a.add(n);
return a;
}
public static void sieve(boolean[] isPrime,int n)
{
for(int i=1;i<n;i++)
isPrime[i] = true;
isPrime[0] = false;
isPrime[1] = false;
for(int i=2;i*i<n;i++)
{
if(isPrime[i] == true)
{
for(int j=(2*i);j<n;j+=i)
isPrime[j] = false;
}
}
}
public static int GCD(int a,int b)
{
if(b==0)
return a;
else
return GCD(b,a%b);
}
public static long GCD(long a,long b)
{
if(b==0)
return a;
else
return GCD(b,a%b);
}
public static void extendedEuclid(int A,int B)
{
if(B==0)
{
d = A;
p = 1 ;
q = 0;
}
else
{
extendedEuclid(B, A%B);
int temp = p;
p = q;
q = temp - (A/B)*q;
}
}
public static long LCM(long a,long b)
{
return (a*b)/GCD(a,b);
}
public static int LCM(int a,int b)
{
return (a*b)/GCD(a,b);
}
public static int binaryExponentiation(int x,int n)
{
int result=1;
while(n>0)
{
if(n % 2 ==1)
result=result * x;
x=x*x;
n=n/2;
}
return result;
}
public static long binaryExponentiation(long x,long n)
{
long result=1;
while(n>0)
{
if(n % 2 ==1)
result=result * x;
x=x*x;
n=n/2;
}
return result;
}
public static int modularExponentiation(int x,int n,int M)
{
int result=1;
while(n>0)
{
if(n % 2 ==1)
result=(result * x)%M;
x=(x%M*x)%M;
n=n/2;
}
return result;
}
public static long modularExponentiation(long x,long n,long M)
{
long result=1;
while(n>0)
{
if(n % 2 ==1)
result=(result%M * x%M)%M;
x=(x%M * x%M)%M;
n=n/2;
}
return result;
}
public static long modInverse(int A,int M)
{
return modularExponentiation(A,M-2,M);
}
public static long modInverse(long A,long M)
{
return modularExponentiation(A,M-2,M);
}
public static boolean isPrime(int n)
{
if (n <= 1) return false;
if (n <= 3) return true;
if (n%2 == 0 || n%3 == 0)
return false;
for (int i=5; i*i<=n; i=i+6)
{
if (n%i == 0 || n%(i+2) == 0)
return false;
}
return true;
}
public static long[] shuffle(long[] a, Random gen){
for(int i = 0, n = a.length;i < n;i++){
int ind = gen.nextInt(n-i)+i;
long d = a[i];
a[i] = a[ind];
a[ind] = d;
}
return a;
}
static class pair implements Comparable<pair>
{
Long x;
Long y;
pair(long a,long l)
{
this.x=a;
this.y=l;
}
public int compareTo(pair o) {
int result = x.compareTo(o.x);
if(result==0)
result = y.compareTo(o.y);
return result;
}
public String toString()
{
return x+" "+y;
}
public boolean equals(Object o)
{
if (o instanceof pair)
{
pair p = (pair)o;
return p.x == x && p.y == y ;
}
return false;
}
public int hashCode()
{
return new Long(x).hashCode()*31 + new Long(y).hashCode();
}
}
} | Java | ["cab", "acdb"] | 1 second | ["abc", "abdc"] | null | Java 8 | standard input | [
"data structures",
"greedy",
"strings"
] | e758ae072b8aed53038e4593a720276d | First line contains non-empty string s (1 ≤ |s| ≤ 105), consisting of lowercase English letters. | 1,700 | Print resulting string u. | standard output | |
PASSED | 8575060c2de8637e8e0c21b5181db427 | train_001.jsonl | 1492266900 | Petya recieved a gift of a string s with length up to 105 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal.You should write a program that will help Petya win the game. | 256 megabytes |
import java.util.*;
public class ms {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] ary = new int[127];
String s = in.nextLine();
for ( int i = 0; i < s.length(); i++ ) {
ary[s.charAt(i)]++;
}
Stack<Character> st = new Stack<>();
StringBuilder sb = new StringBuilder();
for ( int i = 0; i < s.length(); i++ ) {
char c = s.charAt(i);
st.push(c);
ary[st.peek()]--;
while( !st.empty() && isGreater(st.peek(),ary) ) {
sb.append(Character.toString(st.pop()));
}
}
System.out.println(sb.toString());
}
public static boolean isGreater(char c, int[] ary) {
for ( int i = 64; i < c; i++) {
if (ary[i] != 0)
return false;
}
return true;
}
} | Java | ["cab", "acdb"] | 1 second | ["abc", "abdc"] | null | Java 8 | standard input | [
"data structures",
"greedy",
"strings"
] | e758ae072b8aed53038e4593a720276d | First line contains non-empty string s (1 ≤ |s| ≤ 105), consisting of lowercase English letters. | 1,700 | Print resulting string u. | standard output | |
PASSED | 5139bb173b3740df3078caa2c0b18e21 | train_001.jsonl | 1492266900 | Petya recieved a gift of a string s with length up to 105 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal.You should write a program that will help Petya win the game. | 256 megabytes | import java.io.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
public class Main {
private FastScanner in;
private PrintWriter out;
void solve() throws IOException {
String s = in.nextString();
StringBuffer t = new StringBuffer("");
StringBuffer u = new StringBuffer("");
Map<Character, Integer> rightmostPos = new HashMap<>();
char c;
int len = s.length();
for (int i = 0; i < len; i++) {
rightmostPos.put(s.charAt(i), i);
}
for (int i = 0; i < len; i++) {
c = s.charAt(i);
if (t.length() == 0 || t.charAt(t.length() - 1) > c) {
t.append(c);
} else {
boolean lowerToCome = false;
char top = t.charAt(t.length() - 1);
for (char j = 'a'; (j < top) && !lowerToCome; j++) {
lowerToCome = rightmostPos.getOrDefault(j, 0) > i;
}
while (!lowerToCome && t.length() > 0 && t.charAt(t.length() - 1) <= c) {
String tmp = t.toString();
int idx = tmp.length() -1;
while (idx >= 0 && tmp.charAt(idx) <= top ) {
idx--;
}
for (int j = tmp.length() - 1; j > idx; j--) {
u.append(t.charAt(j));
}
if (idx < 0) {
t.replace(0, t.length(), "");
} else {
t.replace(idx+1, t.length(), "");
}
if (t.length() > 0) {
top = t.charAt(t.length() - 1);
for (char j = 'a'; (j < top) && !lowerToCome; j++) {
lowerToCome = rightmostPos.getOrDefault(j, 0) > i;
}
}
}
t.append(c);
}
}
for (int i = t.length() - 1; i >= 0; i--) {
u.append(t.charAt(i));
}
out.println(u.toString());
out.flush();
}
Main() {
in = new FastScanner();
out = new PrintWriter(System.out);
}
public static void main(String[] args) throws IOException {
new Main().solve();
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastScanner(String s) {
try {
br = new BufferedReader(new FileReader(s));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
}
}
return st.nextToken();
}
public String nextString() { return nextToken(); }
public int nextInt() {
return Integer.parseInt(nextToken());
}
public long nextLong() {
return Long.parseLong(nextToken());
}
public double nextDouble() {
return Double.parseDouble(nextToken());
}
}
} | Java | ["cab", "acdb"] | 1 second | ["abc", "abdc"] | null | Java 8 | standard input | [
"data structures",
"greedy",
"strings"
] | e758ae072b8aed53038e4593a720276d | First line contains non-empty string s (1 ≤ |s| ≤ 105), consisting of lowercase English letters. | 1,700 | Print resulting string u. | standard output | |
PASSED | a16e3fb7d115efa785a561e9a1995232 | train_001.jsonl | 1492266900 | Petya recieved a gift of a string s with length up to 105 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal.You should write a program that will help Petya win the game. | 256 megabytes | import java.io.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
public class Main {
private FastScanner in;
private PrintWriter out;
void solve() throws IOException {
String s = in.nextString();
StringBuffer t = new StringBuffer("");
StringBuffer u = new StringBuffer("");
Map<Character, Integer> rightmostPos = new HashMap<>();
char c;
int len = s.length();
for (int i = 0; i < len; i++) {
rightmostPos.put(s.charAt(i), i);
}
for (int i = 0; i < len; i++) {
c = s.charAt(i);
if (t.length() == 0 || t.charAt(t.length() - 1) > c) {
t.append(c);
} else {
boolean lowerToCome = false;
char top = t.charAt(t.length() - 1);
for (char j = 'a'; (j < top) && !lowerToCome; j++) {
lowerToCome = rightmostPos.getOrDefault(j, 0) > i;
}
while (!lowerToCome && t.length() > 0 && t.charAt(t.length() - 1) <= c) {
String tmp = t.toString();
int idx = tmp.length() -1;
while (idx >= 0 && tmp.charAt(idx) <= top ) {
idx--;
}
for (int j = tmp.length() - 1; j > idx; j--) {
u.append(t.charAt(j));
}
if (idx < 0) {
t.replace(0, t.length(), "");
} else {
t.replace(idx+1, t.length(), "");
}
if (t.length() > 0) {
top = t.charAt(t.length() - 1);
for (char j = 'a'; (j < top) && !lowerToCome; j++) {
lowerToCome = rightmostPos.getOrDefault(j, 0) > i;
}
}
}
t.append(c);
}
}
for (int i = t.length() - 1; i >= 0; i--) {
u.append(t.charAt(i));
}
out.println(u.toString());
out.flush();
}
Main() {
in = new FastScanner();
out = new PrintWriter(System.out);
}
public static void main(String[] args) throws IOException {
new Main().solve();
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastScanner(String s) {
try {
br = new BufferedReader(new FileReader(s));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
}
}
return st.nextToken();
}
public String nextString() { return nextToken(); }
public int nextInt() {
return Integer.parseInt(nextToken());
}
public long nextLong() {
return Long.parseLong(nextToken());
}
public double nextDouble() {
return Double.parseDouble(nextToken());
}
}
} | Java | ["cab", "acdb"] | 1 second | ["abc", "abdc"] | null | Java 8 | standard input | [
"data structures",
"greedy",
"strings"
] | e758ae072b8aed53038e4593a720276d | First line contains non-empty string s (1 ≤ |s| ≤ 105), consisting of lowercase English letters. | 1,700 | Print resulting string u. | standard output | |
PASSED | 6e683c7af45a7e4d8bc5d4ebe83bd1c1 | train_001.jsonl | 1492266900 | Petya recieved a gift of a string s with length up to 105 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal.You should write a program that will help Petya win the game. | 256 megabytes | import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
public class Minimal{//PRACTICE18
public static void main(String args[]){
FastScanner sc=new FastScanner(System.in);
String s=sc.nextLine();
int n=s.length();
Stack<Character> s1=new Stack<>();
TreeMap<Character,Integer> map = new TreeMap<>();
for(int i=0;i<n;i++){
char ch=s.charAt(i);
if(map.containsKey(ch)){
int k=map.get(ch);
map.put(ch,k+1);
}
else
map.put(ch,1);
}
String q="";
int k=0;
while(!map.isEmpty() || !s1.isEmpty()){
if(map.isEmpty()){
while(!s1.isEmpty())
//q=q+s1.pop();
System.out.print(s1.pop());
}
else if(s1.isEmpty() || s1.peek()>map.firstKey()){
char ch=s.charAt(k);
s1.push(ch);
int x=map.get(ch);
if(x==1)
map.remove(ch);
else
map.put(ch,x-1);
k++;
}
else{
//q=q+s1.pop();
System.out.print(s1.pop());
}
}
System.out.println();
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner(InputStream stream) {
try {
br = new BufferedReader(new
InputStreamReader(stream));
} catch (Exception e) {
e.printStackTrace();
}
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
String nextLine() {
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
int nextInt() {
return Integer.parseInt(next());
}
}
} | Java | ["cab", "acdb"] | 1 second | ["abc", "abdc"] | null | Java 8 | standard input | [
"data structures",
"greedy",
"strings"
] | e758ae072b8aed53038e4593a720276d | First line contains non-empty string s (1 ≤ |s| ≤ 105), consisting of lowercase English letters. | 1,700 | Print resulting string u. | standard output | |
PASSED | c436cf659e64a20fd1ea4a1ffb650a49 | train_001.jsonl | 1492266900 | Petya recieved a gift of a string s with length up to 105 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal.You should write a program that will help Petya win the game. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC {
public void solve(int testNumber, InputReader in, OutputWriter out) {
char[] s = in.next().toCharArray();
int n = s.length;
char[] res = new char[n];
int sz = 0;
int[] nxt = new int[n];
Arrays.fill(nxt, -1);
int lastMin = -1;
char cur = 'z';
char[] Q = new char[n];
int st = 0, en = 0;
for (int i = n - 1; i >= 0; i--) {
if (s[i] <= cur) {
nxt[i] = lastMin;
lastMin = i;
cur =s[i];
}
}
for (int i = 0; i < n; i++) {
int nxtIdx = nxt[i];
if (nxtIdx == -1) {
Q[en++] = s[i];
} else {
res[sz++] = s[i];
while (en > st && Q[en - 1] <= s[nxtIdx]) {
res[sz++] = Q[--en];
}
}
}
while (en > st) {
res[sz++] = Q[--en];
}
out.println(new String(res));
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void println(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (this.numChars == -1) {
throw new InputMismatchException();
} else {
if (this.curChar >= this.numChars) {
this.curChar = 0;
try {
this.numChars = this.stream.read(this.buf);
} catch (IOException var2) {
throw new InputMismatchException();
}
if (this.numChars <= 0) {
return -1;
}
}
return this.buf[this.curChar++];
}
}
public String next() {
int c;
while (isSpaceChar(c = this.read())) {
;
}
StringBuilder result = new StringBuilder();
result.appendCodePoint(c);
while (!isSpaceChar(c = this.read())) {
result.appendCodePoint(c);
}
return result.toString();
}
public static boolean isSpaceChar(int c) {
return c == 32 || c == 10 || c == 13 || c == 9 || c == -1;
}
}
}
| Java | ["cab", "acdb"] | 1 second | ["abc", "abdc"] | null | Java 8 | standard input | [
"data structures",
"greedy",
"strings"
] | e758ae072b8aed53038e4593a720276d | First line contains non-empty string s (1 ≤ |s| ≤ 105), consisting of lowercase English letters. | 1,700 | Print resulting string u. | standard output | |
PASSED | cd341778f244bfee4a14a3a11777d40a | train_001.jsonl | 1492266900 | Petya recieved a gift of a string s with length up to 105 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal.You should write a program that will help Petya win the game. | 256 megabytes | import java.util.*;
import java.io.*;
import java.text.*;
//Solution Credits: Taranpreet Singh
public class Main{
//SOLUTION BEGIN
void solve(int TC) throws Exception{
String s = n();
TreeMap<Character, Integer> set = new TreeMap<>();
for(int i = 0; i< s.length(); i++){
set.put(s.charAt(i), set.getOrDefault(s.charAt(i), 0)+1);
}
Stack<Character> st = new Stack<>();
StringBuilder ans = new StringBuilder("");
for(int i = 0; i< s.length();i++){
while(!st.isEmpty() && set.firstKey() >= st.peek())
ans.append(st.pop());
char c = s.charAt(i);
int g = set.get(c);
if(g==1)set.remove(c);
else set.put(c, g-1);
st.add(s.charAt(i));
}
while(!st.isEmpty())ans.append(st.pop());
pn(ans.toString());
}
//SOLUTION ENDS
long mod = (int)1e9+7, IINF = (long)1e10;
final int MAX = (int)3e5+1, INF = (int)1e9, root = 3;
DecimalFormat df = new DecimalFormat("0.000000000000");
double PI = 3.141592653589793238462643383279502884197169399375105820974944, eps = 1e-8;
static boolean multipleTC = false, memory = false;
FastReader in;PrintWriter out;
void run() throws Exception{
in = new FastReader();
out = new PrintWriter(System.out);
int T = (multipleTC)?ni():1;
//Solution Credits: Taranpreet Singh
for(int i = 1; i<= T; i++)solve(i);
out.flush();
out.close();
}
public static void main(String[] args) throws Exception{
if(memory)new Thread(null, new Runnable() {public void run(){try{new Main().run();}catch(Exception e){e.printStackTrace();}}}, "1", 1 << 28).start();
else new Main().run();
}
long gcd(long a, long b){return (b==0)?a:gcd(b,a%b);}
int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);}
int bit(long n){return (n==0)?0:(1+bit(n&(n-1)));}
void p(Object o){out.print(o);}
void pn(Object o){out.println(o);}
void pni(Object o){out.println(o);out.flush();}
String n(){return in.next();}
String nln(){return in.nextLine();}
int ni(){return Integer.parseInt(in.next());}
long nl(){return Long.parseLong(in.next());}
double nd(){return Double.parseDouble(in.next());}
class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws Exception{
br = new BufferedReader(new FileReader(s));
}
String next(){
while (st == null || !st.hasMoreElements()){
try{
st = new StringTokenizer(br.readLine());
}catch (IOException e){
e.printStackTrace();
}
}
return st.nextToken();
}
String nextLine(){
String str = "";
try{
str = br.readLine();
}catch (IOException e){
e.printStackTrace();
}
return str;
}
}
} | Java | ["cab", "acdb"] | 1 second | ["abc", "abdc"] | null | Java 8 | standard input | [
"data structures",
"greedy",
"strings"
] | e758ae072b8aed53038e4593a720276d | First line contains non-empty string s (1 ≤ |s| ≤ 105), consisting of lowercase English letters. | 1,700 | Print resulting string u. | standard output | |
PASSED | b1e340375c0d214b8012f2b44c80da9f | train_001.jsonl | 1492266900 | Petya recieved a gift of a string s with length up to 105 characters for his birthday. He took two more empty strings t and u and decided to play a game. This game has two possible moves: Extract the first character of s and append t with this character. Extract the last character of t and append u with this character. Petya wants to get strings s and t empty and string u lexicographically minimal.You should write a program that will help Petya win the game. | 256 megabytes | import java.util.*;
public class ms{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
int[] arr = new int[26];
for(int i=0;i<s.length();i++){
arr[s.charAt(i)-'a']++;
}
String res="";
Stack<Character> st = new Stack<Character>();
StringBuilder sb = new StringBuilder();
for(int i=0;i<s.length();i++){
// if(minimal(arr,s.charAt(i))){
// res=res+s.charAt(i);
// arr[s.charAt(i)-'a']--;
// }
// else{
// st.push(s.charAt(i));
// }
char c = s.charAt(i);
st.push(c);
arr[c-'a']--;
while(!st.isEmpty() && minimal(arr,st.peek())){
sb.append(st.pop());
}
}
// while(!st.isEmpty()){
// res=res+st.pop();
// }
System.out.println(sb);
}
public static boolean minimal(int[] arr,char c){
int p = c-'a';
//System.out.println(p);
for(int i=0;i<p;i++){
// System.out.println(arr[i]);
if(arr[i]!=0){
return false;
}
}
return true;
}
} | Java | ["cab", "acdb"] | 1 second | ["abc", "abdc"] | null | Java 8 | standard input | [
"data structures",
"greedy",
"strings"
] | e758ae072b8aed53038e4593a720276d | First line contains non-empty string s (1 ≤ |s| ≤ 105), consisting of lowercase English letters. | 1,700 | Print resulting string u. | standard output | |
PASSED | 9c15ffda338c3e9c37ba19f4fd660a75 | train_001.jsonl | 1594479900 | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given $$$n$$$ nonnegative integers $$$a_1, a_2, \dots, a_n$$$ arranged in a circle, where $$$n$$$ must be odd (ie. $$$n-1$$$ is divisible by $$$2$$$). Formally, for all $$$i$$$ such that $$$2 \leq i \leq n$$$, the elements $$$a_{i - 1}$$$ and $$$a_i$$$ are considered to be adjacent, and $$$a_n$$$ and $$$a_1$$$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;
public class TaskD {
public static void main(String[] arg) {
final FastScanner in = new FastScanner(System.in);
final PrintWriter out = new PrintWriter(System.out);
final int n = in.nextInt();
final long [] aa = new long [n];
for(int i=0;i<n;i++) {
aa[i]=in.nextLong();
}
if(n==1){
out.println(aa[0]);
out.close();
in.close();
return;
}
long max = solutionTwoArrays(n, aa);
reverse(n,aa);
max = Math.max(solutionTwoArrays(n,aa), max);
out.println(max);
out.close();
in.close();
}
static void reverse(int n, long [] aa){
for(int i=0;i<n/2; i++){
long temp = aa[i];
aa[i]=aa[n-i-1];
aa[n-i-1]=temp;
}
}
private static long solution(int n, long [] a){
long evenSum = 0;
int m = (n+1)/2;
for(int i=0;i<m;i++){
evenSum +=a[2*i];
}
long max = evenSum;
for(int i=0;i<=n-2;i++){
evenSum-=a[(2*i)%n];
evenSum+=a[(2*(i+m))%n];
max = Math.max(max, evenSum);
}
return max;
}
private static long solutionTwoArrays(int n, long[] aa) {
//aa -> [1,2,3,4,5]
//prefix ->[1,4,9]
long [] prefixSum = new long[n/2+1];
{ int j=0;
prefixSum[j] = aa[0];
j++;
for (int i = 2; i < n; i += 2) {
prefixSum[j]=prefixSum[j-1]+aa[i];
j++;
}
}
//suffix -> [6,4]
long [] sufixSum = new long[n/2+1];
{
int j=(n/2)-1;
sufixSum[j]=aa[n-2];
j--;
for(int i=n-4;i>=0;i-=2){
sufixSum[j]=sufixSum[j+1]+aa[i];
j--;
}
}
long max = 0;
for(int i=0;i<=n/2;i++){
max = Math.max(sufixSum[i]+prefixSum[i], max);
}
max = Math.max(aa[n-1]+sufixSum[0],max);
max = Math.max(aa[0]+sufixSum[0],max);
// max = Math.max(prefixSum[n/2]-aa[0]+aa[1],max);
return max;
}
private static class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner(InputStream stream) {
try {
br = new BufferedReader(new InputStreamReader(stream));
} catch (Exception e) {
e.printStackTrace();
}
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readIntArr(int n) {
int[] result = new int[n];
for (int i = 0; i < n; i++) {
result[i] = Integer.parseInt(next());
}
return result;
}
void close() {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
long nextLong() {
return Long.parseLong(next());
}
}
}
| Java | ["3\n7 10 2", "1\n4"] | 2 seconds | ["17", "4"] | NoteFor the first test case, here's how a circular value of $$$17$$$ is obtained:Pick the number at index $$$3$$$. The sum of adjacent elements equals $$$17$$$. Delete $$$7$$$ and $$$10$$$ from the circle and replace $$$2$$$ with $$$17$$$.Note that the answer may not fit in a $$$32$$$-bit integer. | Java 8 | standard input | [
"dp",
"greedy",
"games",
"brute force"
] | a9473e6ec81c10c4f88973ac2d60ad04 | The first line contains one odd integer $$$n$$$ ($$$1 \leq n < 2 \cdot 10^5$$$, $$$n$$$ is odd) — the initial size of the circle. The second line contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$0 \leq a_{i} \leq 10^9$$$) — the initial numbers in the circle. | 2,100 | Output the maximum possible circular value after applying some sequence of operations to the given circle. | standard output | |
PASSED | 3f3aeb5babbac0891a8a7a8123764601 | train_001.jsonl | 1594479900 | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given $$$n$$$ nonnegative integers $$$a_1, a_2, \dots, a_n$$$ arranged in a circle, where $$$n$$$ must be odd (ie. $$$n-1$$$ is divisible by $$$2$$$). Formally, for all $$$i$$$ such that $$$2 \leq i \leq n$$$, the elements $$$a_{i - 1}$$$ and $$$a_i$$$ are considered to be adjacent, and $$$a_n$$$ and $$$a_1$$$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;
public class TaskD {
public static void main(String[] arg) {
final FastScanner in = new FastScanner(System.in);
final PrintWriter out = new PrintWriter(System.out);
final int n = in.nextInt();
final long [] aa = new long [n];
for(int i=0;i<n;i++) {
aa[i]=in.nextLong();
}
if(n==1){
out.println(aa[0]);
out.close();
in.close();
return;
}
long max = solutionTwoArrays(n, aa);
reverse(n,aa);
max = Math.max(solutionTwoArrays(n,aa), max);
out.println(max);
out.close();
in.close();
}
static void reverse(int n, long [] aa){
for(int i=0;i<n/2; i++){
long temp = aa[i];
aa[i]=aa[n-i-1];
aa[n-i-1]=temp;
}
}
private static long solution(int n, long [] a){
long evenSum = 0;
int m = (n+1)/2;
for(int i=0;i<m;i++){
evenSum +=a[2*i];
}
long max = evenSum;
for(int i=0;i<=n-2;i++){
evenSum-=a[(2*i)%n];
evenSum+=a[(2*(i+m))%n];
max = Math.max(max, evenSum);
}
return max;
}
private static long solutionTwoArrays(int n, long[] aa) {
//aa -> [1,2,3,4,5]
//prefix ->[1,4,9]
long [] prefixSum = new long[n/2+1];
{ int j=0;
prefixSum[j] = aa[0];
j++;
for (int i = 2; i < n; i += 2) {
prefixSum[j]=prefixSum[j-1]+aa[i];
j++;
}
}
//suffix -> [6,4]
long [] sufixSum = new long[n/2+1];
{
int j=(n/2)-1;
sufixSum[j]=aa[n-2];
j--;
for(int i=n-4;i>=0;i-=2){
sufixSum[j]=sufixSum[j+1]+aa[i];
j--;
}
}
long max = 0;
for(int i=0;i<=n/2;i++){
max = Math.max(sufixSum[i]+prefixSum[i], max);
}
max = Math.max(aa[n-1]+sufixSum[0],max);
max = Math.max(aa[0]+sufixSum[0],max);
max = Math.max(prefixSum[n/2]-aa[0]+aa[1],max);
return max;
}
private static class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner(InputStream stream) {
try {
br = new BufferedReader(new InputStreamReader(stream));
} catch (Exception e) {
e.printStackTrace();
}
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readIntArr(int n) {
int[] result = new int[n];
for (int i = 0; i < n; i++) {
result[i] = Integer.parseInt(next());
}
return result;
}
void close() {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
long nextLong() {
return Long.parseLong(next());
}
}
}
| Java | ["3\n7 10 2", "1\n4"] | 2 seconds | ["17", "4"] | NoteFor the first test case, here's how a circular value of $$$17$$$ is obtained:Pick the number at index $$$3$$$. The sum of adjacent elements equals $$$17$$$. Delete $$$7$$$ and $$$10$$$ from the circle and replace $$$2$$$ with $$$17$$$.Note that the answer may not fit in a $$$32$$$-bit integer. | Java 8 | standard input | [
"dp",
"greedy",
"games",
"brute force"
] | a9473e6ec81c10c4f88973ac2d60ad04 | The first line contains one odd integer $$$n$$$ ($$$1 \leq n < 2 \cdot 10^5$$$, $$$n$$$ is odd) — the initial size of the circle. The second line contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$0 \leq a_{i} \leq 10^9$$$) — the initial numbers in the circle. | 2,100 | Output the maximum possible circular value after applying some sequence of operations to the given circle. | standard output | |
PASSED | f60396288019f1d7240a82f77852c6f9 | train_001.jsonl | 1594479900 | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given $$$n$$$ nonnegative integers $$$a_1, a_2, \dots, a_n$$$ arranged in a circle, where $$$n$$$ must be odd (ie. $$$n-1$$$ is divisible by $$$2$$$). Formally, for all $$$i$$$ such that $$$2 \leq i \leq n$$$, the elements $$$a_{i - 1}$$$ and $$$a_i$$$ are considered to be adjacent, and $$$a_n$$$ and $$$a_1$$$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
DOmkarAndCircle solver = new DOmkarAndCircle();
solver.solve(1, in, out);
out.close();
}
static class DOmkarAndCircle {
public void solve(int testNumber, Scanner sc, PrintWriter pw) {
int n = sc.nextInt();
int[] arr = new int[n];
int[] arr2 = new int[n + (n + 1) / 2];
for (int i = 0; i < n; i++)
arr[i] = sc.nextInt();
for (int i = 0, j = 0; i < n; i += 2, j++) {
arr2[j] = arr[i];
arr2[j + n] = arr[i];
}
for (int i = 1, j = (n + 1) / 2; i < n; i += 2, j++) {
arr2[j] = arr[i];
}
long max = 0;
long sum = 0;
for (int i = 0; i < arr2.length; i++) {
if (i < (n + 1) / 2)
sum += arr2[i];
else {
sum -= arr2[i - (n + 1) / 2];
sum += arr2[i];
}
max = Math.max(max, sum);
}
pw.println(max);
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["3\n7 10 2", "1\n4"] | 2 seconds | ["17", "4"] | NoteFor the first test case, here's how a circular value of $$$17$$$ is obtained:Pick the number at index $$$3$$$. The sum of adjacent elements equals $$$17$$$. Delete $$$7$$$ and $$$10$$$ from the circle and replace $$$2$$$ with $$$17$$$.Note that the answer may not fit in a $$$32$$$-bit integer. | Java 8 | standard input | [
"dp",
"greedy",
"games",
"brute force"
] | a9473e6ec81c10c4f88973ac2d60ad04 | The first line contains one odd integer $$$n$$$ ($$$1 \leq n < 2 \cdot 10^5$$$, $$$n$$$ is odd) — the initial size of the circle. The second line contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$0 \leq a_{i} \leq 10^9$$$) — the initial numbers in the circle. | 2,100 | Output the maximum possible circular value after applying some sequence of operations to the given circle. | standard output | |
PASSED | aab754402194820ef771c52523b9e9bc | train_001.jsonl | 1594479900 | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given $$$n$$$ nonnegative integers $$$a_1, a_2, \dots, a_n$$$ arranged in a circle, where $$$n$$$ must be odd (ie. $$$n-1$$$ is divisible by $$$2$$$). Formally, for all $$$i$$$ such that $$$2 \leq i \leq n$$$, the elements $$$a_{i - 1}$$$ and $$$a_i$$$ are considered to be adjacent, and $$$a_n$$$ and $$$a_1$$$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | 256 megabytes |
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String[] cmd=br.readLine().split(" ");
int n=Integer.valueOf(cmd[0]);
long[] arr=new long[2*n];
cmd=br.readLine().split(" ");
for(int i=0;i<n;i++)
{
arr[i]=Long.valueOf(cmd[i]);
arr[n+i]=arr[i];
}
long ans=0;
long[] sum=new long[2*n];
for(int i=0;i<2*n;i++)
{
sum[i]=arr[i];
if(i-2>=0)
sum[i]=sum[i]+sum[i-2];
}
for(int i=n-1;i<2*n;i++)
{
long x=sum[i];
if(i-n-1>=0)
x=x-sum[i-n-1];
ans=Math.max(x,ans);
}
System.out.println(ans);
}
}
| Java | ["3\n7 10 2", "1\n4"] | 2 seconds | ["17", "4"] | NoteFor the first test case, here's how a circular value of $$$17$$$ is obtained:Pick the number at index $$$3$$$. The sum of adjacent elements equals $$$17$$$. Delete $$$7$$$ and $$$10$$$ from the circle and replace $$$2$$$ with $$$17$$$.Note that the answer may not fit in a $$$32$$$-bit integer. | Java 8 | standard input | [
"dp",
"greedy",
"games",
"brute force"
] | a9473e6ec81c10c4f88973ac2d60ad04 | The first line contains one odd integer $$$n$$$ ($$$1 \leq n < 2 \cdot 10^5$$$, $$$n$$$ is odd) — the initial size of the circle. The second line contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$0 \leq a_{i} \leq 10^9$$$) — the initial numbers in the circle. | 2,100 | Output the maximum possible circular value after applying some sequence of operations to the given circle. | standard output | |
PASSED | fa3fe33e0b99c63fd84e54030e03cfd1 | train_001.jsonl | 1594479900 | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given $$$n$$$ nonnegative integers $$$a_1, a_2, \dots, a_n$$$ arranged in a circle, where $$$n$$$ must be odd (ie. $$$n-1$$$ is divisible by $$$2$$$). Formally, for all $$$i$$$ such that $$$2 \leq i \leq n$$$, the elements $$$a_{i - 1}$$$ and $$$a_i$$$ are considered to be adjacent, and $$$a_n$$$ and $$$a_1$$$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(in);
int n = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
int[] tArr = new int[n];
for (int i = 0; i < n; i++) {
tArr[i] = Integer.parseInt(st.nextToken());
}
int[] sliding = new int[2*n];
int index = 0;
for (int i = 0; i < n; i+=2) {
sliding[index] = tArr[i];
sliding[index+n] = tArr[i];
index++;
}
for (int i = 1; i < n; i+=2) {
sliding[index] = tArr[i];
sliding[index+n] = tArr[i];
index++;
}
int size = (n+1)/2;
long max = 0;
long curSum = 0;
for (int i = 0; i < 2*n; i++) {
curSum += sliding[i];
if (i >= size) {
curSum -= sliding[i-size];
max = Math.max(curSum, max);
}
}
System.out.println(max);
}
} | Java | ["3\n7 10 2", "1\n4"] | 2 seconds | ["17", "4"] | NoteFor the first test case, here's how a circular value of $$$17$$$ is obtained:Pick the number at index $$$3$$$. The sum of adjacent elements equals $$$17$$$. Delete $$$7$$$ and $$$10$$$ from the circle and replace $$$2$$$ with $$$17$$$.Note that the answer may not fit in a $$$32$$$-bit integer. | Java 8 | standard input | [
"dp",
"greedy",
"games",
"brute force"
] | a9473e6ec81c10c4f88973ac2d60ad04 | The first line contains one odd integer $$$n$$$ ($$$1 \leq n < 2 \cdot 10^5$$$, $$$n$$$ is odd) — the initial size of the circle. The second line contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$0 \leq a_{i} \leq 10^9$$$) — the initial numbers in the circle. | 2,100 | Output the maximum possible circular value after applying some sequence of operations to the given circle. | standard output | |
PASSED | 670b7446ec1833cce55588adae98fe0c | train_001.jsonl | 1594479900 | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given $$$n$$$ nonnegative integers $$$a_1, a_2, \dots, a_n$$$ arranged in a circle, where $$$n$$$ must be odd (ie. $$$n-1$$$ is divisible by $$$2$$$). Formally, for all $$$i$$$ such that $$$2 \leq i \leq n$$$, the elements $$$a_{i - 1}$$$ and $$$a_i$$$ are considered to be adjacent, and $$$a_n$$$ and $$$a_1$$$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main implements Runnable{
FastScanner sc;
PrintWriter pw;
final class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
e.printStackTrace();
}
}
public long nlo() {
return Long.parseLong(next());
}
public String next() {
if (st.hasMoreTokens()) return st.nextToken();
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
e.printStackTrace();
}
return st.nextToken();
}
public int ni() {
return Integer.parseInt(next());
}
public String nli() {
String line = "";
if (st.hasMoreTokens()) line = st.nextToken();
else try {
return br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
while (st.hasMoreTokens()) line += " " + st.nextToken();
return line;
}
public double nd() {
return Double.parseDouble(next());
}
}
public static void main(String[] args) throws Exception
{
new Thread(null,new Main(),"codeforces",1<<28).start();
}
public void run()
{
sc=new FastScanner();
pw=new PrintWriter(System.out);
solve();
pw.flush();
pw.close();
}
public long gcd(long a,long b)
{
return b==0L?a:gcd(b,a%b);
}
public long ppow(long a,long b,long mod)
{
if(b==0L)
return 1L;
long tmp=1;
while(b>1L)
{
if((b&1L)==1)
tmp*=a;
a*=a;
a%=mod;
tmp%=mod;
b>>=1;
}
return (tmp*a)%mod;
}
public int gcd(int x,int y)
{
return y==0?x:gcd(y,x%y);
}
//////////////////////////////////
///////////// LOGIC ///////////
////////////////////////////////
public void solve() {
int t=1;
while(t-->0)
{
int n=sc.ni();
long[] arr=new long[n];
for(int i=0;i<n;i++)
arr[i]=sc.ni();
if(n==1)
{
pw.println(arr[0]);
return;
}
long[] prr=new long[n];
long[] srr=new long[n];
prr[0]=arr[0];
prr[1]=arr[1];
for(int i=2;i<n;i++)
prr[i]=arr[i]+prr[i-2];
srr[n-1]=arr[n-1];
srr[n-2]=arr[n-2];
for(int i=n-3;i>=0;i--)
srr[i]=arr[i]+srr[i+2];
long ans=srr[0];
for(int i=1;i<n;i++)
ans=Math.max(ans,srr[i]+prr[i-1]);
pw.println(ans);
}
}
} | Java | ["3\n7 10 2", "1\n4"] | 2 seconds | ["17", "4"] | NoteFor the first test case, here's how a circular value of $$$17$$$ is obtained:Pick the number at index $$$3$$$. The sum of adjacent elements equals $$$17$$$. Delete $$$7$$$ and $$$10$$$ from the circle and replace $$$2$$$ with $$$17$$$.Note that the answer may not fit in a $$$32$$$-bit integer. | Java 8 | standard input | [
"dp",
"greedy",
"games",
"brute force"
] | a9473e6ec81c10c4f88973ac2d60ad04 | The first line contains one odd integer $$$n$$$ ($$$1 \leq n < 2 \cdot 10^5$$$, $$$n$$$ is odd) — the initial size of the circle. The second line contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$0 \leq a_{i} \leq 10^9$$$) — the initial numbers in the circle. | 2,100 | Output the maximum possible circular value after applying some sequence of operations to the given circle. | standard output | |
PASSED | eb43f808f2032013592810968ddab451 | train_001.jsonl | 1594479900 | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given $$$n$$$ nonnegative integers $$$a_1, a_2, \dots, a_n$$$ arranged in a circle, where $$$n$$$ must be odd (ie. $$$n-1$$$ is divisible by $$$2$$$). Formally, for all $$$i$$$ such that $$$2 \leq i \leq n$$$, the elements $$$a_{i - 1}$$$ and $$$a_i$$$ are considered to be adjacent, and $$$a_n$$$ and $$$a_1$$$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | 256 megabytes | import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import java.util.Comparator;
public class scratch_25 {
static class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/**
* call this method to initialize reader for InputStream
*/
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
/**
* get next word
*/
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(
reader.readLine());
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
}
static class ans {
int a;
int b;
public ans(int a, int b) {
this.a = a;
this.b = b;
}
}
static class sort implements Comparator<ans> {
@Override
public int compare(ans a1, ans a2) {
return a1.b - a2.b;
}
}
public static void main(String[] args) throws IOException {
Reader.init(System.in);
int n= Reader.nextInt();
long arr[]= new long[n];
for (int i = 0; i <n ; i++) {
arr[i]= Reader.nextLong();
//arr[i+n]= arr[i];
}
// System.out.println(Arrays.toString(arr)); // we need to calculate even sum for all numbers till n
long arr1[]= new long[n+n];
int count=0;
for (int i = 0; i <n ; i+=2) {
arr1[count]= arr[i];
arr1[count+n]= arr[i];
count++;
}
for (int i = 1; i <n ; i+=2) {
arr1[count]= arr[i];
arr1[count+n]= arr[i];
count++;
}
// System.out.println(Arrays.toString(arr1));
int g=(n+1)/2;
int ptr1=0;
int ptr2= ptr1+g-1;
long sum=0;
for (int i = ptr1; i <=ptr2 ; i++) {
sum+=arr1[i];
}
long max=0;
max= Math.max(sum,max);
//long d=0;
ptr1++;
ptr2++;
while(ptr1<n+1){
// System.out.print(" "+arr1[ptr1]);
//
// System.out.print(" "+arr1[ptr2]);
sum=sum-arr1[ptr1-1]+arr1[ptr2];
// System.out.println(" sum="+sum);
max= Math.max(sum,max);
ptr1++;
ptr2++;
}
System.out.println(max);
}
static int nCr(int n, int r)
{
return fact(n) / (fact(r) *
fact(n - r));
}
// Returns factorial of n
static int fact(int n)
{
int res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
return res;
}
public static long bfs(long x1, long y1, long x2, long y2, long num , HashMap<Long,ArrayList<long[]>> map) {
HashMap<Long, ArrayList<Long>> vis= new HashMap<>();
ArrayList<Long> ar= new ArrayList<>();
ar.add(y1);
vis.put(x1,ar);
LinkedList<long[]> q = new LinkedList<>();
long arr1[] = {x1,y1,num};
q.addLast(arr1);
int[][] arr = {{1, 1}, {1, -1}, {-1, 1}, {-1, -1}, {0, 1}, {0, -1}, {1, 0}, {-1,0}};
// int min= Integer.MAX_VALUE;
while (!q.isEmpty()){
// System.out.println("bs");
long arr2[]= q.removeFirst();
long x= arr2[0];
long y= arr2[1];
long dis= arr2[2];
if (x == x2 && y == y2) {
//vis[x1][y1] = 1;
return dis;
}
for (int i = 0; i <arr.length ; i++) {
// System.out.println("bc");
long xn= x+arr[i][0];
long yn= y+arr[i][1];
long dn= dis+1;
if(map.containsKey(xn)) {
ArrayList<long[]> ar1 = map.get(xn);
boolean b = false;
for (int j = 0; j < ar1.size(); j++) {
long l1 = ar1.get(j)[0];
long l2 = ar1.get(j)[1];
// System.out.println(l1+" "+l2);
if (l1 <= yn && yn <= l2) {
// System.out.println("fuck");
b = true;
break;
}
}
boolean b1 = true;
if(!vis.containsKey(xn) && b){
long ar3[] = {xn, yn, dn};
q.addLast(ar3);
ArrayList<Long> arq= new ArrayList<>();
arq.add(yn);
vis.put(xn,arq);
// vis.get(yn).add(xn);
}
else if(vis.containsKey(xn)){
ArrayList<Long> a = vis.get(xn);
for (int j = 0; j < a.size(); j++) {
if (a.get(j) == yn) {
b1 = false;
break;
}
}
if (b && b1) {
long ar3[] = {xn, yn, dn};
q.addLast(ar3);
vis.get(xn).add(yn);
}
}}
}
}
return -1;
}
public static ArrayList<String> getpath(int curr,int end){
if(curr>end){
ArrayList<String> arr= new ArrayList<>();
return arr;
}
else if(curr==end){
ArrayList<String> arr= new ArrayList<>();
arr.add("");
return arr;
}
ArrayList<String> myarr= new ArrayList<>();
for (int i = 1; i <=6 ; i++) {
ArrayList<String> rarr= getpath(curr+i,end);
for (int j = 0; j <rarr.size() ; j++){
String str= rarr.get(j);
String str1=i+str;
myarr.add(str1);
}
}
return myarr;}
static long fc(long n) {
long count=0;
for (int i = 1; i <=n ; i++) {
if(n%i==0){
count++;
}}
return count;
}
static long pow(long b, long p,long m){
if(p==0){
return 1;
}
long sq= pow(b,p/2,m)%m;
sq=(sq*sq)%m;
if(p%2!=0){
sq=(sq*b)%m;
}
return sq;
}
public static char[] getCharArray(char[] array) {
String _array = "";
for(int i = 0; i < array.length; i++) {
if(_array.indexOf(array[i]) == -1) // check if a char already exist, if not exist then return -1
_array = _array+array[i]; // add new char
}
return _array.toCharArray();
}
static void merge(long arr[], int l, int m, int r)
{
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
long L[] = new long [n1];
long R[] = new long [n2];
/*Copy data to temp arrays*/
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
// Main function that sorts arr[l..r] using
// merge()
static void sort(long arr[], int l, int r)
{
if (l < r)
{
// Find the middle point
int m = (l+r)/2;
// Sort first and second halves
sort(arr, l, m);
sort(arr , m+1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
static boolean ans(int check, long arr[],int n,int med,int k){
long ans=0;
for (int i = med; i <n ; i++) {
if(check>=arr[i]){
ans+=check-arr[i];
}
}
return ans<=k;
}
static boolean isPrime(long n)
{
// Corner cases
if (n <= 1) return false;
if (n <= 3) return true;
// This is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0) return false;
for (long i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static int partition(double arr[],int arr1[],int arr2[], int low, int high)
{
double pivot = arr[high];
int i = (low-1); // index of smaller element
for (int j=low; j<high; j++)
{
// If current element is smaller than the pivot
if (arr[j] < pivot)
{
i++;
// swap arr[i] and arr[j]
double temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
int temp1=arr1[i];
arr1[i]=arr1[j];
arr1[j]=temp1;
int temp2=arr2[i];
arr2[i]=arr2[j];
arr2[j]=temp2;
}
}
// swap arr[i+1] and arr[high] (or pivot)
double temp = arr[i+1];
arr[i+1] = arr[high];
arr[high] = temp;
int temp1= arr1[i+1];
arr1[i+1]=arr1[high];
arr1[high]= temp1;
int temp2= arr2[i+1];
arr2[i+1]=arr2[high];
arr2[high]= temp2;
return i+1;
}
/* The main function that implements QuickSort()
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
static void sort(double arr[],int arr1[],int arr2[], int low, int high)
{
if (low < high)
{
/* pi is partitioning index, arr[pi] is
now at right place */
int pi = partition(arr,arr1,arr2, low, high);
// Recursively sort elements before
// partition and after partition
sort(arr,arr1,arr2, low, pi-1);
sort(arr,arr1, arr2,pi+1, high);
}
}
static int removeDuplicates(int arr[], int n)
{
// Return, if array is empty
// or contains a single element
if (n==0 || n==1)
return n;
int[] temp = new int[n];
// Start traversing elements
int j = 0;
for (int i=0; i<n-1; i++)
// If current element is not equal
// to next element then store that
// current element
if (arr[i] != arr[i+1])
temp[j++] = arr[i];
// Store the last element as whether
// it is unique or repeated, it hasn't
// stored previously
temp[j++] = arr[n-1];
// Modify original array
for (int i=0; i<j; i++)
arr[i] = temp[i];
return j;
}
static long maxPrimeFactors( long n)
{
// Initialize the maximum prime
// factor variable with the
// lowest one
long maxPrime = -1;
// Print the number of 2s
// that divide n
while (n % 2 == 0) {
maxPrime = 2;
// equivalent to n /= 2
n >>= 1;
}
// n must be odd at this point,
// thus skip the even numbers
// and iterate only for odd
// integers
for (int i = 3; i <= Math.sqrt(n); i += 2) {
while (n % i == 0) {
maxPrime = i;
n = n / i;
}
}
// This condition is to handle
// the case when n is a prime
// number greater than 2
if (n > 2)
maxPrime = n;
return maxPrime;
}
} | Java | ["3\n7 10 2", "1\n4"] | 2 seconds | ["17", "4"] | NoteFor the first test case, here's how a circular value of $$$17$$$ is obtained:Pick the number at index $$$3$$$. The sum of adjacent elements equals $$$17$$$. Delete $$$7$$$ and $$$10$$$ from the circle and replace $$$2$$$ with $$$17$$$.Note that the answer may not fit in a $$$32$$$-bit integer. | Java 8 | standard input | [
"dp",
"greedy",
"games",
"brute force"
] | a9473e6ec81c10c4f88973ac2d60ad04 | The first line contains one odd integer $$$n$$$ ($$$1 \leq n < 2 \cdot 10^5$$$, $$$n$$$ is odd) — the initial size of the circle. The second line contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$0 \leq a_{i} \leq 10^9$$$) — the initial numbers in the circle. | 2,100 | Output the maximum possible circular value after applying some sequence of operations to the given circle. | standard output | |
PASSED | d53349abe74b5530f6482fb83a62735f | train_001.jsonl | 1594479900 | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given $$$n$$$ nonnegative integers $$$a_1, a_2, \dots, a_n$$$ arranged in a circle, where $$$n$$$ must be odd (ie. $$$n-1$$$ is divisible by $$$2$$$). Formally, for all $$$i$$$ such that $$$2 \leq i \leq n$$$, the elements $$$a_{i - 1}$$$ and $$$a_i$$$ are considered to be adjacent, and $$$a_n$$$ and $$$a_1$$$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | 256 megabytes | // package Quarantine;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class OmkarAndCircle {
public static void main(String[] args)throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
int a[]=new int[n+1];
StringTokenizer st=new StringTokenizer(br.readLine());
for(int i=1;i<=n;i++){
a[i]=Integer.parseInt(st.nextToken());
}
if(n==1){
System.out.println(a[1]);
return;
}
int dp[]=new int[2*n+1];
int j=1;
for(int i=1;i<=n;i+=2){
dp[j++]=a[i];
}
for(int i=2;i<n;i+=2){
dp[j++]=a[i];
}
for(int i=1;i<=n;i+=2){
dp[j++]=a[i];
}
for(int i=2;i<n;i+=2){
dp[j++]=a[i];
}
long max=0;
for(int i=1;i<=n/2+1;i++){
max+=dp[i];
}
long sum=max;
for(int i=n/2+2;i<=2*n;i++){
sum+=dp[i];
sum-=dp[i-n/2-1];
max=Math.max(max,sum);
}
System.out.println(max);
}
}
| Java | ["3\n7 10 2", "1\n4"] | 2 seconds | ["17", "4"] | NoteFor the first test case, here's how a circular value of $$$17$$$ is obtained:Pick the number at index $$$3$$$. The sum of adjacent elements equals $$$17$$$. Delete $$$7$$$ and $$$10$$$ from the circle and replace $$$2$$$ with $$$17$$$.Note that the answer may not fit in a $$$32$$$-bit integer. | Java 8 | standard input | [
"dp",
"greedy",
"games",
"brute force"
] | a9473e6ec81c10c4f88973ac2d60ad04 | The first line contains one odd integer $$$n$$$ ($$$1 \leq n < 2 \cdot 10^5$$$, $$$n$$$ is odd) — the initial size of the circle. The second line contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$0 \leq a_{i} \leq 10^9$$$) — the initial numbers in the circle. | 2,100 | Output the maximum possible circular value after applying some sequence of operations to the given circle. | standard output | |
PASSED | 553d5f8bcb88cc04da7ce204b2b71a5b | train_001.jsonl | 1594479900 | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given $$$n$$$ nonnegative integers $$$a_1, a_2, \dots, a_n$$$ arranged in a circle, where $$$n$$$ must be odd (ie. $$$n-1$$$ is divisible by $$$2$$$). Formally, for all $$$i$$$ such that $$$2 \leq i \leq n$$$, the elements $$$a_{i - 1}$$$ and $$$a_i$$$ are considered to be adjacent, and $$$a_n$$$ and $$$a_1$$$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class D {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader file = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(file.readLine());
long[] evenSums = new long[n+2];
long[] oddSums = new long[n+2];
long[] nums = new long[n];
StringTokenizer st = new StringTokenizer(file.readLine());
for(int i = 0; i < n; i++) {
nums[i] = Integer.parseInt(st.nextToken());
evenSums[i+1] = evenSums[i];
oddSums[i+1] = oddSums[i];
if(i % 2 == 0) {
oddSums[i+1] += nums[i];
}
else {
evenSums[i+1] += nums[i];
}
}
evenSums[n+1] = evenSums[n];
oddSums[n+1] = oddSums[n];
long best = oddSums[n];
for(int i = 0; i < n-1; i++) {
if(i%2==0) {
best = Math.max(best, nums[i]+nums[i+1]+oddSums[i]+evenSums[n]-evenSums[i+2]);
}
else {
best = Math.max(best, nums[i]+nums[i+1]+evenSums[i]+oddSums[n]-oddSums[i+2]);
}
}
System.out.println(best);
}
}
| Java | ["3\n7 10 2", "1\n4"] | 2 seconds | ["17", "4"] | NoteFor the first test case, here's how a circular value of $$$17$$$ is obtained:Pick the number at index $$$3$$$. The sum of adjacent elements equals $$$17$$$. Delete $$$7$$$ and $$$10$$$ from the circle and replace $$$2$$$ with $$$17$$$.Note that the answer may not fit in a $$$32$$$-bit integer. | Java 8 | standard input | [
"dp",
"greedy",
"games",
"brute force"
] | a9473e6ec81c10c4f88973ac2d60ad04 | The first line contains one odd integer $$$n$$$ ($$$1 \leq n < 2 \cdot 10^5$$$, $$$n$$$ is odd) — the initial size of the circle. The second line contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$0 \leq a_{i} \leq 10^9$$$) — the initial numbers in the circle. | 2,100 | Output the maximum possible circular value after applying some sequence of operations to the given circle. | standard output | |
PASSED | c6ef392dcfa1f8ec8c996c0542318014 | train_001.jsonl | 1594479900 | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given $$$n$$$ nonnegative integers $$$a_1, a_2, \dots, a_n$$$ arranged in a circle, where $$$n$$$ must be odd (ie. $$$n-1$$$ is divisible by $$$2$$$). Formally, for all $$$i$$$ such that $$$2 \leq i \leq n$$$, the elements $$$a_{i - 1}$$$ and $$$a_i$$$ are considered to be adjacent, and $$$a_n$$$ and $$$a_1$$$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.InputMismatchException;
import java.util.*;
import java.io.*;
import java.lang.*;
public class Main{
public static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars==-1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
}
catch (IOException e) {
throw new InputMismatchException();
}
if(numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
int c = read();
while(isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if(c<'0'||c>'9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static long gcd(long a, long b){
if (a == 0)
return b;
return gcd(b % a, a);
}
static long lcm(long a, long b) {
return (a*b)/gcd(a, b);
}
public static void sortbyColumn(int arr[][], int col)
{
Arrays.sort(arr, new Comparator<int[]>() {
@Override
public int compare(final int[] entry1,
final int[] entry2) {
if (entry1[col] > entry2[col])
return 1;
else
return -1;
}
});
}
static long func(long a[],long size,int s){
long max1=a[s];
long maxc=a[s];
for(int i=s+1;i<size;i++){
maxc=Math.max(a[i],maxc+a[i]);
max1=Math.max(maxc,max1);
}
return max1;
}
public static class Pair<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<Pair<U, V>> {
public U x;
public V y;
public Pair(U x, V y) {
this.x = x;
this.y = y;
}
public int hashCode() {
return (x == null ? 0 : x.hashCode() * 31) + (y == null ? 0 : y.hashCode());
}
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Pair<U, V> p = (Pair<U, V>) o;
return (x == null ? p.x == null : x.equals(p.x)) && (y == null ? p.y == null : y.equals(p.y));
}
public int compareTo(Pair<U, V> b) {
int cmpU = x.compareTo(b.x);
return cmpU != 0 ? cmpU : y.compareTo(b.y);
}
public String toString() {
return String.format("(%s, %s)", x.toString(), y.toString());
}
}
static class MultiSet<U extends Comparable<U>> {
public int sz = 0;
public TreeMap<U, Integer> t;
public MultiSet() {
t = new TreeMap<>();
}
public void add(U x) {
t.put(x, t.getOrDefault(x, 0) + 1);
sz++;
}
public void remove(U x) {
if (t.get(x) == 1) t.remove(x);
else t.put(x, t.get(x) - 1);
sz--;
}
}
static void shuffle(long[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
long temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
//static LinkedList<Integer> li;
//static LinkedList<Integer> ans;
static int ans1=0,ans2=0;
static long dist[];
static int visited[];
static ArrayList<Integer> adj[];
public static void main(String args[]){
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter w = new PrintWriter(outputStream);
int t,i,j;
int n=in.nextInt();
long arr[]=new long[n];
for(i=0;i<n;i++){
arr[i]=in.nextLong();
}
int m=(n+1)/2;
long sum=0;
for(i=0;i<m;i++){
sum+=arr[2*i];
}
long ans=sum;
for(i=0;i<n;i++){
sum+=arr[2*(i+m)%n];
sum-=arr[2*i%n];
ans=Math.max(ans,sum);
}
w.println(ans);
w.close();
}
} | Java | ["3\n7 10 2", "1\n4"] | 2 seconds | ["17", "4"] | NoteFor the first test case, here's how a circular value of $$$17$$$ is obtained:Pick the number at index $$$3$$$. The sum of adjacent elements equals $$$17$$$. Delete $$$7$$$ and $$$10$$$ from the circle and replace $$$2$$$ with $$$17$$$.Note that the answer may not fit in a $$$32$$$-bit integer. | Java 8 | standard input | [
"dp",
"greedy",
"games",
"brute force"
] | a9473e6ec81c10c4f88973ac2d60ad04 | The first line contains one odd integer $$$n$$$ ($$$1 \leq n < 2 \cdot 10^5$$$, $$$n$$$ is odd) — the initial size of the circle. The second line contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$0 \leq a_{i} \leq 10^9$$$) — the initial numbers in the circle. | 2,100 | Output the maximum possible circular value after applying some sequence of operations to the given circle. | standard output | |
PASSED | 6c92c9525abb16d9ad88d9703460bef6 | train_001.jsonl | 1594479900 | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given $$$n$$$ nonnegative integers $$$a_1, a_2, \dots, a_n$$$ arranged in a circle, where $$$n$$$ must be odd (ie. $$$n-1$$$ is divisible by $$$2$$$). Formally, for all $$$i$$$ such that $$$2 \leq i \leq n$$$, the elements $$$a_{i - 1}$$$ and $$$a_i$$$ are considered to be adjacent, and $$$a_n$$$ and $$$a_1$$$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | 256 megabytes | import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
public class Solution {
public static class FastReader {
BufferedReader br;
StringTokenizer root;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (root == null || !root.hasMoreTokens()) {
try {
root = new StringTokenizer(br.readLine());
} catch (Exception addd) {
addd.printStackTrace();
}
}
return root.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
long nextLong() {
return Long.parseLong(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (Exception addd) {
addd.printStackTrace();
}
return str;
}
}
public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out));
public static FastReader sc = new FastReader();
static long mod = (int) (1000000000),MAX=(int)(1e5+10);
public static void main(String[] args) throws IOException{
int n = sc.nextInt();
long[] a = new long[n];
for(int i=0;i<n;++i) a[i] = sc.nextLong();
long[] pre = new long[2*n+2];
for(int i=0;i<2*n;++i) {
pre[i+2] = pre[i] + a[i%n];
}
long max = 0;
for(int i = 0;i < n;i++){
max = Math.max(max, pre[i+n+1] - pre[i]);
}
out.println(max);
out.close();
}
}
| Java | ["3\n7 10 2", "1\n4"] | 2 seconds | ["17", "4"] | NoteFor the first test case, here's how a circular value of $$$17$$$ is obtained:Pick the number at index $$$3$$$. The sum of adjacent elements equals $$$17$$$. Delete $$$7$$$ and $$$10$$$ from the circle and replace $$$2$$$ with $$$17$$$.Note that the answer may not fit in a $$$32$$$-bit integer. | Java 8 | standard input | [
"dp",
"greedy",
"games",
"brute force"
] | a9473e6ec81c10c4f88973ac2d60ad04 | The first line contains one odd integer $$$n$$$ ($$$1 \leq n < 2 \cdot 10^5$$$, $$$n$$$ is odd) — the initial size of the circle. The second line contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$0 \leq a_{i} \leq 10^9$$$) — the initial numbers in the circle. | 2,100 | Output the maximum possible circular value after applying some sequence of operations to the given circle. | standard output | |
PASSED | 0cc81e75a546b955a98c6b0f51087f2d | train_001.jsonl | 1594479900 | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given $$$n$$$ nonnegative integers $$$a_1, a_2, \dots, a_n$$$ arranged in a circle, where $$$n$$$ must be odd (ie. $$$n-1$$$ is divisible by $$$2$$$). Formally, for all $$$i$$$ such that $$$2 \leq i \leq n$$$, the elements $$$a_{i - 1}$$$ and $$$a_i$$$ are considered to be adjacent, and $$$a_n$$$ and $$$a_1$$$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | 256 megabytes | //package learning;
import java.util.*;
import java.io.*;
import java.lang.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class NitsLocal {
static ArrayList<String> s1;
static boolean[] prime;
static int n = 200001;
static void sieve() {
Arrays.fill(prime , true);
prime[0] = prime[1] = false;
for(int i = 2 ; i * i <= n ; ++i) {
if(prime[i]) {
for(int k = i * i; k<= n ; k+=i) {
prime[k] = false;
}
}
}
}
public static void main(String[] args) {
InputReader sc = new InputReader(System.in);
//prime = new boolean[n + 1];
// sieve();
//prime[1] = false;
/*
int n = sc.ni();
int k = sc.ni();
int []vis = new int[n+1];
int []a = new int[k];
for(int i=0;i<k;i++)
{
a[i] = i+1;
}
int j = k+1;
int []b = new int[n+1];
for(int i=1;i<=n;i++)
{
b[i] = -1;
}
int y = n - k +1;
int tr = 0;
int y1 = k+1;
while(y-- > 0)
{
System.out.print("? ");
for(int i=0;i<k;i++)
{
System.out.print(a[i] + " ");
}
System.out.flush();
int pos = sc.ni();
int a1 = sc.ni();
b[pos] = a1;
for(int i=0;i<k;i++)
{
if(a[i] == pos)
{
a[i] = y1;
y1++;
}
}
}
ArrayList<Integer> a2 = new ArrayList<>();
if(y >= k)
{
int c = 0;
int k1 = 0;
for(int i=1;i<=n;i++)
{
if(b[i] != -1)
{
c++;
a[k1] = i;
a2.add(b[i]);
k1++;
}
if(c==k)
break;
}
Collections.sort(a2);
System.out.print("? ");
for(int i=0;i<k;i++)
{
System.out.print(a[i] + " ");
}
System.out.println();
System.out.flush();
int pos = sc.ni();
int a1 = sc.ni();
int ans = -1;
for(int i=0;i<a2.size();i++)
{
if(a2.get(i) == a1)
{
ans = i+1;
break;
}
}
System.out.println("!" + " " + ans);
System.out.flush();
}
else
{
int k1 = 0;
a = new int[k];
for(int i=1;i<=n;i++)
{
if(b[i] != -1)
{
a[k1] = i;
a2.add(b[i]);
k1++;
}
}
for(int i=1;i<=n;i++)
{
if(b[i] == -1)
{
a[k1] = i;
k1++;
if(k1==k)
break;
}
}
int ans = -1;
while(true)
{
System.out.print("? ");
for(int i=0;i<k;i++)
{
System.out.print(a[i] + " ");
}
System.out.println();
System.out.flush();
int pos = sc.ni();
int a1 = sc.ni();
int f = 0;
if(b[pos] != -1)
{
Collections.sort(a2);
for(int i=0;i<a2.size();i++)
{
if(a2.get(i) == a1)
{
ans = i+1;
f = 1;
System.out.println("!" + " " + ans);
System.out.flush();
break;
}
}
if(f==1)
break;
}
else
{
b[pos] = a1;
a = new int[k];
a2.add(a1);
for(int i=1;i<=n;i++)
{
if(b[i] != -1)
{
a[k1] = i;
a2.add(b[i]);
k1++;
}
}
for(int i=1;i<=n;i++)
{
if(b[i] == -1)
{
a[k1] = i;
k1++;
if(k1==k)
break;
}
}
}
}
*/
/*
int n = sc.ni();
int []a = sc.nia(n);
int []b = sc.nia(n);
Integer []d = new Integer[n];
int []d1 = new int[n];
for(int i=0;i<n;i++)
{
d[i] = (a[i] - b[i]);
}
int l = 0;
int r = n-1;
Arrays.sort(d);
long res = 0;
while(l < r)
{
if(d[l] + d[r] > 0)
{
res += (long) (r-l);
r--;
}
else
l++;
}
w.println(res);
*/
/*
int n = sc.ni();
ArrayList<Integer> t1 = new ArrayList<>();
int []p1 = new int[n+1];
int []q1 = new int[n-1];
int []q2 = new int[n-1];
for(int i=0;i<n-1;i++)
{
int p = sc.ni();
int q = sc.ni();
t1.add(q);
p1[p]++;
q1[i] = p;
q2[i] = q;
}
int res = 0;
for(int i=0;i<t1.size();i++)
{
if(p1[t1.get(i)] == 0)
{
res = t1.get(i);
//break;
}
}
int y = 1;
for(int i=0;i<n-1;i++)
{
if(q2[i] == res)
{
w.println("0");
}
else
{
w.println(y + " ");
y++;
}
}
*/
/*
int n = sc.ni();
int k = sc.ni();
Integer []a = new Integer[n];
for(int i=0;i<n;i++)
{
a[i] = sc.ni();
a[i] = a[i]%k;
}
HashMap<Integer,Integer> hm = new HashMap<>();
for(int i=0;i<n;i++)
{
if(!hm.containsKey(a[i]))
hm.put(a[i], 1);
else
hm.put(a[i], hm.get(a[i]) + 1);
}
Arrays.sort(a);
int z = 0;
long res = 0;
HashMap<Integer,Integer> v = new HashMap<>();
for(int i=0;i<n;i++)
{
if(a[i] == 0)
res++;
else
{
if(a[i] == k-a[i] && !v.containsKey(a[i]))
{
res += (long) hm.get(a[i]) / 2;
v.put(a[i],1);
}
else
{
if(hm.containsKey(a[i]) && hm.containsKey(k-a[i]))
{
int temp = a[i];
if(!v.containsKey(a[i]) && !v.containsKey(k-a[i]))
{
res += (long) Math.min(hm.get(temp), hm.get(k-temp));
v.put(temp,1);
v.put(k-temp,1);
}
}
}
}
}
w.println(res);
*/
/*
int []a = new int[4];
//G0
a[0] = 0;
a[1] = 1;
a[2] = 0;
a[3] = 1;
int []b = new int[4];
//g1
b[0] = 0;
b[1] = 1;
b[2] = 1;
b[3] = 0;
int [][]dp1 = new int[4][4];
int [][]dp2 = new int[4][4];
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
dp1[i][j] = (a[i] | a[j]);
}
// w.println();
}
w.println();
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
dp2[i][j] = (b[i] | b[j]);
}
//w.println();
}
int a1 = 0;
int b1 = 0;
int c1 = 0;
int d1 = 0;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(dp1[i][j] == 0 && dp2[i][j] == 0)
a1++;
else if(dp1[i][j] == 1 && dp2[i][j] == 1)
b1++;
else if(dp1[i][j] == 0 && dp2[i][j] == 1)
c1++;
else
d1++;
}
}
w.println(a1+ " " + b1 + " " + c1 +" "+ d1);
Scanner sc = new Scanner(System.in);
int q = sc.nextInt();
while(q-- > 0)
{
long x = sc.nextLong();
long y = sc.nextLong();
long a = (x - y)/2;
long a1 = 0, b1 = 0;
int lim = 8;
int f = 0;
for (int i=0; i<8*lim; i++)
{
long pi = (y & (1 << i));
long qi = (a & (1 << i));
if (pi == 0 && qi == 0)
{
}
else if (pi == 0 && qi > 0)
{
a1 = ((1 << i) | a1);
b1 = ((1 << i) | b1);
}
else if (pi > 0 && qi == 0)
{
a1 = ((1 << i) | a1);
}
else
{
f = 1;
}
}
if(a1+b1 != x)
f =1;
if(f==1)
System.out.println("-1");
else
{
if(a1 > b1)
{
long temp = a1;
a1 = b1;
b1 = temp;
}
System.out.println(a1 + " " + b1);
}
}
*/
int t = 1;
while(t-- > 0)
{
int n = sc.ni();
int []a = sc.nia(n);
int []temp = new int[2*n];
int st = 0;
for(int i=0;i<n;i+=2)
{
temp[st] = a[i];
st++;
}
for(int i=1;i<n;i+=2)
{
temp[st] = a[i];
st++;
}
for(int i=n;i<2*n;i++)
{
temp[i] = temp[i-n];
}
int req = (n+1)/2;
long sum = 0;
for(int i=0;i<req;i++)
{
sum += (long) temp[i];
}
long max = -1;
max = Math.max(sum,max);
for(int i=req;i<2*n;i++)
{
sum -= temp[i-req];
sum += temp[i];
max = Math.max(sum,max);
}
w.println(max);
}
w.close();
}
static int l,r;
static long nCrModPFermat(int n, int r,
long p)
{
// Base case
if (r == 0)
return 1;
// Fill factorial array so that we
// can find all factorial of r, n
// and n-r
return (fact[n]* modInverse(fact[r], p) % p * modInverse(fact[n-r], p) % p) % p;
}
static long []fact;
static int []res;
static int c = 0;
static class Pair{
int fir;
int last;
Pair(int fir,int last)
{
this.fir = fir;
this.last = last;
}
}
static class m1 implements Comparator<Pair>{
public int compare(Pair a, Pair b)
{
if(a.fir < b.fir)
return -1;
else if(a.fir > b.fir)
return 1;
else
{
return 0;
}
}
}
static long calc(long x)
{
double res = Math.sqrt(1 + 24*(double)x);
res -= 1.0;
res = res / (double) 6;
return (long) res;
}
public static final BigInteger MOD = BigInteger.valueOf(998244353L);
static BigInteger fact(int x)
{
BigInteger res = BigInteger.ONE;
for(int i=2;i<=x;i++)
{
res = res.multiply(BigInteger.valueOf(i)).mod(MOD);
}
return res;
}
public static long calc(int x,int y,int z)
{
long res = ((long) (x-y) * (long) (x-y)) + ((long) (z-y) * (long) (z-y)) + ((long) (x-z) * (long) (x-z));
return res;
}
public static int upperBound(ArrayList<Integer>arr, int length, int value) {
int low = 0;
int high = length;
while (low < high) {
final int mid = (low + high) / 2;
if (value >= arr.get(mid)) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
public static int lowerBound(ArrayList<Integer> arr, int length, int value) {
int low = 0;
int high = length;
while (low < high) {
final int mid = (low + high) / 2;
//checks if the value is less than middle element of the array
if (value <= arr.get(mid)) {
high = mid;
} else {
low = mid + 1;
}
}
return low;
}
static HashMap<Long,Integer> map;
static class Coin{
long a;
long b;
Coin(long a, long b)
{
this.a = a;
this.b = b;
}
}
static ArrayList<Long> fac;
static HashSet<Long> hs;
public static void primeFactors(long n)
{
// Print the number of 2s that divide n
while (n % 2 == 0) {
fac.add(2l);
hs.add(2l);
n /= 2;
}
// n must be odd at this point. So we can
// skip one element (Note i = i +2)
for (long i = 3; i*i <= n; i += 2) {
// While i divides n, print i and divide n
while (n % i == 0) {
fac.add(i);
hs.add(i);
n /= i;
}
}
// This condition is to handle the case whien
// n is a prime number greater than 2
if (n > 2)
fac.add(n);
}
static int smallestDivisor(int n)
{
// if divisible by 2
if (n % 2 == 0)
return 2;
// iterate from 3 to sqrt(n)
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0)
return i;
}
return n;
}
static boolean IsP(String s,int l,int r)
{
while(l <= r)
{
if(s.charAt(l) != s.charAt(r))
{
return false;
}
l++;
r--;
}
return true;
}
static class Student{
int id;
int val;
Student(int id,int val)
{
this.id = id;
this.val = val;
}
}
static int upperBound(ArrayList<Integer> a, int low, int high, int element){
while(low < high){
int middle = low + (high - low)/2;
if(a.get(middle) >= element)
high = middle;
else
low = middle + 1;
}
return low;
}
static long func(long t,long e,long h,long a, long b)
{
if(e*a >= t)
return t/a;
else
{
return e + Math.min(h,(t-e*a)/b);
}
}
public static int countSetBits(int number){
int count = 0;
while(number>0){
++count;
number &= number-1;
}
return count;
}
static long modexp(long x,long n,long M)
{
long power = n;
long result=1;
x = x%M;
while(power>0)
{
if(power % 2 ==1)
result=(result * x)%M;
x=(x*x)%M;
power = power/2;
}
return result;
}
static long modInverse(long A,long M)
{
return modexp(A,M-2,M);
}
static long gcd(long a,long b)
{
if(a==0)
return b;
else
return gcd(b%a,a);
}
static class Temp{
int a;
int b;
int c;
int d;
Temp(int a,int b,int c,int d)
{
this.a = a;
this.b = b;
this.c =c;
this.d = d;
//this.d = d;
}
}
static long sum1(int t1,int t2,int x,int []t)
{
int mid = (t2-t1+1)/2;
if(t1==t2)
return 0;
else
return sum1(t1,mid-1,x,t) + sum1(mid,t2,x,t);
}
static String replace(String s,int a,int n)
{
char []c = s.toCharArray();
for(int i=1;i<n;i+=2)
{
int num = (int) (c[i] - 48);
num += a;
num%=10;
c[i] = (char) (num+48);
}
return new String(c);
}
static String move(String s,int h,int n)
{
h%=n;
char []c = s.toCharArray();
char []temp = new char[n];
for(int i=0;i<n;i++)
{
temp[(i+h)%n] = c[i];
}
return new String(temp);
}
public static int ip(String s){
return Integer.parseInt(s);
}
static class multipliers implements Comparator<Long>{
public int compare(Long a,Long b) {
if(a<b)
return 1;
else if(b<a)
return -1;
else
return 0;
}
}
static class multipliers1 implements Comparator<Coin>{
public int compare(Coin a,Coin b) {
if(a.a < b.a)
return 1;
else if(a.a > b.a)
return -1;
else{
if(a.b < b.b)
return 1;
else if(a.b > b.b)
return -1;
else
return 0;
}
}
}
// Java program to generate power set in
// lexicographic order.
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int ni() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nl() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nia(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ni();
}
return a;
}
public String rs() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
static PrintWriter w = new PrintWriter(System.out);
static class Student1
{
int id;
//int x;
int b;
//long z;
Student1(int id,int b)
{
this.id = id;
//this.x = x;
//this.s = s;
this.b = b;
// this.z = z;
}
}
} | Java | ["3\n7 10 2", "1\n4"] | 2 seconds | ["17", "4"] | NoteFor the first test case, here's how a circular value of $$$17$$$ is obtained:Pick the number at index $$$3$$$. The sum of adjacent elements equals $$$17$$$. Delete $$$7$$$ and $$$10$$$ from the circle and replace $$$2$$$ with $$$17$$$.Note that the answer may not fit in a $$$32$$$-bit integer. | Java 8 | standard input | [
"dp",
"greedy",
"games",
"brute force"
] | a9473e6ec81c10c4f88973ac2d60ad04 | The first line contains one odd integer $$$n$$$ ($$$1 \leq n < 2 \cdot 10^5$$$, $$$n$$$ is odd) — the initial size of the circle. The second line contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$0 \leq a_{i} \leq 10^9$$$) — the initial numbers in the circle. | 2,100 | Output the maximum possible circular value after applying some sequence of operations to the given circle. | standard output | |
PASSED | f2457741196d2eefb3ea7dbfd477e527 | train_001.jsonl | 1594479900 | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given $$$n$$$ nonnegative integers $$$a_1, a_2, \dots, a_n$$$ arranged in a circle, where $$$n$$$ must be odd (ie. $$$n-1$$$ is divisible by $$$2$$$). Formally, for all $$$i$$$ such that $$$2 \leq i \leq n$$$, the elements $$$a_{i - 1}$$$ and $$$a_i$$$ are considered to be adjacent, and $$$a_n$$$ and $$$a_1$$$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | 256 megabytes | import java.util.*;
public class OMKARISSOHOT{
public static void main(String[] args)
{
Scanner omkar = new Scanner(System.in);
long[] arr = new long[omkar.nextInt()];
if(arr.length == 1)
{
System.out.println(omkar.nextLong());
return;
}
for(int i = 0; i < arr.length; i++)
{
arr[i] = omkar.nextLong();
}
long sum = 0;
for(int i = 0; i < arr.length; i++)
{
sum += arr[i];
}
long[] skippySums = new long[arr.length];
long temp = 0;
for(int i = 0; i < arr.length/2; i++)
{
temp += arr[2*i];
}
skippySums[0] = temp;
temp = 0;
for(int i = 0; i < arr.length/2; i++)
{
temp += arr[2*i+1];
}
skippySums[1] = temp;
for(int i = 2; i < arr.length; i++)
{
skippySums[i] = skippySums[i-2] - arr[i-2] + arr[(arr.length-1+i-2) % arr.length];
}
long min = skippySums[0];
for(int i = 0; i < arr.length; i++)
{
if(skippySums[i] < min)
{
min = skippySums[i];
}
}
long u = sum-min;
System.out.println(""+u);
}
} | Java | ["3\n7 10 2", "1\n4"] | 2 seconds | ["17", "4"] | NoteFor the first test case, here's how a circular value of $$$17$$$ is obtained:Pick the number at index $$$3$$$. The sum of adjacent elements equals $$$17$$$. Delete $$$7$$$ and $$$10$$$ from the circle and replace $$$2$$$ with $$$17$$$.Note that the answer may not fit in a $$$32$$$-bit integer. | Java 8 | standard input | [
"dp",
"greedy",
"games",
"brute force"
] | a9473e6ec81c10c4f88973ac2d60ad04 | The first line contains one odd integer $$$n$$$ ($$$1 \leq n < 2 \cdot 10^5$$$, $$$n$$$ is odd) — the initial size of the circle. The second line contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$0 \leq a_{i} \leq 10^9$$$) — the initial numbers in the circle. | 2,100 | Output the maximum possible circular value after applying some sequence of operations to the given circle. | standard output | |
PASSED | e89f38effde3c98d45bf31cda5a5b5e3 | train_001.jsonl | 1594479900 | Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given $$$n$$$ nonnegative integers $$$a_1, a_2, \dots, a_n$$$ arranged in a circle, where $$$n$$$ must be odd (ie. $$$n-1$$$ is divisible by $$$2$$$). Formally, for all $$$i$$$ such that $$$2 \leq i \leq n$$$, the elements $$$a_{i - 1}$$$ and $$$a_i$$$ are considered to be adjacent, and $$$a_n$$$ and $$$a_1$$$ are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations. | 256 megabytes | import java.io.*;
public class OmkarLastCircle {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t=1;
while(t-->0)
{
int n=Integer.parseInt(br.readLine());
String str[]=br.readLine().trim().split(" ");
long a[]=new long[2*n];
if(n==1)
{
System.out.println(str[0]);
}
else
{
for(int i=0;i<n;i++)
{
a[i]=Long.parseLong(str[i]);
a[i+n]=Long.parseLong(str[i]);
}
long[] sum=new long[2*n];
for(int i=0;i<2*n;i++)
{
sum[i]=a[i];
if(i>=2)
sum[i]=sum[i]+sum[i-2];
}
long ans=0;
for(int i=n;i<2*n;i++)
{
long x=sum[i];
if(i-n-1>=0)
x=x-sum[i-n-1];
ans=Math.max(x,ans);
}
System.out.println(ans);
}
}
}
}
| Java | ["3\n7 10 2", "1\n4"] | 2 seconds | ["17", "4"] | NoteFor the first test case, here's how a circular value of $$$17$$$ is obtained:Pick the number at index $$$3$$$. The sum of adjacent elements equals $$$17$$$. Delete $$$7$$$ and $$$10$$$ from the circle and replace $$$2$$$ with $$$17$$$.Note that the answer may not fit in a $$$32$$$-bit integer. | Java 8 | standard input | [
"dp",
"greedy",
"games",
"brute force"
] | a9473e6ec81c10c4f88973ac2d60ad04 | The first line contains one odd integer $$$n$$$ ($$$1 \leq n < 2 \cdot 10^5$$$, $$$n$$$ is odd) — the initial size of the circle. The second line contains $$$n$$$ integers $$$a_{1},a_{2},\dots,a_{n}$$$ ($$$0 \leq a_{i} \leq 10^9$$$) — the initial numbers in the circle. | 2,100 | Output the maximum possible circular value after applying some sequence of operations to the given circle. | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.