题目描述 Given a list of N integers with absolute values no larger than 10 15, find a non empty subset of these numbers which minimizes the absolute value of the sum of its elements. In case there are multiple subsets, choose the one with fewer elements.…
1514: Packs Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 61  Solved: 4[Submit][Status][Web Board] Description Give you n packs, each of it has a value v and a weight w. Now you should find some packs, and the total of these value is max, total of…
题目链接:https://vjudge.net/problem/POJ-3977 题意:给一个大小<=35的集合,找一个非空子集合,使得子集合元素和的绝对值最小,如果有多个这样的集合,找元素个数最少的. 思路:显然,可以用折半搜索,分别枚举一半,最大是2的18次方,复杂度能够满足.因为集合非空,枚举时考虑只在前一半选和只在后一半选的情况.对于前一半后一半都选的情况,把前一半的结果存下来,排序,枚举后一半的时候在前一半里二分查找最合适的即可. 思路不难,实现有很多细节,最开始用dfs写得一直wa,…
题目链接:http://poj.org/problem?id=3977 给你n个数,找到一个子集,使得这个子集的和的绝对值是最小的,如果有多种情况,输出子集个数最少的: n<=35,|a[i]|<=10e15 子集个数共有2^n个,所以不能全部枚举,但是可以分为两部分枚举: 枚举一半的所有情况,然后后一半二分即可: #include<iostream> #include<algorithm> #include<string.h> #include<st…
链接: http://poj.org/problem?id=3977 题意: 给你n个数,n最大35,让你从中选几个数,不能选0个,使它们和的绝对值最小,如果有一样的,取个数最小的 思路: 子集个数共有2^n个,所以不能全部枚举,但是可以分为两部分枚举:枚举一半的所有情况,然后后一半二分即可: 代码: #include"bits/stdc++.h" #define N 45 using namespace std; typedef long long LL; int n; LL a[N…
POJ 3273 Monthly Expense二分查找(最大值最小化问题) 题目:Monthly Expense Description Farmer John is an astounding accounting wizard and has realized he might run out of money to run the farm. He has already calculated and recorded the exact amount of money (1 ≤ mon…
题目内容 Vjudge链接 给你\(n\)个数,求出这\(n\)个数的一个非空子集,使子集中的数加和的绝对值最小,在此基础上子集中元素的个数应最小. 输入格式 输入含多组数据,每组数据有两行,第一行是元素组合\(n\)(若\(n\)为0表示输入结束),第二行有\(n\)个数,表示要给出的\(n\)个数. 数据范围 \(n\le 35\) 输出格式 每组数据输出一行两个数中间用空格隔开,表示最小的绝对值和该子集的元素个数. 样例输入 1 10 3 20 100 -100 0 样例输出 10 1 0…
SubsetTime Limit: 30000MS        Memory Limit: 65536KTotal Submissions: 6754        Accepted: 1277 DescriptionGiven a list of N integers with absolute values no larger than 1015, find a non empty subset of these numbers which minimizes the absolute v…
Sumsets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11946   Accepted: 3299 Description Given S, a set of integers, find the largest d such that a + b + c = d where a, b, c, and d are distinct elements of S. Input Several S, each cons…
子序列 题目大意:给定一串数字序列,要你从中挑一定个数的数字使这些数字和绝对值最小,求出最小组合数 题目的数字最多35个,一看就是要数字枚举了,但是如果直接枚举,复杂度就是O(2^35)了,显然行不通,所以我们把它的组合拆成两半(前n/2个数字和后n-n/2个数字),然后给前部分和或者后部分和的组合排序,然后再用另一半在其二分查找,看最接近的和,这就是折半枚举的思想 不过这一题有很多细节: 1.和是不固定的,要左右各查找1个,如果有重复元素,一定要越过重复元素再找(lower_bound和upp…
POJ 3273 Monthly Expense 此题与POJ3258有点类似,一开始把判断条件写错了,wa了两次,二分查找可以有以下两种: ){ mid=(lb+ub)/; if(C(mid)<=m) ub=mid; ; //此时下限过小 } out(ub);//out(lb) 我一开始是写的下面这种,下面这种要单独判断lb和ub的值,因为用下面这种判断lb,ub都可能成立 ){ mid=(lb+ub)/; if(C(mid)<=m) ub=mid; else lb=mid; } if(C(…
传送门:Problem 2785 题意: 给定 n 行数,每行都有 4 个数A,B,C,D. 要从每列中各抽取出一个数,问使四个数的和为0的所有方案数. 相同数字不同位置当作不同数字对待. 题解: 如果采用暴力的话,从4个数列中选择数组合,共有(N^4)种选择,故时间复杂度为O(N^4),指定会超时. 但,如果将它们分成 AB,CD两组,每组只有 N^2 个组合,而 N 的数据范围为 N < 4000,故采用此种方法不会超时. AC代码: #include<iostream> #incl…
[题目链接] http://poj.org/problem?id=3977 [题目大意] 在n个数(n<36)中选取一些数,使得其和的绝对值最小. [题解] 因为枚举所有数选或者不选,复杂度太高无法承受, 我们考虑减小枚举的范围,我们将前一半进行枚举,保存其子集和, 然后后一半枚举子集和取反在前一半中寻找最接近的,两部分相加用以更新答案. [代码] #include <cstdio> #include <utility> #include <algorithm>…
4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 25675   Accepted: 7722 Case Time Limit: 5000MS Description The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how…
题目描述: Prime Gift time limit per test 3.5 seconds memory limit per test 256 megabytes input standard input output standard output Opposite to Grisha's nice behavior, Oleg, though he has an entire year at his disposal, didn't manage to learn how to sol…
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #define N 2000 #define M 1000010 #define inf 1<<30 using namespace std; struct Edge{ int to,val,next; }edge[M]; ]; void addedge(int from,int to,int val…
题目地址:http://poj.org/problem?id=2456 最大化最小值问题.二分牛之间的间距,然后验证. #include<cstdio> #include<iostream> #include<string.h> #include<algorithm> #include<math.h> #include<stdbool.h> #include<time.h> #include<stdlib.h>…
题目地址:http://poj.org/problem?id=1064 有N条绳子,它们的长度分别为Ai,如果从它们中切割出K条长度相同的绳子,这K条绳子每条最长能有多长. 二分绳子长度,然后验证即可.复杂度o(nlogm) #include<cstdio> #include<iostream> #include<string.h> #include<algorithm> #include<math.h> #include<stdbool.…
POJ 3579 题意 双重二分搜索:对列数X计算∣Xi – Xj∣组成新数列的中位数 思路 对X排序后,与X_i的差大于mid(也就是某个数大于X_i + mid)的那些数的个数如果小于N / 2的话,说明mid太大了.以此为条件进行第一重二分搜索,第二重二分搜索是对X的搜索,直接用lower_bound实现. #include <iostream> #include <algorithm> #include <cstdio> #include <cmath&g…
题目地址:http://poj.org/problem?id=3122 二分每块饼的体积.为了保证精度,可以先二分半径的平方r*r,最后再乘以PI.要注意一点,要分的人数要包括自己,及f+1. #include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<cmath> using namespace std; ; const double PI…
题目链接 Description The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how many quadruplet (a, b, c, d ) ∈ A x B x C x D are such that a + b + c + d = 0 . In the following, we assume that all lists have…
题目链接:http://poj.org/problem?id=3104 Drying Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9440   Accepted: 2407 Description It is very hard to wash and especially to dry clothes in winter. But Jane is a very smart girl. She is not afrai…
#include<iostream> #include<algorithm> #include<cstring> #include<map> #include<set> #define MAX_N 35 #define MAX_T 500005 using namespace std; typedef long long ll; map<ll,int> ma[2]; ll n,m,a[MAX_N],tot[2]={0,0}; ll d…
The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how many quadruplet (a, b, c, d ) ∈ A x B x C x D are such that a + b + c + d = 0 . In the following, we assume that all lists have the same size n .…
Subset Time Limit: 30000MS   Memory Limit: 65536K Total Submissions: 5721   Accepted: 1083 Description Given a list of N integers with absolute values no larger than 1015, find a non empty subset of these numbers which minimizes the absolute value of…
题意:有一个N(N <= 35)个数的集合,每个数的绝对值小于等于1015,找一个非空子集,使该子集中所有元素的和的绝对值最小,若有多个,则输出个数最小的那个. 分析: 1.将集合中的元素分成两半,分别二进制枚举子集并记录子集所对应的和以及元素个数. 2.枚举其中一半,二分查找另一半,不断取最小值. #pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<c…
2017-08-01 21:45:19 writer:pprp 题目: • POJ 3977• 给定n个数,求一个子集(非空)• 使得子集内元素和的绝对值最小• n ≤ 35 AC代码如下:(难点:枚举出sum) #include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm&…
Eqs Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 13955   Accepted: 6851 Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 The coefficients are given integers from the interval [-50,50]. It i…
Subset Time Limit: 30000MS   Memory Limit: 65536K Total Submissions: 3161   Accepted: 564 Description Given a list of N integers with absolute values no larger than 1015, find a non empty subset of these numbers which minimizes the absolute value of…
题目链接: http://poj.org/problem?id=2452 题意:在区间[1,n]上找到满足 a[i]<a[k]<a[j] (i<=k<=j) 的最大子区间 (j-i)如不存在输出 -1. 思路:枚举i,找到 i右边第一个不大于(不是小于) a[i]的数a[k](二分查找+RMQ某段区间的最小值是否小于a[i].最后确定到一个点),于是我们可以得到在区间[i,k-1]范围内的数都会大于 a[i] ,所以对于下标i,它对应的最长区间必定在[i,k-1]之间. 所以,我们…