// Codeforces Round #241 (Div. 2) C Booking System
// Problem: http://codeforces.com/contest/416/problem/C
import java.io.*;
import java.math.*;
import java.util.*;
import org.omg.CORBA.Environment;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
private static boolean LOCAL_TEST = false;
public static class Visitor {
public int Size;
public int Money;
public int Index;
}
public static class VisComp implements Comparator<Visitor> {
@Override
public int compare(Visitor o1, Visitor o2) {
if (o1.Money == o2.Money)
return o1.Size - o2.Size;
else
return o2.Money - o1.Money;
}
}
public static class Table {
public int Index;
public int Size;
}
public static class TableComp implements Comparator<Table> {
@Override
public int compare(Table o1, Table o2) {
return o1.Size - o2.Size;
}
}
private static void solve() throws IOException
{
MainCodeforces1 main = new MainCodeforces1();
int nReq = in.nextInt();
ArrayList<Visitor> vis = new ArrayList<MainCodeforces1.Visitor>();
for (int i = 0; i < nReq; i++) {
Visitor v = new Visitor();
v.Size = in.nextInt();
v.Money = in.nextInt();
v.Index = i + 1;
vis.add(v);
}
Collections.sort(vis, new VisComp());
for (int i = 0; i < vis.size(); i++) {
// out.println("" + vis.get(i).Money + " - " + vis.get(i).Size);
}
int kTables = in.nextInt();
ArrayList<Table> tables = new ArrayList<MainCodeforces1.Table>();
boolean[] tblFilled = new boolean[kTables];
for (int i = 0; i < kTables; i++) {
Table tbl = new Table();
tbl.Index = i + 1;
tbl.Size = in.nextInt();
tables.add(tbl
);
}
Collections.sort(tables, new TableComp());
ArrayList<String> str = new ArrayList<String>();
int nAccepted = 0;
int sum = 0;
for (int i = 0; i < vis.size(); i++) {
Visitor v = vis.get(i);
for (int j = 0; j < tblFilled.length; j++) {
Table t = tables.get(j);
if (!tblFilled[j] && t.Size >= v.Size) {
nAccepted++;
sum += v.Money;
tblFilled[j] = true;
str.add("" + v.Index + " " + t.Index);
break;
}
}
}
out.println("" + nAccepted + " " + sum);
for (String s : str) {
out.println(s);
}
}
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();
}
}
}
Some solution examples of problems in topcoder, codeeval, google code jam, interviewstreet, onlinejudge-uva, codeforces, projecteuler, etc
Showing posts with label greedy. Show all posts
Showing posts with label greedy. Show all posts
Sunday, April 13, 2014
Codeforces Round #241 (Div. 2) B Art Union
// Codeforces Round #241 (Div. 2) B Art Union
// Problems: http://codeforces.com/contest/416/problem/B
import java.io.*;
import java.math.*;
import java.util.*;
import org.omg.CORBA.Environment;
//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 m = in.nextInt();
int n = in.nextInt();
int[] finish = new int[n + 1];
int[] ans = new int[m + 1];
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
int t = in.nextInt();
if (j == 1)
finish[j] += t;
else
finish[j] = Math.max(finish[j - 1], finish[j]) + t;
}
ans[i] = finish[n];
}
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= m; i++) {
if (i > 1)
sb.append(" ");
sb.append(ans[i]);
}
out.println(sb);
}
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();
}
}
}
// Problems: http://codeforces.com/contest/416/problem/B
import java.io.*;
import java.math.*;
import java.util.*;
import org.omg.CORBA.Environment;
//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 m = in.nextInt();
int n = in.nextInt();
int[] finish = new int[n + 1];
int[] ans = new int[m + 1];
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
int t = in.nextInt();
if (j == 1)
finish[j] += t;
else
finish[j] = Math.max(finish[j - 1], finish[j]) + t;
}
ans[i] = finish[n];
}
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= m; i++) {
if (i > 1)
sb.append(" ");
sb.append(ans[i]);
}
out.println(sb);
}
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();
}
}
}
Saturday, April 5, 2014
Codeforces Round #202 (Div. 2) B Color The Fence
// Codeforces Round #202 (Div. 2) B Color The Fence
// Problem: http://codeforces.com/contest/349/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 v = in.nextInt();
int[] a = new int[10];
int minA = Integer.MAX_VALUE;
int aa = 0;
for (int i = 1; i <= 9; i++) {
a[i] = in.nextInt();
if (a[i] <= minA) {
minA = a[i];
aa = i;
}
}
if (minA > v) {
out.println(-1);
return;
}
int nDigit = v / minA;
int[] res = new int[nDigit];
for (int i = 0; i < res.length; i++) {
res[i] = aa;
}
int rem = v - (nDigit * minA);
int pos = 0;
while (rem > 0) {
boolean replace = false;
for (int i = 9; i > aa; i--) {
int x = a[i] - minA;
if (rem >= x) {
rem = rem - x;
res[pos] = i;
replace = true;
break;
}
}
if (!replace)
break;
pos++;
if (pos == nDigit)
break;
}
StringBuilder sb = new StringBuilder(nDigit);
for (int i = 0; i < res.length; i++) {
sb.append(Integer.toString(res[i]));
}
out.println(sb.toString());
}
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();
}
}
}
// Problem: http://codeforces.com/contest/349/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 v = in.nextInt();
int[] a = new int[10];
int minA = Integer.MAX_VALUE;
int aa = 0;
for (int i = 1; i <= 9; i++) {
a[i] = in.nextInt();
if (a[i] <= minA) {
minA = a[i];
aa = i;
}
}
if (minA > v) {
out.println(-1);
return;
}
int nDigit = v / minA;
int[] res = new int[nDigit];
for (int i = 0; i < res.length; i++) {
res[i] = aa;
}
int rem = v - (nDigit * minA);
int pos = 0;
while (rem > 0) {
boolean replace = false;
for (int i = 9; i > aa; i--) {
int x = a[i] - minA;
if (rem >= x) {
rem = rem - x;
res[pos] = i;
replace = true;
break;
}
}
if (!replace)
break;
pos++;
if (pos == nDigit)
break;
}
StringBuilder sb = new StringBuilder(nDigit);
for (int i = 0; i < res.length; i++) {
sb.append(Integer.toString(res[i]));
}
out.println(sb.toString());
}
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();
}
}
}
Sunday, March 30, 2014
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();
}
}
}
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();
}
}
}
Friday, March 21, 2014
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) 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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
Wednesday, September 4, 2013
Codeforces Round #197 (Div. 2) B Xenia and Ringroad
// Codeforces Round #197 (Div. 2) B Xenia and Ringroad
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();
long time = 0;
int[] a = new int[m];
for (int i = 0; i < a.length; i++) {
a[i] = in.nextInt();
}
for (int i = 0; i < a.length; i++) {
if (i == 0)
time += a[i] - 1;
else {
if (a[i] >= a[i - 1])
time += a[i] - a[i - 1];
else
time += (n - a[i - 1]) + a[i];
}
}
out.println(time);
}
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();
}
}
}
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();
long time = 0;
int[] a = new int[m];
for (int i = 0; i < a.length; i++) {
a[i] = in.nextInt();
}
for (int i = 0; i < a.length; i++) {
if (i == 0)
time += a[i] - 1;
else {
if (a[i] >= a[i - 1])
time += a[i] - a[i - 1];
else
time += (n - a[i - 1]) + a[i];
}
}
out.println(time);
}
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, July 26, 2013
Topcoder SRM 582 DIV 2 L2 SpaceWarDiv2
// Topcoder SRM 582 DIV 2 L2 SpaceWarDiv2
import java.util.*;
import java.math.*;
//tc; rename the class name before submitting
public class SpaceWarDiv2 {
public static void main(String[] args) {
SpaceWarDiv2 obj = new SpaceWarDiv2();
System.out.println(
obj.minimalFatigue(
new int[] { 1, 8, 5, 2, 10, 8, 2 },
new int[] { 1, 7, 1, 1, 5, 3, 1 },
new int[] { 20, 20, 20, 20, 20, 20, 20 }
));
}
public int minimalFatigue(int[] magicalGirlStrength, int[] enemyStrength,
int[] enemyCount) {
HashMap<Integer, Integer> enemyCnt = new HashMap<Integer, Integer>();
for (int i = 0; i < enemyStrength.length; i++) {
int strength = enemyStrength[i];
int cnt = enemyCount[i];
if (enemyCnt.containsKey(strength))
enemyCnt.put(strength, enemyCnt.get(strength) + cnt);
else
enemyCnt.put(strength, cnt);
}
ArrayList<Integer> strengthList = new ArrayList<Integer>(enemyCnt.keySet());
Collections.sort(strengthList, Collections.reverseOrder());
int N = magicalGirlStrength.length;
Arrays.sort(magicalGirlStrength);
int[] fatigue = new int[N];
if (strengthList.get(0) > magicalGirlStrength[N - 1])
return -1;
int maxFatigue = -1;
for (int i = 0; i < strengthList.size(); i++) {
int st = strengthList.get(i);
int cnt = enemyCnt.get(st);
while (cnt > 0) {
boolean found = false;
for (int j = N - 1; j >= 0; j--) {
if (cnt == 0)
break;
if (st > magicalGirlStrength[j])
continue;
else {
if (fatigue[j] < maxFatigue || maxFatigue == -1) {
found = true;
fatigue[j]++;
cnt--;
maxFatigue = Math.max(maxFatigue, fatigue[j]);
}
}
}
if (!found && cnt > 0) {
for (int j = N - 1; j >= 0; j--) {
if (cnt == 0)
break;
if (st > magicalGirlStrength[j])
continue;
else {
if (fatigue[j] == maxFatigue || maxFatigue == -1) {
found = true;
fatigue[j]++;
cnt--;
maxFatigue = Math.max(maxFatigue, fatigue[j]);
break;
}
}
}
}
}
}
return maxFatigue;
}
}
import java.util.*;
import java.math.*;
//tc; rename the class name before submitting
public class SpaceWarDiv2 {
public static void main(String[] args) {
SpaceWarDiv2 obj = new SpaceWarDiv2();
System.out.println(
obj.minimalFatigue(
new int[] { 1, 8, 5, 2, 10, 8, 2 },
new int[] { 1, 7, 1, 1, 5, 3, 1 },
new int[] { 20, 20, 20, 20, 20, 20, 20 }
));
}
public int minimalFatigue(int[] magicalGirlStrength, int[] enemyStrength,
int[] enemyCount) {
HashMap<Integer, Integer> enemyCnt = new HashMap<Integer, Integer>();
for (int i = 0; i < enemyStrength.length; i++) {
int strength = enemyStrength[i];
int cnt = enemyCount[i];
if (enemyCnt.containsKey(strength))
enemyCnt.put(strength, enemyCnt.get(strength) + cnt);
else
enemyCnt.put(strength, cnt);
}
ArrayList<Integer> strengthList = new ArrayList<Integer>(enemyCnt.keySet());
Collections.sort(strengthList, Collections.reverseOrder());
int N = magicalGirlStrength.length;
Arrays.sort(magicalGirlStrength);
int[] fatigue = new int[N];
if (strengthList.get(0) > magicalGirlStrength[N - 1])
return -1;
int maxFatigue = -1;
for (int i = 0; i < strengthList.size(); i++) {
int st = strengthList.get(i);
int cnt = enemyCnt.get(st);
while (cnt > 0) {
boolean found = false;
for (int j = N - 1; j >= 0; j--) {
if (cnt == 0)
break;
if (st > magicalGirlStrength[j])
continue;
else {
if (fatigue[j] < maxFatigue || maxFatigue == -1) {
found = true;
fatigue[j]++;
cnt--;
maxFatigue = Math.max(maxFatigue, fatigue[j]);
}
}
}
if (!found && cnt > 0) {
for (int j = N - 1; j >= 0; j--) {
if (cnt == 0)
break;
if (st > magicalGirlStrength[j])
continue;
else {
if (fatigue[j] == maxFatigue || maxFatigue == -1) {
found = true;
fatigue[j]++;
cnt--;
maxFatigue = Math.max(maxFatigue, fatigue[j]);
break;
}
}
}
}
}
}
return maxFatigue;
}
}
Subscribe to:
Posts (Atom)