poj3663 Costume Party

Costume Party

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11793   Accepted: 4770

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 hasN 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

题意:给n个数字,从中任意选2个数字不大于s的可能有多少种

解题思路:先将数字从小到大排序,然后从最后开始向前遍历,如果当前遍历的数(第i个)和前一个数之和小于等于s,那么一定有i*(i-1)/2对数存在,接下来我们从i向后遍历,找到一个数与第i个数之和小于等于s,全部找出来即可,复杂度为O(nlgn)+O(n)

参考代码:

#include <iostream>
#include <algorithm>
using namespace std;
#define MAX 20000+2
int main(){
	int a[MAX],n,s;
	while (cin>>n>>s){
		for (int i=0;i<n;i++){
			cin>>a[i];
		}
		sort(a,a+n);
		int ans=0,k;
		for (k=n-1;k>0;k--){
			if (a[k]+a[k-1]<=s)
				break;
		}
		ans+=k*(k-1)/2;
		for (;k<n;k++){
			for (int i=0;i<k;i++){
				if (a[k]+a[i]<=s)
					ans++;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}
时间: 2024-08-24 13:13:41

poj3663 Costume Party的相关文章

POJ 3663 Costume Party (二分查找)

Costume Party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12297   Accepted: 4891 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

POJ 3663 Costume Party (二分查找)

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 S (1 ≤ S ≤ 1,000,000). FJ has N cows (2 ≤ N ≤ 20,000) conveniently numbered

[思路题] poj 3663 Costume Party

题意: 给n个数,和m,问从n个数中选出两个数x,y使得x+y<=m的方法有多少种,xy顺序无关. 思路: 预处理所有的数,默认x<y来枚举. 对于一个x,使得满足条件的y一定是 [x,m-y]范围内的.就算一下有多少个数. 然后单独算一下相等的情况. 代码: #include"cstdlib" #include"cstdio" #include"cstring" #include"cmath" #include&

Halloween Costumes LightOJ - 1422

Gappu has a very busy weekend ahead of him. Because, next weekend is Halloween, and he is planning to attend as many parties as he can. Since it's Halloween, these parties are all costume parties, Gappu always selects his costumes in such a way that

根据76大细分词性对单词进行归组(二)

词性的重要性不言而喻,尤其是对于自然语言处理来说,哪怕就是记单词,根据词性对单词进行归组也是非常有帮助的. superword是一个Java实现的英文单词分析软件,主要研究英语单词音近形似转化规律.前缀后缀规律.词之间的相似性规律等等. 各大词性及其包括的词: 32.N-COUNT-COLL(可数集合名词) (词数:50) 1 aristocracy army array audience band 2 cast chapter command commission committee 3 co

【字源大挪移—读书笔记】 第二部分:字根

[2] 字根:[2.1]表示[否定]的字根.[2.2]表示[方位]的字根.[2.3]表示[程度]的字根.[2.4]表示[状态]的字根.[2.5]表示[现象]的字根.[2.6]表示[身体]的字根.[2.7]表示[姿势]的字根.[2.8]表示[心,心里活动]的字根.[2.9]表示[行为动作]的字根.[2.10]表示[感官动作]的字根.[2.11]表示[感觉]的字根.[2.12]表示[生命]的字根.[2.13]表示[死亡]的字根.[2.14]表示[社会]的字根 [2.1]表示[否定]的字根 -neg-

英语新闻常用词汇与短语

英语新闻常用词汇与短语 经济篇 accumulated deficit 累计赤字 active trade balance 贸易顺差 adverse trade balance 贸易逆差 aid 援助 allocation of funds 资金分配 allotment 拨款 allowance/grant/subsidy 补贴,补助金,津贴 amortization 摊销,摊还,分期偿付 annuity 年金 article 物品,商品 assigned 过户 autarchy 闭关自守 av

jquery.barddialog.js

/// <reference path="jquery-2.1.1.min.js" /> /** * @license jquery.bardDialog 1.0.0 * (c) 1986-infinity China, Shanghai. CalosChen * License: MIT * Description:A responsive dialog box which provides several simple interface methods. Free t

设计模式 学习 2:

六个创建型模式 简单工厂: 问题: Sunny软件公司欲基于Java语言开发一套图表库,该图表库可以为应用系统提供各种不同外观的图表,例如柱状图.饼状图.折线图等.Sunny软件公司图表库设计人员希望为应用系统开发人员提供一套灵活易用的图表库,而且可以较为方便地对图表库进行扩展,以便能够在将来增加一些新类型的图表. //抽象图表接口:抽象产品类 interface Chart { public void display(); } //柱状图类:具体产品类 class HistogramChart