CF 2018.09.06 00:35 题解(补)

比赛题目链接:http://codeforces.com/contest/1040

A. Palindrome Dance

A group of nn dancers rehearses a performance for the closing ceremony. The dancers are arranged in a row, they've studied their dancing moves and can't change positions. For some of them, a white dancing suit is already bought, for some of them — a black one, and for the rest the suit will be bought in the future.

On the day when the suits were to be bought, the director was told that the participants of the olympiad will be happy if the colors of the suits on the scene will form a palindrome. A palindrome is a sequence that is the same when read from left to right and when read from right to left. The director liked the idea, and she wants to buy suits so that the color of the leftmost dancer's suit is the same as the color of the rightmost dancer's suit, the 2nd left is the same as 2nd right, and so on.

The director knows how many burls it costs to buy a white suit, and how many burls to buy a black suit. You need to find out whether it is possible to buy suits to form a palindrome, and if it's possible, what's the minimal cost of doing so. Remember that dancers can not change positions, and due to bureaucratic reasons it is not allowed to buy new suits for the dancers who already have suits, even if it reduces the overall spending.

Input

The first line contains three integers nn, aa, and bb (1≤n≤201≤n≤20, 1≤a,b≤1001≤a,b≤100) — the number of dancers, the cost of a white suit, and the cost of a black suit.

The next line contains nn numbers cici, ii-th of which denotes the color of the suit of the ii-th dancer. Number 00 denotes the white color, 11 — the black color, and 22 denotes that a suit for this dancer is still to be bought.

Output

If it is not possible to form a palindrome without swapping dancers and buying new suits for those who have one, then output -1. Otherwise, output the minimal price to get the desired visual effect.

Examples

input

5 100 1
0 1 2 1 2

output

101

input

3 10 12
1 2 0

output

-1

input

3 12 1
0 1 0

output

0

Note

In the first sample, the cheapest way to obtain palindromic colors is to buy a black suit for the third from left dancer and a white suit for the rightmost dancer.

In the second sample, the leftmost dancer's suit already differs from the rightmost dancer's suit so there is no way to obtain the desired coloring.

In the third sample, all suits are already bought and their colors form a palindrome.

题意:将字符串变成只有0或1的回文串,其中2变成0或1的代价已知,求最小代价。

思路:签到题,只需要找最小值即可,状态稍微多了一些。

用时:约30min(大部分时间浪费在理解题意上,词汇量的锅。。)

代码:

#include<iostream>
using namespace std;
int main() {
	int n,x,y,a[30],ans=0,game=1;
	scanf("%d%d%d",&n,&x,&y);
	for(int i=0;i<n;i++) scanf("%d",&a[i]);
	for(int i=0;i<n/2;i++) {
		if(a[i]!=2&&a[n-1-i]!=2) {
			if(a[i]!=a[n-1-i]) {
				game=0;
				break;
			}
		}
		else {
			if(a[i]==2&&a[n-1-i]==2) ans+=2*min(x,y);
			else if(a[i]==2) {
				if(a[n-i-1]==0) ans+=x;
				else ans+=y; 
			}
			else {
				if(a[i]==0) ans+=x;
				else ans+=y;
			}
		}
	}
	if(n%2==1&&a[n/2]==2) ans+=min(x,y);
	if(game==1) printf("%d",ans);
	else printf("-1"); 
}

 

B. Shashlik Cooking

Long story short, shashlik is Miroslav's favorite food. Shashlik is prepared on several skewers simultaneously. There are two states for each skewer: initial and turned over.

This time Miroslav laid out nn skewers parallel to each other, and enumerated them with consecutive integers from 11 to nn in order from left to right. For better cooking, he puts them quite close to each other, so when he turns skewer number ii, it leads to turning kk closest skewers from each side of the skewer ii, that is, skewers number i−ki−k, i−k+1i−k+1, ..., i−1i−1, i+1i+1, ..., i+k−1i+k−1, i+ki+k (if they exist).

For example, let n=6n=6 and k=1k=1. When Miroslav turns skewer number 33, then skewers with numbers 22, 33, and 44 will come up turned over. If after that he turns skewer number 11, then skewers number 11, 33, and 44 will be turned over, while skewer number 22 will be in the initial position (because it is turned again).

As we said before, the art of cooking requires perfect timing, so Miroslav wants to turn over all nn skewers with the minimal possible number of actions. For example, for the above example n=6n=6 and k=1k=1, two turnings are sufficient: he can turn over skewers number 22 and 55.

Help Miroslav turn over all nn skewers.

Input

The first line contains two integers nn and kk (1≤n≤10001≤n≤1000, 0≤k≤10000≤k≤1000) — the number of skewers and the number of skewers from each side that are turned in one step.

Output

The first line should contain integer ll — the minimum number of actions needed by Miroslav to turn over all nn skewers. After than print llintegers from 11 to nn denoting the number of the skewer that is to be turned over at the corresponding step.

Examples

input

7 2

output

2
1 6 

input

5 1

output

2
1 4 

Note

In the first example the first operation turns over skewers 11, 22 and 33, the second operation turns over skewers 44, 55, 66 and 77.

In the second example it is also correct to turn over skewers 22 and 55, but turning skewers 22 and 44, or 11 and 55 are incorrect solutions because the skewer 33 is in the initial state after these operations.

题意:先选中n个排成一行的点中的一个点,以该点为中心,k为半径,将所有的点标记,问全部标记n个点至少要选中多少点,并输出应该标记的点的序号(标记2次的点算是没有标记)

思路:以2*k+1为基本单位每隔该单位取一点,初始点用循环检查判断。

用时:27min

备注:答案不唯一,我只是选择了最有规律最好写的方法。

代码:

#include<iostream>
using namespace std;
int main() {
	int n,k,k1,ans,i,j;
	scanf("%d%d",&n,&k);
	k1=2*k+1;
	if(n%k1==0) ans=n/k1;
	else ans=n/k1+1;
	for(i=1;i<=k;i++) {
		if(i==k+1) break;
		//printf("%d %d %d %d %d\n",k,i,ans,k1,n);
		if(k+i+(ans-1)*k1>=n) break;
	}
	printf("%d\n",ans);
	for(j=0;j<ans;j++) {
		printf("%d ",i);
		i+=k1;
	}
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值