Codeforces Beta Round #3

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;
}

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章