【转】ZOJ Problem Set - 1101 Gamblers:非暴力枚举

思路:

将所有数据从小到大排序,这样每次从序列的后面寻找答案ans,自然的确保了最大赌资性.

接下来就要枚举其他三个数了 进而 判断 此时的ans是否存在 其他三个数 和 等于ans.

而在三个数中至少有一个数在ans的前面 其他的两个数可能在ans的后面 因为存在负数.

这样就可以先枚举两个 赌注 进而第三个赌注可以由ans与 两个赌注 差 确定 从而二分搜索

如果存在第三个赌注 那么ans的赌注就是答案

如果枚举所有ans都找不到对应的第三个赌注则no solution

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
const int Size=1000;
int a[Size+1];
int n;

int bsearch(int start, int val)
{
        int L=start+1;
        int R=n-1;
        while(L<=R)
        {
                int mid=(L+R)/2;
                if( a[mid] == val )
                    return mid;
                else if(a[mid]<val)
                    L=mid+1;
                else
                    R=mid-1;
        }
        return 0;
}

int Enumer()
{
        for(int i=n-1; i>0; i--)
            for(int j=0; j<i; j++)
                for(int k=j+1; k<n; k++){
                        int tmp=a[i]-a[j]-a[k];
                        int pos=bsearch(k, tmp);
                        if(pos&&pos!=i)
                            return i;
                }
        return 0;
}

int main()
{
        while(cin>>n&&n)
        {
                for(int i=0; i<n; i++)
                    cin>>a[i];
                sort(a, a+n);
                if(Enumer())
                    cout<<a[Enumer()]<<endl;
                else
                    cout<<"no solution"<<endl;
        }
        return 0;
}

  

时间: 2024-07-30 20:50:28

【转】ZOJ Problem Set - 1101 Gamblers:非暴力枚举的相关文章

HDU 4430 &amp; ZOJ 3665 Yukari&#39;s Birthday(二分+枚举)

题目链接: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=4430 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4888 Problem Description Today is Yukari's n-th birthday. Ran and Chen hold a celebration party for her. Now comes the most import

ZOJ Problem Set - 2563 Long Dominoes 【状压dp】

题目:ZOJ Problem Set - 2563 Long Dominoes 题意:给出1*3的小矩形,求覆盖m*n的矩阵的最多的不同的方法数? 分析:有一道题目是1 * 2的,比较火,链接:这里 这个差不多,就是当前行的状态对上一行有影响,对上上一行也有影响.所以 定义状态:dp[i][now][up]表示在第 i 行状态为now ,上一行状态为 up 时的方案数. 然后转移方程:dp[i][now][up] = sum ( dp[i-1][up][uup] ) 前提是合法 合法性的判断是比

ZOJ Problem Set - 3820 Building Fire Stations 【树的直径 + 操作 】

题目:problemId=5374" target="_blank">ZOJ Problem Set - 3820 Building Fire Stations 题意:给出n个点,n-1条边的一棵树.然后要在两个点上建立两个消防站.让全部点的到消防站最大距离的点的这个距离最小. 分析:首先先求这个树的直径.然后在树的直径的中点处把树分成两棵树.然后在把两棵树分别取中点的最大值就是ans值. 这个题目数据有点水了感觉... AC代码: #include <cstdi

ZOJ Problem Set - 1025解题报告

ZOJ Problem Set - 1025 题目分类:动态规划 原题地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1025   题目大意就是有很多木头,都有各自的长度和重量.现在要加工这些木头,如果加工某根木头的长度和重量大于等于它上一根木头的长度和重量,那么加工它不需要时 间,否则要花1分钟.现给出一堆木头的长度和重量,要求加工完这堆木头可以花的最少时间.例如给出5根木头长度重量分别为(4,9), (5,2),

ZOJ Problem Set - 3321 并查集

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3321 Circle Time Limit: 1 Second      Memory Limit: 32768 KB Your task is so easy. I will give you an undirected graph, and you just need to tell me whether the graph is just a circle.

ZOJ Problem Set - 3804 YY&#39;s Minions

学习:换一个角度考虑问题.YY's Minions Time Limit: 2 Seconds      Memory Limit: 65536 KB Despite YY's so much homework, she would like to take some time to play with her minions first. YY lines her minions up to an N*M matrix. Every minion has two statuses: awake

ZOJ Problem Set - 3195 Design the city 【Tarjan离线LCA】

题目:ZOJ Problem Set - 3195 Design the city 题意:给出一个图,求三点的连起来的距离. 分析:分别求出三点中任意两点的距离 / 2  = ans AC代码: #include <iostream> #include <cstdio> #include <cstring> #include <vector> using namespace std; #define N 50010 #define M 20010 struc

ZOJ Problem Set - 3229 Shoot the Bullet 【有上下界网络流+流量输出】

题目:ZOJ Problem Set - 3229 Shoot the Bullet 分类:有源有汇有上下界网络流 题意:有 n 天和 m 个girls,然后每天给一部分girls拍照,每个girls 有拍照的下限,即最少要拍这么多张,然后每天有k个女孩拍照,摄影师最多可以拍num张,然后 k 个女该每天拍照数量值有上下限,然后问你有没有满足这样条件的给女孩拍照的最大方案,然后按照输入输出每天给女孩拍照的张数. 做这道题目推荐先做:这儿 分析:首先它让你判断能不能满足条件. 按照题目给出的条件很

ZOJ Problem Set - 3203 Light Bulb 【三分法】

题目:ZOJ Problem Set - 3203 Light Bulb 题意: 如图,有个人在地上走,然后他的影子可以投影到墙上或者地上,问影子最长是多少? 分析: 我们知道,三分法是解决一个凹或凸函数的极大极小值,发现这个影子从刚好投影到右下角开始便是一个凸函数,他的影子长度是先递增后递减的,所以可以用三分法. 三分法的原理: AC代码: #include <cstdio> #include <cstring> #include <vector> #include