Codeforces Hello 2024 (1919)A-C JAVA题解

本文描述了一个两人钱包游戏中,爱丽丝和鲍勃交替移除硬币并可能交换钱包的游戏规则。当两人策略最优时,分析了如何通过计算绝对值之和乘以长度来确定胜者。
摘要由CSDN通过智能技术生成

A - Wallet Exchange

题目

Alice and Bob are bored, so they decide to play a game with their wallets. Alice has $a$ coins in her wallet, while Bob has $b$ coins in his wallet.

Both players take turns playing, with Alice making the first move. In each turn, the player will perform the following steps

**in order**:

1.  Choose to exchange wallets with their opponent, or to keep their current wallets.
2.  Remove $1$ coin from the player's current wallet. The current wallet cannot have $0$ coins before performing this step.

The player who cannot make a valid move on their turn loses. If both Alice and Bob play optimally, determine who will win the game.

Input

Each test contains multiple test cases. The first line contains a single integer t— the number of test cases. The description of the test cases follows.

The first and only line of each test case contains two integers $a$ and $b$ ($1 \le a, b \le 10^9) — the number of coins in Ali

Output

For each test case, output "Alice" if Alice will win the game, and "Bob" if Bob will win the game.

ce's and Bob's wallets, respectively.

翻译

爱丽丝和鲍勃很无聊,于是他们决定用自己的钱包玩一个游戏。爱丽丝的钱包里有 a 枚硬币,而鲍勃的钱包里有 b 枚硬币。

双方轮流玩,由爱丽丝先走棋。在每个回合中,玩家将按**顺序执行以下步骤:

1.  选择与对手交换钱包,或保留现有钱包。
2.  从玩家当前钱包中取出 1 个硬币。在执行此步骤之前,当前钱包中不能有 0 枚硬币。

无法在自己的回合中做出有效举动的玩家输。如果爱丽丝和鲍勃都以最佳方式下棋,则决定谁将赢得游戏。

题解

直接将两个钱包看成一个然后判断奇偶


import java.util.Arrays;
import java.util.Scanner;
public class Main {


	public static void main(String[] args) throws IOException {
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
	while((n--)>0) {
		Long a,b;
		a=sc.nextLong();
		b=sc.nextLong();
		if((a+b)%2==0)System.out.println("Bob");
		else System.out.println("Alice");
	}
	}
}

 B - Plus-Minus Split

题目

You are given a string $s$ of length $n$ consisting of characters "+" and "-" s represents an array $a$ of length n defined by

You will do the following process to calculate your penalty:

1.  Split $a$ into non-empty arrays $b_1,b_2,\ldots,b_k$ such that $b_1+b_2+\ldots+b_k=a^\dagger$, where $+$ denotes array concatenation.
2.  The penalty of a single array is the absolute value of its sum multiplied by its length. In other words, for some array $c$ of length $m$, its penalty is calculated as $p(c)=|c_1+c_2+\ldots+c_m| \cdot m$.
3.  The total penalty that you will receive is $p(b_1)+p(b_2)+\ldots+p(b_k)$.

If you perform the above process optimally, find the minimum possible penalty you will receive.

dagger$Some valid ways to split

a=[3,1,4,1,5]into (b1,b2,…,bk)are ([3],[1],[4],[1],[5]), ([3,1],[4,1,5])and ([3,1,4,1,5])while some invalid ways to split a are ([3,1],[1,5])([3],[],[1,4],[1,5]) and ([3,4],[5,1,1])

翻译

题解

统计“+”和“-”的个数求差值


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main {


	public static void main(String[] args) throws IOException {
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
	while((n--)>0) {
		Long a;
		a=sc.nextLong();
		String s;
		s=sc.next();
	  int op=0;
	  int up=0;
	for(int i=0;i<a;i++) {
	if(s.charAt(i)=='-')op++;
	else up++;
		
	}
	System.out.println(Math.abs(op-up));
	}
	}
}

C - Grouping Increases 

题目

You are given an array $a$ of size $n$. You will do the following process to calculate your penalty:

1.  Split array $a$ into two (possibly empty) subsequences s and t such that every element of $a$ is either in s or t.
2.  For an array $b$ of size $m$, define the penalty $p(b)$ of an array $b$ as the number of indices $i$ between 1 and m - 1 where  bi<bi+1.
3.  The total penalty you will receive is p(s) + p(t)

If you perform the above process optimally, find the minimum possible penalty you will receive.

A sequence x is a subsequence of a sequence y if x can be obtained from y by the deletion of several (possibly, zero or all) elements.

 Some valid ways to split array  a=[3,1,4,1,5]into (s,t) are ([3,4,1,5],[1])([1,1],[3,4,5])and ([],[3,1,4,1,5]) while some invalid ways to split a are ([3,4,5],[1]), ([3,1,4,1],[1,5])and ([1,3,4],[5,1])

翻译

题解

贪心,数组分成两个子序列,让连续上升子序列数最小

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main {

static int []a=new int [200005];
	public static void main(String[] args) throws IOException {
		Scanner sc=new Scanner(System.in);
		int t=sc.nextInt();
	while((t--)>0) {
	int n=sc.nextInt();
	for(int i=0;i<n;i++) {
		a[i]=sc.nextInt();
		
	}
	int b=0x3fffffff;
	int c=0x3fffffff;
	int ans=0;
	for(int i=0;i<n;i++) {
		if(b>c) {int x=b;b=c;c=x;};
		if(a[i]<=b)b=a[i];
		else if(a[i]<=c)c=a[i];
		else {b=a[i];ans++;}
	}
	System.out.println(ans);
	}
	}
	
}
  • 24
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值