src
stringlengths 95
64.6k
| complexity
stringclasses 7
values | problem
stringlengths 6
50
| from
stringclasses 1
value |
---|---|---|---|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author ankur
*/
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();
long k = in.nextLong();
HashMap<Long, Integer> hm = new HashMap<>();
long ar[] = in.nextLongArray(n);
for (int i = 0; i < n; i++) {
long dist = ar[i] + k;
long min = k;
for (int j = 0; j < n; j++) {
min = Math.min(min, Math.abs(ar[j] - dist));
}
if (min == k) {
hm.put(dist, 1);
}
dist = ar[i] - k;
min = k;
for (int j = 0; j < n; j++) {
min = Math.min(min, Math.abs(ar[j] - dist));
}
if (min == k) {
hm.put(dist, 1);
}
}
out.print(hm.size());
}
}
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar;
private int snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
//*-*------clare------
//remeber while comparing 2 non primitive data type not to use ==
//remember Arrays.sort for primitive data has worst time case complexity of 0(n^2) bcoz it uses quick sort
//again silly mistakes ,yr kb tk krta rhega ye mistakes
//try to write simple codes ,break it into simple things
//knowledge>rating
/*
public class Main
implements Runnable{
public static void main(String[] args) {
new Thread(null,new Main(),"Main",1<<26).start();
}
public void run() {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();//chenge the name of task
solver.solve(1, in, out);
out.close();
}
*/
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 = 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 nextLong() {
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 long[] nextLongArray(int n) {
long a[] = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nextLong();
}
return a;
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
}
| linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashSet;
import java.util.StringTokenizer;
import java.util.TreeMap;
public class AA {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = sc.nextInt();
int d =sc.nextInt();
int[] hotels = new int[n];
for (int i = 0; i < hotels.length; i++) {
hotels[i] = sc.nextInt();
}
int count = 0;
for (int i = 0; i < hotels.length-1; i++) {
int one = hotels[i];
int two = hotels[i+1];
double mid = (two-one)*1.0/2.0;
if(mid==d) {
count++;
// System.out.println("hello"+" "+i+" "+(i+1));
}
else if(mid>d) {
// System.out.println("hello2"+" "+i+" "+(i+1));
count+=2;
}
}
count+=2;
System.out.println(count);
out.flush();
out.close();
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}//19 91 991 919 199
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();
}
}
}
| linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.io.*;
import java.util.*;
public class TaskA
{
public static void main(String[] args)
{
new TaskA(System.in, System.out);
}
static class Solver implements Runnable
{
int n, d;
long[] arr;
// BufferedReader in;
InputReader in;
PrintWriter out;
void solve() throws IOException
{
n = in.nextInt();
d = in.nextInt();
arr = in.nextLongArray(n);
Set<Long> set = new HashSet<>();
for (int i = 0; i < n; i++)
{
if (i == 0)
set.add(arr[i] - d);
else
{
long left = arr[i] - d;
if (left - arr[i - 1] >= d)
set.add(left);
}
if (i == n - 1)
set.add(arr[i] + d);
else
{
long right = arr[i] + d;
if (arr[i + 1] - right >= d)
set.add(right);
}
}
out.println(set.size());
}
void debug(Object... o)
{
System.err.println(Arrays.deepToString(o));
}
// uncomment below line to change to BufferedReader
// public Solver(BufferedReader in, PrintWriter out)
public Solver(InputReader in, PrintWriter out)
{
this.in = in;
this.out = out;
}
@Override
public void run()
{
try
{
solve();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
static class InputReader
{
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
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 & 15;
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int arraySize)
{
int array[] = new int[arraySize];
for (int i = 0; i < arraySize; i++)
array[i] = nextInt();
return array;
}
public long nextLong()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sign = 1;
if (c == '-')
{
sign = -1;
c = read();
}
long result = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
result *= 10;
result += c & 15;
c = read();
} while (!isSpaceChar(c));
return result * sign;
}
public long[] nextLongArray(int arraySize)
{
long array[] = new long[arraySize];
for (int i = 0; i < arraySize; i++)
array[i] = nextLong();
return array;
}
public float nextFloat()
{
float result, div;
byte c;
result = 0;
div = 1;
c = (byte) read();
while (c <= ' ')
c = (byte) read();
boolean isNegative = (c == '-');
if (isNegative)
c = (byte) read();
do
{
result = result * 10 + c - '0';
} while ((c = (byte) read()) >= '0' && c <= '9');
if (c == '.')
while ((c = (byte) read()) >= '0' && c <= '9')
result += (c - '0') / (div *= 10);
if (isNegative)
return -result;
return result;
}
public double nextDouble()
{
double ret = 0, div = 1;
byte c = (byte) read();
while (c <= ' ')
c = (byte) read();
boolean neg = (c == '-');
if (neg)
c = (byte) read();
do
{
ret = ret * 10 + c - '0';
} while ((c = (byte) read()) >= '0' && c <= '9');
if (c == '.')
while ((c = (byte) read()) >= '0' && c <= '9')
ret += (c - '0') / (div *= 10);
if (neg)
return -ret;
return ret;
}
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 result = new StringBuilder();
do
{
result.appendCodePoint(c);
c = read();
} while (!isNewLine(c));
return result.toString();
}
public boolean isNewLine(int c)
{
return c == '\n';
}
public boolean isSpaceChar(int c)
{
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public void close()
{
try
{
stream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public InputReader(InputStream stream)
{
this.stream = stream;
}
}
static class CMath
{
static long power(long number, long power)
{
if (number == 1 || number == 0 || power == 0)
return 1;
if (power == 1)
return number;
if (power % 2 == 0)
return power(number * number, power / 2);
else
return power(number * number, power / 2) * number;
}
static long modPower(long number, long power, long mod)
{
if (number == 1 || number == 0 || power == 0)
return 1;
number = mod(number, mod);
if (power == 1)
return number;
long square = mod(number * number, mod);
if (power % 2 == 0)
return modPower(square, power / 2, mod);
else
return mod(modPower(square, power / 2, mod) * number, mod);
}
static long moduloInverse(long number, long mod)
{
return modPower(number, mod - 2, mod);
}
static long mod(long number, long mod)
{
return number - (number / mod) * mod;
}
static int gcd(int a, int b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
static long min(long... arr)
{
long min = arr[0];
for (int i = 1; i < arr.length; i++)
min = Math.min(min, arr[i]);
return min;
}
static long max(long... arr)
{
long max = arr[0];
for (int i = 1; i < arr.length; i++)
max = Math.max(max, arr[i]);
return max;
}
static int min(int... arr)
{
int min = arr[0];
for (int i = 1; i < arr.length; i++)
min = Math.min(min, arr[i]);
return min;
}
static int max(int... arr)
{
int max = arr[0];
for (int i = 1; i < arr.length; i++)
max = Math.max(max, arr[i]);
return max;
}
}
static class Utils
{
static boolean nextPermutation(int[] arr)
{
for (int a = arr.length - 2; a >= 0; --a)
{
if (arr[a] < arr[a + 1])
{
for (int b = arr.length - 1; ; --b)
{
if (arr[b] > arr[a])
{
int t = arr[a];
arr[a] = arr[b];
arr[b] = t;
for (++a, b = arr.length - 1; a < b; ++a, --b)
{
t = arr[a];
arr[a] = arr[b];
arr[b] = t;
}
return true;
}
}
}
}
return false;
}
}
public TaskA(InputStream inputStream, OutputStream outputStream)
{
// uncomment below line to change to BufferedReader
// BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Thread thread = new Thread(null, new Solver(in, out), "TaskA", 1 << 29);
try
{
thread.start();
thread.join();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
finally
{
in.close();
out.flush();
out.close();
}
}
}
| linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args)
{
InputReader in = new InputReader(System.in);
OutputWriter out = new OutputWriter(System.out);
int n = in.nextInt();
int d = in.nextInt();
int[]a = new int[n];
int ans=2;
for (int i =0;i<n;i++)
{
a[i] = in.nextInt();
}
for (int i =0;i<n-1;i++)
{
if (a[i+1]-a[i]==2*d)
ans++;
if (a[i+1]-a[i]>2*d)
ans+=2;
}
out.printLine(ans);
out.flush();
}
}
class pair implements Comparable
{
int key;
int value;
public pair(Object key, Object value) {
this.key = (int)key;
this.value=(int)value;
}
@Override
public int compareTo(Object o) {
pair temp =(pair)o;
return key-temp.key;
}
}
class Graph {
int n;
ArrayList<Integer>[] adjList;
public Graph(int n) {
this.n = n;
adjList = new ArrayList[n];
for (int i = 0; i < n; i++)
adjList[i] = new ArrayList<>();
}
}
class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
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 nextInt() {
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 nextLong() {
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[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
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 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;
}
}
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();
}
} | linear | 1004_A. Sonya and Hotels | CODEFORCES |
//package math_codet;
import java.io.*;
import java.util.*;
/******************************************
* AUTHOR: AMAN KUMAR SINGH *
* INSTITUITION: KALYANI GOVERNMENT ENGINEERING COLLEGE *
******************************************/
public class lets_do {
InputReader in;
PrintWriter out;
Helper_class h;
final long mod=1000000007;
public static void main(String[] args) throws java.lang.Exception{
new lets_do().run();
}
void run() throws Exception{
in=new InputReader();
out = new PrintWriter(System.out);
h = new Helper_class();
int t=1;
while(t-->0)
solve();
out.flush();
out.close();
}
long[] arr;
int n;
HashMap<Integer,Integer> hmap=new HashMap<Integer,Integer>();
HashSet<Integer> hset=new HashSet<Integer>();
void solve(){
n=h.ni();
long d=h.nl();
int i=0;
arr=new long[n];
for(i=0;i<n;i++)
arr[i]=h.nl();
int count=2;
for(i=0;i<n-1;i++){
if(Math.abs(arr[i]-arr[i+1])>(2*d))
count+=2;
else if(Math.abs(arr[i]-arr[i+1])==(2*d))
count+=1;
}
h.pn(count);
}
class Helper_class{
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 bitcount(long n){return (n==0)?0:(1+bitcount(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());}
long mul(long a,long b){
if(a>=mod)a%=mod;
if(b>=mod)b%=mod;
a*=b;
if(a>=mod)a%=mod;
return a;
}
long modPow(long a, long p){
long o = 1;
while(p>0){
if((p&1)==1)o = mul(o,a);
a = mul(a,a);
p>>=1;
}
return o;
}
long add(long a, long b){
if(a>=mod)a%=mod;
if(b>=mod)b%=mod;
if(b<0)b+=mod;
a+=b;
if(a>=mod)a-=mod;
return a;
}
}
class InputReader{
BufferedReader br;
StringTokenizer st;
public InputReader(){
br = new BufferedReader(new InputStreamReader(System.in));
}
public InputReader(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;
}
}
} | linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.FilterInputStream;
import java.io.BufferedInputStream;
import java.io.InputStream;
/**
* @author khokharnikunj8
*/
public class Main {
public static void main(String[] args) {
new Thread(null, new Runnable() {
public void run() {
new Main().solve();
}
}, "1", 1 << 26).start();
}
void solve() {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
ScanReader in = new ScanReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
ASonyaAndHotels solver = new ASonyaAndHotels();
solver.solve(1, in, out);
out.close();
}
static class ASonyaAndHotels {
public void solve(int testNumber, ScanReader in, PrintWriter out) {
int n = in.scanInt();
int d = in.scanInt();
int[] ar = new int[n];
int ans = 2;
for (int i = 0; i < n; i++) ar[i] = in.scanInt();
for (int i = 0; i < n - 1; i++) {
if (ar[i + 1] - ar[i] == 2 * d) ans++;
else if (ar[i + 1] - ar[i] > 2 * d) ans += 2;
}
out.println(ans);
}
}
static class ScanReader {
private byte[] buf = new byte[4 * 1024];
private int index;
private BufferedInputStream in;
private int total;
public ScanReader(InputStream inputStream) {
in = new BufferedInputStream(inputStream);
}
private int scan() {
if (index >= total) {
index = 0;
try {
total = in.read(buf);
} catch (Exception e) {
e.printStackTrace();
}
if (total <= 0) return -1;
}
return buf[index++];
}
public int scanInt() {
int integer = 0;
int n = scan();
while (isWhiteSpace(n)) n = scan();
int neg = 1;
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
integer *= 10;
integer += n - '0';
n = scan();
}
}
return neg * integer;
}
private boolean isWhiteSpace(int n) {
if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true;
else return false;
}
}
}
| linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.util.Scanner;
public class R495A {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int n=scan.nextInt(), k=scan.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++) a[i]=scan.nextInt();
int res=2;
for(int i=0;i<n-1;i++) {
if(a[i+1]-a[i]>2*k) res+=2;
else if(a[i+1]-a[i]==2*k) res++;
}
System.out.println(res);
}
} | linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.*;
import java.util.Map.Entry;
public class Main {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
private PrintWriter pw;
private long mod = 998244353;
private StringBuilder ans_sb;
private int sqrt;
private void soln() {
int n = nextInt();
int d = nextInt();
int[] arr = nextIntArray(n);
int cnt = 2;
for(int i=0;i<n-1;i++) {
int a1 = arr[i];
int a2 = arr[i+1];
a1 += d;
a2 -= d;
if(a1 < a2) {
cnt+=2;
}else if(a1==a2)
cnt++;
}
pw.println(cnt);
}
private class Pair implements Comparable<Pair>{
int x,i;
public Pair(int a, int b) {
x = a;
i = b;
}
@Override
public int compareTo(
Pair o)
{
return this.x - o.x;
}
}
private int cc = 0;
private void dfs(int c, int p, LinkedList<Integer>[] tree, int[] t, int[] tin, int[] tout, int[] arr) {
//debug(c);
t[cc] = arr[c];
tin[c] = cc++;
Iterator<Integer> it = tree[c].listIterator();
while(it.hasNext()) {
int x = it.next();
if(x != p) {
dfs(x, c, tree,t,tin,tout,arr);
}
}
tout[c] = cc;
}
public class Segment
{
private Node[] tree;
private int[] lazy;
private int size;
private int n;
private int[] base;
private class Node
{
private int on;
private int off;
}
public Segment(int n, int[] arr)
{
this.base=arr;
int x = (int) (Math.ceil(Math.log(n) / Math.log(2)));
size = 2 * (int) Math.pow(2, x) - 1;
tree = new Node[size];
lazy = new int[size];
this.n = n;
//this.set = set1;
build(0, 0, n - 1);
}
public void build(int id, int l, int r)
{
if (l == r)
{
tree[id] = new Node();
if(base[l] == 1)
tree[id].on++;
else
tree[id].off++;
return;
}
int mid = ((l + r)>>1);
build((id<<1)|1, l, mid);
build((id<<1)+2, mid + 1, r);
tree[id] = merge(tree[(id<<1)|1], tree[(id<<1)+2]);
//System.out.println(l+" "+r+" "+tree[id].l+" "+tree[id].r+" "+tree[id].ans);
}
public Node merge(Node n1, Node n2) {
Node ret = new Node();
if(n1 == null && n2 == null)
return null;
else if(n1 == null) {
ret.on = n2.on;
ret.off = n2.off;
}
else if(n2 == null) {
ret.on = n1.on;
ret.off = n1.off;
}
else {
ret.on = n1.on+n2.on;
ret.off = n2.off + n1.off;
}
return ret;
}
public int query(int l, int r)
{
Node ret = queryUtil(l, r, 0, 0, n - 1);
if(ret == null) {
return 0;
}
else
return ret.on;
}
private Node queryUtil(int x, int y, int id, int l, int r)
{
if (l > y || x > r)
return null;
if (x <= l && r <= y)
{
//shift(id,l,r);
return tree[id];
}
int mid = ((l + r)>>1);
shift(id,l,r);
Node q1 = queryUtil(x, y, (id<<1)|1, l, mid);
Node q2 = queryUtil(x, y, (id<<1)+2, mid + 1, r);
return merge(q1, q2);
}
public void update(int x, int y, int c) {
update1(x, y, c, 0, 0, n-1);
}
private void update1(int x, int y, int colour, int id, int l, int r)
{
//System.out.println(l+" "+r+" "+x);
if (x > r || y < l)
return;
if (x <= l && r <= y)
{
lazy[id]++;
// lazy[id] %= 2;
switchNode(tree[id]);
return;
}
int mid = ((l + r)>>1);
//shift(id);
if(y<=mid)
update1(x, y, colour, (id<<1)|1, l, mid);
else if(x>mid)
update1(x, y, colour, (id<<1)+2, mid + 1, r);
else {
update1(x, y, colour, (id<<1)|1, l, mid);
update1(x, y, colour, (id<<1)+2, mid + 1, r);
}
tree[id] = merge(tree[(id<<1)|1], tree[(id<<1)+2]);
}
private void shift(int id,int l, int r)
{
lazy[id] %= 2;
if(lazy[id] != 0) {
if(l != r) {
lazy[(id<<1)|1] += lazy[id];
lazy[(id<<1)+2] += lazy[id];
switchNode(tree[(id<<1)+2]);
switchNode(tree[(id<<1)|1]);
}
//switchNode(tree[(id<<1)+2]);
lazy[id] = 0;
}
}
private void switchNode(Node d) {
d.on ^= d.off;
d.off ^= d.on;
d.on ^= d.off;
}
}
private void debug(Object... o) {
System.out.println(Arrays.deepToString(o));
}
private long pow(long a, long b, long c) {
if (b == 0)
return 1;
long p = pow(a, b / 2, c);
p = (p * p) % c;
return (b % 2 == 0) ? p : (a * p) % c;
}
private long gcd(long n, long l) {
if (l == 0)
return n;
return gcd(l, n % l);
}
public static void main(String[] args) throws Exception {
new Thread(null, new Runnable() {
@Override
public void run() {
new Main().solve();
}
}, "1", 1 << 26).start();
//new Main().solve();
}
public StringBuilder solve() {
InputReader(System.in);
/*
* try { InputReader(new FileInputStream("C:\\Users\\hardik\\Desktop\\in.txt"));
* } catch(FileNotFoundException e) {}
*/
pw = new PrintWriter(System.out);
// ans_sb = new StringBuilder();
soln();
pw.close();
// System.out.println(ans_sb);
return ans_sb;
}
public void InputReader(InputStream stream1) {
stream = stream1;
}
private boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
private 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++];
}
private 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;
}
private 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;
}
private String nextToken() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
private 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();
}
private int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
private long[] nextLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
private void pArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
return;
}
private void pArray(long[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
return;
}
private boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return isWhitespace(c);
}
private char nextChar() {
int c = read();
while (isSpaceChar(c))
c = read();
char c1 = (char) c;
while (!isSpaceChar(c))
c = read();
return c1;
}
private interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
} | linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.util.*;
import java.io.*;
import java.text.DecimalFormat;
public class Main{
final long mod = (int)1e9+7, IINF = (long)1e19;
final int MAX = (int)5e5+1, MX = (int)1e7+1, INF = (int)1e9, root = 3;
DecimalFormat df = new DecimalFormat("0.0000000000000");
double eps = 1e-9, PI = 3.141592653589793238462643383279502884197169399375105820974944;
static boolean multipleTC = false, memory = false;
FastReader in;PrintWriter out;
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();
}
void run() throws Exception{
in = new FastReader();
out = new PrintWriter(System.out);
for(int i = 1, T= (multipleTC)?ni():1; i<= T; i++)solve(i);
out.flush();
out.close();
}
void solve(int TC) throws Exception{
int n = ni();
long d = nl();
long ans = 2;
long[] p = new long[n];
for(int i = 0; i< n; i++)p[i] = nl();
for(int i = 1; i< n; i++){
if(p[i]-p[i-1] == 2*d)ans++;
else if(p[i]-p[i-1]>2*d)ans+=2;
}
pn(ans);
}
int[] reverse(int[] a){
int[] o = new int[a.length];
for(int i = 0; i< a.length; i++)o[i] = a[a.length-i-1];
return o;
}
int[] sort(int[] a){
if(a.length==1)return a;
int mid = a.length/2;
int[] b = sort(Arrays.copyOfRange(a,0,mid)), c = sort(Arrays.copyOfRange(a,mid,a.length));
for(int i = 0, j = 0, k = 0; i< a.length; i++){
if(j<b.length && k<c.length){
if(b[j]<c[k])a[i] = b[j++];
else a[i] = c[k++];
}else if(j<b.length)a[i] = b[j++];
else a[i] = c[k++];
}
return a;
}
long[] sort(long[] a){
if(a.length==1)return a;
int mid = a.length/2;
long[] b = sort(Arrays.copyOfRange(a,0,mid)), c = sort(Arrays.copyOfRange(a,mid,a.length));
for(int i = 0, j = 0, k = 0; i< a.length; i++){
if(j<b.length && k<c.length){
if(b[j]<c[k])a[i] = b[j++];
else a[i] = c[k++];
}else if(j<b.length)a[i] = b[j++];
else a[i] = c[k++];
}
return a;
}
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 bitcount(long n){return (n==0)?0:(1+bitcount(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;
}
}
} | linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.io.*;
import java.util.*;
public class A implements Runnable{
public static void main (String[] args) {new Thread(null, new A(), "_cf", 1 << 28).start();}
public void run() {
FastScanner fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
System.err.println("Go!");
int n = fs.nextInt();
int d = fs.nextInt();
int[] a = fs.nextIntArray(n);
sort(a);
long res = 0;
if(n == 1) {
System.out.println(2);
return;
}
HashSet<Integer> set = new HashSet<>();
for(int i = 0; i < n; i++) {
int one = a[i] - d;
int min = Integer.MAX_VALUE;
for(int j = 0; j < n; j++) {
int dist = Math.abs(a[j] - one);
min = Math.min(min, dist);
}
if(min == d) set.add(one);
one = a[i] + d;
min = Integer.MAX_VALUE;
for(int j = 0; j < n; j++) {
int dist = Math.abs(a[j] - one);
min = Math.min(min, dist);
}
if(min == d) set.add(one);
}
System.out.println(set.size());
out.close();
}
void sort (int[] a) {
int n = a.length;
for(int i = 0; i < 50; i++) {
Random r = new Random();
int x = r.nextInt(n), y = r.nextInt(n);
int temp = a[x];
a[x] = a[y];
a[y] = temp;
}
Arrays.sort(a);
}
class FastScanner {
public int BS = 1<<16;
public char NC = (char)0;
byte[] buf = new byte[BS];
int bId = 0, size = 0;
char c = NC;
double num = 1;
BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) throws FileNotFoundException {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
}
public char nextChar(){
while(bId==size) {
try {
size = in.read(buf);
}catch(Exception e) {
return NC;
}
if(size==-1)return NC;
bId=0;
}
return (char)buf[bId++];
}
public int nextInt() {
return (int)nextLong();
}
public long nextLong() {
num=1;
boolean neg = false;
if(c==NC)c=nextChar();
for(;(c<'0' || c>'9'); c = nextChar()) {
if(c=='-')neg=true;
}
long res = 0;
for(; c>='0' && c <='9'; c=nextChar()) {
res = (res<<3)+(res<<1)+c-'0';
num*=10;
}
return neg?-res:res;
}
public double nextDouble() {
double cur = nextLong();
return c!='.' ? cur:cur+nextLong()/num;
}
public String next() {
StringBuilder res = new StringBuilder();
while(c<=32)c=nextChar();
while(c>32) {
res.append(c);
c=nextChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while(c<=32)c=nextChar();
while(c!='\n') {
res.append(c);
c=nextChar();
}
return res.toString();
}
public boolean hasNext() {
if(c>32)return true;
while(true) {
c=nextChar();
if(c==NC)return false;
else if(c>32)return true;
}
}
public int[] nextIntArray(int n) {
int[] res = new int[n];
for(int i = 0; i < n; i++) res[i] = nextInt();
return res;
}
}
} | linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.util.Arrays;
import java.util.Scanner;
public class SonyaAndHotels {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int d = sc.nextInt();
int[] locs = new int[n];
for (int i = 0; i < n; i++) {
locs[i] = sc.nextInt();
}
Arrays.sort(locs);
int count = 2;
for (int i = 0; i < locs.length-1; i++) {
if(locs[i+1]-locs[i]==2*d){
count++;
}else if(locs[i+1]-locs[i]>2*d){
count+=2;
}
}
System.out.println(count);
}
}
| linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.util.Arrays;
import java.util.Scanner;
public class CF495A {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
long d = s.nextLong();
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = s.nextLong();
}
Arrays.sort(arr);
long ans = 2;
for (int i = 0; i < n - 1; i++) {
if(arr[i + 1] - arr[i] > 2 * d){
ans += 2;
}else if(arr[i + 1] - arr[i] == 2 * d){
ans += 1;
}
}
System.out.println(ans);
}
}
| linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.io.*;
import java.util.*;
public class A {
public static void main(String[] args)throws Throwable {
MyScanner sc=new MyScanner();
PrintWriter pw=new PrintWriter(System.out);
int n=sc.nextInt();
int d=sc.nextInt();
int [] a=new int [n];
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
int ans=2;
for(int i=0;i<n-1;i++){
if(a[i+1]-a[i]<2*d)
continue;
if(a[i+1]-a[i]==2*d)
ans++;
else
ans+=2;
}
pw.println(ans);
pw.flush();
pw.close();
}
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;}
}
} | linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.io.*;
import java.util.*;
public class A {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Task solver = new Task();
solver.solve(1, in, out);
out.close();
}
static class Task {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int d = in.nextInt();
int a[] = new int[n];
int c[] = new int[2 * n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
c[2 * i] = a[i] - d;
c[2 * i + 1] = a[i] + d;
}
Arrays.sort(c);
int ans = 0;
for (int i = 0; i < 2 * n; i++) {
if (i != 0 && c[i] == c[i - 1]) continue;
int mind = d + 1;
for (int j = 0; j < n; j++)
mind = Math.min(mind, Math.abs(a[j] - c[i]));
if (mind == d) {
ans += 1;
}
}
out.println(ans);
}
}
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());
}
public long nextLong() {
return Long.parseLong(next());
}
}
} | linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
long d = reader.nextLong();
int[] a = new int[n];
for(int i = 0; i < n; i++)
a[i] = reader.nextInt();
Arrays.sort(a);
int ans = 2;
for(int i = 0; i < n - 1; i++){
if(a[i + 1] - a[i] > 2 * d) {
ans += 2;
}
else if(a[i + 1] - a[i] == 2 * d)
ans++;
}
System.out.println(ans);
}
} | linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.StringTokenizer;
public class q1 {
public static MyScanner in = new MyScanner();
public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out), true);
public static MyViewer view = new MyViewer();
public static BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
public static Random rand = new Random(System.currentTimeMillis());
public static class Pair<A, B> {
private final A first;
private final B second;
public Pair(A first, B second) {
super();
this.first = first;
this.second = second;
}
public int hashCode() {
int hashFirst = first != null ? first.hashCode() : 0;
int hashSecond = second != null ? second.hashCode() : 0;
return (hashFirst + hashSecond) * hashSecond + hashFirst;
}
public boolean equals(Object other) {
if (other instanceof Pair) {
Pair otherPair = (Pair) other;
return
((this.first == otherPair.first ||
(this.first != null && otherPair.first != null &&
this.first.equals(otherPair.first))) &&
(this.second == otherPair.second ||
(this.second != null && otherPair.second != null &&
this.second.equals(otherPair.second))));
}
return false;
}
public String toString() {
return "(" + first + ", " + second + ")";
}
public A getFirst() {
return first;
}
public B getSecond() {
return second;
}
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
private boolean randomInput = false;
private Random rand;
void randomInput(boolean r) {
randomInput = r;
rand = new Random(System.currentTimeMillis());
//rand = new Random(42);
}
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public MyScanner(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
}
public String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public int nextInt(int val) {
return randomInput ? val : Integer.parseInt(next());
}
public int nextInt(int low, int high) {
if (randomInput) {
return rand.nextInt(high - low + 1) + low;
} else {
return Integer.parseInt(next());
}
}
long nextLong() {
return Long.parseLong(next());
}
long nextLong(long val) {
return randomInput ? val : Long.parseLong(next());
}
long nextLong(long low, long high) {
if (randomInput) {
return low + ((long) (rand.nextDouble() * (high - low + 1)));
} else {
return Long.parseLong(next());
}
}
double nextDouble() {
return Double.parseDouble(next());
}
int[] arrayInt(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
int[] arrayInt(int n, int low, int high) {
int[] a = new int[n];
if (randomInput) {
for (int i = 0; i < n; i++) {
a[i] = rand.nextInt(high - low + 1) + low;
}
} else {
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
}
return a;
}
ArrayList<Integer> list(int n) {
ArrayList<Integer> a = new ArrayList<Integer>(n);
for (int i = 0; i < n; i++) {
a.add(nextInt());
}
return a;
}
ArrayList<Integer> list(int n, int low, int high) {
ArrayList<Integer> a = new ArrayList<Integer>(n);
if (randomInput) {
for (int i = 0; i < n; i++) {
a.add(rand.nextInt(high - low + 1) + low);
}
} else {
for (int i = 0; i < n; i++) {
a.add(nextInt());
}
}
return a;
}
long[] arrayLong(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nextLong();
}
return a;
}
long[] arrayLong(int n, long low, long high) {
long[] a = new long[n];
if (randomInput) {
for (int i = 0; i < n; i++) {
double r = rand.nextDouble();
a[i] = (long) (r * (double) (high - low + 1)) + low;
if (a[i] == 0) {
out.println("Ouch : " + r);
}
}
} else {
for (int i = 0; i < n; i++) {
a[i] = nextLong();
}
}
return a;
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
void close() {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
ArrayList<ArrayList<Integer>> randomTree(int n) {
ArrayList<ArrayList<Integer>> edges = new ArrayList<>();
for (int i = 0; i < n; i++) {
edges.add(new ArrayList<>());
}
for (int i = 1; i < n; i++) {
int par = rand.nextInt(i);
edges.get(par).add(i);
}
return edges;
}
}
static class MyViewer {
private static boolean print = true;
public void on() {
print = true;
}
public void off() {
print = false;
}
public <T extends List> void list(T a) {
if (!print) return;
if (a == null) {
out.println("List: NULL");
return;
}
if (a.size() == 0) {
out.println("List: []");
return;
}
out.print("List: [" + a.get(0));
for (int i = 1; i < a.size(); i++) {
out.print(", " + a.get(i));
}
out.println("] Len: " + a.size());
}
public <T> void array(T[] a) {
if (!print) return;
if (a == null) {
out.println("Array: NULL");
return;
}
if (a.length == 0) {
out.println("Array: []");
return;
}
out.print("Array: [" + a[0]);
for (int i = 1; i < a.length; i++) {
out.print(", " + a[i]);
}
out.println("] Len: " + a.length);
}
public void array(boolean[] a) {
if (!print) return;
if (a == null) {
out.println("boolean[]: NULL");
return;
}
if (a.length == 0) {
out.println("boolean[]: Len: 0");
return;
}
out.print("boolean[] Len: " + a.length + " [");
for (boolean x : a) {
out.print(x + ", ");
}
out.println("\b\b]");
}
public void array(int[] a) {
if (!print) return;
if (a == null) {
out.println("Array: NULL");
return;
}
if (a.length == 0) {
out.println("Array: []");
return;
}
out.print("int[] Len: " + a.length + " [");
for (int x : a) {
out.print(x + ", ");
}
out.println("\b\b]");
}
public void array(long[] a) {
if (!print) return;
if (a == null) {
out.println("Array: NULL");
return;
}
if (a.length == 0) {
out.println("Array: []");
return;
}
out.print("long Array: [");
for (long x : a) {
out.print(x + ", ");
}
out.println("\b\b] Len: " + a.length);
}
public void matrix(int[][] a, int cutoff) {
if (cutoff == 0)
cutoff = Integer.MAX_VALUE;
if (a == null) {
out.println("Matrix: NULL");
return;
}
for (int i = 0; i < a.length; i++) {
if (i < cutoff) {
printMatrixRow(a[i], cutoff);
} else {
out.println(" ...");
printMatrixRow(a[a.length - 1], cutoff);
break;
}
}
}
public void matrix(long[][] a, long cutoff) {
if (cutoff == 0)
cutoff = Long.MAX_VALUE;
if (a == null) {
out.println("Matrix: NULL");
return;
}
for (int i = 0; i < a.length; i++) {
if (i < cutoff) {
printMatrixRow(a[i], cutoff);
} else {
out.println(" ...");
printMatrixRow(a[a.length - 1], cutoff);
break;
}
}
}
public void matrix(boolean[][] a, int cutoff) {
if (cutoff == 0)
cutoff = Integer.MAX_VALUE;
if (a == null) {
out.println("Matrix: NULL");
return;
}
for (int i = 0; i < a.length; i++) {
if (i < cutoff) {
printMatrixRow(a[i], cutoff);
} else {
out.println(" ...");
printMatrixRow(a[a.length - 1], cutoff);
break;
}
}
}
private void printMatrixRow(int[] a, int cutoff) {
for (int j = 0; j < a.length; j++) {
if (j < cutoff) {
out.printf("%6d ", a[j]);
} else {
out.printf(" ... %6d", a[a.length - 1]);
break;
}
}
out.println();
}
private void printMatrixRow(long[] a, long cutoff) {
for (int j = 0; j < a.length; j++) {
if (j < cutoff) {
out.printf("%6d ", a[j]);
} else {
out.printf(" ... %6d", a[a.length - 1]);
break;
}
}
out.println();
}
private void printMatrixRow(boolean[] a, int cutoff) {
for (int j = 0; j < a.length; j++) {
if (j < cutoff) {
out.print(a[j] ? "T " : "F ");
} else {
out.print(" ... " + (a[a.length - 1] ? "T" : "F"));
break;
}
}
out.println();
}
}
public static void main(String[] args) throws IOException {
int n = in.nextInt();
int d = in.nextInt();
int[] a = in.arrayInt(n);
int count = 2;
for(int i = 0 ;i < n-1; i++) {
if( a[i+1] - a[i] == 2 * d )
count += 1;
if( a[i+1] - a[i] > 2 * d)
count += 2;
}
out.println(count);
log.flush();
in.close();
}
}
| linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.math.*;
import java.util.*;
import java.io.*;
public class Main{
void solve() {
int n=ni();
long d=nl();
long x[]=new long[n+1];
for(int i=1;i<=n;i++) x[i]=nl();
Arrays.sort(x,1,n+1);
int ans=2;
for(int i=2;i<=n;i++){
long x1=x[i-1]+d,x2=x[i]-d;
if(x[i]-x1>=d) ans++;
if(x2-x[i-1]>=d && x1!=x2) ans++;
// pw.println(ans);
}
pw.println(ans);
}
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 void tr(Object... o) { if(INPUT.length() > 0)System.out.println(Arrays.deepToString(o)); }
} | linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.util.*;
import java.io.*;
import java.math.*;
public class Test5 {
static StreamTokenizer st = new StreamTokenizer(new InputStreamReader(System.in));
static int[] m;
public static void main(String[] z) throws Exception {
PrintWriter pw = new PrintWriter(System.out);
Scanner s = new Scanner(System.in);
int a = ni(), b=ni(), o=2;
m = new int[a];
for(int q=0; q<a; q++) m[q] = ni();
Arrays.sort(m);
for(int q=1; q<a; q++){
if(m[q]-m[q-1]==b*2) o++;
else if(m[q]-m[q-1]>b*2) o+=2;
}
System.out.println(o);
pw.flush();
}
static int ni() throws Exception{
st.nextToken();
return (int)st.nval;
}
static String ns() throws Exception{
st.nextToken();
return st.sval;
}
static long gcd(long a, long b){
for(; a>0 && b>0;)
if(a>b) a%=b;
else b%=a;
return a+b;
}
static class PyraSort {
private static int heapSize;
public static void sort(int[] a) {
buildHeap(a);
while (heapSize > 1) {
swap(a, 0, heapSize - 1);
heapSize--;
heapify(a, 0);
}
}
private static void buildHeap(int[] a) {
heapSize = a.length;
for (int i = a.length / 2; i >= 0; i--) {
heapify(a, i);
}
}
private static void heapify(int[] a, int i) {
int l = 2 * i + 2;
int r = 2 * i + 1;
int largest = i;
if (l < heapSize && a[i] < a[l]) {
largest = l;
}
if (r < heapSize && a[largest] < a[r]) {
largest = r;
}
if (i != largest) {
swap(a, i, largest);
heapify(a, largest);
}
}
private static void swap(int[] a, int i, int j) {
a[i] ^= a[j] ^= a[i];
a[j] ^= a[i];
}
}
}
| linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.util.*;
import java.io.*;
import static java.lang.Math.*;
public class Main
{
static MyScanner scan;
static PrintWriter pw;
public static void main(String[] args) {
new Thread(null,null,"_",1<<25)
{
public void run()
{
try
{
solve();
}
catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
}.start();
}
static void solve() throws IOException {
scan = new MyScanner();
pw = new PrintWriter(System.out, true);
StringBuilder sb = new StringBuilder();
int n = ni();
int d = ni();
int arr[] = new int[n];
int cnt = 2;
for(int i=0;i<n;++i)
arr[i] = ni();
for(int i=0;i<n-1;++i)
{
if(arr[i+1]-d==arr[i]+d)
{
++cnt;
continue;
}
if(arr[i+1]-(arr[i]+d)>d)
++cnt;
if((arr[i+1]-d)-arr[i]>d)
++cnt;
}
pl(cnt);
pw.flush();
pw.close();
}
static long MMI(long A,long MOD)
{
return modpow(A,MOD-2,MOD);
}
static long modpow(long x,long y,long MOD)
{
if(y==0)
return 1;
if((y&1)==0)
return modpow((x*x)%MOD,y>>1,MOD);
else return (x*modpow(x,y-1,MOD))%MOD;
}
static class Pair implements Comparable<Pair>
{
int x,y;
Pair(int x,int y)
{
this.x=x;
this.y=y;
}
public int compareTo(Pair other)
{
if(this.x!=other.x)
return this.x-other.x;
return this.y-other.y;
}
public String toString()
{
return "{"+x+","+y+"}";
}
}
static int ni() throws IOException
{
return scan.nextInt();
}
static long nl() throws IOException
{
return scan.nextLong();
}
static double nd() throws IOException
{
return scan.nextDouble();
}
static String ne() throws IOException
{
return scan.next();
}
static String nel() throws IOException
{
return scan.nextLine();
}
static void pl()
{
pw.println();
}
static void p(Object o)
{
pw.print(o+" ");
}
static void pl(Object o)
{
pw.println(o);
}
static void psb(StringBuilder sb)
{
pw.print(sb);
}
static class MyScanner
{
BufferedReader br;
StringTokenizer st;
MyScanner()
{
br = new BufferedReader(new InputStreamReader(System.in));
}
String nextLine()throws IOException
{
return br.readLine();
}
String next() throws IOException
{
if(st==null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
int nextInt() throws IOException
{
return Integer.parseInt(next());
}
long nextLong() throws IOException
{
return Long.parseLong(next());
}
double nextDouble() throws IOException
{
return Double.parseDouble(next());
}
}
} | linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class ProblemA {
String fileName = "prizes";
public void solve() throws IOException {
int n = nextInt();
int d = nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
int ans = 2;
for (int i = 1; i < n; i++) {
if (a[i] - a[i - 1] == 2 * d)
ans++;
if (a[i] - a[i - 1] > 2 * d)
ans += 2;
}
out.println(ans);
}
public void run() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out, true);
// out = new PrintWriter(fileName + ".out");
// br = new BufferedReader(new FileReader(fileName + ".in"));
// out = new PrintWriter(fileName + ".out");
solve();
out.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
BufferedReader br;
StringTokenizer in;
static PrintWriter out;
public String nextToken() throws IOException {
while (in == null || !in.hasMoreTokens()) {
in = new StringTokenizer(br.readLine());
}
return in.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
public long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
public static void main(String[] args) throws IOException {
new ProblemA().run();
}
} | linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.util.*;
import org.omg.CORBA.INTERNAL;
import java.awt.List;
import java.io.*;
import java.lang.*;
import java.lang.reflect.Array;
public class code1
{
public static long[][] cnt;
public static void main(String[] args)
{
InputReader in = new InputReader(System.in);
PrintWriter pw = new PrintWriter(System.out);
//Code starts..
int n = in.nextInt();
long d = in.nextInt();
long[] a = new long[n];
for(int i=0; i<n; i++)
a[i] = in.nextLong();
int ans = 0;
//ans++;
HashSet<Long> set = new HashSet<>();
/*set.add(a[0]-d);
if(Math.abs(a[0]+d-a[1])>=d)
{
ans++;
set.add(a[0]+d);
}
*/
for(int i=1; i<n; i++)
{
//pw.println(a[i]+" "+a[i-1]);
long dis = (long) Math.abs(a[i]-a[i-1]);
//pw.println(dis);
if(dis==2*d)
ans++;
if(dis-(long)2*d>0)
ans += 2;
}
pw.println(ans+2);
//Code ends....
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;
}
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 c = 0;
public static long mod = 1000000007;
public static int d;
public static int p;
public static int q;
public static boolean flag;
public static long INF= Long.MAX_VALUE;
public static long fun(int[] a, int[] b, int m,int n) {
long result =0;
for(int i=0; i<m; i++)
for(int j=0; j<m; j++)
{
long[] fib = new long[Math.max(2, n+2)];
fib[1] = a[i];
fib[2] = b[j];
for(int k=3; k<=n; k++)
fib[k] = (fib[k-1]%mod + fib[k-2]%mod)%mod;
result = (result%mod + fib[n]%mod)%mod;
}
return result;
}
public static double slope(pair p1, pair p2)
{
double m = INF;
if((p1.x - p2.x)!=0)
m = (p1.y - p2.y)/(p1.x - p2.x);
return Math.abs(m);
}
public static int count(String[] s, int f)
{
int count = 0;
int n = s[0].length();
if(f==1)
{
for(int i = 0; i<n; i++)
{
for(int j=0; j<n; j++)
{
if(i%2==0)
{
if(j%2==0 && s[i].charAt(j)=='0')
count++;
if(j%2==1 && s[i].charAt(j)=='1')
count++;
}
if(i%2==1)
{
if(j%2==1 && s[i].charAt(j)=='0')
count++;
if(j%2==0 && s[i].charAt(j)=='1')
count++;
}
}
}
}
else
{
count = 0;
for(int i = 0; i<n; i++)
{
for(int j=0; j<n; j++)
{
if(i%2==1)
{
if(j%2==0 && s[i].charAt(j)=='0')
count++;
if(j%2==1 && s[i].charAt(j)=='1')
count++;
}
if(i%2==0)
{
if(j%2==1 && s[i].charAt(j)=='0')
count++;
if(j%2==0 && s[i].charAt(j)=='1')
count++;
}
}
}
}
return count;
}
public static int gcd(int p2, int p22)
{
if (p2 == 0)
return (int) p22;
return gcd(p22%p2, p2);
}
public static int findGCD(int arr[], int n)
{
int result = arr[0];
for (int i=1; i<n; i++)
result = gcd(arr[i], result);
return result;
}
public static void nextGreater(long[] a, int[] ans)
{
Stack<Integer> stk = new Stack<>();
stk.push(0);
for(int i=1; i<a.length; i++)
{
if(!stk.isEmpty())
{
int s = stk.pop();
while(a[s]<a[i])
{
ans[s] = i;
if(!stk.isEmpty())
s = stk.pop();
else
break;
}
if(a[s]>=a[i])
stk.push(s);
}
stk.push(i);
}
return;
}
public static void nextGreaterRev(long[] a, int[] ans)
{
int n = a.length;
int[] pans = new int[n];
Arrays.fill(pans, -1);
long[] arev = new long[n];
for(int i=0; i<n; i++)
arev[i] = a[n-1-i];
Stack<Integer> stk = new Stack<>();
stk.push(0);
for(int i=1; i<n; i++)
{
if(!stk.isEmpty())
{
int s = stk.pop();
while(arev[s]<arev[i])
{
pans[s] = n - i-1;
if(!stk.isEmpty())
s = stk.pop();
else
break;
}
if(arev[s]>=arev[i])
stk.push(s);
}
stk.push(i);
}
//for(int i=0; i<n; i++)
//System.out.print(pans[i]+" ");
for(int i=0; i<n; i++)
ans[i] = pans[n-i-1];
return;
}
public static void nextSmaller(long[] a, int[] ans)
{
Stack<Integer> stk = new Stack<>();
stk.push(0);
for(int i=1; i<a.length; i++)
{
if(!stk.isEmpty())
{
int s = stk.pop();
while(a[s]>a[i])
{
ans[s] = i;
if(!stk.isEmpty())
s = stk.pop();
else
break;
}
if(a[s]<=a[i])
stk.push(s);
}
stk.push(i);
}
return;
}
public static long lcm(int[] numbers) {
long lcm = 1;
int divisor = 2;
while (true) {
int cnt = 0;
boolean divisible = false;
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] == 0) {
return 0;
} else if (numbers[i] < 0) {
numbers[i] = numbers[i] * (-1);
}
if (numbers[i] == 1) {
cnt++;
}
if (numbers[i] % divisor == 0) {
divisible = true;
numbers[i] = numbers[i] / divisor;
}
}
if (divisible) {
lcm = lcm * divisor;
} else {
divisor++;
}
if (cnt == numbers.length) {
return lcm;
}
}
}
public static long fact(long n) {
long factorial = 1;
for(int i = 1; i <= n; i++)
{
factorial *= i;
}
return factorial;
}
public static void factSieve(int[] a, int n) {
for(int i=2; i<=n; i+=2)
a[i] = 2;
for(int i=3; i<=n; i+=2)
{
if(a[i]==0)
{
a[i] = i;
for(int j=i; j*i<=n; j++)
{
a[i*j] = i;
}
}
}
int k = 1000;
while(k!=1)
{
System.out.print(a[k]+" ");
k /= a[k];
}
}
public static int lowerLimit(int[] a, int n) {
int ans = 0;
int ll = 0;
int rl = a.length-1;
// System.out.println(a[rl]+" "+n);
if(a[0]>n)
return 0;
if(a[0]==n)
return 1;
else if(a[rl]<=n)
return rl+1;
while(ll<=rl)
{
int mid = (ll+rl)/2;
if(a[mid]==n)
{
ans = mid + 1;
break;
}
else if(a[mid]>n)
{
rl = mid-1;
}
else
{
ans = mid+1;
ll = mid+1;
}
}
return ans;
}
public static long choose(long total, long choose){
if(total < choose)
return 0;
if(choose == 0 || choose == total)
return 1;
return (choose(total-1,choose-1)+choose(total-1,choose))%mod;
}
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 long[] sort(long[] a)
{
Random gen = new Random();
int n = a.length;
for(int i=0;i<n;i++)
{
int ind = gen.nextInt(n-i)+i;
long temp = a[ind];
a[ind] = a[i];
a[i] = temp;
}
Arrays.sort(a);
return a;
}
public static pair[] sort(pair[] a)
{
Random gen = new Random();
int n = a.length;
for(int i=0;i<n;i++)
{
int ind = gen.nextInt(n-i)+i;
pair temp = a[ind];
a[ind] = a[i];
a[i] = temp;
}
Arrays.sort(a);
return a;
}
public static int[] sort(int[] a)
{
Random gen = new Random();
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;
}
Arrays.sort(a);
return a;
}
public static int floorSearch(int arr[], int low, int high, int x)
{
if (low > high)
return -1;
if (x > arr[high])
return high;
int mid = (low+high)/2;
if (mid > 0 && arr[mid-1] < x && x < arr[mid])
return mid-1;
if (x < arr[mid])
return floorSearch(arr, low, mid-1, x);
return floorSearch(arr, mid+1, high, x);
}
public static void swap(int a, int b){
int temp = a;
a = b;
b = temp;
}
public static ArrayList<Integer> primeFactorization(int n)
{
ArrayList<Integer> a =new ArrayList<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 lowerbound(ArrayList<Long> net, long c2) {
int i=Collections.binarySearch(net, c2);
if(i<0)
i = -(i+2);
return i;
}
public static int lowerboundArray(long[] psum, long c2) {
int i=Arrays.binarySearch(psum, c2);
if(i<0)
i = -(i+2);
return i;
}
public static int lowerboundArray(int[] psum, int c2) {
int i=Arrays.binarySearch(psum, c2);
if(i<0)
i = -(i+2);
return i;
}
public static int uperboundArray(long[] psum, long c2) {
int i=Arrays.binarySearch(psum, c2);
if(i<0)
i = -(i+1);
return i;
}
public static int uperbound(ArrayList<Long> net, long c2) {
int i=Collections.binarySearch(net, c2);
if(i<0)
i = -(i+1);
return i;
}
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 int[] countDer(int n)
{
int der[] = new int[n + 1];
der[0] = 1;
der[1] = 0;
der[2] = 1;
for (int i = 3; i <= n; ++i)
der[i] = (i - 1) * (der[i - 1] + der[i - 2]);
// Return result for n
return der;
}
static long binomialCoeff(int n, int k)
{
long C[][] = new long[n+1][k+1];
int i, j;
// Calculate value of Binomial Coefficient in bottom up manner
for (i = 0; i <= n; i++)
{
for (j = 0; j <= Math.min(i, k); j++)
{
// Base Cases
if (j == 0 || j == i)
C[i][j] = 1;
// Calculate value using previosly stored values
else
C[i][j] = C[i-1][j-1] + C[i-1][j];
}
}
return C[n][k];
}
public static long binaryExponentiation(long x,long n)
{
long result=1;
while(n>0)
{
if(n % 2 ==1)
result=result * x;
x=(x%mod * x%mod)%mod;
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)%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*x)%M;
n=n/2;
}
return result;
}
public static int 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 checkYear(int year)
{
if (year % 400 == 0)
return true;
if (year % 100 == 0)
return false;
if (year % 4 == 0)
return true;
return false;
}
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;
}
static class pair implements Comparable<pair>
{
Long x, y;
pair(long x, long y) {
this.x = x;
this.y = y;
}
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;
if(p.x-x==0 && p.y-y==0)
return true;
else
return false;
}
return false;
}
public int hashCode() {
return new Long(x).hashCode() * 31 + new Long(y).hashCode();
}
}
static class triplet implements Comparable<triplet>
{
Integer x,y;
Long z;
triplet(Integer l,Integer m,long z)
{
this.x = l;
this.y = m;
this.z = z;
}
public int compareTo(triplet o)
{
int result = x.compareTo(o.x);
if(result==0)
result = y.compareTo(o.y);
if(result==0)
result = z.compareTo(o.z);
return result;
}
public boolean equlas(Object o)
{
if(o instanceof triplet)
{
triplet p = (triplet)o;
return x==p.x && y==p.y && z==p.z;
}
return false;
}
public String toString()
{
return x+" "+y+" "+z;
}
public int hashCode()
{
return new Long(x).hashCode()*31 + new Long(y).hashCode() + new Long(z).hashCode();
}
}
static class spair implements Comparable<spair>
{
String x;
Integer y;
spair(String x, int y) {
this.x = x;
this.y = y;
}
public int compareTo(spair o) {
String s1 = x + o.x;
String s2 = o.x + x;
long p1 = cnt[y][0] + cnt[o.y][0];
long p2 = p1;
p1 += cnt[y][1] * cnt[o.y][2];
p2 += cnt[o.y][1] * cnt[y][2];
if(p1==p2)
return 0;
if(p1>p2)
return -1;
return 1;
}
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();
}
}
/*static class node implements Comparable<node>
{
Integer x, y, z;
node(int x,int y, int z)
{
this.x=x;
this.y=y;
this.z=z;
}
public int compareTo(pair o) {
int result = x.compareTo(o.x);
if(result==0)
result = y.compareTo(o.y);
if(result==0)
result = z.compareTo(z);
return result;
}
@Override
public int compareTo(node o) {
// TODO Auto-generated method stub
return 0;
}
}
*/
}
}
| linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
public class Main {
public static void main(String[] args) throws IOException {
// write your code here
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String[] s = reader.readLine().split(" ");
int n = Integer.parseInt(s[0]);
int d = Integer.parseInt(s[1]);
List<Integer> list = new ArrayList<>();
s = reader.readLine().split(" ");
for (int i = 0; i < n; i++) {
list.add(Integer.parseInt(s[i]));
}
HashSet<Integer> set = new HashSet<>();
for (Integer i : list) {
set.add(i - d);
set.add(i + d);
}
HashSet<Integer> set2 = new HashSet<>();
for (Integer i : set) {
for (Integer x : list) {
if (Math.abs(i - x) < d) {
set2.add(i);
}
}
}
set.removeAll(set2);
System.out.println(set.size());
}
}
| linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.util.*;
import java.io.*;
public class Main {
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader fileReader) {
br = new BufferedReader(fileReader);
}
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 boolean ready() throws IOException {
return br.ready();
}
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);PrintWriter pw = new PrintWriter(System.out);
int n=sc.nextInt(),d=sc.nextInt();int[] a = new int[n];int ans=2;a[0]=sc.nextInt();
for (int i=1;i<n;i++){
a[i]=sc.nextInt();
if (a[i]-a[i-1]==2*d)ans++;
else if (a[i]-a[i-1]>2*d)ans+=2;
}
System.out.println(ans);
}
} | linear | 1004_A. Sonya and Hotels | CODEFORCES |
//package round495;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.Set;
public class A {
InputStream is;
PrintWriter out;
String INPUT = "";
void solve()
{
int n = ni(), d = ni();
int[] a = na(n);
Set<Long> set = new HashSet<>();
for(int v : a){
set.add(v-(long)d);
set.add(v+(long)d);
}
int ct = 0;
for(long s : set){
long min = Long.MAX_VALUE;
for(int v : a){
min = Math.min(min, Math.abs(s-v));
}
if(min == d)ct++;
}
out.println(ct);
}
void run() throws Exception
{
is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
tr(System.currentTimeMillis()-s+"ms");
}
public static void main(String[] args) throws Exception { new A().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(!oj)System.out.println(Arrays.deepToString(o)); }
}
| linear | 1004_A. Sonya and Hotels | CODEFORCES |
/*
*
* @Author Ajudiya_13(Bhargav Girdharbhai Ajudiya)
* Dhirubhai Ambani Institute of Information And Communication Technology
*
*/
import java.util.*;
import java.io.*;
import java.lang.*;
public class Code1
{
public static void main(String[] args)
{
InputReader in = new InputReader(System.in);
PrintWriter pw = new PrintWriter(System.out);
int n = in.nextInt();
long d = in.nextLong();
long[]a = new long[n];
for(int i=0;i<n;i++)
a[i] = in.nextLong();
int ans = 1;
for(int i=0;i<n-1;i++)
{
long x = a[i+1]-a[i];
if(x==2*d)
ans++;
else if(x>2*d)
ans+=2;
//System.out.println(ans);
}
ans++;
pw.print(ans);
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;
}
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*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 * x)%M;
x=(x*x)%M;
n=n/2;
}
return result;
}
public static int 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;
}
static class pair implements Comparable<pair>
{
Integer x, y;
pair(int x,int y)
{
this.x=x;
this.y=y;
}
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();
}
}
} | linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.util.*;
import java.io.*;
public class _1004_A {
public static void main(String[] args) throws IOException {
int N = readInt(), D = readInt(); long arr[] = new long[N+2]; arr[0] = -3000000000L; arr[N+1] = -arr[0];
for(int i = 1; i<=N; i++) arr[i] = readInt();
int cnt = 1; if(Math.abs(arr[2]-(arr[1] + D)) >= D) cnt++; for(int i = 2; i<=N; i++) {
if(Math.abs(arr[i-1]-(arr[i] - D)) > D) cnt++;
if(Math.abs(arr[i+1]-(arr[i] + D)) >= D) cnt++;
}
println(cnt); exit();
}
final private static int BUFFER_SIZE = 1 << 16;
private static DataInputStream din = new DataInputStream(System.in);
private static byte[] buffer = new byte[BUFFER_SIZE];
private static int bufferPointer = 0, bytesRead = 0;
static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
public static String readLine() throws IOException {
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = Read()) != -1) {
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public static String read() throws IOException {
byte[] ret = new byte[1024];
int idx = 0;
byte c = Read();
while (c <= ' ') {
c = Read();
}
do {
ret[idx++] = c;
c = Read();
} while (c != -1 && c != ' ' && c != '\n' && c != '\r');
return new String(ret, 0, idx);
}
public static int readInt() throws IOException {
int ret = 0;
byte c = Read();
while (c <= ' ')
c = Read();
boolean neg = (c == '-');
if (neg)
c = Read();
do {
ret = ret * 10 + c - '0';
} while ((c = Read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public static long readLong() throws IOException {
long ret = 0;
byte c = Read();
while (c <= ' ')
c = Read();
boolean neg = (c == '-');
if (neg)
c = Read();
do {
ret = ret * 10 + c - '0';
} while ((c = Read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public static double readDouble() throws IOException {
double ret = 0, div = 1;
byte c = Read();
while (c <= ' ')
c = Read();
boolean neg = (c == '-');
if (neg)
c = Read();
do {
ret = ret * 10 + c - '0';
} while ((c = Read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = Read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private static void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private static byte Read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
static void print(Object o) {
pr.print(o);
}
static void println(Object o) {
pr.println(o);
}
static void flush() {
pr.flush();
}
static void println() {
pr.println();
}
static void exit() throws IOException {
din.close();
pr.close();
System.exit(0);
}
}
| linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.io.*;
import java.util.*;
import java.util.Random;
import java.util.StringTokenizer;
public class Main {
long b = 31;
String fileName = "";
////////////////////// SOLUTION SOLUTION SOLUTION //////////////////////////////
int INF = Integer.MAX_VALUE / 10;
long MODULO = 1000*1000*100;
void solve() throws IOException {
int n = readInt();
int d = readInt();
int[] arr = readIntArray(n);
Arrays.sort(arr);
int ans = 2;
for (int i=0; i < n - 1; ++i){
int cur = arr[i] + d;
if (arr[i+1] - d == cur) ans++;
if (arr[i+1] - d > cur) ans +=2;
}
out.println(ans);
}
class Number implements Comparable<Number>{
int x, cost;
Number(int x, int cost){
this.x = x;
this.cost = cost;
}
@Override
public int compareTo(Number o) {
return Integer.compare(this.cost, o.cost);
}
}
class Point{
int x, y;
Point(int x, int y){
this.x = x;
this.y = y;
}
}
class Vertex implements Comparable<Vertex>{
int num, depth, e, c;
Vertex(int num, int depth, int e, int c){
this.num = num;
this.e = e;
this.depth = depth;
this.c = c;
}
@Override
public int compareTo(Vertex o) {
return Integer.compare(this.e, o.e);
}
}
///////////////////////////////////////////////////////////////////////////////////////////
class Edge{
int from, to, num;
Edge(int to, int num){
this.to = to;
this.num = num;
}
}
class SparseTable{
int[][] rmq;
int[] logTable;
int n;
SparseTable(int[] a){
n = a.length;
logTable = new int[n+1];
for(int i = 2; i <= n; ++i){
logTable[i] = logTable[i >> 1] + 1;
}
rmq = new int[logTable[n] + 1][n];
for(int i=0; i<n; ++i){
rmq[0][i] = a[i];
}
for(int k=1; (1 << k) < n; ++k){
for(int i=0; i + (1 << k) <= n; ++i){
int max1 = rmq[k - 1][i];
int max2 = rmq[k-1][i + (1 << (k-1))];
rmq[k][i] = Math.max(max1, max2);
}
}
}
int max(int l, int r){
int k = logTable[r - l];
int max1 = rmq[k][l];
int max2 = rmq[k][r - (1 << k) + 1];
return Math.max(max1, max2);
}
}
long checkBit(long mask, int bit){
return (mask >> bit) & 1;
}
class Dsu{
int[] parent;
int countSets;
Dsu(int n){
countSets = n;
parent = new int[n];
for(int i=0; i<n; ++i){
parent[i] = i;
}
}
int findSet(int a){
if(parent[a] == a) return a;
parent[a] = findSet(parent[a]);
return parent[a];
}
void unionSets(int a, int b){
a = findSet(a);
b = findSet(b);
if(a!=b){
countSets--;
parent[a] = b;
}
}
}
static int checkBit(int mask, int bit) {
return (mask >> bit) & 1;
}
boolean isLower(char c){
return c >= 'a' && c <= 'z';
}
////////////////////////////////////////////////////////////
class SegmentTree{
int[] t;
int n;
SegmentTree(int n){
t = new int[4*n];
build(new int[n+1], 1, 1, n);
}
void build (int a[], int v, int tl, int tr) {
if (tl == tr)
t[v] = a[tl];
else {
int tm = (tl + tr) / 2;
build (a, v*2, tl, tm);
build (a, v*2+1, tm+1, tr);
}
}
void update (int v, int tl, int tr, int l, int r, int add) {
if (l > r)
return;
if (l == tl && tr == r)
t[v] += add;
else {
int tm = (tl + tr) / 2;
update (v*2, tl, tm, l, Math.min(r,tm), add);
update (v*2+1, tm+1, tr, Math.max(l,tm+1), r, add);
}
}
int get (int v, int tl, int tr, int pos) {
if (tl == tr)
return t[v];
int tm = (tl + tr) / 2;
if (pos <= tm)
return t[v] + get (v*2, tl, tm, pos);
else
return t[v] + get (v*2+1, tm+1, tr, pos);
}
}
class Fenwik {
long[] t;
int length;
Fenwik(int[] a) {
length = a.length + 100;
t = new long[length];
for (int i = 0; i < a.length; ++i) {
inc(i, a[i]);
}
}
void inc(int ind, int delta) {
for (; ind < length; ind = ind | (ind + 1)) {
t[ind] = Math.max(delta, t[ind]);
}
}
long getMax(int r) {
long sum = 0;
for (; r >= 0; r = (r & (r + 1)) - 1) {
sum = Math.max(sum, t[r]);
}
return sum;
}
}
int gcd(int a, int b){
return b == 0 ? a : gcd(b, a%b);
}
long gcd(long a, long b){
return b == 0 ? a : gcd(b, a%b);
}
long binPow(long a, long b, long m) {
if (b == 0) {
return 1;
}
if (b % 2 == 1) {
return ((a % m) * (binPow(a, b - 1, m) % m)) % m;
} else {
long c = binPow(a, b / 2, m);
return (c * c) % m;
}
}
int minInt(int... values) {
int min = Integer.MAX_VALUE;
for (int value : values) min = Math.min(min, value);
return min;
}
int maxInt(int... values) {
int max = Integer.MIN_VALUE;
for (int value : values) max = Math.max(max, value);
return max;
}
public static void main(String[] args) throws NumberFormatException, IOException {
// TODO Auto-generated method stub
new Main().run();
}
void run() throws NumberFormatException, IOException {
solve();
out.close();
};
BufferedReader in;
PrintWriter out;
StringTokenizer tok;
String delim = " ";
Random rnd = new Random();
Main() throws FileNotFoundException {
try {
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
} catch (Exception e) {
if (fileName.isEmpty()) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
} else {
in = new BufferedReader(new FileReader(fileName + ".in"));
out = new PrintWriter(fileName + ".out");
}
}
tok = new StringTokenizer("");
}
String readLine() throws IOException {
return in.readLine();
}
String readString() throws IOException {
while (!tok.hasMoreTokens()) {
String nextLine = readLine();
if (null == nextLine) {
return null;
}
tok = new StringTokenizer(nextLine);
}
return tok.nextToken();
}
int readInt() throws NumberFormatException, IOException {
return Integer.parseInt(readString());
}
byte readByte() throws NumberFormatException, IOException {
return Byte.parseByte(readString());
}
int[] readIntArray (int n) throws NumberFormatException, IOException {
int[] a = new int[n];
for(int i=0; i<n; ++i){
a[i] = readInt();
}
return a;
}
Integer[] readIntegerArray (int n) throws NumberFormatException, IOException {
Integer[] a = new Integer[n];
for(int i=0; i<n; ++i){
a[i] = readInt();
}
return a;
}
long readLong() throws NumberFormatException, IOException {
return Long.parseLong(readString());
}
double readDouble() throws NumberFormatException, IOException {
return Double.parseDouble(readString());
}
}
| linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.FileNotFoundException;
import java.util.StringTokenizer;
import java.io.Writer;
import java.io.BufferedReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Asgar Javadov
*/
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);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
static class TaskA {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt();
int d = in.nextInt();
int[] a = in.readIntArray(n);
int ans = 1;
for (int i = 0; i < a.length - 1; ++i) {
int left = a[i] + d;
int right = a[i + 1] - d;
if (left < right) {
ans += 2;
} else if (left == right)
ans++;
}
out.println(ans + 1);
}
}
static class OutputWriter extends PrintWriter {
public OutputWriter(OutputStream outputStream) {
super(outputStream);
}
public OutputWriter(Writer writer) {
super(writer);
}
public OutputWriter(String filename) throws FileNotFoundException {
super(filename);
}
public void close() {
super.close();
}
}
static class InputReader extends BufferedReader {
StringTokenizer tokenizer;
public InputReader(InputStream inputStream) {
super(new InputStreamReader(inputStream), 32768);
}
public InputReader(String filename) {
super(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(filename)));
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(readLine());
} catch (IOException e) {
throw new RuntimeException();
}
}
return tokenizer.nextToken();
}
public Integer nextInt() {
return Integer.valueOf(next());
}
public int[] readIntArray(int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++)
array[i] = nextInt();
return array;
}
}
}
| linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.util.*;
public class A
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int odd = -1;
int even = -1;
int oc = 0;
int ec = 0;
for(int i=0;i < n;i++)
{
if(scan.nextInt() % 2 == 0)
{
ec++;
even = i+1;
}
else
{
oc++;
odd = i+1;
}
}
if(ec == 1)
System.out.println(even);
else
System.out.println(odd);
}
}
| linear | 25_A. IQ test | CODEFORCES |
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class A {
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
List<Integer> list = new ArrayList<Integer>(), list2;
for (; n-- > 0;) {
list.add(scan.nextInt());
}
list2 = new ArrayList<Integer>(list);
Collections.sort(list2, new Comparator<Integer>() {
public int compare(Integer o1, Integer o2) {
return o1 % 2 - o2 % 2;
}
});
System.out.println(list.indexOf(list2.get(list2.get(1) % 2 > 0 ? 0
: list2.size() - 1)) + 1);
}
}
| linear | 25_A. IQ test | CODEFORCES |
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class A25 {
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(in.readLine().trim());
String[] toks = in.readLine().trim().split("[ ]+");
int counter = 0;
boolean even = true;
int e = -1, o = -1;
int ec = 0, oc = 0;
for (int i = 0; i < toks.length; i++) {
int x = Integer.parseInt(toks[i]);
if (x % 2 == 0) {
ec ++;
if (e == -1) {
e = i+1;
}
}
else {
oc ++;
if (o == -1) {
o = i+1;
}
}
}
if (ec == 1) {
System.out.println(e);
}
else if (oc == 1) {
System.out.println(o);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new A25().run();
}
}
| linear | 25_A. IQ test | CODEFORCES |
import java.util.Scanner;
public class R025A {
int n;
int[] nums;
public R025A() {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
nums = new int[n];
for(int i=0; i<n; i++) {
nums[i] = scanner.nextInt();
}
}
private void process() {
int[] c = new int[2];
int[] r = new int[2];
for(int i=0; i<n; i++) {
c[nums[i] % 2]++;
if(r[nums[i] %2] == 0) {
r[nums[i] % 2] = i+1;
}
}
System.out.println(r[c[0]==1 ? 0 : 1]);
}
public static void main(String[] args) {
new R025A().process();
}
}
| linear | 25_A. IQ test | CODEFORCES |
//package;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;
import java.lang.*;
import java.io.*;
import java.awt.Point;
public class evenness {
public static void main(String[] args){
try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int i, n, temp=1;
String str = "";
int[] arr;
int r;
while (temp!= '\n'){
temp = System.in.read();
//if (temp=='\n')
//break;
str = str.concat(Character.toString((char)temp));
}
str = str.replaceAll("[^0-9]", "");
n = Integer.parseInt(str);
temp=1;
str="";
arr = new int[n];
for (i=0;i<n;i++){
while (temp!=' ' && temp!=-1){
temp = System.in.read();
//if (temp==' ' || temp==-1)
//break;
str = str.concat(Character.toString((char)temp));
}
str = str.replaceAll("[^0-9]", "");
arr[i] = Integer.parseInt(str);
str="";
temp=1;
}
r=(arr[2]%2);
if ((arr[0]%2)==(arr[1]%2)){
r=(arr[0]%2);
}
for (i=0;i<n;i++){
if ((arr[i]%2)!=r){
System.out.println(i+1);
break;
}
}
}catch (Exception e){
System.out.println("OH NOES " + e);
}
}
} | linear | 25_A. IQ test | CODEFORCES |
import java.io.*;
import java.util.*;
public class a implements Runnable {
private void solve() throws IOException {
int n = nextInt();
int oddcnt = 0, evencnt = 0;
int odd = 0, even = 0;
for (int i = 0; i < n; i++) {
int a = nextInt();
if (a % 2 == 0) {
even = i + 1;
evencnt++;
} else {
odd = i + 1;
oddcnt++;
}
}
if (oddcnt == 1) {
System.out.println(odd);
} else {
System.out.println(even);
}
}
public static void main(String[] args) {
new Thread(new a()).start();
}
BufferedReader br;
StringTokenizer st;
PrintWriter out;
boolean eof = false;
public void run() {
Locale.setDefault(Locale.US);
try {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
} catch (Throwable e) {
e.printStackTrace();
System.exit(239);
}
}
String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
eof = true;
return "0";
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
} | linear | 25_A. IQ test | CODEFORCES |
import java.io.*;
public class IQTest
{
public static void main(String[] args)
{
try
{
BufferedReader in = new BufferedReader(
new InputStreamReader(System.in));
String str = in.readLine();
int n = Integer.parseInt(str);
int odd = -1, even = -1, odds = 0, evens = 0;
//while (n-- > 0)
//{
str = in.readLine();
String[] numbers = str.split(" ");
int index = 1;
for (String number: numbers)
{
int i = Integer.parseInt(number);
if (i % 2 == 0)
{
++evens;
if (even == -1)
even = index;
}
else
{
++odds;
if (odd == -1)
odd = index;
}
++index;
}
//}
System.out.println((evens > odds ? odd : even));
}
catch (Exception e)
{
e.printStackTrace();
}
}
} | linear | 25_A. IQ test | CODEFORCES |
//package round25;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;
public class A {
private Scanner in;
private PrintWriter out;
private String INPUT = "";
public void solve()
{
int n = ni();
int[] u = new int[n];
int fe = -1, fo = -1;
int ne = -1, no = -1;
for(int i = 0;i < n;i++){
u[i] = ni();
if(u[i] % 2 == 0){
if(fe == -1){
fe = i + 1;
}else{
ne = i + 1;
}
}else{
if(fo == -1){
fo = i + 1;
}else{
no = i + 1;
}
}
}
if(ne > 0){
out.println(fo);
}else{
out.println(fe);
}
}
public void run() throws Exception
{
in = INPUT.isEmpty() ? new Scanner(System.in) : new Scanner(INPUT);
out = new PrintWriter(System.out);
solve();
out.flush();
}
public static void main(String[] args) throws Exception
{
new A().run();
}
private int ni() { return Integer.parseInt(in.next()); }
private static void tr(Object... o) { System.out.println(o.length == 1 ? o[0] : Arrays.toString(o)); }
}
| linear | 25_A. IQ test | CODEFORCES |
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class A {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
br.readLine();
String[] ss = br.readLine().split(" ");
int n = ss.length;
int[] a = new int[n];
for (int i = 0; i < n; ++i)
a[i] = Integer.parseInt(ss[i]);
for (int i = 0; i < n; ++i) {
boolean ok = true;
for (int j = 0; j < n; ++j)
if (j != i && a[j] % 2 == a[i] % 2)
ok = false;
if (ok)
System.out.println(i + 1);
}
}
}
| linear | 25_A. IQ test | CODEFORCES |
import java.util.Scanner;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] nums = new int[n];
int kisu = 0;
int gusu = 0;
for(int i = 0 ; i < n ; i++){
nums[i] = sc.nextInt();
if(nums[i] % 2 == 0)gusu++;
if(nums[i] % 2 == 1)kisu++;
}
int ans = -1;
if(gusu == 1){
for(int i = 0 ; i < n ; i++){
if(nums[i]%2 == 0){
ans = i+1;
break;
}
}
}
else{
for(int i = 0 ; i < n ; i++){
if(nums[i]%2 == 1){
ans = i+1;
break;
}
}
}
System.out.println(ans);
}
}
| linear | 25_A. IQ test | CODEFORCES |
import java.util.*;
public class A {
int n;
int[] arr;
void run(){
Scanner s = new Scanner(System.in);
n = s.nextInt();
arr = new int[n];
int even, odd;
even = 0;
odd = 0;
for (int i = 0; i < n; i++) {
arr[i] = s.nextInt();
if(arr[i]%2==0)even++;
else odd++;
}
if(even>odd){
for (int i = 0; i < n; i++) {
if(arr[i]%2==1){
System.out.println(i+1);
System.exit(0);
}
}
}
else{
for (int i = 0; i < n; i++) {
if(arr[i]%2==0){
System.out.println(i+1);
System.exit(0);
}
}
}
}
public static void main(String[] args) {
new A().run();
}
}
| linear | 25_A. IQ test | CODEFORCES |
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class ProblemA {
public static void main(String[] args) throws Exception{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(System.out);
int n = Integer.parseInt(reader.readLine());
String [] split = reader.readLine().split("\\s+");
int value;
int [] count = new int[2];
int [] pos = new int[2];
for(int i = 0; i < split.length; i++){
value = Integer.parseInt(split[i]);
count[value % 2] ++;
pos[value % 2] = i + 1;
}
writer.println((count[0] == 1) ? pos[0] : pos[1]);
writer.flush();
writer.close();
}
}
| linear | 25_A. IQ test | CODEFORCES |
import java.util.Scanner;
import static java.lang.Math.*;
import static java.lang.Integer.*;
import static java.lang.Long.*;
import static java.util.Arrays.*;
public class A {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while (s.hasNext()) {
int n = s.nextInt();
int[] a = new int[n];
int odd = 0;
int even = 0;
int po = -1;
int ev = -1;
for(int i=0;i<n;i++){
a[i] = s.nextInt();
if(a[i] % 2 == 0) {
even ++;
ev = i + 1;
} else {
odd++;
po = i + 1;
}
}
if(odd == 1) {
System.out.println(po);
}else{
System.out.println(ev);
}
}
}
} | linear | 25_A. IQ test | CODEFORCES |
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class Main {
private static StreamTokenizer in;
private static PrintWriter out;
private static int nextInt() throws Exception {
in.nextToken();
return (int)in.nval;
}
private static String nextString() throws Exception {
in.nextToken();
return in.sval;
}
public static void main(String[] args) throws Exception {
in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
out = new PrintWriter(System.out);
int n = nextInt();
byte f = (byte)nextInt();
byte s = (byte)nextInt();
byte t = (byte)nextInt();
boolean bf = false;
boolean bs = false;
boolean bt = false;
if((f&1) == 0){bf = true;}
if((s&1) == 0){bs = true;}
if((t&1) == 0){bt = true;}
//System.out.println(bf+""+bs+""+bt);
if((!bf)&&bs&&bt){System.out.println(1);return;}
if(bf&&(!bs)&&bt){System.out.println(2);return;}
if(bf&&bs&&(!bt)){System.out.println(3);return;}
if(bf&&!bs&&!bt){System.out.println(1);return;}
if(!bf&&bs&&!bt){System.out.println(2);return;}
if(!bf&&!bs&&bt){System.out.println(3);return;}
for(int i = 4; i<=n; i++){
byte g = (byte) nextInt();
if(((g+f)&1) == 1){System.out.println(i); return;}
}
out.flush();
}
} | linear | 25_A. IQ test | CODEFORCES |
import java.io.*;
import java.math.*;
import java.util.*;
import java.util.List;
import java.util.Queue;
import java.awt.*;
import static java.util.Arrays.fill;
public class Solution implements Runnable {
private BufferedReader br = null;
private PrintWriter pw = null;
private StringTokenizer stk = new StringTokenizer("");
public static void main(String[] args) {
new Thread(new Solution()).run();
}
public void run() {
/*
* try { // br = new BufferedReader(new FileReader("input.txt")); pw =
* new PrintWriter("output.txt"); } catch (FileNotFoundException e) {
* e.printStackTrace(); }
*/
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new OutputStreamWriter(System.out));
solver();
pw.close();
}
private void nline() {
try {
if (!stk.hasMoreTokens())
stk = new StringTokenizer(br.readLine());
} catch (IOException e) {
throw new RuntimeException("KaVaBUnGO!!!", e);
}
}
private String nstr() {
while (!stk.hasMoreTokens())
nline();
return stk.nextToken();
}
private int ni() {
return Integer.valueOf(nstr());
}
private double nd() {
return Double.valueOf(nstr());
}
String nextLine() {
try {
return br.readLine();
} catch (IOException e) {
}
return null;
}
private void solver() {
int n = ni();
ArrayList<Integer> ar = new ArrayList<Integer>();
int sum = 0;
for (int i = 0; i < n; i++) {
ar.add(ni() % 2);
sum += ar.get(i);
}
int flag = 0;
if(sum==1)flag = 1;
for(int i =0;i<n;i++)if(ar.get(i)==flag)System.out.println(i+1);
}
void exit() {
System.exit(0);
}
}
| linear | 25_A. IQ test | CODEFORCES |
import java.io.*;
public class Main {
public static void main (String [] args) throws IOException {
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
do {
int n = Integer.parseInt (br.readLine ());
//args;
int [] ns = new int [(args = br.readLine ().split (" ")).length];
int evenCount = 0, oddCount = 0, evI = 1, oddI = 1;
for (int i = 0; i < ns.length; i++) {
if ((ns [i] = Integer.parseInt (args [i])) % 2 == 0) {
evenCount ++;
evI = i;
} else {
oddCount ++;
oddI = i;
}
}
if (evenCount == 1) System.out.println (evI + 1);
else System.out.println (oddI + 1);
} while (br.ready ());
}
} | linear | 25_A. IQ test | CODEFORCES |
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Saransh
*/
import java.util.*;
import java.io.*;
import java.math.*;
import java.lang.*;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
try
{
Parserdoubt pd=new Parserdoubt(System.in);
int t=pd.nextInt();
int inde=0,indo=0,o=0,e=0;
for(int i=0;i<t;i++)
{
if(pd.nextInt()%2==0)
{
inde=i;
e++;
}
else
{
o++;
indo=i;
}
}
if(o==1)
{
System.out.println(indo+1);
}
else
{
System.out.println(inde+1);
}
}
catch(Exception e){}
}
}
class Parserdoubt
{
final private int BUFFER_SIZE = 1 << 17;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Parserdoubt(InputStream in)
{
din = new DataInputStream(in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String nextString() throws Exception
{
StringBuffer sb=new StringBuffer("");
byte c = read();
while (c <= ' ') c = read();
do
{
sb.append((char)c);
c=read();
}while(c>' ');
return sb.toString();
}
public char nextChar() throws Exception
{
byte c=read();
while(c<=' ') c= read();
return (char)c;
}
public int nextInt() throws Exception
{
int ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = c == '-';
if (neg) c = read();
do
{
ret = ret * 10 + c - '0';
c = read();
} while (c > ' ');
if (neg) return -ret;
return ret;
}
public long nextLong() throws Exception
{
long ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = c == '-';
if (neg) c = read();
do
{
ret = ret * 10 + c - '0';
c = read();
} while (c > ' ');
if (neg) return -ret;
return ret;
}
private void fillBuffer() throws Exception
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1) buffer[0] = -1;
}
private byte read() throws Exception
{
if (bufferPointer == bytesRead) fillBuffer();
return buffer[bufferPointer++];
}
} | linear | 25_A. IQ test | CODEFORCES |
import java.util.Scanner;
public class A {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int a[] = new int[100];
for (int i = 0;i<n;i++) a[i] = in.nextInt()%2;
if (a[0]==a[1] || a[0]==a[2]){
for (int i = 1;i<n;i++)
if (a[i] != a[0]) {
System.out.println(i+1);
break;
}
} else{
System.out.println(1);
}
}
}
| linear | 25_A. IQ test | CODEFORCES |
import java.io.*;
import java.util.*;
public class Main implements Runnable {
public void _main() throws IOException {
int n = nextInt();
int even = 0, odd = 0, atEven = -1, atOdd = -1;
for (int i = 0; i < n; i++) {
if (nextInt() % 2 == 0) {
atEven = i;
++even;
}
else {
atOdd = i;
++odd;
}
}
if (odd == 1)
out.print(atOdd + 1);
else
out.print(atEven + 1);
}
private BufferedReader in;
private PrintWriter out;
private StringTokenizer st;
private String next() throws IOException {
while (st == null || !st.hasMoreTokens()) {
String rl = in.readLine();
if (rl == null)
return null;
st = new StringTokenizer(rl);
}
return st.nextToken();
}
private int nextInt() throws IOException {
return Integer.parseInt(next());
}
private long nextLong() throws IOException {
return Long.parseLong(next());
}
private double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public static void main(String[] args) {
new Thread(new Main()).start();
}
public void run() {
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
_main();
out.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(202);
}
}
}
| linear | 25_A. IQ test | CODEFORCES |
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class IQTest {
public static void main(String args[]) throws Exception {
BufferedReader stdin =
new BufferedReader(new InputStreamReader(System.in));
String line;
line = stdin.readLine();
int n = Integer.parseInt(line);
line = stdin.readLine();
List even = new ArrayList();
List odd = new ArrayList();
String[] kk = line.split(" ");
for(int i=0;i<n;i++) {
if(Integer.parseInt(kk[i])%2==0)
even.add(i);
else
odd.add(i);
}
if(even.size()==1)
System.out.println((Integer)even.get(0)+1);
else
System.out.println((Integer)odd.get(0)+1);
}
}
| linear | 25_A. IQ test | CODEFORCES |
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.ArrayList;
public class A {
private static StreamTokenizer in;
private static PrintWriter out;
private static int nextInt() throws Exception {
in.nextToken();
return (int) in.nval;
}
private static String nextString() throws Exception {
in.nextToken();
return in.sval;
}
public static void main(String[] args) throws Exception {
in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
out = new PrintWriter(System.out);
ArrayList<Integer> p = new ArrayList<Integer>();
ArrayList<Integer> o = new ArrayList<Integer>();
int n = nextInt();
for (int i=0; i<n; i++) {
int a = nextInt();
if (a % 2 == 0) p.add(i+1);
else o.add(i+1);
}
if (p.size() < o.size()) out.println(p.get(0));
else out.println(o.get(0));
out.flush();
}
} | linear | 25_A. IQ test | CODEFORCES |
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class A25 {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(System.out);
static int nextInt() throws Exception {
in.nextToken();
return (int)in.nval;
}
static String nextString() throws Exception {
in.nextToken();
return in.sval;
}
public static void main(String[] args) throws Exception {
int n = nextInt();
int[] c = new int[2];
int[] f = new int[2];
for (int i = 0; i < n; i++) {
int x = nextInt(), p = x%2;
if (c[p]++ == 0) f[p] = i+1;
}
out.println(c[0] == 1 ? f[0] : f[1]);
out.flush();
}
}
| linear | 25_A. IQ test | CODEFORCES |
import java.util.*;
public class Task25a {
/**
* @param args
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a1 = 0, a2 = 0;
int n1 = 0, n2 = 0;
for (int i = 1; i <= n; i++) {
int c = sc.nextInt();
if (c % 2 == 1) {
a1 = i;
n1++;
} else {
a2 = i;
n2++;
}
}
if (n1 == 1) {
System.out.println(a1);
} else {
System.out.println(a2);
}
}
}
| linear | 25_A. IQ test | CODEFORCES |
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 IQTest implements Runnable
{
public static void main(String[] args) throws Exception
{
new IQTest().run();
}
private void solve() throws Exception
{
int n = nextInt();
int a[] = new int[n];
for (int i = 0; i < a.length; i++)
{
a[i] = nextInt();
}
int c0 = 0, c1 = 0;
for (int i = 0; i < a.length; i++)
{
a[i] = a[i] % 2;
if (a[i] == 0)
c0++;
else
c1++;
}
int f = 0;
if (c0 > c1)
f = 1;
else
f = 0;
int r = 0;
for (int i = 0; i < a.length; i++)
{
if (a[i] == f)
{
r = i + 1;
break;
}
}
out.println(r);
}
// -------------- Input/Output routines below ---------------//
private BufferedReader in;
PrintWriter out;
StringTokenizer tokenizer;
public void run()
{
// String problem = this.getClass().getName();
try
{
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(new BufferedOutputStream(System.out));
solve();
out.flush();
in.close();
out.close();
}
catch (Exception e)
{
e.printStackTrace();
// System.exit(1);
}
}
String nextToken() throws IOException
{
while (tokenizer == null || !tokenizer.hasMoreTokens())
{
tokenizer = new StringTokenizer(in.readLine());
}
return tokenizer.nextToken();
}
int nextInt() throws IOException
{
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException
{
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException
{
return Double.parseDouble(nextToken());
}
} | linear | 25_A. IQ test | CODEFORCES |
import java.util.Scanner;
public class R025A {
int n;
int[] nums;
public R025A() {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
nums = new int[n];
for(int i=0; i<n; i++) {
nums[i] = scanner.nextInt();
}
}
private void process() {
int odd = 0;
int even = 0;
int lastOdd = 0;
int lastEven = 0;
for(int i=0; i<n; i++) {
if(nums[i] % 2 == 0) {
even++;
lastEven = i+1;
} else {
odd++;
lastOdd = i+ 1;
}
}
if(odd == 1) System.out.println(lastOdd);
else System.out.println(lastEven);
}
public static void main(String[] args) {
new R025A().process();
}
}
| linear | 25_A. IQ test | CODEFORCES |
import java.util.Locale;
import java.util.Scanner;
public class A {
public static void main(String[] args) {
Locale.setDefault(Locale.US);
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String[] number = new String[n];
sc.nextLine();
String l = sc.nextLine();
number = l.split(" ");
int oe = 1;
if((Integer.valueOf(number[0])%2 +
Integer.valueOf(number[1])%2 +
Integer.valueOf(number[2])%2) > 1) {
oe = 0;
}
for(int i=0;i<n;i++) {
if((Integer.valueOf(number[i])%2)==oe) {
System.out.println(i+1);
break;
}
}
}
} | linear | 25_A. IQ test | CODEFORCES |
//package codeforces.br25;
import java.io.*;
import java.math.*;
import java.util.*;
public class ProblemA {
public void solve() {
boolean oj = true;
try {
Reader reader = oj ? new InputStreamReader(System.in) : new FileReader("A.in");
Writer writer = oj ? new OutputStreamWriter(System.out) : new FileWriter("A.out");
BufferedReader br = new BufferedReader(reader);
StreamTokenizer st = new StreamTokenizer(reader);
PrintWriter out = new PrintWriter(writer);
int n = Integer.valueOf(br.readLine());
String s = br.readLine();
MyTokenizer tok = new MyTokenizer(s);
int[] a = new int[2];
int[] ind = new int[2];
int[] c = new int[2];
for(int i=0;i<n;i++) {
int p = (int)tok.getNum();
c[p%2]++;
a[p%2] = p;
ind[p%2] = i;
}
int b = ind[0];
if (c[0] > c[1])
b = ind[1];
out.printf("%d", b + 1);
br.close();
out.close();
reader.close();
writer.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
}
}
public static void main(String[] args) {
ProblemA f = new ProblemA();
f.solve();
}
private class MyTokenizer {
private String s;
private int cur;
public MyTokenizer(String s) {
this.s = s;
cur = 0;
}
public void skip() {
while (cur < s.length() && (s.charAt(cur) == ' ' || s.charAt(cur) == '\n')) {
cur++;
}
}
public double getNum() {
skip();
String snum = "";
while (cur < s.length() && (s.charAt(cur) >= '0' && s.charAt(cur) <= '9' || s.charAt(cur) == '.' || s.charAt(cur) == '-')) {
snum += s.charAt(cur);
cur++;
}
return Double.valueOf(snum);
}
public String getString() {
skip();
String s2 = "";
while (cur < s.length() && (((s.charAt(cur) >= 'a' && s.charAt(cur) <= 'z')) || ((s.charAt(cur) >= 'A' && s.charAt(cur) <= 'Z')))) {
s2 += s.charAt(cur);
cur++;
}
return s2;
}
public char getCurrentChar() throws Exception {
if (cur < s.length())
return s.charAt(cur);
else
throw new Exception("Current character out of string length");
}
public void moveNextChar() {
if (cur < s.length())
cur++;
}
public boolean isFinished() {
return cur >= s.length();
}
}
}
| linear | 25_A. IQ test | CODEFORCES |
import java.io.*;
import java.util.*;
public class A {
int n;
void run()throws IOException{
Scanner sc = new Scanner(new InputStreamReader(System.in));
n = sc.nextInt();
int i,tmp,even,odd,e,o;
even=odd=e=o=0;
for(i=1;i<=n;i++){
tmp = sc.nextInt();
if(tmp%2==0){
e++;
if(even==0) even=i;
} else{
o++;
if(odd==0) odd=i;
}
}
if(e>1) System.out.println(odd);
else System.out.println(even);
}
public static void main(String[] args)throws IOException {
new A().run();
}
}
| linear | 25_A. IQ test | CODEFORCES |
import static java.util.Arrays.*;
import static java.lang.Math.*;
import static java.math.BigInteger.*;
import java.util.*;
import java.math.*;
import java.io.*;
public class A implements Runnable
{
String file = "input";
void init() throws IOException
{
//input = new BufferedReader(new FileReader(file + ".in"));
input = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(new BufferedOutputStream(System.out));
}
void solve() throws IOException
{
int odd = 0, even = 0;
int n = nextInt();
int[] a = new int[n];
for(int i = 0; i < n; i++)
{
a[i] = nextInt();
if(a[i] % 2 == 0) even++;
else odd++;
}
if(even >= 2)
{
for(int i = 0; i < n; i++) if(a[i] % 2 == 1)
{
System.out.println(i + 1);
return;
}
}
else
{
for(int i = 0; i < n; i++) if(a[i] % 2 == 0)
{
System.out.println(i + 1);
return;
}
}
}
String next() throws IOException
{
while(st == null || !st.hasMoreTokens()) st = new StringTokenizer(input.readLine());
return st.nextToken();
}
int nextInt() throws IOException
{
return Integer.parseInt(next());
}
long nextLong() throws IOException
{
return Long.parseLong(next());
}
double nextDouble() throws IOException
{
return Double.parseDouble(next());
}
void print(Object... o)
{
System.out.println(deepToString(o));
}
void gcj(Object o)
{
String s = String.valueOf(o);
out.println("Case #" + test + ": " + s);
System.out.println("Case #" + test + ": " + s);
}
BufferedReader input;
PrintWriter out;
StringTokenizer st;
int test;
public static void main(String[] args) throws IOException
{
new Thread(null, new A(), "", 1 << 20).start();
}
public void run()
{
try
{
init();
solve();
out.close();
}
catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
} | linear | 25_A. IQ test | CODEFORCES |
import java.io.*;
public class Main
{
public static void main(String []args)throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=0;
n=Integer.parseInt(br.readLine());
String inp="";
inp=br.readLine();
int no[]=new int[n];
String tinp[]=inp.split(" ");
for(int i=0;i<n;i++)
{
no[i]=Integer.parseInt(tinp[i]);
}
int eve=0,odd=0;
for(int i=0;i<3;i++)
{
int rem=no[i]%2;
if(rem==0)
eve++;
else
odd++;
}
if(eve>1)
{
for(int i=0;i<n;i++)
{
if(no[i]%2==1)
{
System.out.println(i+1);
break;
}
}
}
else
{
for(int i=0;i<n;i++)
{
if(no[i]%2==0)
{
System.out.println(i+1);
break;
}
}
}
}
}
| linear | 25_A. IQ test | CODEFORCES |
import java.util.Scanner;
public class Solution {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; ++i) {
arr[i] = scanner.nextInt();
}
boolean isOdd = false;
if ((arr[0] % 2 == 0 && arr[1] % 2 == 0) || (arr[0] % 2 == 0 && arr[2] % 2 == 0)
|| (arr[1] % 2 == 0 && arr[2] % 2 == 0)) {
isOdd = true;
}
if (isOdd) {
for (int i = 0; i < n; ++i) {
if (arr[i] % 2 == 1) {
System.out.println(i + 1);
break;
}
}
} else {
for (int i = 0; i < n; ++i) {
if (arr[i] % 2 == 0) {
System.out.println(i + 1);
break;
}
}
}
}
}
| linear | 25_A. IQ test | CODEFORCES |
import java.util.Scanner;
public class Contest25_A {
static int[] parity;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int numberEntries = scan.nextInt();
scan.nextLine();
String[] numbers = scan.nextLine().split(" ");
numbers = parity(numbers);
int evenOdd = evenOdd(parity);
for (int i = 0; i < parity.length; i++) {
if (parity[i] == evenOdd) {
System.out.println(i + 1);
System.exit(0);
}
}
}
public static int evenOdd(int[] parity) {
/*
* Determines what I should be looking for
* Return 0 if even
* Return 1 if odd
*/
int numberOnes = 0;
int numberZeroes = 0;
int one = parity[0];
int two = parity[1];
int three = parity[2];
if (one == 1)
numberOnes++;
else
numberZeroes++;
if (two == 1)
numberOnes++;
else
numberZeroes++;
if (three == 1)
numberOnes++;
else
numberZeroes++;
if (numberOnes >= 2)
return 0;
else
return 1;
}
public static String[] parity(String[] numbers) {
parity = new int[numbers.length];
for (int i = 0; i < numbers.length; i++) {
parity[i] = Integer.parseInt(numbers[i]) % 2;
numbers[i] = Integer.toString(parity[i]);
}
return numbers;
}
}
| linear | 25_A. IQ test | CODEFORCES |
import java.util.Scanner;
/**
*
* @author Ronak
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner in=new Scanner(System.in);
int n=in.nextInt();
if(n>=3&&n<=100)
{
int num[]=new int[n];
for(int i=0;i<n;i++)
{
num[i]=in.nextInt();
}
int even=0,odd=0,ceven=0,codd=0;
for(int i=0;i<n;i++)
{
if(num[i]%2==0)
{
even++;
ceven=i+1;
}
else
{
odd++;
codd=i+1;
}
}
if(odd==1)
{
System.out.println(""+codd);
}
else
{
System.out.println(""+ceven);
}
}
}
}
| linear | 25_A. IQ test | CODEFORCES |
import java.util.Scanner;
public class A {
public static void main(String[] args) {
new A().run();
}
private void run() {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int ce = 0;
int co = 0;
int le = 0;
int lo = 0;
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
if (x % 2 == 0) {
ce++;
le = i + 1;
} else {
co++;
lo = i + 1;
}
}
System.out.println(ce == 1 ? le : lo);
}
}
| linear | 25_A. IQ test | CODEFORCES |
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws Exception {
//System.setIn(new FileInputStream("1"));
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
in.close();
out.close();
}
private static void solve() throws Exception {
int n = nextInt();
int co = 0, ce = 0;
int[] v = new int[n];
for (int i = 0; i < n; i++) {
v[i] = nextInt();
}
for (int i = 0; i < n; i++) {
if (v[i] % 2 == 0) {
co++;
} else {
ce++;
}
}
if (co == 1) {
for (int i = 0; i < n; i++) {
if (v[i] % 2 == 0) {
out.println(i + 1);
return;
}
}
} else {
for (int i = 0; i < n; i++) {
if (v[i] % 2 != 0) {
out.println(i + 1);
return;
}
}
}
}
private static BufferedReader in;
private static PrintWriter out;
private static StringTokenizer st;
static String nextString() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(nextString());
}
static double nextDouble() throws IOException {
return Double.parseDouble(nextString());
}
}
| linear | 25_A. IQ test | CODEFORCES |
import java.util.Scanner;
public class A_IQTest {
static int n;
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
n = s.nextInt();
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = s.nextInt();
}
int ei = -1;
int oi = -1;
int ecnt = 0;
int ocnt = 0;
for (int i = 0; i < n; i++) {
if(nums[i] % 2 == 0){
ei = i;
ecnt++;
}else{
oi = i;
ocnt++;
}
}
if(ecnt == 1){
System.out.println(ei+1);
}else{
System.out.println(oi+1);
}
}
}
| linear | 25_A. IQ test | CODEFORCES |
import java.util.*;
public class Main {
public static void main(String[] args) {
Main iq = new Main();
Scanner sc = new Scanner(System.in);
int n;
n = sc.nextInt();
int[] naturalNumbers = new int[n];
for (int i = 0; i < naturalNumbers.length; i++) {
naturalNumbers[i] = sc.nextInt();
}
System.out.println(iq.diffInEvenness(n, naturalNumbers));
}
public int diffInEvenness(int n, int[] naturalNumbers) {
int even, odd, lastEvenIndex, lastOddIndex;
even = odd = lastEvenIndex = lastOddIndex = 0;
for (int i = 0; i < naturalNumbers.length; i++) {
if((naturalNumbers[i] % 2) == 0) {
even++;
lastEvenIndex = i + 1;
}
else {
odd++;
lastOddIndex = i + 1;
}
}
return (even > odd ? lastOddIndex : lastEvenIndex);
}
} | linear | 25_A. IQ test | CODEFORCES |
import java.util.Scanner;
public class Iq {
static void metod() throws Exception {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] m = new int[n];
for (int i = 0; i < n; i++)
m[i] = in.nextInt();
byte k = 0;
if (m[0] % 2 == 0) {
if (m[1] % 2 == 0) {
k = 0;
} else {
if (m[2] % 2 == 0) {
System.out.println(2);
return;
} else {
System.out.println(1);
return;
}
}
} else {
if (m[1] % 2 == 1) {
k = 1;
} else {
if (m[2] % 2 == 0) {
System.out.println(1);
return;
} else {
System.out.println(2);
return;
}
}
}
if (k == 0) {
for (int i = 0; i < m.length; i++) {
if (m[i] % 2 == 1) {
System.out.println(i + 1);
break;
}
}
} else {
for (int i = 0; i < m.length; i++) {
if (m[i] % 2 == 0) {
System.out.println(i + 1);
break;
}
}
}
}
public static void main(String args[]) throws Exception {
Iq.metod();
}
} | linear | 25_A. IQ test | CODEFORCES |
import java.util.*;
public class IQ {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
ArrayList<Integer> e=new ArrayList<Integer>();
ArrayList<Integer> o=new ArrayList<Integer>();
int size=sc.nextInt();
for(int w=0;w<size;w++)
{
int x=sc.nextInt();
if(x%2==0)e.add(w+1);
else o.add(w+1);
}
if(e.size()==1)System.out.println(e.get(0));
else System.out.println(o.get(0));
}
}
| linear | 25_A. IQ test | CODEFORCES |
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.InputMismatchException;
/**
* @author Egor Kulikov ([email protected])
* Created on 14.03.2010
*/
public class TaskA implements Runnable {
private InputReader in;
private PrintWriter out;
public static void main(String[] args) {
new Thread(new TaskA()).start();
// new Template().run();
}
public TaskA() {
// String id = getClass().getName().toLowerCase();
// try {
// System.setIn(new FileInputStream(id + ".in"));
// System.setOut(new PrintStream(new FileOutputStream(id + ".out")));
// } catch (FileNotFoundException e) {
// throw new RuntimeException();
// }
in = new InputReader(System.in);
out = new PrintWriter(System.out);
}
public void run() {
// int numTests = in.readInt();
// for (int testNumber = 0; testNumber < numTests; testNumber++) {
// }
int n = in.readInt();
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = in.readInt();
boolean odd = a[0] % 2 + a[1] % 2 + a[2] % 2 >= 2;
for (int i = 0; i < n; i++) {
if (a[i] % 2 == 1 && !odd)
out.println(i + 1);
if (a[i] % 2 == 0 && odd)
out.println(i + 1);
}
out.close();
}
private static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1000];
private int curChar, numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
private 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 long readLong() {
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 String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuffer res = new StringBuffer();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private String readLine0() {
StringBuffer buf = new StringBuffer();
int c = read();
while (c != '\n' && c != -1) {
buf.appendCodePoint(c);
c = read();
}
return buf.toString();
}
public String readLine() {
String s = readLine0();
while (s.trim().length() == 0)
s = readLine0();
return s;
}
public String readLine(boolean ignoreEmptyLines) {
if (ignoreEmptyLines)
return readLine();
else
return readLine0();
}
public BigInteger readBigInteger() {
try {
return new BigInteger(readString());
} catch (NumberFormatException e) {
throw new InputMismatchException();
}
}
public char readCharacter() {
int c = read();
while (isSpaceChar(c))
c = read();
return (char) c;
}
public double readDouble() {
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 < '0' || c > '9') {
if (c == 'e' || c == 'E') {
int e = readInt();
return res * Math.pow(10, e);
}
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') {
int e = readInt();
return res * Math.pow(10, e);
}
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
}
} | linear | 25_A. IQ test | CODEFORCES |
import java.io.*;
import java.util.*;
public class Main implements Runnable {
private BufferedReader in;
private PrintWriter out;
private StringTokenizer st;
private void eat(String line)
{
st = new StringTokenizer(line);
}
private String next() throws IOException
{
while(!st.hasMoreTokens()) {
String line = in.readLine();
if(line == null)
return null;
eat(line);
}
return st.nextToken();
}
private int nextInt() throws IOException
{
return Integer.parseInt(next());
}
public void run()
{
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(new OutputStreamWriter(System.out));
eat("");
go();
out.close();
} catch(Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
public static void main(String[] args)
{
new Thread(new Main()).start();
}
public void go() throws IOException
{
int n = nextInt();
int[] v = new int[n], count = new int[2];
for(int i = 0; i < n; ++i) {
v[i] = nextInt();
++count[v[i] % 2];
}
int residue = count[0] == 1 ? 0 : 1;
for(int i = 0; i < n; ++i)
if(v[i] % 2 == residue)
out.println(i + 1);
}
}
| linear | 25_A. IQ test | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s = in.readLine();
String[] p = in.readLine().split(" ");
List<Integer> a = new ArrayList<Integer>();
for (String k : p) {
a.add(Integer.parseInt(k));
}
int n = a.size();
int c1 = 0;
int c2 = 0;
int c1p = 0;
int c2p = 0;
for (int i = 0; i < n; i++) {
if (a.get(i) % 2 == 0) {
c1++;
c1p = i;
} else {
c2++;
c2p = i;
}
}
if (c1 < c2) {
System.out.println(c1p + 1);
} else {
System.out.println(c2p + 1);
}
}
}
| linear | 25_A. IQ test | CODEFORCES |
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws Exception {
int i,j,k;
int counter[] = new int[2];
int a[] = new int[200];
int needed;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
for (i=1;i<=N;i++) {
a[i] = Integer.parseInt(st.nextToken());
counter[a[i]%2]++;
}
if (counter[0] == 1) {
needed = 0;
} else {
needed = 1;
}
for (i=1;i<=N;i++) {
if (a[i]%2 == needed) {
System.out.println(i);
return;
}
}
}
}
| linear | 25_A. IQ test | CODEFORCES |
import java.util.*;
public class a {
/**
* @param args
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++) a[i] = in.nextInt()%2;
int z = 0;
for(int i=0;i<n;i++) z+=(a[i] == 0)?1:0;
if (z == 1) z = 0;
else z = 1;
for(int i=0;i<n;i++)
if (a[i] == z){
System.out.println(i+1);
break;
}
}
}
| linear | 25_A. IQ test | CODEFORCES |
import java.util.Scanner;
public class IQ {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] a = new int[n];
for(int i = 0; i < n; i++)
a[i] = scan.nextInt();
for(int i = 0; i < n; i++) {
boolean x = a[i] % 2 == 0;
int c = 0;
for(int j = 0; j < n; j++) {
if(x != (a[j] % 2 == 0))
c++;
}
if(c == n-1) {
System.out.println(i+1);
break;
}
}
}
}
| linear | 25_A. IQ test | CODEFORCES |
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
Scanner sc = new Scanner(new InputStreamReader(System.in));
int n = sc.nextInt(),even = 0,odd = 0,evI = 0,OdI = 0;
for (int i = 0; i < n; i++) {
if(sc.nextInt()%2 == 1){
odd++;
OdI = i+1;
}else{
even++;
evI = i+1;
}
}
if(even < odd)
System.out.println(evI);
else
System.out.println(OdI);
}
}
| linear | 25_A. IQ test | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class A {
/**
* @param args
* @throws IOException
* @throws NumberFormatException
*/
public static void main(String[] args) throws NumberFormatException, IOException {
// TODO Auto-generated method stub
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int nums = Integer.parseInt(reader.readLine());
String line = reader.readLine();
int[] ar = new int[nums];
String[] par = line.split(" ");
for(int i=0; i<nums; i++){
ar[i] = Integer.parseInt(par[i]);
}
A a = new A();
System.out.print(a.getDif(ar));
}
private int getDif(int[] input){
int odd = 0, even = 0, d = 0;
int p = 0, q = 0;
for(int i=0; i<input.length; i++){
int t = input[i]%2;
if(t==0){
even++;
p = i+1;
}
else{
odd++;
q = i+1;
}
if(odd>0 && even>0 && (odd!=even)){
if(even>odd)
return q;
else
return p;
}
}
return d;
}
}
| linear | 25_A. IQ test | CODEFORCES |
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
public class A {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
int[] list = new int[n];
for(int i = 0; i < list.length; i++)
list[i] = Integer.parseInt(st.nextToken());
int odd = 0;
int even = 0;
for(int x: list)
if(x%2==1) {
odd++;
}
else {
even++;
}
for(int i = 1; i <= list.length; i++) {
if(list[i-1]%2==1 && odd == 1) {
System.out.println(i);
return;
}
else if(list[i-1]%2 == 0 && even == 1){
System.out.println(i);
return;
}
}
}
}
| linear | 25_A. IQ test | CODEFORCES |
/**
* Created by IntelliJ IDEA.
* User: dev_il
* Date: 03.08.2010
* Time: 0:59:04
* To change this template use File | Settings | File Templates.
*/
import java.io.*;
import static java.lang.Math.*;
import java.util.*;
public class IQTest implements Runnable {
BufferedReader in;
PrintWriter out;
StringTokenizer tok;
public static void main(String[] args) {
new Thread(new IQTest()).start();
}
void solve() throws IOException {
int n = nextInt();
List<Integer> l1 = new ArrayList<Integer>();
List<Integer> l2 = new ArrayList<Integer>();
for (int i = 0; i < n; ++i) {
int k = nextInt();
if (k % 2 == 0) l1.add(i + 1);
else l2.add(i + 1);
}
if (l1.size() == 1)
out.println(l1.get(0));
else out.println(l2.get(0));
}
public void run() {
try {
in = new BufferedReader(new InputStreamReader(System.in));
// in = new BufferedReader(new FileReader(new File("input.txt")));
out = new PrintWriter(System.out);
// out = new PrintWriter(new File("output.txt"));
solve();
out.flush();
out.close();
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
String nextLine() throws IOException {
tok = null;
return in.readLine();
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
String nextToken() throws IOException {
while (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
}
| linear | 25_A. IQ test | CODEFORCES |
import java.io.*;
import java.util.*;
import org.omg.PortableServer.ForwardRequestHelper;
public class A{
private BufferedReader in;
private StringTokenizer st;
void solve() throws IOException{
int n = nextInt();
int i = 0;
int[]nums = new int[n];
int numeven = 0;
int numodd = 0;
while(n-->0){
nums[i] = nextInt();
if(nums[i]%2==0) numeven++;
else numodd++;
i++;
}
for (int j = 0; j < nums.length; j++) {
if(numeven==1&&nums[j]%2==0){
System.out.println(j+1); break;
}
if(numodd==1&&nums[j]%2!=0){
System.out.println(j+1); break;
}
}
}
A() throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
eat("");
solve();
}
private void eat(String str) {
st = new StringTokenizer(str);
}
String next() throws IOException {
while (!st.hasMoreTokens()) {
String line = in.readLine();
if (line == null) {
return null;
}
eat(line);
}
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public static void main(String[] args) throws IOException {
new A();
}
int gcd(int a,int b){
if(b>a) return gcd(b,a);
if(b==0) return a;
return gcd(b,a%b);
}
}
| linear | 25_A. IQ test | CODEFORCES |
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.util.*;
public class Main
{
static Graph graph[];
public static void add_edge(int u,int v)
{
graph[u].adj.add(graph[v]);
graph[v].adj.add(graph[u]);
}
public static void dfs(int index)
{
Graph z=graph[index];
z.vis=1;Graph v;
for( int i=0;i<z.adj.size();i++)
{
v=z.adj.get(i);
if(v.vis==0)
{
v.dist=z.dist+1;
v.parent=z.val;
dfs(v.val);
}
}
}
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;
}
}
public static void main(String[] args)
{
FastReader sc=new FastReader();
int n=sc.nextInt();
Pair arr[]=new Pair[n];
Pair pref[]=new Pair[n];
Pair suff[]=new Pair[n];
for( int i=0;i<n;i++)
{
long u=sc.nextLong();
long v=sc.nextLong();
arr[i]=new Pair(u,v);
pref[i]=new Pair(0,0);
suff[i]=new Pair(0,0);
}
pref[0].x=arr[0].x;
pref[0].y=arr[0].y;
for( int i=1;i<n;i++)
{
pref[i].x=(long)Math.max(pref[i-1].x,arr[i].x);
pref[i].y=(long)Math.min(pref[i-1].y,arr[i].y);
}
suff[n-1].x=arr[n-1].x;
suff[n-1].y=arr[n-1].y;
for( int i=n-2;i>=0;i--)
{
suff[i].x=(long)Math.max(suff[i+1].x,arr[i].x);
suff[i].y=(long)Math.min(suff[i+1].y,arr[i].y);
}
long max=Long.MIN_VALUE;
long ans=0;
for( int i=0;i<n;i++)
{
long val=Long.MAX_VALUE;
long val1=Long.MAX_VALUE;
if(i!=0&&i!=n-1)
{
val=(long)Math.min(pref[i-1].y,suff[i+1].y)-(long)Math.max(pref[i-1].x,suff[i+1].x);
}
else if(i!=n-1)
{
val=suff[i+1].y-suff[i+1].x;
}
else
val=pref[i-1].y-pref[i-1].x;
ans=val;
if(ans<0)
ans=0;
max=(long)Math.max(ans,max);
}
System.out.println(max);
}
}
class mycomparator implements Comparator<Graph>
{
public int compare(Graph a, Graph b)
{
return b.dist-a.dist;
}
}
class Graph
{
int vis,col,val;int parent;int deg;int dist;
ArrayList<Graph> adj;
Graph(int val)
{
vis=0;
col=-1;
adj=new ArrayList<>();
parent=-1;
this.val=val;
deg=0;
dist=-1;
}
}
class Pair
{
long x,y;
Pair( long x, long y)
{
this.x=x;
this.y=y;
}
} | linear | 1029_C. Maximal Intersection | CODEFORCES |
import java.util.Scanner;
//import java.util.Scanner;
public class SingleWildcard {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input =new Scanner(System.in);
int a = input.nextInt();
int b = input.nextInt();
char[] s1 =new char[a];
s1 = input.next().toCharArray();
char[] s2 = new char[b];
s2 = input.next().toCharArray();
boolean condition = false;
for(int i=0; i<a;i++){
if(s1[i]=='*'){
condition= true;
break;
}
}
if(!condition){
if(match(s1,s2)){
System.out.println("YES");
}
else
System.out.println("NO");
return;
}
else{
int i=0;
if(s1.length-1>s2.length){
System.out.println("NO");
return;
}
while(i<s1.length && i<s2.length && s1[i]==s2[i]){
i++;
}
int j=s2.length-1;
int k = s1.length-1;
while(j>=0 && k>=0 && s1[k]==s2[j] && i<=j){
j--;
k--;
}
//System.out.println(i);
if(i==k && i>=0 && i<s1.length && s1[i]=='*' ){
System.out.println("YES");
return;
}
System.out.println("NO");
}
}
static boolean match(char[] s1,char[] s2){
if(s1.length!=s2.length)return false;
for(int i=0; i<s1.length;i++){
if(s1[i]!=s2[i])return false;
}
return true;
}
}
| linear | 1023_A. Single Wildcard Pattern Matching | CODEFORCES |
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
//package newpackage;
import java.util.*;
/**
*
* @author parpaorsa
*/
public class NewClass {
static Scanner in=new Scanner(System.in);
public static void main(String[] args) {
int n = in.nextInt(),ans=Integer.MAX_VALUE,t=0;
String x = in.next();
for (int i = 0; i < n; i++) {
if(x.charAt(i)=='-')t--;
else t++;
ans=Math.min(ans,t);
}
if(ans <= 0)
System.out.println(Math.abs(ans)+t);
else
System.out.println(t);
}
}
| linear | 1159_A. A pile of stones | CODEFORCES |
import jdk.nashorn.internal.objects.NativeArray;
import javax.swing.JOptionPane ;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane;
import java.sql.SQLSyntaxErrorException;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Vector;
import static jdk.nashorn.internal.objects.NativeArray.sort;
import static jdk.nashorn.internal.runtime.ScriptObject.toPropertyDescriptor;
public class Dialog1 {
private static int n ;
private static String s ;
private static char[] a;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
n = input.nextInt() ;
s = input.next() ;
a = s.toCharArray();
for(int i = 0 ; i < 200 ; ++i) {
int cur = i ;
boolean fl = true ;
for(int j = 0 ; j < n ; ++j) {
if(a[j] == '+')
++cur ;
else
--cur ;
if(cur < 0)
fl = false ;
}
if(fl) {
System.out.print(cur);
return ;
}
}
}
} | linear | 1159_A. A pile of stones | CODEFORCES |
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext()) {
int n=sc.nextInt();
String s=sc.next();
int sum=0;
for(int i=0;i<s.length();i++) {
if(s.charAt(i)=='+') {
sum++;
}
if(s.charAt(i)=='-'&&sum!=0) {
sum--;
}
}
System.out.println(sum);
}
}
} | linear | 1159_A. A pile of stones | CODEFORCES |
/*
* Created on 17.05.2019
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @author Wolfgang Weck
*/
public class C01Easy {
public static void main(String[] args) {
try (BufferedReader r = new BufferedReader(new InputStreamReader(System.in))) {
final String[] line = r.readLine().split(" ");
final int N = Integer.parseInt(line[0]), P = Integer.parseInt(line[1]);
final String[] numS = r.readLine().split(" ");
if (numS.length != N) throw new IllegalArgumentException();
final int[] n = new int[N];
int sum1 = 0, sum2 = 0;
for (int i = 0; i < N; i++) {
n[i] = Integer.parseInt(numS[i]) % P;
sum2 += n[i];
if (sum2 >= P) sum2 -= P;
}
int max = sum2;
for (int i = 0; i < N; i++) {
sum1 += n[i];
if (sum1 >= P) sum1 -= P;
sum2 -= n[i];
if (sum2 < 0) sum2 += P;
final int s = sum1 + sum2;
if (s > max) max = s;
}
System.out.println(max);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
| linear | 958_C1. Encryption (easy) | CODEFORCES |
import java.util.Scanner;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author madis
*/
public class Spreadsheet {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
in.nextLine();
for (int i = 0; i < N; i++) {
String str = in.nextLine();
if (str.indexOf("R") == 0 && str.indexOf("R") + 1 < str.indexOf("C") && isNum(str.charAt(1))) {
int row = Integer.parseInt(str.substring(str.indexOf("R") + 1, str.indexOf("C")));
int col = Integer.parseInt(str.substring(str.indexOf("C") + 1));
System.out.println(convertRC(row, col));
} else {
String row = "";
int j = 0;
while (str.charAt(j) >= 'A' && str.charAt(j) <= 'Z') {
row += str.charAt(j);
j++;
}
int num = Integer.parseInt(str.substring(j));
System.out.println(convertAB(row, num));
}
}
}
static String convertAB(String str, int num) {
String result = "";
int col = 0;
for (int i = 0; i < str.length(); i++) {
col += (int)Math.pow(26, (str.length()) - (i + 1)) * (str.charAt(i) - 'A' + 1);
}
result += "R" + num;
result += "C" + col;
return result;
}
static String convertRC(int row, int column) {
String result = "";
while (column > 0) {
int index = column % 26;
char c;
if (index == 0) {
c = 'Z';
column = column - 26;
} else {
c = (char) ('A' + index - 1);
column = column - index;
}
result += c;
column = column / 26;
}
String res = "";
for (int i = 0; i < result.length(); i++) {
res += result.charAt(result.length() - (i + 1));
}
res += row;
return res;
}
static boolean isNum(char x){
return x>'0'&&x<='9';
}
}
| linear | 1_B. Spreadsheets | CODEFORCES |
import static java.lang.Math.*;
import static java.lang.System.currentTimeMillis;
import static java.lang.System.exit;
import static java.lang.System.arraycopy;
import static java.util.Arrays.sort;
import static java.util.Arrays.binarySearch;
import static java.util.Arrays.fill;
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
try {
if (new File("input.txt").exists())
System.setIn(new FileInputStream("input.txt"));
} catch (SecurityException e) {
}
new Thread() {
public void run() {
try {
new Main().run();
} catch (IOException e) {
}
}
}.start();
}
BufferedReader in;
PrintWriter out;
StringTokenizer st = new StringTokenizer("");
int MAXDEG = 5;
private void run() throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
int n = nextInt();
int[] sumDegs = new int[MAXDEG + 1];
sumDegs[0] = 1;
int deg = 1;
for (int i = 1; i <= MAXDEG; i++) {
deg *= 26;
sumDegs[i] = sumDegs[i - 1] + deg;
}
for (int i = 0; i < n; i++) {
String s = nextLine();
String pref = "";
int endPos = -1;
for (int j = 0; j < s.length(); j++) {
if (!isLet(s.charAt(j))) {
endPos = j;
break;
}
pref += s.charAt(j);
}
int num = -1;
try {
num = Integer.parseInt(s.substring(endPos));
} catch (Exception e) {
}
if (num != -1) {
int col = sumDegs[pref.length() - 1];
int val = 0;
for (int j = 0; j < pref.length(); j++) {
val = val * 26 + (pref.charAt(j) - 'A');
}
col += val;
out.println("R" + num + "C" + col);
} else {
int row = Integer.parseInt(s.substring(1, s.indexOf('C')));
int col = Integer.parseInt(s.substring(s.indexOf('C') + 1));
int len = MAXDEG;
while (col < sumDegs[len])
len--;
len++;
col -= sumDegs[len - 1];
String res = "";
while (col > 0) {
res = (char) (col % 26 + 'A') + res;
col /= 26;
}
while (res.length() < len)
res = "A" + res;
out.println(res + row);
}
}
in.close();
out.close();
}
private boolean isLet(char c) {
return c >= 'A' && c <= 'Z';
}
void chk(boolean b) {
if (b)
return;
System.out.println(new Error().getStackTrace()[1]);
exit(999);
}
void deb(String fmt, Object... args) {
System.out.printf(Locale.US, fmt + "%n", args);
}
String nextToken() throws IOException {
while (!st.hasMoreTokens())
st = new StringTokenizer(in.readLine());
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
String nextLine() throws IOException {
st = new StringTokenizer("");
return in.readLine();
}
boolean EOF() throws IOException {
while (!st.hasMoreTokens()) {
String s = in.readLine();
if (s == null)
return true;
st = new StringTokenizer(s);
}
return false;
}
}
| linear | 1_B. Spreadsheets | CODEFORCES |
import java.util.Scanner;
import java.io.PrintWriter;
/**
* @author Egor Kulikov ([email protected])
*/
public class Spreadsheets implements Runnable {
private Scanner in = new Scanner(System.in);
private PrintWriter out = new PrintWriter(System.out);
private String s, ans;
public static void main(String[] args) {
new Thread(new Spreadsheets()).start();
}
private void read() {
s = in.next();
}
private void solve() {
if (s.matches("R\\d+C\\d+")) {
s = s.replace('R', ' ').replace('C', ' ');
Scanner ss = new Scanner(s);
int r = ss.nextInt();
int c = ss.nextInt();
c--;
StringBuffer b = new StringBuffer();
int c26 = 26;
int cc = 0;
while (cc + c26 <= c) {
cc += c26;
c26 *= 26;
}
c -= cc;
while (c26 > 1) {
c26 /= 26;
b.append((char) (c / c26 + 'A'));
c %= c26;
}
ans = b.toString() + r;
} else {
int p = 0;
while (!Character.isDigit(s.charAt(p))) {
p++;
}
int c26 = 1;
int cc = 0;
for (int i = 0; i < p; i++) {
cc += c26;
c26 *= 26;
}
for (int i = 0; i < p; i++) {
c26 /= 26;
cc += c26 * (s.charAt(i) - 'A');
}
ans = "R" + s.substring(p) + "C" + cc;
}
}
private void write() {
out.println(ans);
}
public void run() {
int n = in.nextInt();
for (int i = 0; i < n; i++) {
read();
solve();
write();
}
out.close();
}
} | linear | 1_B. Spreadsheets | CODEFORCES |
import java.io.*;
import java.util.*;
public class Main {
// static Scanner in;
static PrintWriter out;
static BufferedReader in;
public static void main(String[] args) throws Exception {
// in = new Scanner(System.in);
out = new PrintWriter(System.out);
in = new BufferedReader(new InputStreamReader(System.in));
int n = new Integer(in.readLine());
for (int i = 0; i < n; i++) {
String s = in.readLine();
int x = 0;
while (s.charAt(x) - 'A' >= 0 && s.charAt(x) - 'Z' <= 0) x++;
int y = s.length() - 1;
while (s.charAt(y) - '0' >= 0 && s.charAt(y) - '9' <= 0) y--;
if (x > y) {
int k = 1;
int a = 1;
for (int j = 1; j < x; j++) {
k *= 26;
a += k;
}
for (int j = 0; j < x; j++) {
a += k*(s.charAt(j) - 'A');
k /= 26;
}
int b = Integer.parseInt(s.substring(x));
out.println("R" + b + "C" + a);
} else {
while (s.charAt(x) - '0' >= 0 && s.charAt(x) - '9' <= 0) x++;
int b = Integer.parseInt(s.substring(1, x));
int a = Integer.parseInt(s.substring(x + 1));
int num = 0;
int k = 1;
while (a >= k) {
a -= k;
k *= 26;
}
k /= 26;
while (k > 0) {
out.print((char)('A' + (a/k)));
a %= k;
k /= 26;
}
out.println(b);
}
}
out.close();
}
} | linear | 1_B. Spreadsheets | CODEFORCES |
import java.util.*;
import java.math.*;
import static java.lang.Math.*;
import static java.math.BigInteger.*;
import static java.lang.Character.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
public class B {
static final int[] num = new int[7];
static {
for(int i = 1, c = 1; i <= 6; i++, c *= 26)
num[i] = num[i-1] + c;
}
public void run() {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
while(n-- > 0) {
String s = sc.next();
System.out.println(s.matches("R[0-9]+C[0-9]+") ? toABdd(s) : toRC(s));
}
}
String toRC(String s) {
String ss = s.split("[0-9]+")[0];
String sn = s.split("[A-Z]+")[1];
int r = 0;
for(char c : ss.toCharArray())
r = r * 26 + c - 'A';
r += num[ss.length()];
int c = Integer.parseInt(sn);
return "R" + c + "C" + r;
}
String toABdd(String s) {
String[] ss = s.split("[RC]");
int c = Integer.parseInt(ss[1]);
int r = Integer.parseInt(ss[2]);
int a = 0;
while(r > num[++a]);
a--;
r -= num[a];
char[] cs = new char[a];
for(int i = 0; i < a; i++, r /= 26)
cs[a-i-1] = (char) (r % 26 + 'A');
return new String(cs) + c;
}
void debug(Object... os) {
System.err.println(Arrays.deepToString(os));
}
public static void main(String...args) {
new B().run();
}
}
| linear | 1_B. Spreadsheets | CODEFORCES |
import java.io.*;
import java.util.*;
/**
* @author def
* @version 1.0
*/
public class B {
public static void main(String[] args) throws IOException {
new B().solve();
}
void solve() throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
int n = Integer.parseInt(in.readLine());
while (n-- > 0) {
String s = in.readLine();
int pr = s.indexOf('R');
int pc = s.indexOf('C');
if (pr == 0 && pc > 1 && Character.isDigit(s.charAt(1))) {
int r = Integer.parseInt(s.substring(1, pc));
int c = Integer.parseInt(s.substring(pc + 1, s.length()));
out.println(i2s(c) + r);
} else {
int i = 0;
while (!Character.isDigit(s.charAt(i))) ++i;
out.println("R" + Integer.parseInt(s.substring(i, s.length())) + "C" + s2i(s.substring(0, i)));
}
}
out.close();
}
int s2i(String s) {
int r = 0;
for (int i = 0; i < s.length(); ++i) {
r = r * 26 + (s.charAt(i) - 'A' + 1);
}
return r;
}
String i2s(int i) {
StringBuffer s = new StringBuffer();
while (i > 0) {
i -= 1;
s.append((char)('A' + (i % 26)));
i /= 26;
}
return s.reverse().toString();
}
BufferedReader in;
PrintWriter out;
}
| linear | 1_B. Spreadsheets | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringReader;
import java.util.StringTokenizer;
public class Solution {
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
new Solution();
}
}).start();
}
Solution() {
String data = "2\nR1C18\nR1";
PrintWriter pw = new PrintWriter(System.out);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//BufferedReader in = new BufferedReader(new StringReader(data));
try {
int n = Integer.parseInt(in.readLine());
int[] res = new int[2];
for (int i = 0; i < n; i++) {
String s = in.readLine();
if (isSecondType(s, res)) {
pw.println(toFirstType(res));
} else {
pw.println(toSecondType(s));
}
}
} catch (IOException e) {
} finally {
pw.flush();
pw.close();
}
}
private String toSecondType(String s) {
int i = 0;
for (; i < s.length(); i++) {
if (s.charAt(i) < 'A' || s.charAt(i) > 'Z') break;
}
String first = s.substring(0, i);
String second = s.substring(i, s.length());
StringBuilder sb = new StringBuilder();
sb.append("R");
sb.append(second);
sb.append("C");
sb.append(stringToNum(first));
return sb.toString();
}
private int stringToNum(String first) {
int k = 0;
int res = 0;
int p = 1;
for (int i = first.length() - 1; i >= 0; i--) {
int v = first.charAt(i) - 'A' + k;
k = 1;
res += p * v;
p *= 26;
}
return res + 1;
}
private String toFirstType(int[] res) {
StringBuilder sb = new StringBuilder();
sb.append(numToString(res[1]));
sb.append(res[0]);
return sb.toString();
}
private String numToString(int num) {
StringBuilder sb = new StringBuilder();
while (num > 0) {
num--;
char c = (char) (num % 26 + 'A');
sb.append(c);
num /= 26;
}
return sb.reverse().toString();
}
private boolean isSecondType(String s, int[] res) {
try {
return doIsSecondType(s, res);
} catch (Exception e) {
return false;
}
}
private boolean doIsSecondType(String s, int[] res) {
StringTokenizer st = new StringTokenizer(s, "RC", true);
String token = st.nextToken();
if (!token.equals("R")) return false;
token = st.nextToken();
if (!isDigit(token)) return false;
res[0] = Integer.parseInt(token);
token = st.nextToken();
if (!token.equals("C")) return false;
token = st.nextToken();
if (!isDigit(token)) return false;
res[1] = Integer.parseInt(token);
return true;
}
private boolean isDigit(String token) {
for (int i = 0; i < token.length(); i++) {
if (token.charAt(i) < '0' || token.charAt(i) > '9') return false;
}
return true;
}
}
| linear | 1_B. Spreadsheets | CODEFORCES |
import java.awt.Label;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class Main implements Runnable {
private boolean _ReadFromFile = false;
private boolean _WriteToFile = false;
static final String TASK_ID = "in";
static final String IN_FILE = TASK_ID + ".in";
static final String OUT_FILE = TASK_ID + ".out";
BufferedReader reader;
StringTokenizer tokenizer;
PrintWriter writer;
private String alphabet;
private void core() throws Exception {
int n = nextInt();
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int test = 0; test < n; test++) {
String input = reader.readLine();
StringTokenizer st = new StringTokenizer(input, alphabet);
ArrayList<Integer> have = new ArrayList<Integer>();
while (st.hasMoreElements()) {
String kString = st.nextToken();
have.add(Integer.parseInt(kString));
}
if (have.size() == 2)
writer.println(twoInts(have.get(0), have.get(1)));
else {
String row = "";
int col = 0;
for (int i = 0; i < input.length(); i++) {
if (Character.isDigit(input.charAt(i))) {
row = input.substring(0, i);
col = Integer.parseInt(input.substring(i));
break;
}
}
writer.println(oneInt(row, col));
}
}
}
private String oneInt(String row, int col) {
return "R" + col + "C" + toNum(row);
}
private int toNum(String row) {
int res = 0;
for (int i = 0; i < row.length(); i++) {
res = res * 26 + row.charAt(i) - 'A' + 1;
}
return res;
}
private String twoInts(Integer row, Integer col) {
return toAlpha(col) + row;
}
private String toAlpha(Integer col) {
String res = "";
while (col > 0) {
if (col % 26 > 0) {
res = alphabet.charAt(col % 26 - 1) + res;
col /= 26;
}
else {
res = "Z" + res;
col -= 26;
col /= 26;
}
}
return res;
}
void debug(Object...os) {
System.out.println(Arrays.deepToString(os));
}
//--------------------- IO stuffs ---------------------
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new Main());
thread.start();
thread.join();
}
public void run() {
try {
reader = _ReadFromFile ? new BufferedReader(new FileReader(IN_FILE)) : new BufferedReader(new InputStreamReader(System.in));
writer = _WriteToFile ? new PrintWriter(OUT_FILE) : new PrintWriter(new BufferedOutputStream(System.out));
tokenizer = null;
core();
reader.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
int nextInt() throws Exception {
return Integer.parseInt(nextToken());
}
long nextLong() throws Exception {
return Long.parseLong(nextToken());
}
double nextDouble() throws Exception {
return Double.parseDouble(nextToken());
}
String nextToken() throws Exception {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
}
| linear | 1_B. Spreadsheets | CODEFORCES |
import java.util.*;
import java.math.*;
import java.io.*;
public class B implements Runnable {
public static void main(String[] args) {
new Thread(new B()).start();
}
StringTokenizer st;
PrintWriter out;
BufferedReader br;
boolean eof = false, in_out = false, std = false;
String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
eof = true;
return "0";
}
}
return st.nextToken();
}
String nextLine() {
String ret = "";
try {
ret = br.readLine();
} catch (Exception e) {
ret = "";
}
if (ret == null) {
eof = true;
return "$";
}
return ret;
}
String nextString() {
return nextToken();
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
BigInteger nextBigInteger() {
return new BigInteger(nextToken());
}
public void run() {
// long time = System.currentTimeMillis();
try {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.flush();
} catch (Exception e) {
e.printStackTrace();
System.exit(111);
}
// System.err.println(System.currentTimeMillis() - time);
}
void solve() {
int n = nextInt();
for (int i = 0; i < n; i++) {
solve2();
}
}
void solve2() {
String s = nextToken();
boolean wasNum = false, isFirst = false;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) >= '0' && s.charAt(i) <= '9') {
wasNum = true;
} else if (wasNum) {
isFirst = true;
break;
}
}
if (isFirst) {
StringTokenizer e = new StringTokenizer(s, "RC");
int y = Integer.parseInt(e.nextToken());
int x = Integer.parseInt(e.nextToken()) - 1;
go1(x, y);
} else {
go2(s);
}
}
void go1(int x, int y) {
long cur = 26;
int len = 1;
while (x >= cur) {
x -= cur;
cur *= 26;
len++;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < len; i++) {
sb.append((char) ('A' + x % 26));
x /= 26;
}
out.println(sb.reverse().toString() + y);
}
void go2(String s) {
int id = -1;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) <= '9' && s.charAt(i) >= '0') {
id = i;
break;
}
}
String s1 = s.substring(0, id);
String s2 = s.substring(id);
int x = 0;
int cur = 26;
for (int i = 1; i < s1.length(); i++) {
x += cur;
cur *= 26;
}
int d = 0;
for (int i = 0; i < s1.length(); i++) {
d *= 26;
d += s1.charAt(i) - 'A';
}
x += d;
out.println("R" + s2 + "C" + (x + 1));
}
} | linear | 1_B. Spreadsheets | CODEFORCES |
import java.io.*;
import java.math.*;
import java.util.*;
import java.util.List;
import java.awt.*;
public class Trains1_2 implements Runnable {
private BufferedReader br = null;
private PrintWriter pw = null;
private StringTokenizer stk = new StringTokenizer("");
public static void main(String[] args) {
new Thread(new Trains1_2()).run();
}
public void run() {
/*
* try { br = new BufferedReader(new FileReader("knapsackfixed.in")); pw
* = new PrintWriter("knapsackfixed.out"); } catch
* (FileNotFoundException e) { e.printStackTrace(); }
*/
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new OutputStreamWriter(System.out));
solver();
pw.close();
}
private void nline() {
try {
if (!stk.hasMoreTokens())
stk = new StringTokenizer(br.readLine());
} catch (IOException e) {
throw new RuntimeException("KaVaBUnGO!!!", e);
}
}
private String nstr() {
while (!stk.hasMoreTokens())
nline();
return stk.nextToken();
}
private int ni() {
return Integer.valueOf(nstr());
}
private long nl() {
return Long.valueOf(nstr());
}
private double nd() {
return Double.valueOf(nstr());
}
boolean isNumber(char c) {
if (c <= '9' && c >= '0')
return true;
return false;
}
String to10(String s) {
long ans = 0;
for (int i = s.length() - 1; i >= 0; i--) {
ans += (s.charAt(i) - 'A' + 1) * Math.pow(26, s.length() - i - 1);
}
return String.valueOf(ans);
}
String to26(String s){
String ans="";
int a = Integer.valueOf(s);
while(a>26){
a--;
int k = a%26;
ans=ans+(char)((int)'A'+k);
a/=26;
}
ans+=(char)(a+'A'-1);
String ans1 = "";
for(int i = ans.length()-1; i>=0; i--)ans1+=ans.charAt(i);
return ans1;
}
String rev(String s) {
String ans = "";
int format = 0;
int git = 0;
String token1 = "";
String token2 = "";
String token3 = "", token4 = "";
for (int i = 0; i < s.length(); i++) {
if (!isNumber(s.charAt(i)))
token1 += s.charAt(i);
else
break;
git++;
}
for (int i = git; i < s.length(); i++) {
if (isNumber(s.charAt(i)))
token2 += s.charAt(i);
else
break;
git++;
}
if (s.length() == git)
format = 1;
else {
format = 2;
for (int i = git; i < s.length(); i++) {
if (!isNumber(s.charAt(i)))
token3 += s.charAt(i);
else
break;
git++;
}
for (int i = git; i < s.length(); i++)
if (isNumber(s.charAt(i)))
token4 += s.charAt(i);
else
break;
}
if (format == 1) {
ans += "R";
ans += token2;
ans += "C";
ans += to10(token1);
}else{
ans+=to26(token4);
ans+=token2;
}
return ans;
}
private void solver() {
int n = ni();
for (int i = 0; i < n; i++)
System.out.println(rev(nstr()));
}
void exit() {
System.exit(0);
}
}
| linear | 1_B. Spreadsheets | CODEFORCES |
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.text.NumberFormat;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Locale;
import java.util.Queue;
import java.util.Random;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class Solution implements Runnable {
public static void main(String[] args) {
(new Thread(new Solution())).start();
}
BufferedReader in;
PrintWriter out;
StringTokenizer st;
String nextToken() throws IOException {
while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(in.readLine());
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
String sti(String s) {
int res = 0;
int q = 1;
int qq = 0;
for (int i = 0; i < s.length(); i++) {
if (i > 0) qq += q;
res = res * 26 + s.charAt(i) - 'A';
q *= 26;
}
return Integer.toString(qq + res + 1);
}
String its(String s) {
int q = Integer.parseInt(s);
String res = "";
int w = 26;
int l = 1;
while (q > w) {
q -= w;
l++;
w *= 26;
}
q--;
if (q == 0) return "A";
while (q > 0) {
res = "" + (char)('A' + q % 26) + res;
l--;
q /= 26;
}
for (; l > 0; l--) res = "A" + res;
return res;
}
void solve() throws IOException {
int n = nextInt();
for (int i = 0; i < n; i++) {
String s = nextToken();
int j = 0;
while (!Character.isDigit(s.charAt(j))) j++;
int q = j + 1;
while (q < s.length() && Character.isDigit(s.charAt(q))) q++;
if (q == s.length()) {
out.println("R" + s.substring(j) + "C" + sti(s.substring(0, j)));
} else {
String w = s.substring(j, q);
while (!Character.isDigit(s.charAt(q))) q++;
out.println(its(s.substring(q)) + w);
}
}
}
public void run() {
try {
// in = new BufferedReader(new FileReader(new File("input.txt")));
// out = new PrintWriter(new File("output.txt"));
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
} catch (IOException e) {
e.printStackTrace();
}
out.flush();
}
}
| linear | 1_B. Spreadsheets | CODEFORCES |
import java.io.*;
import java.util.*;
public class Solution {
private BufferedReader in;
private PrintWriter out;
private StringTokenizer st;
void solve() throws IOException {
String s = next();
int u = s.indexOf('R');
int v = s.indexOf('C');
if (u == 0 && v != -1 && u < v) {
String a = s.substring(u + 1, v);
String b = s.substring(v + 1);
try {
int aa = Integer.parseInt(a);
int bb = Integer.parseInt(b) - 1;
int pow = 26, len = 1;
while (bb >= pow) {
bb -= pow;
pow *= 26;
++len;
}
String r = "";
for (int i = 0; i < len; ++i) {
r = ((char)(bb % 26 + 'A')) + r;
bb /= 26;
}
out.println(r + aa);
return;
} catch (NumberFormatException e) {
}
}
u = 0;
while (u < s.length() && Character.isLetter(s.charAt(u))) {
++u;
}
String a = s.substring(0, u);
String b = s.substring(u);
out.println("R" + b + "C" + toInt(a));
}
private int toInt(String a) {
int r = 0;
for (int i = 0; i < a.length(); ++i) {
r *= 26;
r += a.charAt(i) - 'A';
}
int pow = 1;
for (int i = 0; i < a.length(); ++i) {
r += pow;
pow *= 26;
}
return r;
}
Solution() throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
eat("");
int tests = nextInt();
for (int test = 0; test < tests; ++test) {
solve();
}
in.close();
out.close();
}
private void eat(String str) {
st = new StringTokenizer(str);
}
String next() throws IOException {
while (!st.hasMoreTokens()) {
String line = in.readLine();
if (line == null) {
return null;
}
eat(line);
}
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public static void main(String[] args) throws IOException {
new Solution();
}
}
| linear | 1_B. Spreadsheets | CODEFORCES |
import java.util.*;
import java.io.*;
public class Main
{
public static void main(String args[]) throws IOException
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String s;
int i,j;
for(i=0;i<n;i++)
{
s = sc.next();
j = 0;
boolean ok;
while((s.charAt(j)>='A')&&(s.charAt(j)<='Z')) j++;
while((j<s.length())&&(s.charAt(j)>='0')&&(s.charAt(j)<='9')) j++;
if (j==s.length()) ok = true; else ok = false;
String s1="",s2="";
if (ok)
{
j = 0;
while((s.charAt(j)>='A')&&(s.charAt(j)<='Z'))
{
s1 += s.charAt(j);
j++;
}
while(j<s.length())
{
s2 += s.charAt(j);
j++;
}
int v = 0,p = 1;
for(j=s1.length()-1;j>=0;j--)
{
v += p*(s1.charAt(j)-'A'+1);
p*=26;
}
System.out.println("R"+s2+"C"+v);
} else
{
j = 1;
while((s.charAt(j)>='0')&&(s.charAt(j)<='9'))
{
s1 += s.charAt(j);
j++;
}
j++;
while(j<s.length())
{
s2 += s.charAt(j);
j++;
}
Integer a = new Integer(s2);
String s3="";
int d;
while(a > 0)
{
d = a%26; a/=26;
if (d==0) {d=26; a--;}
s3 = Character.toUpperCase(Character.forDigit(9+d,36)) + s3;
}
System.out.println(s3+s1);
}
}
}
} | linear | 1_B. Spreadsheets | CODEFORCES |
import java.util.Scanner;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author madi
*/
public class Speadsheets {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
String code = "";
for (int i = 0; i < n; i++) {
long chResult = 0;
long chResult1 = 0;
long nResult = 0;
long nResult1 = 0;
boolean t = false;
boolean k = false;
code = sc.nextLine();
for (int j = 0; j < code.length(); j++) {
char c = code.charAt(j);
if (('Z' - c) < 33) {
if (t) {
chResult1 = chResult;
chResult = 0;
t = false;
k = true;
}
chResult = chResult * 26 + (26 - ('Z' - c));
} else {
t = true;
if (k) {
nResult1 = nResult;
nResult = 0;
k = false;
}
nResult = nResult * 10 + (9 - ('9' - c));
}
}
if (chResult1 == 0) {
System.out.println("R" + nResult + "C" + chResult);
} else {
System.out.println(convert(nResult) + nResult1);
}
}
}
private static String convert(long number) {
String [] chars = new String[]{"Z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y"};
String result = "";
int rem = 0;
int m = 0;
while (number > 0) {
m = 0;
rem = (int) (number % 26);
result = chars[rem] + result;
/*if (number == 26) {
number = -1;
}*/
if (number % 26 == 0) {
m = 1;
}
number = number / 26;
number = number - m;
}
return result;
}
} | linear | 1_B. Spreadsheets | CODEFORCES |
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.regex.Pattern;
//Stephen Fulwider
//Parser class for efficient input in Java
// Use just as you would Scanner.
// Make sure any method that uses this class throws Exception.
// Email any bugs or problems found to [email protected]
class Parser
{
final private int BUFFER_SIZE = 1 << 16;
private java.io.DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Parser(java.io.InputStream in)
{
din = new java.io.DataInputStream(in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public int nextInt() throws Exception
{
byte c = read();
while (c <= ' ')
c = read();
boolean neg = c == '-';
if (neg)
c = read();
int ret = 0;
do
{
ret = ret * 10 + c - '0';
c = read();
} while (c > ' ');
bufferPointer--;
if (neg)
return -ret;
return ret;
}
public long nextLong() throws Exception
{
byte c = read();
while (c <= ' ')
c = read();
boolean neg = c == '-';
if (neg)
c = read();
long ret = 0;
do
{
ret = ret * 10 + c - '0';
c = read();
} while (c > ' ');
bufferPointer--;
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws Exception
{
byte c = read();
while (c <= ' ')
c = read();
boolean neg = c == '-';
if (neg)
c = read();
boolean seenDot = false;
double div = 1;
double ret = 0;
do
{
if (c == '.')
seenDot = true;
else
{
if (seenDot)
div *= 10;
ret = ret * 10 + c - '0';
}
c = read();
} while (c > ' ');
bufferPointer--;
ret /= div;
if (neg)
return -ret;
return ret;
}
public char nextChar() throws Exception
{
byte c = read();
while (c <= ' ')
c = read();
return (char) c;
}
public String next() throws Exception
{
StringBuilder ret = new StringBuilder();
byte c = read();
while (c <= ' ')
c = read();
do
{
ret.append((char) c);
c = read();
} while (c > ' ');
bufferPointer--;
return ret.toString();
}
// read a string into an ALREADY ALLOCATED array, returns the number of characters read
public int next(char[] ret) throws Exception
{
byte c = read();
while (c <= ' ')
c = read();
int bRead = 0;
do
{
ret[bRead++] = (char) c;
c = read();
} while (c > ' ');
bufferPointer--;
return bRead;
}
public String nextLine() throws Exception
{
StringBuilder ret = new StringBuilder();
byte c = read();
while (c != '\r' && c != '\n' && c != -1)
{
ret.append((char) c);
c = read();
}
if (c == '\r')
read();
return ret.toString();
}
public boolean hasNext() throws Exception
{
byte c;
do
{
c = read();
if (c == -1)
{
bufferPointer--;
return false;
}
} while (c <= ' ');
bufferPointer--;
return true;
}
private void fillBuffer() throws Exception
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
}
private byte read() throws Exception
{
if (bufferPointer == bytesRead) fillBuffer();
if (bytesRead == -1) return -1;
return buffer[bufferPointer++];
}
}
class Printer
{
final private int BUFFER_SIZE = 1 << 16;
private java.io.DataOutputStream dout;
private byte[] buffer;
private int bufferPointer;
Printer(java.io.OutputStream out) throws Exception
{
dout = new java.io.DataOutputStream(out);
buffer = new byte[BUFFER_SIZE];
bufferPointer = 0;
}
public void println() throws Exception
{
write((byte) '\n');
}
// print int
public void println(int n) throws Exception
{
print(n);
println();
}
public void print(int n) throws Exception
{
if (n >= 0)
print(n, true);
else
{
write((byte) '-');
print(-n, true);
}
}
private void print(int n, boolean first) throws Exception
{
if (n == 0)
{
if (first)
write((byte) (n + '0'));
}
else
{
print(n / 10, false);
write((byte) ((n % 10) + '0'));
}
}
// print long
public void println(long n) throws Exception
{
print(n);
println();
}
public void print(long n) throws Exception
{
if (n >= 0)
print(n, true);
else
{
write((byte) '-');
print(-n, true);
}
}
private void print(long n, boolean first) throws Exception
{
if (n == 0)
{
if (first)
write((byte) (n + '0'));
}
else
{
print(n / 10, false);
write((byte) ((n % 10) + '0'));
}
}
// print double
public void println(double d) throws Exception
{
print(d);
println();
}
public void print(double d) throws Exception
{
print("double printing is not yet implemented!");
}
// print char
public void println(char c) throws Exception
{
print(c);
println();
}
public void print(char c) throws Exception
{
write((byte) c);
}
// print String
public void println(String s) throws Exception
{
print(s);
println();
}
public void print(String s) throws Exception
{
int len = s.length();
for (int i = 0; i < len; i++)
print(s.charAt(i));
}
public void close() throws Exception
{
flushBuffer();
}
private void flushBuffer() throws Exception
{
dout.write(buffer, 0, bufferPointer);
bufferPointer = 0;
}
private void write(byte b) throws Exception
{
buffer[bufferPointer++] = b;
if (bufferPointer == BUFFER_SIZE)
flushBuffer();
}
}
public class Main
{
public static void main(String[] args) throws Exception
{
new Main();
}
final int oo = (int)1e9;
// InputStream stream = new FileInputStream("in");
InputStream stream = System.in;
Parser in = new Parser(stream);
Printer out = new Printer(System.out);
Printer err = new Printer(System.err);
long start_time = System.nanoTime();
// PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
// PrintWriter err = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.err)));
int Sn;
char[] S = new char[1000];
char[] B = new char[1000];
Main() throws Exception
{
for (int T=in.nextInt(); T-->0; )
{
Sn = in.next(S);
if (matchRxCy())
toLet();
else
toNum();
}
long end_time = System.nanoTime();
err.println("Time: " + ((end_time-start_time)/1e9) + "(s).");
if (stream instanceof FileInputStream)
{
err.println("~~~~~~~~~~~~~~~~~~~~~~~~~~!!!!!!!!!!!!!!!!!!!!!!!!!~~~~~~~~~~~~~~~~~~~~~~~~~~");
err.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~CHANGE INPUT STREAM~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
err.println("~~~~~~~~~~~~~~~~~~~~~~~~~~!!!!!!!!!!!!!!!!!!!!!!!!!~~~~~~~~~~~~~~~~~~~~~~~~~~");
}
out.close();
err.close();
}
boolean matchRxCy()
{
boolean ok=S[0]=='R'&&(S[1]>='0'&&S[1]<='9');
if (!ok) return false;
int i;
for (i=2; i<Sn && S[i]!='C'; ++i);
return i<Sn;
}
void toLet() throws Exception
{
int r = 0;
int i=1;
while (S[i]!='C')
r=r*10+S[i++]-'0';
int c = 0;
++i;
while (i<Sn)
c=c*10+S[i++]-'0';
int bi=0;
while (c>0)
{
B[bi++]=(char)((c-1)%26+'A');
c=(c-1)/26;
}
for (int j=bi-1; j>=0; --j)
out.print(B[j]);
i=1;
while (S[i]!='C')
out.print(S[i++]);
out.println();
}
void toNum() throws Exception // this works fine now, i think
{
int c=0;
int i=0;
int v=1;
while (Character.isLetter(S[i++]))
v*=26;
v/=26;
i=0;
while (Character.isLetter(S[i]))
{
c+=v*(S[i++]-'A'+1);
v/=26;
}
int r=0;
while (i<Sn)
r=r*10+S[i++]-'0';
out.print('R');
out.print(r);
out.print('C');
out.print(c);
out.println();
}
}
| linear | 1_B. Spreadsheets | CODEFORCES |
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Round1B {
public static void main(String[] args) throws Exception {
new Round1B().run();
}
private void run() throws Exception {
Scanner sc = new Scanner(System.in);
int tc = Integer.parseInt(sc.nextLine().trim());
while (tc > 0) {
String s = sc.nextLine().trim();
if (s.matches("R[0-9]+C[0-9]+")) {
Pattern p = Pattern.compile("R([0-9]+)C([0-9]+)");
Matcher m = p.matcher(s);
if (m.matches()) {
int rows = Integer.parseInt(m.group(1));
int cols = Integer.parseInt(m.group(2));
String col = "";
while (cols > 0) {
int mod = (cols - 1) % 26;
col = (char)('A' + mod) + col;
cols = (cols - 1) / 26;
}
System.out.println(col + rows);
} else {
throw new Exception();
}
} else {
Pattern p = Pattern.compile("([A-Z]+)([0-9]+)");
Matcher m = p.matcher(s);
if (m.matches()) {
int rows = Integer.parseInt(m.group(2));
int cols = 0;
int mul = 1;
for (int i = m.group(1).length() - 1; i >= 0; i--) {
cols += mul * (m.group(1).charAt(i) - 'A' + 1);
mul *= 26;
}
System.out.printf("R%dC%d\n", rows, cols);
}
else {
throw new Exception();
}
}
tc--;
}
sc.close();
}
}
| linear | 1_B. Spreadsheets | CODEFORCES |