B - Buildings are Colorful!
Time limit : 1sec / Memory limit : 256MB
Max Score:
350 Points
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.
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
- 1≤K≤N≤15
- 1≤ai≤109
Scoring
Subtask 1 [ 120 points]- N=K
- N≤5
- ai≤7
- There are no additional constraints.
Sample Input 1
Copy
5 5 3949 3774 3598 3469 3424
Sample Output 1
Copy
1541The 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
7The 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);
}
}