Sunday, March 30, 2014

Codeforces Round #200 (Div. 2) B Simple Molecules

// Codeforces Round #200 (Div. 2) B Simple Molecules
// Problem: http://codeforces.com/contest/344/problem/B

import java.io.*;
import java.math.*;
import java.util.*;

//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
private static boolean LOCAL_TEST = false;

private static void solve() throws IOException
{
int a = in.nextInt();
int b = in.nextInt();
int c = in.nextInt();
boolean valid = false;
for (int i = 0; i <= a; i++) {
int b12 = i;
int b31 = a - i;
int b23 = c - b31;
valid = (b12 >= 0 && b23 >= 0 && b31 >= 0 && b == (b12 + b23));
if (valid) {
out.println(b12 + " " + b23 + " " + b31);
return;
}
}
out.println("Impossible");
}

public static void main(String[] args) throws IOException {
// helpers for input/output
out = System.out;
try {
String cname = System.getenv("COMPUTERNAME");
if (!cname.equals(""))
LOCAL_TEST = true;
} catch (Exception e) {
}
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin.txt");
}
else {
boolean usingFileForIO = false;
if (usingFileForIO) {
// using input.txt and output.txt as I/O
in = new MyScanner("input.txt");
out = new PrintStream("output.txt");
}
else {
in = new MyScanner();
out = System.out;
}
}

solve();
}

// =====================================
static class MyScanner {
BufferedReader bufReader;
StringTokenizer strTok;

public MyScanner() throws IOException
{
bufReader = new BufferedReader(new InputStreamReader(System.in));
strTok = new StringTokenizer("");
}

public MyScanner(String inputFile) throws IOException {
bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(
inputFile)));
strTok = new StringTokenizer("");
}

String GetNextToken() throws IOException {
if (!strTok.hasMoreTokens())
strTok = new StringTokenizer(bufReader.readLine());
return strTok.nextToken();
}

public int nextInt() throws IOException {
return Integer.valueOf(GetNextToken());
}

public long nextLong() throws IOException {
return Long.valueOf(GetNextToken());
}

public double nextDouble() throws IOException {
return Double.valueOf(GetNextToken());
}

public String nextString() throws IOException {
return GetNextToken();
}

public String nextLine() throws IOException {
return bufReader.readLine();
}

public int countTokens() {
return strTok.countTokens();
}

public boolean hasMoreTokens() {
return strTok.hasMoreTokens();
}
}

}

Codeforces Round #200 (Div. 2) A Magnets

// Codeforces Round #200 (Div. 2) A Magnets
// Problem: http://codeforces.com/contest/344/problem/A

import java.io.*;
import java.math.*;
import java.util.*;

//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
private static boolean LOCAL_TEST = false;

private static void solve() throws IOException
{
int n = in.nextInt();
int grp = 1;
String prev = in.nextString();
for (int i = 1; i < n; i++) {
String s = in.nextString();
if (!s.equals(prev))
grp++;
prev = s;
}
out.println(grp);
}

public static void main(String[] args) throws IOException {
// helpers for input/output
out = System.out;
try {
String cname = System.getenv("COMPUTERNAME");
if (!cname.equals(""))
LOCAL_TEST = true;
} catch (Exception e) {
}
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin.txt");
}
else {
boolean usingFileForIO = false;
if (usingFileForIO) {
// using input.txt and output.txt as I/O
in = new MyScanner("input.txt");
out = new PrintStream("output.txt");
}
else {
in = new MyScanner();
out = System.out;
}
}

solve();
}

// =====================================
static class MyScanner {
BufferedReader bufReader;
StringTokenizer strTok;

public MyScanner() throws IOException
{
bufReader = new BufferedReader(new InputStreamReader(System.in));
strTok = new StringTokenizer("");
}

public MyScanner(String inputFile) throws IOException {
bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(
inputFile)));
strTok = new StringTokenizer("");
}

String GetNextToken() throws IOException {
if (!strTok.hasMoreTokens())
strTok = new StringTokenizer(bufReader.readLine());
return strTok.nextToken();
}

public int nextInt() throws IOException {
return Integer.valueOf(GetNextToken());
}

public long nextLong() throws IOException {
return Long.valueOf(GetNextToken());
}

public double nextDouble() throws IOException {
return Double.valueOf(GetNextToken());
}

public String nextString() throws IOException {
return GetNextToken();
}

public String nextLine() throws IOException {
return bufReader.readLine();
}

public int countTokens() {
return strTok.countTokens();
}

public boolean hasMoreTokens() {
return strTok.hasMoreTokens();
}
}

}

Codeforces Round #199 (Div. 2) A Xenia and Divisors

// Codeforces Round #199 (Div. 2) A Xenia and Divisors

import java.io.*;
import java.math.*;
import java.util.*;

//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
private static boolean LOCAL_TEST = false;

private static void solve() throws IOException
{
int n = in.nextInt();
int[] x = new int[n];
for (int i = 0; i < x.length; i++) {
x[i] = in.nextInt();
}
Arrays.sort(x);
int group = n / 3;
for (int i = 0; i < group; i++) {
int a = x[i];
int b = x[i + group];
int c = x[i + group + group];
if (a < b && b < c && b % a == 0 && c % b == 0)
continue;
else {
out.println(-1);
return;
}
}
for (int i = 0; i < group; i++) {
int a = x[i];
int b = x[i + group];
int c = x[i + group + group];
{
out.print(a + " ");
out.print(b + " ");
out.println(c);
}
}
}

public static void main(String[] args) throws IOException {
// helpers for input/output
out = System.out;
try {
String cname = System.getenv("COMPUTERNAME");
if (!cname.equals(""))
LOCAL_TEST = true;
} catch (Exception e) {
}
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin.txt");
}
else {
boolean usingFileForIO = false;
if (usingFileForIO) {
// using input.txt and output.txt as I/O
in = new MyScanner("input.txt");
out = new PrintStream("output.txt");
}
else {
in = new MyScanner();
out = System.out;
}
}

solve();
}

// =====================================
static class MyScanner {
BufferedReader bufReader;
StringTokenizer strTok;

public MyScanner() throws IOException
{
bufReader = new BufferedReader(new InputStreamReader(System.in));
strTok = new StringTokenizer("");
}

public MyScanner(String inputFile) throws IOException {
bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(
inputFile)));
strTok = new StringTokenizer("");
}

String GetNextToken() throws IOException {
if (!strTok.hasMoreTokens())
strTok = new StringTokenizer(bufReader.readLine());
return strTok.nextToken();
}

public int nextInt() throws IOException {
return Integer.valueOf(GetNextToken());
}

public long nextLong() throws IOException {
return Long.valueOf(GetNextToken());
}

public double nextDouble() throws IOException {
return Double.valueOf(GetNextToken());
}

public String nextString() throws IOException {
return GetNextToken();
}

public String nextLine() throws IOException {
return bufReader.readLine();
}

public int countTokens() {
return strTok.countTokens();
}

public boolean hasMoreTokens() {
return strTok.hasMoreTokens();
}
}

}

Topcoder SRM 612 DIV 2 L2 EmoticonsDiv2

// Topcoder SRM 612 DIV 2 L2 EmoticonsDiv2

import java.util.*;
import java.math.*;

//rename the class name before submitting
public class EmoticonsDiv2 {
public static void main(String[] args) {
EmoticonsDiv2 obj = new EmoticonsDiv2();
System.out
.println(
obj.printSmiles(
1000
));
}

public int printSmiles(int smiles) {
int[][] dp = new int[1001][1001];
int[] minSecs = new int[1001];
for (int i = 0; i < 1001; i++) {
minSecs[i] = Integer.MAX_VALUE;
}
for (int i = 0; i < 1001; i++) {
for (int j = 0; j < 1001; j++) {
dp[i][j] = -1;
}
}
dp[1][2] = 2;
minSecs[1] = 2;
for (int j = 2; j < 1001; j++) {
dp[1][j] = j;
minSecs[j] = j;
}
for (int i = 2; i < 1001; i++) {
for (int j = i; j < 1001; j += i) {
if (j == i) {
dp[i][j] = minSecs[j] + 1;
}
else {
dp[i][j] = dp[i][j - i] + 1;
}
minSecs[j] = Math.min(minSecs[j], dp[i][j]);
}
}

return minSecs[smiles];
}
}

Thursday, March 27, 2014

Topcoder SRM 612 DIV 2 L1 LeftAndRightHandedDiv2

// Topcoder SRM 612 DIV 2 L1 LeftAndRightHandedDiv2
import java.util.*;
import java.math.*;

public class LeftAndRightHandedDiv2 {
public static void main(String[] args) {
LeftAndRightHandedDiv2 obj = new LeftAndRightHandedDiv2();
System.out
.println(
obj.count(
"LR"
));
}

public int count(String S) {
int c = 0;
for (int i = 1; i < S.length(); i++) {
if (S.charAt(i) == 'L' && S.charAt(i - 1) == 'R')
c++;
}
return c;
}
}

Friday, March 21, 2014

Topcoder SRM 613 DIV 2 L1 TaroString

// Topcoder SRM 613 DIV 2 L1 TaroString

import java.util.*;
import java.math.*;

//rename the class name before submitting
public class TaroString {
public static void main(String[] args) {
TaroString obj = new TaroString();
System.out.println(
obj.getAnswer(
""
));
}

public String getAnswer(String S) {
String x = "";
for (int i = 0; i < S.length(); i++) {
if (S.charAt(i) == 'C')
x += "C";
if (S.charAt(i) == 'A')
x += "A";
if (S.charAt(i) == 'T')
x += "T";
}
if (x.equals("CAT"))
return "Possible";
else
return "Impossible";
}

public static int GetPrice(char c) {
int price;
if (c >= '0' && c <= '9')
price = c - '0';
else if (c >= 'A' && c <= 'Z')
price = 10 + c - 'A';
else
price = 36 + c - 'a';
return price;
}
}

Topcoder SRM 512 DIV 2 L2 MysteriousRestaurant

// Topcoder SRM 512 DIV 2 L2 MysteriousRestaurant

import java.util.*;
import java.math.*;

//rename the class name before submitting
public class MysteriousRestaurant {
public static void main(String[] args) {
MysteriousRestaurant obj = new MysteriousRestaurant();
System.out.println(
obj.maxDays(
new String[]
{}, 0
));
}

public int maxDays(String[] prices, int budget) {
int days = 0;
int[] minps = new int[7];
for (int i = 0; i < 7; i++) {
if (i == prices.length)
break;

int minp = Integer.MAX_VALUE;
for (int j = 0; j < prices[0].length(); j++) {
char c = prices[i].charAt(j);
int price = GetPrice(c);
minp = Math.min(minp, price);
}
minps[i] = minp;
if (budget >= minp) {
budget -= minp;
days++;
}
else
return days;
}

for (int i = 7; i < prices.length; i++) {
int minp = Integer.MAX_VALUE;
for (int j = 0; j < prices[0].length(); j++) {
int price = 0;
for (int k = i % 7; k <= i; k += 7) {
char c1 = prices[k].charAt(j);
price += GetPrice(c1);
}
if (price < minp) {
minp = price;
}
}
if (budget + minps[i % 7] - minp >= 0) {
days++;
budget = budget - minp + minps[i % 7];
minps[i % 7] = minp;
}
else
break;
}

return days;
}

public static int GetPrice(char c) {
int price;
if (c >= '0' && c <= '9')
price = c - '0';
else if (c >= 'A' && c <= 'Z')
price = 10 + c - 'A';
else
price = 36 + c - 'a';
return price;
}
}

Sunday, March 16, 2014

Codeforces Round #223 (Div. 2) B Sereja and Stairs

// Codeforces Round #223 (Div. 2) B Sereja and Stairs
// Problem: http://codeforces.com/contest/381/problem/B

import java.io.*;
import java.math.*;
import java.util.*;

//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
private static boolean LOCAL_TEST = false;

private static void solve() throws IOException
{
int n = in.nextInt();
Integer[] x = new Integer[n];
for (int i = 0; i < x.length; i++) {
x[i] = in.nextInt();
}
Arrays.sort(x, Collections.reverseOrder());
ArrayList<Integer> list2 = new ArrayList<Integer>();
HashSet<Integer> set1 = new HashSet<Integer>();
list2.add(x[0]);
for (int i = 1; i < x.length; i++) {
if (x[i] < x[i - 1]) {
list2.add(x[i]);
}
else {
if (!x[i].equals(x[0])) {
set1.add(x[i]);
}
}
}
ArrayList<Integer> list1 = new ArrayList<Integer>();
list1.addAll(set1);
Collections.sort(list1);
list1.addAll(list2);
out.println(list1.size());
for (int i = 0; i < list1.size(); i++) {
if (i > 0)
out.print(" ");
out.print(list1.get(i));
}
out.println();
}

public static void main(String[] args) throws IOException {
// helpers for input/output
out = System.out;
try {
String cname = System.getenv("COMPUTERNAME");
if (!cname.equals(""))
LOCAL_TEST = true;
} catch (Exception e) {
}
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin.txt");
}
else {
boolean usingFileForIO = false;
if (usingFileForIO) {
// using input.txt and output.txt as I/O
in = new MyScanner("input.txt");
out = new PrintStream("output.txt");
}
else {
in = new MyScanner();
out = System.out;
}
}

solve();
}

// =====================================
static class MyScanner {
BufferedReader bufReader;
StringTokenizer strTok;

public MyScanner() throws IOException
{
bufReader = new BufferedReader(new InputStreamReader(System.in));
strTok = new StringTokenizer("");
}

public MyScanner(String inputFile) throws IOException {
bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(
inputFile)));
strTok = new StringTokenizer("");
}

String GetNextToken() throws IOException {
if (!strTok.hasMoreTokens())
strTok = new StringTokenizer(bufReader.readLine());
return strTok.nextToken();
}

public int nextInt() throws IOException {
return Integer.valueOf(GetNextToken());
}

public long nextLong() throws IOException {
return Long.valueOf(GetNextToken());
}

public double nextDouble() throws IOException {
return Double.valueOf(GetNextToken());
}

public String nextString() throws IOException {
return GetNextToken();
}

public String nextLine() throws IOException {
return bufReader.readLine();
}

public int countTokens() {
return strTok.countTokens();
}

public boolean hasMoreTokens() {
return strTok.hasMoreTokens();
}
}

}

Codeforces Round #223 (Div. 2) A Sereja and Dima

// Codeforces Round #223 (Div. 2) A Sereja and Dima
import java.io.*;
import java.math.*;
import java.util.*;

//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
private static boolean LOCAL_TEST = false;

private static void solve() throws IOException
{
int n = in.nextInt();
Integer[] x = new Integer[n];
for (int i = 0; i < x.length; i++) {
x[i] = in.nextInt();
}
int p1 = 0;
int p2 = 0;
int idxLeft = 0;
int idxRight = n - 1;
int i = 0;
while (idxLeft <= idxRight) {
int chosen;
if (x[idxLeft] > x[idxRight]) {
chosen = x[idxLeft];
idxLeft++;
}
else {
chosen = x[idxRight];
idxRight--;
}
if (i % 2 == 0)
p1 += chosen;
else
p2 += chosen;
i++;
}
out.print(p1);
out.print(" ");
out.print(p2);
}

public static void main(String[] args) throws IOException {
// helpers for input/output
out = System.out;
try {
String cname = System.getenv("COMPUTERNAME");
if (!cname.equals(""))
LOCAL_TEST = true;
} catch (Exception e) {
}
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin.txt");
}
else {
boolean usingFileForIO = false;
if (usingFileForIO) {
// using input.txt and output.txt as I/O
in = new MyScanner("input.txt");
out = new PrintStream("output.txt");
}
else {
in = new MyScanner();
out = System.out;
}
}

solve();
}

// =====================================
static class MyScanner {
BufferedReader bufReader;
StringTokenizer strTok;

public MyScanner() throws IOException
{
bufReader = new BufferedReader(new InputStreamReader(System.in));
strTok = new StringTokenizer("");
}

public MyScanner(String inputFile) throws IOException {
bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(
inputFile)));
strTok = new StringTokenizer("");
}

String GetNextToken() throws IOException {
if (!strTok.hasMoreTokens())
strTok = new StringTokenizer(bufReader.readLine());
return strTok.nextToken();
}

public int nextInt() throws IOException {
return Integer.valueOf(GetNextToken());
}

public long nextLong() throws IOException {
return Long.valueOf(GetNextToken());
}

public double nextDouble() throws IOException {
return Double.valueOf(GetNextToken());
}

public String nextString() throws IOException {
return GetNextToken();
}

public String nextLine() throws IOException {
return bufReader.readLine();
}

public int countTokens() {
return strTok.countTokens();
}

public boolean hasMoreTokens() {
return strTok.hasMoreTokens();
}
}

}

Codeforces Round #224 (Div. 2) C Arithmetic Progression

//Codeforces Round #224 (Div. 2) C Arithmetic Progression
import java.io.*;
import java.math.*;
import java.util.*;

//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
private static boolean LOCAL_TEST = false;

private static void solve() throws IOException
{
int n = in.nextInt();
int[] x = new int[n];
for (int i = 0; i < x.length; i++) {
x[i] = in.nextInt();
}
ArrayList<Integer> ansList = new ArrayList<Integer>();
if (n == 1) {
out.println(-1);
return;
}
else {
Arrays.sort(x);
int minDiff = Integer.MAX_VALUE;
if (n == 2) {
if ((x[1] - x[0]) % 2 == 0) {
minDiff = (x[1] - x[0]);
ansList.add(x[0] - minDiff);
if (minDiff > 0)
ansList.add(x[0] + minDiff / 2);
if (minDiff > 0)
ansList.add(x[1] + minDiff);
}
else {
minDiff = (x[1] - x[0]);
ansList.add(x[0] - minDiff);
ansList.add(x[1] + minDiff);
}
}
else {
HashSet<Integer> diffSet = new HashSet<Integer>();
for (int i = 1; i < x.length; i++) {
int diff = x[i] - x[i - 1];
diffSet.add(diff);
minDiff = Math.min(diff, minDiff);
}
if (diffSet.size() == 1) {
ansList.add(x[0] - minDiff);
if (minDiff > 0)
ansList.add(x[x.length - 1] + minDiff);
}
else if (diffSet.size() > 2) {
out.println(0);
return;
}
else {// if (diffSet.size() == 2) {
if (minDiff == 0) {
out.println(0);
return;
}

boolean added = false;
for (int i = 0; i < x.length - 1; i++) {
if (x[i + 1] - x[i] == minDiff)
continue;
else if (x[i + 1] - x[i] == 2 * minDiff) {
if (!added) {
ansList.add(x[i] + minDiff);
added = true;
}
else {
out.println(0);
return;
}
}
else {
out.println(0);
return;
}
}
}
}
}
out.println(ansList.size());
for (int i = 0; i < ansList.size(); i++) {
out.print(ansList.get(i));
out.print(" ");
}
}

public static void main(String[] args) throws IOException {
// helpers for input/output
out = System.out;
try {
String cname = System.getenv("COMPUTERNAME");
if (!cname.equals(""))
LOCAL_TEST = true;
} catch (Exception e) {
}
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin.txt");
}
else {
boolean usingFileForIO = false;
if (usingFileForIO) {
// using input.txt and output.txt as I/O
in = new MyScanner("input.txt");
out = new PrintStream("output.txt");
}
else {
in = new MyScanner();
out = System.out;
}
}

solve();
}

// =====================================
static class MyScanner {
BufferedReader bufReader;
StringTokenizer strTok;

public MyScanner() throws IOException
{
bufReader = new BufferedReader(new InputStreamReader(System.in));
strTok = new StringTokenizer("");
}

public MyScanner(String inputFile) throws IOException {
bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(
inputFile)));
strTok = new StringTokenizer("");
}

String GetNextToken() throws IOException {
if (!strTok.hasMoreTokens())
strTok = new StringTokenizer(bufReader.readLine());
return strTok.nextToken();
}

public int nextInt() throws IOException {
return Integer.valueOf(GetNextToken());
}

public long nextLong() throws IOException {
return Long.valueOf(GetNextToken());
}

public double nextDouble() throws IOException {
return Double.valueOf(GetNextToken());
}

public String nextString() throws IOException {
return GetNextToken();
}

public String nextLine() throws IOException {
return bufReader.readLine();
}

public int countTokens() {
return strTok.countTokens();
}

public boolean hasMoreTokens() {
return strTok.hasMoreTokens();
}
}

}

Friday, March 14, 2014

Codeforces Round #224 (Div. 2) A Ksenia and Pan Scales

// Codeforces Round #224 (Div. 2) A Ksenia and Pan Scales
import java.io.*;
import java.math.*;
import java.util.*;

//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
private static boolean LOCAL_TEST = false;

private static void solve() throws IOException
{
char[] s1 = in.nextLine().toCharArray();
char[] s2 = in.nextLine().toCharArray();
int nLeft = 0;
int nRight = -1;
for (int i = 0; i < s1.length; i++) {
if (s1[i] == '|')
nRight = 0;
else {
if (nRight >= 0)
nRight++;
else
nLeft++;
}
}
int larger = Math.max(nLeft, nRight);
int total = nLeft + nRight + s2.length;
if (total % 2 == 1)
out.println("Impossible");
else {
String res = "";
if (larger > total / 2)
out.println("Impossible");
else {
if (nLeft < total / 2) {
res += new String(s2).substring(0, total / 2 - nLeft);
}
res += new String(s1);
if (nRight < total / 2) {
res += new String(s2).substring(total / 2 - nLeft);
}
out.println(res);
}

}
}

public static void main(String[] args) throws IOException {
// helpers for input/output
out = System.out;
try {
String cname = System.getenv("COMPUTERNAME");
if (!cname.equals(""))
LOCAL_TEST = true;
} catch (Exception e) {
}
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin.txt");
}
else {
boolean usingFileForIO = false;
if (usingFileForIO) {
// using input.txt and output.txt as I/O
in = new MyScanner("input.txt");
out = new PrintStream("output.txt");
}
else {
in = new MyScanner();
out = System.out;
}
}

solve();
}

// =====================================
static class MyScanner {
BufferedReader bufReader;
StringTokenizer strTok;

public MyScanner() throws IOException
{
bufReader = new BufferedReader(new InputStreamReader(System.in));
strTok = new StringTokenizer("");
}

public MyScanner(String inputFile) throws IOException {
bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(
inputFile)));
strTok = new StringTokenizer("");
}

String GetNextToken() throws IOException {
if (!strTok.hasMoreTokens())
strTok = new StringTokenizer(bufReader.readLine());
return strTok.nextToken();
}

public int nextInt() throws IOException {
return Integer.valueOf(GetNextToken());
}

public long nextLong() throws IOException {
return Long.valueOf(GetNextToken());
}

public double nextDouble() throws IOException {
return Double.valueOf(GetNextToken());
}

public String nextString() throws IOException {
return GetNextToken();
}

public String nextLine() throws IOException {
return bufReader.readLine();
}

public int countTokens() {
return strTok.countTokens();
}

public boolean hasMoreTokens() {
return strTok.hasMoreTokens();
}
}

}

Codeforces Round #225 (Div. 2) C Milking cows

// Codeforces Round #225 (Div. 2) C Milking cows

import java.io.*;
import java.math.*;
import java.util.*;

//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
private static boolean LOCAL_TEST = false;

private static void solve() throws IOException
{
int n = in.nextInt();
int[] a = new int[n];
int num0 = 0;
int num1 = 0;
for (int i = 0; i < a.length; i++) {
a[i] = in.nextInt();
if (a[i] == 0)
num0++;
else
num1++;
}
int cnt0 = num0;
long minLost = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] == 0)
cnt0--;
else {
minLost += cnt0;
}
}
out.println(minLost);
}

public static void main(String[] args) throws IOException {
// helpers for input/output
out = System.out;
try {
String cname = System.getenv("COMPUTERNAME");
if (!cname.equals(""))
LOCAL_TEST = true;
} catch (Exception e) {
}
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin.txt");
}
else {
boolean usingFileForIO = false;
if (usingFileForIO) {
// using input.txt and output.txt as I/O
in = new MyScanner("input.txt");
out = new PrintStream("output.txt");
}
else {
in = new MyScanner();
out = System.out;
}
}

solve();
}

// =====================================
static class MyScanner {
BufferedReader bufReader;
StringTokenizer strTok;

public MyScanner() throws IOException
{
bufReader = new BufferedReader(new InputStreamReader(System.in));
strTok = new StringTokenizer("");
}

public MyScanner(String inputFile) throws IOException {
bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(
inputFile)));
strTok = new StringTokenizer("");
}

String GetNextToken() throws IOException {
if (!strTok.hasMoreTokens())
strTok = new StringTokenizer(bufReader.readLine());
return strTok.nextToken();
}

public int nextInt() throws IOException {
return Integer.valueOf(GetNextToken());
}

public long nextLong() throws IOException {
return Long.valueOf(GetNextToken());
}

public double nextDouble() throws IOException {
return Double.valueOf(GetNextToken());
}

public String nextString() throws IOException {
return GetNextToken();
}

public String nextLine() throws IOException {
return bufReader.readLine();
}

public int countTokens() {
return strTok.countTokens();
}

public boolean hasMoreTokens() {
return strTok.hasMoreTokens();
}
}

}

Thursday, March 13, 2014

Codeforces Round #225 (Div. 2) B Multitasking

// Codeforces Round #225 (Div. 2) B Multitasking

import java.io.*;
import java.math.*;
import java.util.*;

//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
private static boolean LOCAL_TEST = false;

private static void solve() throws IOException
{
int n = in.nextInt();
int m = in.nextInt();
int k = in.nextInt();
int[][] num = new int[n][m];
for (int y = 0; y < n; y++) {
for (int x = 0; x < m; x++) {
num[y][x] = in.nextInt();
}
}
out.println(m * (m - 1) / 2);
for (int y = 0; y < m; y++) {
for (int x = y + 1; x < m; x++) {
if (k == 0)
out.print("" + (y + 1) + " " + (x + 1));
else
out.print("" + (x + 1) + " " + (y + 1));
out.println();
}
}
}

public static void main(String[] args) throws IOException {
// helpers for input/output
out = System.out;
try {
String cname = System.getenv("COMPUTERNAME");
if (!cname.equals(""))
LOCAL_TEST = true;
} catch (Exception e) {
}
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin.txt");
}
else {
boolean usingFileForIO = false;
if (usingFileForIO) {
// using input.txt and output.txt as I/O
in = new MyScanner("input.txt");
out = new PrintStream("output.txt");
}
else {
in = new MyScanner();
out = System.out;
}
}

solve();
}

// =====================================
static class MyScanner {
BufferedReader bufReader;
StringTokenizer strTok;

public MyScanner() throws IOException
{
bufReader = new BufferedReader(new InputStreamReader(System.in));
strTok = new StringTokenizer("");
}

public MyScanner(String inputFile) throws IOException {
bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(
inputFile)));
strTok = new StringTokenizer("");
}

String GetNextToken() throws IOException {
if (!strTok.hasMoreTokens())
strTok = new StringTokenizer(bufReader.readLine());
return strTok.nextToken();
}

public int nextInt() throws IOException {
return Integer.valueOf(GetNextToken());
}

public long nextLong() throws IOException {
return Long.valueOf(GetNextToken());
}

public double nextDouble() throws IOException {
return Double.valueOf(GetNextToken());
}

public String nextString() throws IOException {
return GetNextToken();
}

public String nextLine() throws IOException {
return bufReader.readLine();
}

public int countTokens() {
return strTok.countTokens();
}

public boolean hasMoreTokens() {
return strTok.hasMoreTokens();
}
}

}