square869120Contest #4 B.Buildings are Colorful!【二进制枚举】

B - Buildings are Colorful!


Time limit : 1sec / Memory limit : 256MB

Max Score:  350 Points 

Problem Statement

There are  N buildings along the line. The  i-th building from the left is colored in color  i, and its height is currently  ai meters. 
Chokudai is a mayor of the city, and he loves colorful thigs. And now he wants to see at least  K buildings from the left. 

You can increase height of buildings, but it costs  1 yens to increase  1 meters. It means you cannot make building that height is not integer. 
You cannot decrease height of buildings. 
Calculate the minimum cost of satisfying Chokudai's objective. 
Note: "Building  i can see from the left" means there are no  j exists that (height of building  j) ≥ (height of building  i) and  j<i

Input Format

Copy
N K
a1 a2 a3 ... aN

Output Format

Print the minimum cost in one line. In the end put a line break.

Constraints

  • 1KN15
  • 1ai109

Scoring

Subtask 1 [ 120 points] 
  • N=K
Subtask 2 [ 90 points] 
  • N5
  • ai7
Subtask 3 [ 140 points] 
  • There are no additional constraints.

Sample Input 1

Copy
5 5
3949 3774 3598 3469 3424

Sample Output 1

Copy
1541
The optimal solution is (height of buildings from the left)  =[3949,3950,3951,3952,3953].

Sample Input 2

Copy
5 3
7 4 2 6 4

Sample Output 2

Copy
7
The optimal solution is (height of buildings from the left)  =[7,8,2,9,4].
题目大意:

现在给你N个楼房,现在希望看见至少K个楼房,一个楼房被看见的要求就是这个房子的左边没有比他高的房子。

现在给一个楼房加一个高度花费1.


思路:


观察到N并不大,那么暴力枚举即可。

注意细节。


Ac代码:


#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define ll long long int
ll a[20];
ll vis[20];
ll n,k;
ll output;
void Dfs(ll u)
{
    if(u==n+1)
    {
        ll b[20];
        ll cnt=0;
        ll pre=a[1];
        for(ll i=1;i<=n;i++)if(vis[i]==1)cnt++;
        for(ll i=1;i<=n;i++)b[i]=a[i];
        if(cnt==k)
        {
            ll sum=0;
            for(ll i=2;i<=n;i++)
            {
                if(vis[i]==1)
                {
                    if(b[i]>pre)pre=b[i];
                    else
                    {
                        sum+=pre+1-b[i];
                        b[i]=pre+1;
                        pre=b[i];
                    }
                }
                else pre=max(pre,b[i]);
            }
            ll look=1;
            for(ll i=2;i<=n;i++)
            {
                ll flag=0;
                for(ll j=1;j<i;j++)
                {
                    if(b[j]>b[i])flag=1;
                }
                if(flag==0)look++;
            }
            if(look==k)
            {
                output=min(output,sum);
            }
        }
        return ;
    }
    vis[u]=1;
    Dfs(u+1);
    if(u!=1)
    {
        vis[u]=0;
        Dfs(u+1);
    }
}
int main()
{
    while(~scanf("%lld%lld",&n,&k))
    {
        output=2000000000000000000;
        memset(vis,0,sizeof(vis));
        for(ll i=1;i<=n;i++)scanf("%lld",&a[i]);
        Dfs(1);
        printf("%lld\n",output);
    }
}











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值