POJ-3663-Costume Party

Costume Party
Time Limit: 1000MS  Memory Limit: 65536K
Total Submissions: 11237  Accepted: 4512

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

Source

USACO 2008 January Bronze

 

 

前两天比赛的题,脑子抽筋没优化超时了,今天看看,真简单啊,罪过罪过~~~
题意:有一组数组N,要求这N个数两两组合能拼凑出多少对和(Ni+Nj)小于或等于M的组合

AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define MAXN 20020
int a[MAXN];
int main()
{
    int i,j,n,m,sum,temp;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
       memset(a,0,sizeof(a));
       for(i=0;i<n;i++)
       scanf("%d",&a[i]);
       sort(a,a+n);
       temp=n;
       sum=0;
       for(i=0;i<temp;i++)
         for(j=i+1;j<temp;j++)
         {
            if(a[i]+a[j]<=m)
            {
               sum++;
            }
            else
            {
                temp=j;//优化,后面的怎么加都大于长度,就没有必要纳入每次的计算了
                break;
            }
         }
       printf("%d",sum);
    }
    return 0;
}
时间: 2024-08-31 20:02:08

POJ-3663-Costume Party的相关文章

POJ题目分类

初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      (4)递推.      (5)构造法.(poj3295)      (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996) 二.图算法:      (1)图的深度优先遍历和广度优先遍历.      (2)最短路径算法(dijkstra,bellman-ford

POJ:DNA Sorting 特殊的排序

Description One measure of ``unsortedness'' in a sequence is the number of pairs of entries that are out of order with respect to each other. For instance, in the letter sequence ``DAABEC'', this measure is 5, since D is greater than four letters to

POJ 1001 Exponentiation 无限大数的指数乘法 题解

POJ做的很好,本题就是要求一个无限位大的指数乘法结果. 要求基础:无限大数位相乘 额外要求:处理特殊情况的能力 -- 关键是考这个能力了. 所以本题的用例特别重要,再聪明的人也会疏忽某些用例的. 本题对程序健壮性的考查到达了变态级别了. 更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/ 某人贴出的测试用例数据地址: http://poj.org/showmessage?message_id=76017 有

POJ 2240 Arbitrage:最短路 Floyd

Arbitrage:http://poj.org/problem?id=2240 大意: 给你m种货币,给你m种货币兑换规则,问通过这些规则最后能不能盈利.eg:1美元换0.5英镑,1英镑换10法郎,1法郎换0.21美元,这样1美元能换0.5*10*0.21=1.05美元,净赚0.05美元. 思路: 用Floyd找出每两种钱之间的最大兑换关系,遍历一遍,看有没有那种钱币最后能盈利,有就输出Yes,没有就是No.在处理钱币名称与编号之间的关系时,可以用map存(比较好用),当然也可以用字符串比较.

POJ 1860 Currency Exchange:最短路 Bellman-Ford

Currency Exchange:http://poj.org/problem?id=1860 大意:有多种货币,之间可以交换,但是需要手续费,也就是说既有汇率又有手续费.问经过交换之后能不能赚. 思路:Bellman_Ford,因为要求最长路,所以松弛条件改一下就好了. Tips: 3              2                  1                20.0货币的数量   兑换点的数量     主人公拥有的货币量    主人公拥有货币的价值1 2 1.00

POJ 1258 Agri-Net:最小生成树 Prim 模版题

Agri-Net:http://poj.org/problem?id=1258 大意:新镇长竞选宣言就是将网络带到每一个农场,给出农场个数,两两之间建光缆的耗费,求所有都联通的最小耗费. 思路:最小生成树,因为边比较稠密,用Prim做. PS:对于比较稠密的图,用Prim,对于比较稀疏的图,用 Kruskal.Kruskal是找边的过程,稀疏的话会比较快. 更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

POJ 1789 Truck History:最小生成树 Prim

Truck History:http://poj.org/problem?id=1789 大意:用一个7位的string代表一个编号,两个编号之间的距离代表这两个编号之间不同字母的个数.一个编号只能由另一个编号变化的来,变化的字母的数量就是这两个编号之间相应的距离,现在要找出一个变化方案,使得总代价最小,也就是距离之和最小. 思路:将每个字符串当成一个节点,求出每个节点之间需要变化的次数为边的权值,用Prim建立最小生成树(稠密图). 更多精彩内容:http://www.bianceng.cnh

POJ 2485 Highways:最小生成树 Prim

Highways:http://poj.org/problem?id=2485 大意:给你一个用邻接矩阵形式存储的有n个顶点的无向图,让你求它的最小生成树并求出在这个生成树里面最大的边的权值. 思路:用Prim求,判断条件改一下就行. PS:dis数组初始化的时候用memset一直RE,希望有知道怎么回事的不吝赐教,谢了~ 更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/ #include <stdio.h

POJ 3206 Borg Maze:BFS+Prim

Borg Maze:http://poj.org/problem?id=3026 大意:给你一个m*n的迷宫,可以上下左右的走,只能走空格或字母,求出将所有字母连通起来的最小耗费. 思路:先用BFS求出S到所有A的距离,再用Prim求最小生成树,求出最小耗费.这个题坑的不在题,是数据太坑了,在空格处理上没弄好,贡献了好几个WA和CE,看Discuss才知道很坑,最后用G++过了的代码,C++还RE,实在不知道说什么好了  =.= 更多精彩内容:http://www.bianceng.cnhttp

POJ 3687 Labeling Balls:拓扑序列

Labeling Balls:http://poj.org/problem?id=3687 大意:n个重量分别为1-n的小球,给定一些小球间的重量关系. 在符合重量关系的前提下,先输出编号小的球. 思路:也是一道很简单的拓扑排序,不过要倒着来,注意一下要判重边. #include <string.h> #include <iostream> using namespace std; int Map[210][210], indegree[210], Ans[210]; int n,