关闭

POJ 3663:Costume Party

标签: POJ
227人阅读 评论(0) 收藏 举报
分类:

Costume Party
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12607   Accepted: 4977

Description

It's Halloween! Farmer John is taking the cows to a costume party, but unfortunately he only has one costume. The costume fits precisely two cows with a length of (1 ≤ S ≤ 1,000,000). FJ has N cows (2 ≤N ≤ 20,000) conveniently numbered 1..N; cow i has length Li (1 ≤ Li ≤ 1,000,000). Two cows can fit into the costume if the sum of their lengths is no greater than the length of the costume. FJ wants to know how many pairs of two distinct cows will fit into the costume.

Input

* Line 1: Two space-separated integers: N and S
* Lines 2..N+1: Line i+1 contains a single integer: Li

Output

* Line 1: A single integer representing the number of pairs of cows FJ can choose. Note that the order of the two cows does not matter.

Sample Input

4 6
3
5
2
1

Sample Output

4

这两天做水题真是做的够了,这尼玛水平完全没有什么提高。

题意是给出的数组中判断有多少个两个数的和小于等于给定的数。

一开始觉得so easy,结果TLE。。。

之后sort一下,判断大于的就跳出才能符合要求。

代码:

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

int num[20005];

int main()
{	
	int N,S,i,j;
	scanf("%d%d",&N,&S);
	int result=0;
	for(i=1;i<=N;i++)
	{
		scanf("%d",&num[i]);
	}

	sort(num+1,num+N+1);

	for(i=1;i<=N;i++)
	{
		for(j=1;j<i;j++)
		{
			if(num[i]+num[j]<=S)
			{
				result++;
			}
			else
			{
				break;
			}
		}
	}

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

	return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:167967次
    • 积分:6024
    • 等级:
    • 排名:第4157名
    • 原创:448篇
    • 转载:6篇
    • 译文:0篇
    • 评论:82条
    最新评论
    个人说明
    教育背景:
    BUPT

    编程语言:
    C/C++ Python