HDU 3280 Equal Sum Partitions(二分查找)

摘要:
=n){hh+=temp;pos=upper_bound(sum+1,sum+n+1,hh)-(sum+1);if(sum[pos]!=hh){returnfalse;}}returntrue;}intmain(){inti,j,t,ca;sum[0]=0;scanf(“%d”,&t);while(t--){scanf(”%d%d“,&ca,&n);intx;for(i=1;i˂=n;i++){scanf[”%d“,&x);sum[i]=sum[i-1]+x;}for(i=1;i˂/=n;i++)if(fdd(sum[i]))break;printf(“%d%d”,ca,sum[i]);}return0;}

Equal Sum Partitions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 551    Accepted Submission(s): 409


Problem Description
An equal sum partition of a sequence of numbers is a grouping of the numbers (in the same order as the original sequence) in such a way that each group has the same sum. For example, the sequence:
2 5 1 3 3 7
may be grouped as:
(2 5) (1 3 3) (7)
to yield an equal sum of 7.

Note: The partition that puts all the numbers in a single group is an equal sum partition with the sum equal to the sum of all the numbers in the sequence.

For this problem, you will write a program that takes as input a sequence of positive integers and returns the smallest sum for an equal sum partition of the sequence.
 

Input
The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets that follow. The first line of each data set contains the data set number, followed by a space, followed by a decimal integer M, (1 ≤ M ≤ 10000), giving the total number of integers in the sequence. The remaining line(s) in the dataset consist of the values, 10 per line, separated by a single space. The last line in the dataset may contain less than 10 values.
 

Output
For each data set, generate one line of output with the following values: The data set number as a decimal integer, a space, and the smallest sum for an equal sum partition of the sequence.
 

Sample Input
3 1 6 2 5 1 3 3 7 2 6 1 2 3 4 5 6 3 20 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1
 

Sample Output
1 7 2 21 3 2
 

Source
 

Recommend



/*
 题意:n个数,分成若干个集合,要求每一个集合的数和同样,求集合最小值
 思路:枚举当前集合推断是否满足条件

*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

typedef __int64 ll;

#define N 10005
#define INF 0x3f3f3f3f

int sum[N];
int n;

bool fdd(ll temp)
{
     int hh=0;
     int pos=0;
     while(pos!=n)
     {
         hh+=temp;
         pos=upper_bound(sum+1,sum+n+1,hh)-(sum+1);
         if(sum[pos]!=hh)
         {
            return false;
         }
     }
     return true;
}

int main()
{
    int i,j,t,ca;
    sum[0]=0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&ca,&n);
        int x;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&x);
            sum[i]=sum[i-1]+x;
        }


        for(i=1;i<=n;i++)
            if(fdd(sum[i])) break;

        printf("%d %d
",ca,sum[i]);
    }
    return 0;
}


免责声明:文章转载自《HDU 3280 Equal Sum Partitions(二分查找)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇vb6如何将MSHFlexGrid控件中的内容导出为ExcelLinux驱动学习 —— 在/sys下面创建目录示例下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

为什么索引可以让查询变快?终于有人说清楚了!

概述 人类存储信息的发展历程大致经历如下: 由于是个人凭着自己理解总结的,因此可能不一定精确,但是毋庸置疑的是,在当代,各大公司机构部门的数据都是维护在数据库当中的。数据库作为数据存储介质发展的最新产物,必然是具有许多优点的,其中一个很大的优点就是存储在数据库中的数据访问速度非常快。 数据库访问速度快的一个很重要的原因就在于索引index的作用。也就是这...

python之递归函数、二分查找、面向对象、封装(6)

本节内容:递归函数、二分查找、面向对象、什么是类、什么是对象、组合、面向对象的三大特征 1.递归函数 2.二分查找 3.面向对象 3.1.什么是类 3.2.什么是对象 3.3.类名称空间、对象名称空间 4.组合 5.面向对象的三大特征 1、递归函数 递归函数:在一个函数里在调用这个函数本身。 递归的最大深度:998 正如你们刚刚看到的,递归函数如果不受到...

c# 二分查找

   // perform a binary search on the data   public int BinarySearch( int searchElement )   {  private int[] data;      int low = 0; // low end of the search area      int high = d...

程序员必知3大查找(转)

  三种查找算法:顺序查找,二分法查找(折半查找),分块查找,散列表(以后谈)       一、顺序查找的基本思想: 从表的一端开始,顺序扫描表,依次将扫描到的结点关键字和给定值(假定为a)相比较,若当前结点关键字与a相等,则查找成功;若扫描结束后,仍未找到关键字等于a的结点,则查找失败。   说白了就是,从头到尾,一个一个地比,找着相同的就成功,找不到...

二分查找之新手篇——函数使用

首先介绍c++万能头文件 #include<bits/stdc++.h> using namespace std;(比较适合偷懒,但是不能确保不会出错)(用还是可以的,粗错了再回头来改嘛)owo 接下来介绍一下两个二分查找函数; upper_bound()与lower_bound(); ForwardIter lower_bound(Forwa...

UVa 10539 (筛素数、二分查找) Almost Prime Numbers

题意: 求正整数L和U之间有多少个整数x满足形如x=pk 这种形式,其中p为素数,k>1 分析: 首先筛出1e6内的素数,枚举每个素数求出1e12内所有满足条件的数,然后排序。 对于L和U,二分查找出小于U和L的最大数的下标,作差即可得到答案。 1 #include <cstdio> 2 #include <cmath> 3...