Coderforces Rockethon 2014 Trial Contest: Testing Round #10 A2. Skis

A2. Skis
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A warehouse of the oldest sports shop of Berland received n skis, the length of the i-th ski equals ai centimeters. Your task is to find out the largest number of ski packs to sell if a ski pack must contain two skis and the sum of their lengths must equal exactly q centimeters. Note that the Berland skis aren't differentiated as left or right.

Input

The first line contains two positive integers n and q. The second line contains n positive integers a1a2, ..., an (1 ≤ ai ≤ q).

Limits n and q are distinct for distinct problems:

  • for subproblem A1: 1 ≤ n, q ≤ 100,
  • for subproblem A2: 1 ≤ n, q ≤ 106.
Output

Print the required maximum number of packs.

Sample test(s)
input
          
          
7 5
1 2 1 4 3 1 3
output
2


n个数,求和为q的对数

统计每个数xi出现的次数记为a[x],取min(a[x],a[q-x]),求和即可,注意当x+x=q时特判

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<ctime>
#include<cctype>
#include<cmath>
#include<string>
#include<cstring>
#include<queue>
#include<vector>
#define sqr(x) (x)*(x)
#define INF 0x1f1f1f1f
#define PI 3.1415926535
#define mm 1000001

using namespace std;

int a[mm],n,q,x,cnt;

int main()
{
	scanf("%d%d",&n,&q);
	memset(a,0,sizeof(a));
	for (int i=0;i<n;i++)
	{
		scanf("%d",&x);
		a[x]++;
		
	}
	
	cnt=0;
	
	for (int i=0;i<q;i++)	//这里到q/2就可以了 
	{
		if (q%2==0 && i==q>>1)
		{
			cnt+=(a[i]>>1);
			a[i]=0;
			continue;
		}
		cnt+=min(a[i],a[q-i]);
		a[i]=a[q-i]=0;
	}
	
	printf("%d\n",cnt);
	
	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值