分治算法

划分问题:把问题的实例划分成子问题

递归求解:递归解决子问题

合并问题:合并子问题的解得到原问题的解

 

例题1 :最大连续和

                                                                                C. 最大子段和

NN个整数组成的序列 a[1],a[2],a[3],…,a[n],求该序列如 a[i]+a[i+1]+…+a[j]的连续子段和的最大值。当所给的整数均为负数时和为 0。

例如:-2,11,-4,13,-5,-2,和最大的子段为:11,-4,13。和为 20。

Input

第 11 行:整数序列的长度 NN (2≤N≤50000)(2≤N≤50000)

第 2−N+12−N+1 行:NN 个整数 (−109≤A[i]≤109)(−109≤A[i]≤109)

Output

输出最大子段和

Example

input

6
-2
11
-4
13
-5
-2

output

20
#include <iostream>
#include <algorithm>

using namespace std; 


typedef long long ll;

int arr[50010];
ll fenzhi(int x,int y)
{
	if(y-x==1)  return arr[x];              //分解成子问题
	int m=x+(y-x)/2;
    ll maxs=max(fenzhi(x,m),fenzhi(m,y));   //进行递归
    ll L=arr[m-1],R=arr[m],mid=0;           //合并 
    ll v=0;
    for(int i=m-1;i>=x;i--) L=max(L,v+=arr[i]);  
       v=0;
    for(int i=m;i<y;i++)  R=max(R,v+=arr[i]);
    mid=L+R;
    return max(mid,maxs);
}
int main(int argc, char** argv) 
{   
   // freopen("input.txt","r",stdin);
    int n;
	cin>>n;
	for(int i=0;i<n;i++)
	 cin>>arr[i];
	
	
	cout <<fenzhi(0,n)<<endl;;
	
	return 0;
}

                                                                              C. Creative Snap

Thanos wants to destroy the avengers base, but he needs to destroy the avengers along with their base.

Let we represent their base with an array, where each position can be occupied by many avengers, but one avenger can occupy only one position. Length of their base is a perfect power of 2. Thanos wants to destroy the base using minimum power. He starts with the whole base and in one step he can do either of following:

  • if the current length is at least 2, divide the base into 2 equal halves and destroy them separately, or
  • burn the current base. If it contains no avenger in it, it takes AA amount of power, otherwise it takes his B⋅na⋅lB⋅na⋅l amount of power, where nana is the number of avengers and ll is the length of the current base.

Output the minimum power needed by Thanos to destroy the avengers' base.

Input

The first line contains four integers n, k, A and B (1≤n≤30,1≤k≤105,1≤A,B≤104), where 2n2n is the length of the base, kk is the number of avengers and AA and BB are the constants explained in the question.

The second line contains kk integers a1,a2,a3,…,ak (1≤ai≤2n), where aiai represents the position of avenger in the base.

Output

Output one integer — the minimum power needed to destroy the avengers base.

Examples

input

2 2 1 2
1 3

output

6

input

3 2 1 2
1 7

output

8
#include <iostream>
#include <algorithm>

using namespace std;

typedef unsigned long long ull;
int arr[100010];
int n,k,A,B;
ull fenzhi( ull x, ull y)
{
	int num=upper_bound(arr,arr+k,y-1)-lower_bound(arr,arr+k,x);

	ull ans=0;
	ull mins=0x3f3f3f3f*3;  //最大值 
	if(num==0) return A;
	           
	else
	{   
		if(y-x>1)
		{  
		    ull mid=x+(y-x)/2;                
	        mins=fenzhi(x,mid)+fenzhi(mid,y);	  //递归  
		}
		ans=min(B*(y-x)*num,mins);                //合并
	} 
	
	return ans;
}
int main(int argc, char** argv)
{
    cin >>n>>k>>A>>B;
	for(int i=0;i<k;i++)
	cin>>arr[i];
	sort(arr,arr+k);
    cout <<fenzhi(1,(1<<n)+1)<<endl;
	return 0;
}

3.

 

线段数+分治

线段数记录 [l,r] 区间最小值,ans[ ]数组记录区间和,

分治:每次以区间内最小的店的id作为mid,递归计算mid两边怕[l,mid-1],[mid+1,r]的最大值,最后合并[l,r],比较[l,mid-1],[mid+1,r],[l,r],取最大值和区间;

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
#define MAX 100050
#define inf 0x3f3f3f;
typedef long long ll;

struct p
{
    int mins;
    int id;
    friend bool operator <(p a,p b)
    {
        return a.mins<b.mins;
    }
};
struct e
{
    ll sum;
    int l,r;
    friend bool operator <(e a,e b)
    {
        return a.sum<b.sum;
    }
};
struct Tree
{
    int l,r;
    p pi;
}tree[4*MAX];

int n,maxs=0;
ll ans[100005];
ll arr[100005];
void push_up(int i)
{
    tree[i].pi=min(tree[i<<1].pi,tree[i<<1|1].pi);
}
void build(int l,int r,int i)
{
    tree[i].l=l;
    tree[i].r=r;
    if(l==r)
    {
        tree[i].pi.id=l;
        tree[i].pi.mins=arr[l];
        return ;
    }

    int mid=(l+r)>>1;
    build(l,mid,i<<1);
    build(mid+1,r,i<<1|1);
    push_up(i);
}
p query(int l,int r,int i)
{
    if(l<=tree[i].l&&tree[i].r<=r)
    {
        return tree[i].pi;
    }
    int mid=(tree[i].l+tree[i].r)>>1;

    p ps;
    ps.mins=inf;
    ps.id=0;
    if(mid>=l)
        ps=min(query(l,r,i<<1),ps);
    if(mid<r)
        ps=min(query(l,r,i<<1|1),ps);
    return ps;
}
e fenzhi(int l,int r)
{
    if(l>r)
    {
          e a;
          a.l=l,a.r=r,a.sum=-inf;
          return a;
    }

    if(l==r)
    {
        e a;
        a.sum=arr[l]*arr[l],a.l=l,a.r=r;
        return a;
    }

    p mid=query(l,r,1);
    e maxs1=max(fenzhi(l,mid.id-1),fenzhi(mid.id+1,r));
   //cout <<"m1 "<<maxs1.sum<<" "<<maxs1.l<<" "<<maxs1.r<<endl;
    e maxs2;
    maxs2.sum=mid.mins*(ans[r]-ans[l-1]);
    maxs2.l=l;
    maxs2.r=r;
    //cout <<"m2 "<<maxs2.sum<<" "<<maxs2.l<<" "<<maxs2.r<<endl;
    e maxs3=max(maxs1,maxs2);

    return maxs3;

}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        cin >>arr[i];
        ans[i]=arr[i]+ans[i-1];
    }
    build(1,n,1);
    e a=fenzhi(1,n);
    cout << a.sum<< endl;
    cout <<a.l<<" "<<a.r<<endl;
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值