Showing posts with label counting. Show all posts
Showing posts with label counting. Show all posts

Thursday, January 30, 2014

Codeforces Round #218 (Div. 2) A K-Periodic Array

// Codeforces Round #218 (Div. 2) A K-Periodic Array

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 k = in.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
int ngroup = n / k;
int ans;
if (ngroup == 1)
ans = 0;
else {
ans = 0;
for (int i = 0; i < k; i++) {
int num1 = 0;
int num2 = 0;
for (int j = 0; j < ngroup; j++) {
if (a[j * k + i] == 1)
num1++;
else
num2++;
}
ans += Math.min(num1, num2);
}
}
out.println(ans);
}

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, September 20, 2013

Topcoder SRM 589 DIV 2 L1 GooseTattarrattatDiv2

// Topcoder SRM 589 DIV 2 L1 GooseTattarrattatDiv2

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

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

public int getmin(String S) {
int[] cnt = new int[26];
for (int i = 0; i < S.length(); i++) {
int c = S.charAt(i) - 'a';
cnt[c] += 1;
}
int maxCnt = 0;
for (int i = 0; i < cnt.length; i++) {
maxCnt = Math.max(maxCnt, cnt[i]);
}
return S.length() - maxCnt;
}
}

Saturday, September 14, 2013

Topcoder SRM 585 DIV 2 L1 LISNumberDivTwo

// Topcoder SRM 585 DIV 2 L1 LISNumberDivTwo

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

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

public int calculate(int[] seq) {
int cnt = 1;
for (int i = 1; i < seq.length; i++) {
if (seq[i] <= seq[i - 1])
cnt++;
}
return cnt;
}
}

Friday, July 5, 2013

Codeforces Round #190 (Div. 2) B Ciel and Flowers

// Codeforces Round #190 (Div. 2) B Ciel and Flowers

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 r = in.nextInt();
int g = in.nextInt();
int b = in.nextInt();
int[] rgb = new int[] { r, g, b };
Arrays.sort(rgb);
int cnt = rgb[0];
for (int j = 0; j < rgb.length; j++) {
rgb[j] -= cnt;
}
if (rgb[1] % 3 == 2 && rgb[2] % 3 == 2 && cnt > 0) {
cnt -= 1;
rgb[1] += 1;
rgb[2] += 1;
}
cnt += rgb[1] / 3;
cnt += rgb[2] / 3;
out.println(cnt);
}

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, April 17, 2013

Google Code Jam 2009, Qualification Round C Welcome to Code Jam

//Google Code Jam 2009, Qualification Round     C Welcome to Code Jam
import java.awt.Frame;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;

//Google Code Jam
public class GoogleCode1 {
    private static MyScanner in;
    private static PrintStream out;

    private static void solve() throws IOException
    {
        int C = in.nextInt();
        in.nextLine();
        for (int i = 0; i < C; i++) {
            out.print("Case #" + (i + 1) + ": ");
            solveCase();
        }
    }

    static String swcj = "welcome to code jam";
    static char[] wcj;

    private static void solveCase() throws IOException {
        wcj = swcj.toCharArray();
        String stext = in.nextLine();
        char[] cstext = stext.toCharArray();
        int wlen = wcj.length;
        int slen = cstext.length;
        int[][] f = new int[wlen][slen];
        for (int c = 0; c < wlen; c++) {
            for (int d = 0; d < slen; d++) {
                int left = 0;
                int above = 0;
                if (c > 0) {
                    left = f[c - 1][d];
                }
                if (d > 0) {
                    above = f[c][d - 1];
                }

                if (wcj[c] == cstext[d]) {
                    if (c > 0)
                        f[c][d] = left + above;
                    else
                        f[c][d] = left + above + 1;
                }
                else {
                    f[c][d] = above;
                }
                f[c][d] = f[c][d] % 10000;
            }
        }

        int cnt = f[wlen - 1][slen - 1];
        String result = String.format("%04d", cnt % 10000);
        out.println(result);
    }

    public static void main(String[] args) throws IOException {        // helpers for input/output
        boolean usingFileForIO = true;
        if (usingFileForIO) {
            // using input.txt and output.txt as I/O
            in = new MyScanner("E:\\zin.txt");
            out = new PrintStream("E:\\zout.txt");
        }
        else {
            in = new MyScanner();
            out = System.out;
        }

        solve();
    }

    // =====================================
    static class MyScanner {
        Scanner inp = null;

        public MyScanner() throws IOException
        {
            inp = new Scanner(System.in);
        }

        public MyScanner(String inputFile) throws IOException {
            inp = new Scanner(new FileInputStream(inputFile));
        }

        public int nextInt() throws IOException {
            return inp.nextInt();
        }

        public long nextLong() throws IOException {
            return inp.nextLong();
        }

        public double nextDouble() throws IOException {
            return inp.nextDouble();
        }

        public String nextString() throws IOException {
            return inp.next();
        }

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

}