HDU 4277 USACO ORZ(暴力+双向枚举)

USACO ORZ

Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3809    Accepted Submission(s): 1264

Problem Description

Like everyone, cows enjoy variety. Their current fancy is new shapes for pastures. The old rectangular shapes are out of favor; new geometries are the favorite.

I. M. Hei, the lead cow pasture architect, is in charge of creating a triangular pasture surrounded by nice white fence rails. She is supplied with N fence segments and must arrange them into a triangular pasture. Ms. Hei must use all the rails to create three
sides of non-zero length. Calculating the number of different kinds of pastures, she can build that enclosed with all fence segments.

Two pastures look different if at least one side of both pastures has different lengths, and each pasture should not be degeneration.

Input

The first line is an integer T(T<=15) indicating the number of test cases.

The first line of each test case contains an integer N. (1 <= N <= 15)

The next line contains N integers li indicating the length of each fence segment. (1 <= li <= 10000)

Output

For each test case, output one integer indicating the number of different pastures.

Sample Input

1
3
2 3 4

Sample Output

1

Source

2012 ACM/ICPC Asia Regional Changchun Online

题意:给你n条边,组成一个三角形,求能组成的三角形种数。两个三角形三条边一样的算一种。

题解:暴力枚举第一条边,然后双向枚举第二条边,用map去重。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <set>
#include <queue>
#define MP(x,y) make_pair(x,y)
using namespace std;
typedef pair<int,int > ppi;
map<ppi,int> mp;
int a[20];
int b[20];
int sum;
int all;
int bn;
int ans;
int smallest;
int largest;
void dfs(int i,int csum) {
    if(csum>smallest)
        return ;
    if(csum!=0&&all-csum<=largest&&mp.find(MP(csum,all-csum))==mp.end()) {///成立&&判重
        ans++;
        mp[MP(csum,all-csum)]=1;
    }
    if(i==bn)
        return ;
    dfs(i+1,csum+b[i]);
    dfs(i+1,csum);
}
int main() {
    int n;
    int t;
    scanf("%d",&t);
    while(t--) {
        mp.clear();
        scanf("%d",&n);
        sum=0;
        ans=0;
        for(int i=0; i<n; i++) {
            scanf("%d",a+i);
            sum+=a[i];
        }
        int m=(1<<n)-1;
        for(int i=1; i<m; i++) {///枚举第一条边
            all=0;
            bn=0;
            for(int j=0; j<n; j++) {///第二条与第三条边的和
                if((1<<j)&i) {
                    all+=a[j];
                    b[bn++]=a[j];
                }
            }
            largest=sum-all;
            if(largest>=all)
                continue;
            smallest=all/2;
            dfs(0,0);
        }
        printf("%d\n",ans);
    }
    return 0;
}

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

时间: 2024-12-22 04:18:25

HDU 4277 USACO ORZ(暴力+双向枚举)的相关文章

hdu 4277 USACO ORZ DFS

USACO ORZ Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3581    Accepted Submission(s): 1196 Problem Description Like everyone, cows enjoy variety. Their current fancy is new shapes for pastur

hdu 4277 USACO ORZ(dfs+剪枝)

Problem Description Like everyone, cows enjoy variety. Their current fancy is new shapes for pastures. The old rectangular shapes are out of favor; new geometries are the favorite.I. M. Hei, the lead cow pasture architect, is in charge of creating a

hdu 2489 Minimal Ratio Tree(dfs枚举 + 最小生成树)~~~

题目: 链接:点击打开链接 题意: 输入n个点,要求选m个点满足连接m个点的m-1条边权值和sum与点的权值和ans使得sum/ans最小,并输出所选的m个点,如果有多种情况就选第一个点最小的,如果第一个点也相同就选第二个点最小的........ 思路: 求一个图中的一颗子树,使得Sum(edge weight)/Sum(point weight)最小~ 数据量小,暴力枚举~~~~~dfs暴力枚举C(M,N)种情况. 枚举出这M个点之后,Sum(point weight)固定,进行prim或者K

HDU 4961 Boring Sum 暴力

题意:对于所有的A[I],同时找到左边和右边离它最近且是它的倍数的数相乘最后加起来求和. 解题思路:n*sqrt(n)的算法,开始以为过不了,wa了两发因为lld I64d对拍一个小时发现一个小时前交的代码没错只是没变I64d,..具体思路是枚举每个a[i]的因子,找离它最近的那个更新,如果已经没更新就不用更新了.用两个辅助数组记录最近的就行. 解题代码: 1 // File Name: 1002.cpp 2 // Author: darkdream 3 // Created Time: 201

hdu 4737 二分或暴力

http://acm.hdu.edu.cn/showproblem.php?pid=4737 Problem Description There are n numbers in a array, as a0, a1 ... , an-1, and another number m. We define a function f(i, j) = ai|ai+1|ai+2| ... | aj . Where "|" is the bit-OR operation. (i <= j)

hdu 4277

USACO ORZ Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3439    Accepted Submission(s): 1164 Problem Description Like everyone, cows enjoy variety. Their current fancy is new shapes for pastur

HDU 4499 Cannon (暴力搜索)

题意:在n*m的方格里有t个棋子,问最多能放多少个炮且每个炮不能互相攻击(炮吃炮) 炮吃炮:在同一行或同一列且中间有一颗棋子. #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <queue> #include <math.h> #define M 50 #define LL long long using

HDU 4082 Hou Yi&#39;s secret --枚举

题意: 给n个点,问最多有多少个相似三角形(三个角对应相等). 解法: O(n^3)枚举点,形成三角形,然后记录三个角,最后按三个角度依次排个序,算一下最多有多少个连续相等的三元组就可以了. 注意:在同一个坐标的两点只算一次,所以要判一下. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #includ

HDU 4902 线段树||暴力

给定一个序列,两种操作 1:把一段变成x. 2:把一段每个数字,如果他大于x,就变成他和x的gcd,求变换完后,最后的序列. 线段树解法:用lazy标记下即可,优化方法还是很巧妙的, Accepted 4902 515MS 3308K 1941 B C++ #include "stdio.h" #include "string.h" struct node { int l,r,x;// 在叶子节点代表值,树节点代表成端更新的lazy操作. }data[400010]