Codeforces Beta Round #3

本文详细解析了 CodeForces 平台上的四道题目:国王最短路径、货车装载、井字游戏和最小成本括号序列,提供了完整的代码实现和解题思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A - Shortest path of the king

CodeForces - 3A

The king is left alone on the chessboard. In spite of this loneliness, he doesn’t lose heart, because he has business of national importance. For example, he has to pay an official visit to square t. As the king is not in habit of wasting his time, he wants to get from his current position s to square t in the least number of moves. Help him to do this.
在这里插入图片描述
In one move the king can get to the square that has a common side or a common vertex with the square the king is currently in (generally there are 8 different squares he can move to).

Input

The first line contains the chessboard coordinates of square s, the second line — of square t.

Chessboard coordinates consist of two characters, the first one is a lowercase Latin letter (from a to h), the second one is a digit from 1 to 8.

Output

In the first line print n — minimum number of the king’s moves. Then in n lines print the moves themselves. Each move is described with one of the 8: L, R, U, D, LU, LD, RU or RD.

L, R, U, D stand respectively for moves left, right, up and down (according to the picture), and 2-letter combinations stand for diagonal moves. If the answer is not unique, print any of them.

Examples

Input

a8
h1

Output

7
RD
RD
RD
RD
RD
RD
RD

感觉这道题还是挺水的,直接根据两个位置的坐标判断即可。
一开始while循环的条件||写成了&&,错了一次。

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

char arr[1005];

int main(void)
{
	char a, b, c, d;
	int index = 0, cnt = 0;//存储的字符数、走的步数
	scanf("%c%c", &a, &b);
	getchar();
	scanf("%c%c", &c, &d);
	getchar();
	while (a != c || b != d)
	{
		if (a < c)
		{
			arr[index++] = 'R';
			a++;
		}
		else if (a > c)
		{
			arr[index++] = 'L';
			a--;
		}
		if (b < d)
		{
			arr[index++] = 'U';
			b++;
		}
		else if (b > d)
		{
			arr[index++] = 'D';
			b--;
		}
		arr[index++] = '\n';
		cnt++;
	}
	printf("%d\n", cnt);
	for(int i = 0; i < index; i++)
		printf("%c", arr[i]);
	return 0;
}

B - Lorry

CodeForces - 3B

A group of tourists is going to kayak and catamaran tour. A rented lorry has arrived to the boat depot to take kayaks and catamarans to the point of departure. It’s known that all kayaks are of the same size (and each of them occupies the space of 1 cubic metre), and all catamarans are of the same size, but two times bigger than kayaks (and occupy the space of 2 cubic metres).

Each waterborne vehicle has a particular carrying capacity, and it should be noted that waterborne vehicles that look the same can have different carrying capacities. Knowing the truck body volume and the list of waterborne vehicles in the boat depot (for each one its type and carrying capacity are known), find out such set of vehicles that can be taken in the lorry, and that has the maximum total carrying capacity. The truck body volume of the lorry can be used effectively, that is to say you can always put into the lorry a waterborne vehicle that occupies the space not exceeding the free space left in the truck body.

Input

The first line contains a pair of integer numbers n and v (1 ≤ n ≤ 10^5; 1 ≤ v ≤ 10^9), where n is the number of waterborne vehicles in the boat depot, and v is the truck body volume of the lorry in cubic metres. The following n lines contain the information about the waterborne vehicles, that is a pair of numbers ti, pi (1 ≤ ti ≤ 2; 1 ≤ pi ≤ 10^4), where ti is the vehicle type (1 – a kayak, 2 – a catamaran), and pi is its carrying capacity. The waterborne vehicles are enumerated in order of their appearance in the input file.

Output

In the first line print the maximum possible carrying capacity of the set. In the second line print a string consisting of the numbers of the vehicles that make the optimal set. If the answer is not unique, print any of them.

Examples

Input

3 2
1 2
2 7
1 3

Output

7
2

先对大小为1和2的船分别进行排序,再取所有能取的1船,然后再依次判断,如果一个2船的容量更大的话就把1船换成2船,可以是一个2船换两个1船(没有空位置了且一个2船大于两个1船)、一个2船换一个1船(有一个空位置且一个2船大于一个1船)、一个2船换一个2船(有没有空位置均可且一个2船大于一个2船)。
感觉我不适合做这种题,错误不好找啊,错了无数次之后终于过了

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 1e5 + 5;

typedef struct
{
	int pi;
	int id;
}ship;

bool cmp(ship a, ship b)
{
	return a.pi > b.pi;
}

ship arr1[N], arr2[N];
int ID[2 * N];

int main(void)
{
	int n, v, t, p; 
	int now, num1, num2, ans;//目前占用的空间大小,目前使用的两种船的数量,最大的容量 
	int index1, index2;//给出的两种船的数量 
	index1 = index2 = 0;
	scanf("%d%d", &n, &v);
	for (int i = 0; i < n; i++)
	{
		scanf("%d%d", &t, &p);
		if (t == 1)
		{
			arr1[index1].pi = p;
			arr1[index1].id = i + 1;
			index1++;
		}
		else
		{
			arr2[index2].pi = p;
			arr2[index2].id = i + 1;
			index2++;
		}
	}
	sort(arr1, arr1 + index1, cmp);
	sort(arr2, arr2 + index2, cmp);
	now = num1 = num2 = ans = 0;
	for (int i = 0; i < index1; i++)//先装1的 
	{
		if (now == v)//装满了 
			break;
		ans += arr1[i].pi;
		now++;
		ID[num1++] = arr1[i].id;
	}
	for (int i = 0; i < index2; i++)//再装2的 
	{
		if (v - now < 2)//不能再装了 
			break;
		ans += arr2[i].pi;
		now += 2;
		ID[num1 + num2] = arr2[i].id;
		num2++;
	}
	int num = num1 + num2;
	for (int i = num2; i < index2; i++)
	{
		if (v - now == 0 && num1 >= 2 && arr2[i].pi > arr1[num1 - 1].pi + arr1[num1 - 2].pi)//一个2换两个1 
		{
			ans += arr2[i].pi - (arr1[num1 - 1].pi + arr1[num1 - 2].pi);
			ID[num++] = arr2[i].id;
			ID[num1 - 1] = ID[num1 - 2] = 0;
			num1 -= 2;
			num2++;
		}
		else if (v - now == 1 && num1 >= 1 && arr2[i].pi > arr1[num1 - 1].pi)// 一个2换一个1 
		{
			ans += arr2[i].pi - arr1[num1 - 1].pi;
			ID[num1 - 1] = arr2[i].id;
			now++;
			num1--;
			num2++;
		}
		else if (num2 >= 1 && arr2[i].pi > arr2[num2 - 1].pi)//一个2换一个2 
		{
			ans += arr2[i].pi - arr2[num2 - 1].pi;
			ID[num2 - 1] = arr2[i].id;
		}
		else
			break;
	}
	sort(ID, ID + num);
	printf("%d\n", ans);
	for (int i = 0; i < num; i++)
	{
		if (ID[i] != 0)
		{
			if (i != num - 1)
				printf("%d ", ID[i]);
			else
				printf("%d\n", ID[i]);
		}
	}
	return 0;
}

C - Tic-tac-toe

CodeForces - 3C
题解:https://blog.youkuaiyun.com/liuke19950717/article/details/51628792?utm_source=blogxgwz6

D - Least Cost Bracket Sequence

CodeForces - 3D

This is yet another problem on regular bracket sequences.

A bracket sequence is called regular, if by inserting “+” and “1” into it we get a correct mathematical expression. For example, sequences “(())()”, “()” and “(()(()))” are regular, while “)(”, “(()” and “(()))(” are not. You have a pattern of a bracket sequence that consists of characters “(”, “)” and “?”. You have to replace each character “?” with a bracket so, that you get a regular bracket sequence.

For each character “?” the cost of its replacement with “(” and “)” is given. Among all the possible variants your should choose the cheapest.

Input

The first line contains a non-empty pattern of even length, consisting of characters “(”, “)” and “?”. Its length doesn’t exceed 5·104. Then there follow m lines, where m is the number of characters “?” in the pattern. Each line contains two integer numbers ai and bi (1 ≤ ai,  bi ≤ 106), where ai is the cost of replacing the i-th character “?” with an opening bracket, and bi — with a closing one.

Output

Print the cost of the optimal regular bracket sequence in the first line, and the required sequence in the second.

Print -1, if there is no answer. If the answer is not unique, print any of them.

Examples

Input

(??)
1 2
2 8

Output

4
()()

pair作优先队列的元素,先按第一个元素排序,如果相等就比较第二个元素。

#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
using namespace std;

typedef long long ll;
priority_queue <pair<ll, int> > pque;
string s;

int main(void)
{
    cin >> s;
    if (s.size() % 2 == 1)
		return puts("-1"), 0;
    int cnt = 0;
    ll ans = 0;
    for (int i = 0; i < s.size(); i++)
    {
        if (s[i] == '(')
            cnt++;
        else if (s[i] == ')')
            cnt--;
        else//问号 
        {
            int a, b;
			scanf("%d%d", &a, &b);
            cnt--; s[i] = ')'; ans += b;
            pque.push(make_pair(b - a, i));//只存储问号 
        }
        if (cnt < 0)
        {
            if (pque.size() == 0)//如果前面没有问号 
				break;
            pair<int, int> x = pque.top(); pque.pop();
            ans -= x.first; s[x.second] = '(';
            cnt += 2;
        }
    }
    if(cnt != 0)
		return puts("-1"), 0;
    cout << ans << endl;
    cout << s << endl;
    return 0;
}

题解:https://www.cnblogs.com/qscqesze/p/5266725.html

### 关于 Codeforces Round 839 Div 3 的题目与解答 #### 题目概述 Codeforces Round 839 Div 3 是一场面向不同编程水平参赛者的竞赛活动。这类比赛通常包含多个难度层次分明的问题,旨在测试选手的基础算法知识以及解决问题的能力。 对于特定的比赛问题及其解决方案,虽然没有直接提及 Codeforces Round 839 Div 3 的具体细节[^1],但是可以根据以往类似的赛事结构来推测该轮次可能涉及的内容类型: - **输入处理**:给定一组参数作为输入条件,这些参数定义了待解决的任务范围。 - **逻辑实现**:基于输入构建满足一定约束条件的结果集。 - **输出格式化**:按照指定的方式呈现最终答案。 考虑到提供的参考资料中提到的其他几场赛事的信息[^2][^3],可以推断出 Codeforces 圆桌会议的一般模式是围绕着组合数学、图论、动态规划等领域展开挑战性的编程任务。 #### 示例解析 以一个假设的例子说明如何应对此类竞赛中的一个问题。假设有如下描述的一个简单排列生成问题: > 对于每一个测试案例,输出一个符合条件的排列——即一系列数字组成的集合。如果有多种可行方案,则任选其一给出即可。 针对上述要求的一种潜在解法可能是通过随机打乱顺序的方式来获得不同的合法排列形式之一。下面是一个 Python 实现示例: ```python import random def generate_permutation(n, m, k): # 创建初始序列 sequence = list(range(1, n + 1)) # 执行洗牌操作得到新的排列 random.shuffle(sequence) return " ".join(map(str, sequence[:k])) # 测试函数调用 print(generate_permutation(5, 2, 5)) # 输出类似于 "4 1 5 2 3" ``` 此代码片段展示了怎样创建并返回一个长度为 `k` 的随机整数列表,其中元素取自 `[1..n]` 这个区间内,并且保证所有成员都是唯一的。需要注意的是,在实际比赛中应当仔细阅读官方文档所提供的精确规格说明,因为这里仅提供了一个简化版的方法用于解释概念。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值