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取整不行、、
			}
		}

	}

}

猜你喜欢

转载自blog.csdn.net/qq_36706625/article/details/79893057