POJ scales
来源:互联网 发布:网络视频广告限制 编辑:程序博客网 时间:2024/06/08 19:22
The weights have the curious property that when lined up from smallest to biggest, each weight (from the third one on) has at least as much mass as the previous two combined.
FJ wants to determine the maximum mass that he can use his weights to measure exactly. Since the total mass must be no larger than C, he might not be able to put all the weights onto the scale.
Write a program that, given a list of weights and the maximum mass the balance can take, will determine the maximum legal mass that he can weigh exactly.
Input
Line 1: Two space-separated positive integers, N and C.
Lines 2..N+1: Each line contains a single positive integer that is the mass of one weight. The masses are guaranteed to be in non-decreasing order.
Output
Line 1: A single integer that is the largest mass that can be accurately and safely measured.
Sample Input
3 15
1
10
20
Sample Output
11
Hint
Explanation of the sample:
FJ has 3 weights, with masses of 1, 10, and 20 units. He can put at most 15 units on one side of his balance.
The 1 and 10 weights are used to measure 11. Any greater weight that can be formed from the weights will break the balance.
深搜,剪枝很多,一不小心就TLE了。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
int n;
long long c;
int ans;
long long w[1001];
long long sum[1001];
void dfs(int x,long long s)
{
int i,j;
if(s>ans)ans=s;
if(x<1)return;
for(i=x;i>=1;i--)
{
if(s+w[i]<=c&&s+sum[i]>ans)//剪枝,有继续往下找更贴近c值的必要
dfs(i-1,s+w[i]);
}
}
int main()
{
int i,j;
ans=0;
scanf("%d%d",&n,&c);
memset(sum,0,sizeof(sum));
for(i=1;i<=n;i++)
{
scanf("%d",&w[i]);
sum[i]=sum[i-1]+w[i];
}
dfs(n,0);
cout<<ans<<endl;
return 0;
}
- POJ scales
- poj 3172 Scales
- poj 3172 Scales 搜索
- POJ 3971 Scales(DP
- poj 3172 Scales
- POJ 3971 Scales
- poj #3971:scales
- POJ 3172 Scales
- POJ.3172 Scales (DFS)
- POJ 3172 Scales 笔记
- poj 3971 Scales (dp)
- Scales
- poj 3971 Scales (数位dp,好题)
- POJ 3172 Scales(dfs搜索)
- POJ 3172 Scales (剪枝深搜)
- POJ 3172 Scales (01背包 DFS)
- Scales HDU
- TOJ 3348 Scales
- 2018阿里测试工程师电面记录
- 程序员必须知道的数据库增删改查
- Codeforces 797C Minimal string 简单模拟
- 学习笔记
- VIM 编辑器使用
- POJ scales
- PAT 1007. 素数对猜想 python 运行超时问题解决方案
- 特征选择,处理
- 算法:C++实现先递增后递减队列的最值和单调递增队列的绝对值最小的值
- sql单表增删改查
- c++ 文件判空
- Ecology7二次开发:自定义浏览按钮自定义页面设置返回值
- 1864
- Jam的计数法