POJ 3663:Costume Party

原创 2015年07月11日 10:15:04

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


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

poj3663 Costume Party

Costume Party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11793   ...

POJ 3663 Costume Party (二分查找)

Description It's Halloween! Farmer John is taking the cows to a costume party, but unfortunately he...

poj 3633 Costume Party

题目大意:就是给一组数,求有多少对数的和不超过给定值 解题思路:水题。。。。。用来练练自己手写快速排序的。。。。 先把数按从小到大排列,然后用两个序号low,high分别指向第0个数和第n-1个数...

POJ 3342 Party at Hali-Bula (树形dp 树的最大独立集 判多解 好题)

POJ 3342 Party at Hali-Bula (树形dp 树的最大独立集 判多解 好题)

poj 2342 Anniversary party(树形DP基础题)(树形dp模板)

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5888   Accepted: 3389 Description...

poj3268 - Silver Cow Party

想看更多的解题报告: http://blog.csdn.net/wangjian8006/article/details/7870410              &...

POJ 2342 Anniversary party(树形dp)

题目链接: POJ 2342 Anniversary party 题意: 有nn个人要去参加聚会,每个人都不想和他的直接上属出现,给出每个人的从属关系,每个人会有convivialitycon...

POJ 3268 Silver Cow Party(双向最短路)

Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1...

POJ 3268 Silver Cow Party 最短路

题意:n只牛在第x只牛家聚会...然后每只牛都走最短路,问所有牛到聚会现场以及回家用时最长的是多少... 有向图...最短路算法都是求单源最短路径...但是一来一回两次,可以先求一次以x为源点的,就...

poj 3342Party at Hali-Bula

最大独立集问题,加上判断唯一性 入门经典例题,不多说了 /*********************************************** * Author: fisty * ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)