专题5:贪心

题目 

问题 P: Streamline

时间限制: 1 Sec  内存限制: 128 MB
[提交] [状态]

题目描述

We will play a one-player game using a number line and N pieces.
First, we place each of these pieces at some integer coordinate.
Here, multiple pieces can be placed at the same coordinate.
Our objective is to visit all of the M coordinates X1,X2,...,XM with these pieces, by repeating the following move:
Move: Choose a piece and let x be its coordinate. Put that piece at coordinate x+1 or x−1.
Note that the coordinates where we initially place the pieces are already regarded as visited.
Find the minimum number of moves required to achieve the objective.

Constraints
All values in input are integers.
·1≤N≤105
·1≤M≤105
·−105≤Xi≤105
·X1,X2,...,XM are all different.

输入

Input is given from Standard Input in the following format:

N M
X1 X2 ... XM

输出

Find the minimum number of moves required to achieve the objective.

样例输入 Copy

2 5
10 12 1 2 14

样例输出 Copy

5

题意 

取 n个为头,其余的由这n个头左移动1或者右移动1.实际是都把看成左移也未尝不可。

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
typedef pair<int,int> pp;
const int N=1e5+5;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
const double pi=acos(-1);
int A[N],dis[N];
bool cmp(int x,int y)
{
    return x>y;
}
int main()
{
    int n,m,ans=0;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
     scanf("%d",&A[i]);
    }
    sort(A+1,A+1+m,cmp);
    for(int i=1;i<m;i++)
        dis[i]=-A[i+1]+A[i];
    sort(dis+1,dis+m,cmp);
    for(int i=n;i<m;i++)
        ans+=dis[i];
    printf("%d",ans);
    return 0;
}
 
/**************************************************************
    Problem: 12076
    User: 2019UPC110
    Language: C++
    Result: 正确
    Time:65 ms
    Memory:2808 kb
****************************************************************/

题目:

问题 K: Grand Garden

时间限制: 1 Sec  内存限制: 128 MB
[提交] [状态]

题目描述

In a flower bed, there are N flowers, numbered 1,2,......,N. Initially, the heights of all flowers are 0. You are given a sequence h={h1,h2,h3,......} as input. You would like to change the height of Flower k to hk for all k (1≤k≤N), by repeating the following "watering" operation:
Specify integers l and r. Increase the height of Flower x by 
1 for all x such that l≤x≤r.
Find the minimum number of watering operations required to satisfy the condition.

Constraints
·1≤N≤1000≤hi≤100
·All values in input are integers.

输入

Input is given from Standard Input in the following format:
N
h1 h2 h3 ...... hN

 

输出

Print the minimum number of watering operations required to satisfy the condition.

样例输入 Copy

4
1 2 2 1

样例输出 Copy

2

提示

The minimum number of watering operations required is 2. One way to achieve it is:
Perform the operation with (l,r)=(1,3).
Perform the operation with (l,r)=(2,4).

 

 题意:

现在所有树高度为0,每浇水(操作)一次,(l,r)范围内的树就会长高1个单位,求最小操作数和

思路:

就是标记前一次的操作数k,如果现在树要长的高度比k小,说明,此时对应前面相当于此时这棵树多操作k-A【i】次;


#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
typedef pair<int,int> pp;
const int N=1e5+5;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
const double pi=acos(-1);
int A[N];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&A[i]);
    }
    int k=A[1];
    int ans=0;
    for(int i=1; i<=n; i++)
    {
        if(i==n)
        {
            if(A[n]>=k)
             k=A[n];
             ans+=k;
        }
        else
        {
            if(A[i]>=k)
                k=A[i];
            else
            {
                ans+=k-A[i];
                k=A[i];
            }
 
 
        }
 
    }
    printf("%d",ans);
    return 0;
}
 
/**************************************************************
    Problem: 12080
    User: 2019UPC110
    Language: C++
    Result: 正确
    Time:1 ms
    Memory:2412 kb
****************************************************************/

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值