A小紫的均勢博弈
判斷輸入的 n 是奇數還是偶數
import java.io.*;
import java.math.*;
import java.util.*;public class Main {static IoScanner sc = new IoScanner();static final int mod=(int) (1e9+7);static void solve() throws IOException {int n=sc.nextInt();if(n%2==0) {dduoln("yukari");}else {dduoln("kou");}} public static void main(String[] args) throws Exception {int t = 1;
// t = sc.nextInt();while (t-- > 0) {solve();}}static <T> void dduo(T t){System.out.print(t);}static <T> void dduoln(){System.out.println("");}static <T> void dduoln(T t){System.out.println(t);}}class IoScanner {BufferedReader bf;StringTokenizer st;BufferedWriter bw;public IoScanner() {bf = new BufferedReader(new InputStreamReader(System.in));st = new StringTokenizer("");bw = new BufferedWriter(new OutputStreamWriter(System.out));}public String nextLine() throws IOException {return bf.readLine();}public String next() throws IOException {while (!st.hasMoreTokens()) {st = new StringTokenizer(bf.readLine());}return st.nextToken();}public char nextChar() throws IOException {return next().charAt(0);}public int nextInt() throws IOException {return Integer.parseInt(next());}public long nextLong() throws IOException {return Long.parseLong(next());}public double nextDouble() throws IOException {return Double.parseDouble(next());}public float nextFloat() throws IOException {return Float.parseFloat(next());}public BigInteger nextBigInteger() throws IOException {return new BigInteger(next());}public BigDecimal nextDecimal() throws IOException {return new BigDecimal(next());}
}
B小紫的劣勢博弈
排序 然后一人拿一個
import java.io.*;
import java.math.*;
import java.util.*;public class Main {static IoScanner sc = new IoScanner();static final int mod=(int) (1e9+7);static void solve() throws IOException {int n=sc.nextInt();long arr[]=new long[n];for(int i=0;i<n;i++) {arr[i]=sc.nextLong();}Arrays.sort(arr);long ans=0;for(int i=0;i<n;i++) {if(i%2==0) {ans+=arr[i];}else {ans+=arr[i]*(-1);}}dduoln(ans);} public static void main(String[] args) throws Exception {int t = 1;
// t = sc.nextInt();while (t-- > 0) {solve();}}static <T> void dduo(T t){System.out.print(t);}static <T> void dduoln(){System.out.println("");}static <T> void dduoln(T t){System.out.println(t);}}class IoScanner {BufferedReader bf;StringTokenizer st;BufferedWriter bw;public IoScanner() {bf = new BufferedReader(new InputStreamReader(System.in));st = new StringTokenizer("");bw = new BufferedWriter(new OutputStreamWriter(System.out));}public String nextLine() throws IOException {return bf.readLine();}public String next() throws IOException {while (!st.hasMoreTokens()) {st = new StringTokenizer(bf.readLine());}return st.nextToken();}public char nextChar() throws IOException {return next().charAt(0);}public int nextInt() throws IOException {return Integer.parseInt(next());}public long nextLong() throws IOException {return Long.parseLong(next());}public double nextDouble() throws IOException {return Double.parseDouble(next());}public float nextFloat() throws IOException {return Float.parseFloat(next());}public BigInteger nextBigInteger() throws IOException {return new BigInteger(next());}public BigDecimal nextDecimal() throws IOException {return new BigDecimal(next());}
}
C小紫的01串操作
通過觀察我們發現
操作 1001 等效于操作 101
操作 100010011000 等效于操作 101010
然后發現經過小紅和小紫操作
10 可以 10 -> 11
101 可以 101 -> 111
1010 可以 1010 -> 1110 -> 1111
10101 可以 10101 -> 10001 -> 11111
101010 不可以
...
所以只要判斷出現多少次 01 就可以
import java.io.*;
import java.math.*;
import java.util.*;public class Main {static IoScanner sc = new IoScanner();static final int mod=(int) (1e9+7);static void solve() throws IOException {String str=sc.next();int a=1;for(int i=1;i<str.length();i++) {if(str.charAt(i)!=str.charAt(i-1)) {a++;}}if(a<=5) {dduoln("Yes");}else {dduoln("No");}} public static void main(String[] args) throws Exception {int t = 1;t = sc.nextInt();while (t-- > 0) {solve();}}static <T> void dduo(T t){System.out.print(t);}static <T> void dduoln(){System.out.println("");}static <T> void dduoln(T t){System.out.println(t);}}class IoScanner {BufferedReader bf;StringTokenizer st;BufferedWriter bw;public IoScanner() {bf = new BufferedReader(new InputStreamReader(System.in));st = new StringTokenizer("");bw = new BufferedWriter(new OutputStreamWriter(System.out));}public String nextLine() throws IOException {return bf.readLine();}public String next() throws IOException {while (!st.hasMoreTokens()) {st = new StringTokenizer(bf.readLine());}return st.nextToken();}public char nextChar() throws IOException {return next().charAt(0);}public int nextInt() throws IOException {return Integer.parseInt(next());}public long nextLong() throws IOException {return Long.parseLong(next());}public double nextDouble() throws IOException {return Double.parseDouble(next());}public float nextFloat() throws IOException {return Float.parseFloat(next());}public BigInteger nextBigInteger() throws IOException {return new BigInteger(next());}public BigDecimal nextDecimal() throws IOException {return new BigDecimal(next());}
}
D小紫的優勢博弈
準備先暴力一發的 結果直接過了
10010
小紅操作會有 5 種情況 這等于 n
0010
010
10
1
我的想法是一個一個從頭開始遍歷這些字符串
統計 1 0 出現的次數
如果都出現偶數次 那么小紫獲勝
import java.io.*;
import java.math.*;
import java.util.*;// xixi?西
public class Main {static IoScanner sc = new IoScanner();static final int mod=(int) (1e9+7);static void solve() throws IOException {int n=sc.nextInt();String str=sc.next();int cnt=0;for(int i=1;i<n;i++) {int cnt0=0;int cnt1=0;for(int j=i;j<n;j++) {if(str.charAt(j)=='1')cnt1++;if(str.charAt(j)=='0')cnt0++;if(cnt0!=0||cnt1!=0) {if(cnt0%2==0&&cnt1%2==0) {cnt++;break;}}}}dduoln((double)cnt/(double)n);} public static void main(String[] args) throws Exception {int t = 1;
// t = sc.nextInt();while (t-- > 0) {solve();}}static <T> void dduo(T t){System.out.print(t);}static <T> void dduoln(){System.out.println("");}static <T> void dduoln(T t){System.out.println(t);}}class IoScanner {BufferedReader bf;StringTokenizer st;BufferedWriter bw;public IoScanner() {bf = new BufferedReader(new InputStreamReader(System.in));st = new StringTokenizer("");bw = new BufferedWriter(new OutputStreamWriter(System.out));}public String nextLine() throws IOException {return bf.readLine();}public String next() throws IOException {while (!st.hasMoreTokens()) {st = new StringTokenizer(bf.readLine());}return st.nextToken();}public char nextChar() throws IOException {return next().charAt(0);}public int nextInt() throws IOException {return Integer.parseInt(next());}public long nextLong() throws IOException {return Long.parseLong(next());}public double nextDouble() throws IOException {return Double.parseDouble(next());}public float nextFloat() throws IOException {return Float.parseFloat(next());}public BigInteger nextBigInteger() throws IOException {return new BigInteger(next());}public BigDecimal nextDecimal() throws IOException {return new BigDecimal(next());}
}
E小紫的線段染色
首先要知道的是 有符合的情況
一部分染紫色 一部分染紅色 等效于 一部分染紅色 一部分染紫色
我們可以將結構體排序
int arr []{起始 l,結束 r,當前次序 i}
按照 l 從小到大 r 從小到大次之
第一段為紅色 那么紅色的末尾為 arr[0][2]
然后 從第二段 進行遍歷
如果第二段的起始 l 小于紅色末尾
那必須染成紫色
但如果起始 l 小于紫色末尾
那就無法染成了(輸出-1 return)
如果 l 大于紫色末尾 則可以染成紫色 更新紫色末尾為第二段結束的 r
同時記錄當前 arr[][3]
繼續遍歷
如果第三段的起始 l 大于紅色末尾 更新紅色末尾為第二段結束的 r
...
這邊有個坑 就是
得 必須 選出一個染成紫色...
不能全是紅色
import java.util.*;
import java.io.*;
import java.math.*; public class Main {static IoScanner sc = new IoScanner();static final int mod=(int) (1e9+7);public static void main(String[] args) throws IOException {int n=sc.nextInt();List<Long []>list=new ArrayList<>();for(long i=0;i<n;i++) {long l=sc.nextLong();long r=sc.nextLong();list.add(new Long[] {l,r, i+1});}Collections.sort(list,(o1,o2)->{if(o1[0]==o2[0]) {return Long.compare(o1[1], o2[1]);}else {return Long.compare(o1[0], o2[0]);}});ArrayList<Long> anslist=new ArrayList<>();long hongend=list.get(0)[1];long ziend=-1;for(int i=1;i<n;i++) {long start=list.get(i)[0];long end=list.get(i)[1];if(start<=hongend) { // 要變成紫色if(ziend==-1) {// 直接賦值ziend=end;}else {if(start<=ziend) {dduoln("-1");return;}else {ziend=end;}}// 變成紫色anslist.add((list.get(i)[2]));}else {// 繼續紅色hongend=end;}}if(anslist.size()==0) {anslist.add((long) 1);}dduoln(anslist.size());Collections.sort(anslist);for(long i:anslist) {dduo(i+" ");}}static <T> void dduo(T t){System.out.print(t);}static <T> void dduoln(){System.out.println("");}static <T> void dduoln(T t){System.out.println(t);}
}class IoScanner {BufferedReader bf;StringTokenizer st;BufferedWriter bw;public IoScanner() {bf = new BufferedReader(new InputStreamReader(System.in));st = new StringTokenizer("");bw = new BufferedWriter(new OutputStreamWriter(System.out));}public String nextLine() throws IOException {return bf.readLine();}public String next() throws IOException {while (!st.hasMoreTokens()) {st = new StringTokenizer(bf.readLine());}return st.nextToken();}public char nextChar() throws IOException {return next().charAt(0);}public int nextInt() throws IOException {return Integer.parseInt(next());}public long nextLong() throws IOException {return Long.parseLong(next());}public double nextDouble() throws IOException {return Double.parseDouble(next());}public float nextFloat() throws IOException {return Float.parseFloat(next());}public BigInteger nextBigInteger() throws IOException {return new BigInteger(next());}public BigDecimal nextDecimal() throws IOException {return new BigDecimal(next());}
}
F小紫的樹上染色
import java.util.*;
import java.io.*;
import java.math.*; public class Main {static IoScanner sc = new IoScanner();static final int mod=(int) (1e9+7);static int n;static int k;// 鄰邊矩陣static List<List<Integer>> list;public static void main(String[] args) throws IOException {n = sc.nextInt(); k = sc.nextInt(); if(k==n) {dduoln("0");return;}list = new ArrayList<>();for (int i = 0; i <= n; i++) {list.add(new ArrayList<>());}for(int i=0;i<n-1;i++){ // 無向圖int u=sc.nextInt();int v=sc.nextInt();list.get(u).add(v);list.get(v).add(u);}int low = 1;int high = n;int ans=n;while (low <= high) {int mid = (low + high) >> 1;if (check(mid, k)) {ans = mid;high = mid - 1;} else {low = mid + 1;}}dduoln(ans); }static boolean check(int mid, int k) {int[] res = new int[n+5];int cnt = 0;Deque<Object[]> stack = new ArrayDeque<>();stack.push(new Object[]{1, -1, false});while (!stack.isEmpty()) {Object[] cur = stack.pop(); int u = (Integer) cur[0];int p = (Integer) cur[1];boolean judge = (Boolean) cur[2];if (judge==false) { // 未染色stack.push(new Object[]{u, p, true});List<Integer> children = new ArrayList<>();for (int v : list.get(u)) { // v鄰點 u當前頂點 p父頂點if (v != p) {children.add(v); }}
// for(Integer i:children) {
// dduo(i);
// }
// dduoln();Collections.reverse(children); for (int v : children) {stack.push(new Object[]{v, u, false});}} else { // 染色int sum = 0;for (int v : list.get(u)) { // v鄰點 u當前頂點 p父頂點if (v != p) {sum += res[v];}}if (sum + 1 > mid) {cnt++;res[u] = 0;} else {res[u] = sum + 1;}}}if(cnt <= k)return true;else return false;}static <T> void dduo(T t){System.out.print(t);}static <T> void dduoln(){System.out.println("");}static <T> void dduoln(T t){System.out.println(t);}
}class IoScanner {BufferedReader bf;StringTokenizer st;BufferedWriter bw;public IoScanner() {bf = new BufferedReader(new InputStreamReader(System.in));st = new StringTokenizer("");bw = new BufferedWriter(new OutputStreamWriter(System.out));}public String nextLine() throws IOException {return bf.readLine();}public String next() throws IOException {while (!st.hasMoreTokens()) {st = new StringTokenizer(bf.readLine());}return st.nextToken();}public char nextChar() throws IOException {return next().charAt(0);}public int nextInt() throws IOException {return Integer.parseInt(next());}public long nextLong() throws IOException {return Long.parseLong(next());}public double nextDouble() throws IOException {return Double.parseDouble(next());}public float nextFloat() throws IOException {return Float.parseFloat(next());}public BigInteger nextBigInteger() throws IOException {return new BigInteger(next());}public BigDecimal nextDecimal() throws IOException {return new BigDecimal(next());}
}