BZOJ1082 二分搜索

1082: [SCOI2005]栅栏

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 2247  Solved: 952
[Submit][Status][Discuss]

Description

  农夫约翰打算建立一个栅栏将他的牧场给围起来,因此他需要一些特定规格的木材。于是农夫约翰到木材店购
买木材。可是木材店老板说他这里只剩下少部分大规格的木板了。不过约翰可以购买这些木板,然后切割成他所需
要的规格。而且约翰有一把神奇的锯子,用它来锯木板,不会产生任何损失,也就是说长度为10的木板可以切成长
度为8和2的两个木板。你的任务:给你约翰所需要的木板的规格,还有木材店老板能够给出的木材的规格,求约翰
最多能够得到多少他所需要的木板。

Input

  第一行为整数m(m<= 50)表示木材店老板可以提供多少块木材给约翰。紧跟着m行为老板提供的每一块木板的长
度。接下来一行(即第m+2行)为整数n(n <= 1000),表示约翰需要多少木材。接下来n行表示他所需要的每一块木板
的长度。木材的规格小于32767。(对于店老板提供的和约翰需要的每块木板,你只能使用一次)。

Output

  只有一行,为约翰最多能够得到的符合条件的木板的个数。

Sample Input

4

30

40

50

25

10

15

16

17

18

19

20

21

25

24

30

Sample Output

7

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[55],b[1111],sb[1111],sa;
int bl[1111];
int mid,n,m;
bool flag;
void dfs(int ak,int bk,int w)
{
    if(bk==0) flag=1;
    while(ak<=n&&a[ak]<b[1]){w+=a[ak];++ak;}
    if(w+sb[mid]>sa||ak>n) return;
    if(flag) return;
    int t=ak;
    if(b[bk]==b[bk+1]&&bk!=mid) t=bl[bk+1];
    for(int i=t; i<=n; ++i)
    {
        if(a[i]>=b[bk])
        {
            a[i]-=b[bk];
            bl[bk]=i;
            dfs(ak,bk-1,w);
            a[i]+=b[bk];
        }
    }
}
int main()
{
    scanf("%d",&n);
    for(int i=1; i<=n; ++i) scanf("%d",a+i);
    scanf("%d",&m);
    for(int i=1; i<=m; ++i) scanf("%d",b+i);
    sort(a+1,a+n+1);
    sort(b+1,b+m+1);
    while(b[m]>a[n]) --m;
    int tot=0;
    for(int i=1; i<=n; ++i)
        if(a[i]>=b[1]) a[++tot]=a[i];
    n=tot;
    for(int i=1; i<=m; ++i) sb[i]=sb[i-1]+b[i];
    for(int i=1; i<=n; ++i) sa+=a[i];
    int l=1,r=m,ans=0;
    while(l<=r)
    {
        mid=(l+r)>>1;
        flag=0;
        dfs(1,mid,0);
        if(flag) ans=mid,l=mid+1;
        else r=mid-1;
    }
    printf("%d\n",ans);
}

时间: 2024-08-25 20:18:31

BZOJ1082 二分搜索的相关文章

二分搜索之C++实现

二分搜索之C++实现 一.源代码:BinarySearch.cpp 1 #include<iostream> 2 using namespace std; 3 4 /*定义输出一维数组的函数*/ 5 void print(int array[], int n) 6 { 7 for (int i = 0; i < n; i++) 8 { 9 cout << array[i] << " "; 10 } 11 cout << endl;

HDU 2199 Can you solve this equation?(二分搜索)

题目链接 Problem Description Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,can you find its solution between 0 and 100;Now please try your lucky. Input The first line of the input contains an integer T(1<=T<=100) which means the number of

北大ACM2456——Aggressive cows~~二分搜索

这一题,也是简单的二分搜索,求解放置的牛之间的距离尽可能远,也就是最大化最小值. 主要的一步就是将第i头牛放在了x[j]的位置中,第i + 1 头牛就要放在满足x[j] + d < x [k],k的最小值. 下面是AC的代码: #include <iostream> #include <algorithm> using namespace std; int N, M; int X[100005]; bool C(int x) { int last = 0; for(int i

【贪心专题】POJ 3258 River Hopscotch (最大化最小值 贪心+二分搜索)

链接:click here~~ [题意] 一条河长度为 L,河的起点(Start)和终点(End)分别有2块石头,S到E的距离就是L,河中有n块石头,每块石头到S都有唯一的距离,,现在要你移除其中的m块,使得具有最小间距的相邻两块石头之间的距离最大. [解题思路] 又是一道经典的二分搜索,跟前一道一样的思路,不过要注意的是:此题是移除其中的元素,从而达到最大化的最小值. 代码: #include <stdio.h> #include <string.h> #include <

NYOJ306 走迷宫(dfs+二分搜索)

题目描述 http://acm.nyist.net/JudgeOnline/problem.php?pid=306 Dr.Kong设计的机器人卡多非常爱玩,它常常偷偷跑出实验室,在某个游乐场玩之不疲.这天卡多又跑出来了,在SJTL游乐场玩个不停,坐完碰碰车,又玩滑滑梯,这时卡多又走入一个迷宫.整个迷宫是用一个N * N的方阵给出,方阵中单元格中填充了一个整数,表示走到这个位置的难度. 这个迷宫可以向上走,向下走,向右走,向左走,但是不能穿越对角线.走迷宫的取胜规则很有意思,看谁能更快地找到一条路

[LeetCode] Largest BST Subtree 最大的二分搜索子树

Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest means subtree with largest number of nodes in it. Note:A subtree must include all of its descendants.Here's an example: 10 / 5 15 / \ \ 1 8 7 The Largest

POJ 1064 1759 3484 3061 (二分搜索)

POJ 1064 题意 有N条绳子,它们长度分别为Li.如果从它们中切割出K条长度相同的绳子的话,这K条绳子每条最长能有多长?答案保留小数点后2位. 思路 二分搜索.这里要注意精度问题,代码中有详细说明:还有printf的%.2f会四舍五入的,需要*100再取整以截取小数点后两位. #include<stdio.h> #include<string.h> #include<string> #include<iostream> #include<math

K Best(最大化平均数)_二分搜索

Description Demy has n jewels. Each of her jewels has some value vi and weight wi. Since her husband John got broke after recent financial crises, Demy has decided to sell some jewels. She has decided that she would keep k best jewels for herself. Sh

二分搜索

// CodeForces 600B//二分搜索 时间复杂度为O(log n)#include<stdio.h> #include<string.h> #include<stdlib.h> #include<ctype.h> #include<math.h> #include<algorithm> using namespace std; #define N 200100 #define INF 0x3f3f3f3f int a[N]