Szucoder Round #3 (div.2)

地址:http://codeforces.com/problemset/problem/296/A

A. Yaroslav andPermutations

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standardoutput

Yaroslav hasan array that consists of n integers. In one second Yaroslav can swaptwo neighboring array elements. Now Yaroslav is wondering if he can obtain anarray where any two neighboring elements would be distinct in a finite time.

HelpYaroslav.

Input

The firstline contains integer n (1 ≤ n ≤ 100) — the number ofelements in the array. The second line contains n integers a1, a2, ..., an(1 ≤ ai ≤ 1000) — thearray elements.

Output

In the singleline print "YES"(without the quotes) if Yaroslav can obtain the array he needs, and "NO" (without the quotes)otherwise.

Sample test(s)

input

1
1

output

YES

input

3
1 1 2

output

YES

input

4
7 7 7 7

output

NO

Note

In the firstsample the initial array fits well.

In the secondsample Yaroslav can get array: 121. He can swap the last and the second last elements toobtain it.

In the thirdsample Yarosav can't get the array he needs.





比赛时程序:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <cmath>
using namespace std;

int main()
{
	int i,n,a[105],b[10005];
	scanf("%d",&n);
	memset(a,0,sizeof(a));
	memset(b,0,sizeof(b));
	for (i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
		b[a[i]]++;
	}
	for (i=0;i<n;i++)
	{
		if (n-b[a[i]]<b[a[i]]-1)
			break;
	}
	if (i==n)
		printf("YES\n");
	else 
		printf("NO\n");
	return 0;
}


//其实只要统计出现最多的数字就行了,然后该数字出现次数<=(总数-1)/2+1就行了


修改后程序:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <cmath>
using namespace std;

int main()
{
	int i,n,a[105],b[10005],num;
	scanf("%d",&n);
	memset(a,0,sizeof(a));
	memset(b,0,sizeof(b));
	for (i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
		b[a[i]]++;
	}
	num=0;
	for (i=0;i<n;i++)
	{
		if (num<b[a[i]])
			num=b[a[i]];
	}
	if (num<=(n-1)/2+1)
		printf("YES\n");
	else 
		printf("NO\n");
	return 0;
}




地址:http://codeforces.com/problemset/problem/294/B

B. Shaass and Bookshelf

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standardoutput

Shaass has n books.He wants to make a bookshelf for all his books. He wants the bookshelf'sdimensions to be as small as possible. The thickness of the i-th book is ti and itspages' width is equal to wi. The thickness of eachbook is either 1 or 2. All bookshave the same page heights.

Shaass putsthe books on the bookshelf in the following way. First he selects some of thebooks and put them vertically. Then he puts the rest of the books horizontallyabove the vertical books. The sum of the widths of the horizontal books must beno more than the total thickness of the vertical books. A sample arrangement ofthe books is depicted in the figure.

Help Shaassto find the minimum total thickness of the vertical books that we can achieve.

Input

The firstline of the input contains an integer n(1 ≤ n ≤ 100). Each of thenext n linescontains two integers ti and wi denotingthe thickness and width of the i-th bookcorrespondingly, (1 ≤ ti ≤ 2, 1 ≤ wi ≤ 100).

Output

On the onlyline of the output print the minimum total thickness of the vertical books thatwe can achieve.

Sample test(s)

input

5
1 12
1 3
2 15
2 5
2 1

output

5

input

3
1 10
2 1
2 4

output

3



思路:
贪心算法。先把1和2分组(保证贪心能够得到最优解),在总厚度不变的情况下,先放宽度小的。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <cmath>
using namespace std;

int sum(int [],int,int);
void maopao(int [],int);

int main()
{
	int i,j,n,t,w,l1,l2,min,sumthick,sumt;
	int w1[105],w2[105];

	memset(w1,0,sizeof(w1));
	memset(w2,0,sizeof(w2));

	scanf("%d",&n);
	l1=l2=1;
	for (i=0;i<n;i++)
	{
		scanf("%d%d",&t,&w);
		if (t==1)
			w1[l1++]=w;
		else
			w2[l2++]=w;
	}

	maopao(w1,l1);
	maopao(w2,l2);

	min=(l1-1)+(l2-1)*2;

	for (i=0;i<l1;i++)
	{
		sumthick=sum(w1,1,i);
		for (j=0;j<l2;j++)
		{
			sumt=sumthick+sum(w2,1,j);
			if (sumt<=(l1-i-1)+(l2-j-1)*2 && (l1-i-1)+(l2-j-1)*2<min)
				min=(l1-i-1)+(l2-j-1)*2;
		}
	}

	printf("%d\n",min);

	return 0;
}

int sum(int a[],int l,int r)
{
	int total=0,i;
	for (i=l;i<=r;i++)
		total+=a[i];
	return total;
}

void maopao(int a[],int l)
{
	int temp,i,j;
	for (i=1;i<l-1;i++)
	{
		for (j=i+1;j<l;j++)
			if (a[i]>a[j])
			{
				temp=a[i];
				a[i]=a[j];
				a[j]=temp;
			}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值