Codeforces Round #526 (Div. 2) A.B

A. The Fair Nut and Elevator

题目链接:https://codeforces.com/contest/1084/problem/A

题意:

一栋房子有n层楼,同时有个电梯(每次只能载一个人),每层楼都有ai个人。

当电梯从x层到y层时,花费电力|x-y|。

现在要求当电梯位于哪一层时,所有人上下两次费用最少。电梯每次到一层后若电梯里面没人会回到我们选定的楼层。

 

题解:

枚举电梯位于的层数模拟一下就好了。

 

代码如下:

#include <bits/stdc++.h>
using namespace std;

const int N = 105;
int a[N];
int n;

int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    int ans = 99999999;
    for(int x=1;x<=n;x++){
        int sum = 0;
        for(int i=1;i<=n;i++)
            sum+=(abs(x-i)+i+x-2)*a[i]*2;
        ans=min(ans,sum);
    }
    printf("%d",ans);
    return 0;
}

 

 

B. Kvass and the Fair Nut

题目链接https://codeforces.com/contest/1084/problem/B

题意:

给出n个桶,每个桶里面都有一定高度的东西,现在要从里面总共减少s的高度。问最后桶里面最小高度的最大值是多少。当桶里面高度都为0而s为正时,则输出-1。

 

题解:

一开始听他们说是二分,的确二分也可以。但这题没这个必要,直接贪心就好了。

首先我们肯定减少高度较高的高度,当所有高度都一样时,这时就n个n个地减少,这样即可保证高度最小值最大。

 

代码如下:

#include <bits/stdc++.h>
#define INF 9999999999999999
using namespace std;
typedef long long ll;
const int N = 1005;
ll n,s,minx=INF;
ll v[N];
int main(){
    cin>>n>>s;
    for(int i=1;i<=n;i++) scanf("%I64d",&v[i]),minx=min(minx,v[i]);
    ll sum=0;
    for(int i=1;i<=n;i++) sum+=(v[i]-minx);
    if(sum>=s){
        cout<<minx;
        return 0;
    }
    s-=sum;
    ll now = s/n+(s%n!=0);
    ll ans = minx-now;
    if(ans>=0) cout<<ans;
    else cout<<-1;
    return 0;
}

 

The Fair Nut lives in nn story house. aiai people live on the ii-th floor of the house. Every person uses elevator twice a day: to get from the floor where he/she lives to the ground (first) floor and to get from the first floor to the floor where he/she lives, when he/she comes back home in the evening.

It was decided that elevator, when it is not used, will stay on the xx-th floor, but xx hasn't been chosen yet. When a person needs to get from floor aa to floor bb, elevator follows the simple algorithm:

  • Moves from the xx-th floor (initially it stays on the xx-th floor) to the aa-th and takes the passenger.
  • Moves from the aa-th floor to the bb-th floor and lets out the passenger (if aa equals bb, elevator just opens and closes the doors, but stillcomes to the floor from the xx-th floor).
  • Moves from the bb-th floor back to the xx-th.

The elevator never transposes more than one person and always goes back to the floor xx before transposing a next passenger. The elevator spends one unit of electricity to move between neighboring floors. So moving from the aa-th floor to the bb-th floor requires |ab||a−b|units of electricity.

Your task is to help Nut to find the minimum number of electricity units, that it would be enough for one day, by choosing an optimal the xx-th floor. Don't forget than elevator initially stays on the xx-th floor.

posted @ 2018-12-13 20:52  heyuhhh  阅读(120)  评论(0编辑  收藏  举报