1. 程式人生 > >A、B、C、D、Educational Codeforces Round 42 (Rated for Div. 2)

A、B、C、D、Educational Codeforces Round 42 (Rated for Div. 2)

Educational Codeforces Round 42 (Rated for Div. 2)  http://codeforces.com/contest/962

A:Equator

這裡需要注意一個問題,就是直接 / 2 的時候、有奇數偶數的情況、如果是奇數的話、這個答案就是錯誤的、

所以我們反過來求解、用當前得數 * 2  如果大於,輸出,不能繼續疊加、

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;

public class A {
	static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
	static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));

	public static int nextInt() throws IOException {
		in.nextToken();
		return (int) in.nval;
	}

	public static String next() throws IOException {
		in.nextToken();
		return (String) in.sval;
	}

	public static void main(String[] args) throws IOException {
		int n = nextInt();
		int a[] = new int[n];
		long sum = 0;
		for(int i = 0; i < n; i++) {
			a[i] = nextInt();
			sum += a[i];
		}
		
		long half = 0;
		int i = 0;
		while(i < n && 2 * half < sum) {
			half += a[i++];
		}
		out.println(i);
		out.flush();
	}

}

B:Students in Railway Carriage

打比賽的時候、我是用額外的陣列處理空閒的部分、然後、判斷第一個、最後一個有沒有 *、結果太多、處理的太複雜、warry

結束後、隊友給我講了他的思路:

線上求解:用一個變數記錄當前空位的次數、遇到 * 處理一下,記錄變數歸零、注意一下、是人多還是座位多的情況、

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Scanner;

public class B {

	static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
	static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));

	public static int nextInt() throws IOException {
		in.nextToken();
		return (int) in.nval;
	}

	public static String next() throws IOException {
		in.nextToken();
		return (String) in.sval;
	}

	public static void main(String[] args) throws IOException {
		Scanner input = new Scanner(System.in);
		int n, a, b;
		n = input.nextInt();
		a = input.nextInt();
		b = input.nextInt();
		String str = input.next(); // 輸入字串 、next() 輸入是空串、(next不能讀入字元、只能輸入字母)
		
		long ans = 0; // 記錄能坐的學生的個數、、
		int empty = 0; // 記錄空閒的位置,
		int max = Math.max(a, b);
		int min = Math.min(a, b);
		for(int i = 0; i < str.length(); i++) {
			int tmp = Math.max(max, min);
			min = Math.min(max, min);
			max = tmp;
			
			if(str.charAt(i) == '.') { // 可以放學生、 坐下
				empty++;
			}else { // *號 、 結算、
				if((empty & 1) == 1) {  // 奇數、 大的能多放一個、
					ans += Math.min(empty / 2 + 1, max); // 座位多還是人多
					ans += Math.min(min, empty / 2);
					
					max = Math.max(0, max - empty / 2 - 1);
					min = Math.max(0, min - empty / 2);
				}else { // 偶數、、
					ans += Math.min(empty / 2, max); // 座位多還是人多
					ans += Math.min(empty / 2, min);
					
					max = Math.max(0, max - empty / 2);
					min = Math.max(0, min - empty / 2);
				}
				empty = 0;
			}
		}
		// 結算最後一個的、、 一直沒有 * 號的、
		if((empty & 1) == 1) {  // 奇數、 大的能多放一個、
			ans += Math.min(empty / 2 + 1, max);
			ans += Math.min(min, empty / 2);
			
			max = Math.max(0, max - empty / 2 - 1);
			min = Math.max(0, min - empty / 2);
		}else { // 偶數、、
			ans += Math.min(empty / 2, max);
			ans += Math.min(empty / 2, min);
			
			max = Math.max(0, max - empty / 2);
			min = Math.max(0, min - empty / 2);
		}
		out.println(ans);
		out.flush();
		input.close();
	}
	

}

C:Make a Square

處理出刪除0,1,2,,,,,n - 1 個字元的字串、天災在HaseSet集合中,(好像集合是什麼無所謂)

一個字串轉化為陣列、 Math.sqrt 開方一下、強制轉化為(int型別),比較兩個是否相等、以及是否為0

是的話、變數max記錄 更新變數已經這個刪除字元的個數和當前max的最大的、處理完所有的結果後,

輸出原字元 - max 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.HashSet;
import java.util.Scanner;

public class C {
	static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
	static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));

	public static int nextInt() throws IOException {
		in.nextToken();
		return (int) in.nval;
	}

	public static String next() throws IOException {
		in.nextToken();
		return (String) in.sval;
	}
	
	static HashSet<String> st = new HashSet<>();
	static int min;
	public static void main(String[] args) throws IOException {
		Scanner input = new Scanner(System.in);
		String s = input.next(); // next()  不能以字串的形式讀入數字
		int n = s.length();
		int max = -1;
		subsequence(s);
		
		for (String ss : st) {
			StringBuffer b = new StringBuffer(ss);
			for(int i = 0; i < b.length(); i++) {
				if(b.charAt(i) == '0' && b.length() > 1) {
					b.delete(i, i + 1);
					i--;
				}else {
					break;
				}
			}
			
			double d1 = Math.sqrt(Double.parseDouble(b.toString()));
			int d2 = (int)d1;
			if(d1 == d2 && d1 != 0) {
				max = Math.max(max, b.length());
			}
		}
		out.println(max == -1 ? -1 : n - max);
		out.flush();
		input.close();
	}
	
	
	public static void subsequence(String str) {  // 獲取的可能性、
		for(int i = 0;i < str.length(); i++) {
			for(int j = str.length(); j > i; j--) {
				String sub_str = str.substring(i, j);
				if(!st.contains(sub_str)) { // 子字串不在裡面、
					st.add(sub_str);
				}
				
				for(int k = 1; k < sub_str.length() - 1; k++) {
					StringBuffer sb = new StringBuffer(sub_str);
					
					sb.deleteCharAt(k);
					if(!st.contains(sb.toString())) {
						subsequence(sb.toString());
					}
				}
			}
		}
	}

}

D:Merge a Square

用集合來處理,定義一個類,存輸入的數和下標,存入優先佇列中,每次取優先佇列中兩個最小的判斷是否相等、相等,合併,返回優先佇列中,不相等,(小的就處理完畢了,大的放回優先佇列進行下一輪的處理)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.TreeSet;

public class D {
	static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
	static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));

	public static int nextInt() throws IOException {
		in.nextToken();
		return (int) in.nval;
	}

	public static String next() throws IOException {
		in.nextToken();
		return (String) in.sval;
	}

	public static void main(String[] args) throws IOException {
		PriorityQueue<Node> Q = new PriorityQueue<>();
		int n = nextInt();
		for (int i = 0; i < n; i++) {
			Q.offer(new Node(nextInt(), i)); // 輸入新增在優先佇列中、、
		}
		TreeSet<Node> ans = new TreeSet<>();
		while (true) {
			Node node1 = new Node(0, 0), node2 = new Node(0, 0);

			if (Q.size() > 0) {
				node1 = Q.poll();
			} else {
				break;
			}

			if (Q.size() > 0) {
				node2 = Q.poll();
			} else {
				ans.add(node1);
				break;
			}

			if (node1.value == node2.value) {
				node2.value *= 2L;
				Q.offer(node2);
			} else {
				ans.add(node1);
				Q.offer(node2);
			}
		}

		long res[] = new long[n];
		for (Node node : ans) {
			res[node.index] = node.value;
		}

		out.println(ans.size());
		for (int i = 0; i < n; i++) {
			if (res[i] > 0) {
				out.print(res[i] + " ");
			}
		}
		out.println();
		out.flush();
	}

	static class Node implements Comparable<Node> {
		long value;
		int index;

		public Node() {
		}

		public Node(long value, int index) {
			this.value = value;
			this.index = index;
		}

		@Override
		public int compareTo(Node other) { // 按照值的大小,由小到大排序,相等比較下標
			if (this.value == other.value) {
				return this.index - other.index;
			} else {
				return (int) Math.round((this.value - other.value));  // 就是這裡出了問題、、直接int取整不行、、
			}
		}

	}

}