codeforce 557C Arthur and Table

原题地址:http://codeforces.com/problemset/problem/557/C

题意:

一个桌子有n个桌腿,每个桌腿长l,砍断需要体力d,欲使桌子保持平衡,需要砍断部分桌腿,使得剩余的桌腿中长度最大的那些桌腿的数量大于剩余桌腿数量的一半

求使桌子保持平衡最少需要花费的体力

题解:

以某长度Li的桌腿作为最长桌腿,那么大于Li的桌腿全部砍去,小于的部分则砍去最少的,花费最小的桌腿,不断枚举Li,用基排的思想,复杂度n*d;

#include<bits/stdc++.h>

#define clr(x,y) memset((x),(y),sizeof(x))

using namespace std;
typedef long long LL;

const int maxn=1e5;
const int inf=1e9;

struct P
{
    int l,d;
    bool operator < (const P& rhs) const
    {
        return l>rhs.l;
    }
};

P p[maxn+5];
int dnum[205];

int main(void)
{
    #ifdef ex
    freopen ("../in.txt","r",stdin);
    //freopen ("../out.txt","w",stdout);
    #endif

    int n;
    scanf("%d",&n);

    for (int i=1;i<=n;++i)
    {
        scanf("%d",&p[i].l);
    }
    for (int i=1;i<=n;++i)
    {
        scanf("%d",&p[i].d);
        ++dnum[p[i].d];
    }

    sort(p+1,p+1+n);

    int s=1;
    int cnt=n;
    int ans=inf;
    int cost=0;
    for (int i=1;i<=n;++i)
    {
        if (i!=n && p[i].l==p[i+1].l) continue;
        int temp=cost;
        for (int j=s;j<=i;++j)
        {
            --dnum[p[j].d];
            cost+=p[j].d;
        }

        int n1=i-s+1;
        int n2=cnt-2*n1+1;
        for (int j=1;j<=200;++j)
        {
            if (n2<=0) break;

            int k=min(n2,dnum[j]);
            n2-=k;
            temp+=j*k;
        }

        ans=min(ans,temp);

        cnt-=(i-s+1);
        s=i+1;
    }

    printf("%d\n",ans);
}
时间: 2024-10-27 03:00:28

codeforce 557C Arthur and Table的相关文章

Codeforces 557C Arthur and Table 砍桌腿

题意:有n个桌腿,要砍掉某些桌腿使得剩下的桌腿能支撑桌子.规定剩下的桌腿中长度最大的桌腿的数量如果超过一半即可支撑桌子.砍掉每个桌腿需要付出代价.求最小的代价和. 枚举.假如最后剩下的桌腿的最大长度为lenth,这样长度的桌腿有num个.那么长度大于lenth的桌腿肯定都被砍去了,然后在剩下的桌腿中按照代价从大到小选择num - 1个桌腿留下来(不砍),将剩余的再砍去,这样一定是最小的代价和.当然也不是无脑枚举,能枚举是因为代价种类很少,才200个.首先我们按桌腿的长度排序,然后枚举每种长度作为

C. Arthur and Table

链接 [https://codeforces.com/contest/557/problem/C] 题意 给你每个木棍的长度,以及移除每个木棍的代价 使得桌子稳定,最长的木棍必须大于剩下的一半 分析 这个数据范围,就是得暴力和前缀和还有一些技巧 枚举每一种长度,大于它的全部移除, 那么剩下的就尽可能地移除少量的木棍,而且移除可以移除的最小代价 复杂度2e7左右. 代码 #include<bits/stdc++.h> using namespace std; typedef long long

LUXURY15

A - Guess Your Way Out! Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 507C Description Amr bought a new video game "Guess Your Way Out!". The goal of the game is to find an ex

codeforces 557 C

因为期末,很久没刷题了,CF一直掉-- 这个题其实很简单..因为做法很容易想到嘛.. 就是枚举max=x时,最大能保留多少价值,不断更新ans, 结果就是所有价值和减去ans就好 由于最大能够保留的长度是199+200,所以当max=x时,算最大能保留多少价值, 也是一个循环算出当前长度比x小的那个桌子角的最大的那几个价值之和保留就行了, 这里写的比较绕..反正看看代码一下就懂了... 维护一个map就行了 比赛的时候,因为好久没刷题了,一直没想清楚,一直到比赛结束前还剩下十分钟才恍然大悟..

Codeforces Round #311 (Div. 2) A,B,C,D,E

A. Ilya and Diplomas 思路:水题了, 随随便便枚举一下,分情况讨论一下就OK了. code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <algorithm> #include <iostream> #include <cstring> #include <cmath> #define inf 1000000

2017-4-25-Train:Codeforces Round #311 (Div. 2)

A. Ilya and Diplomas(贪心) Soon a school Olympiad in Informatics will be held in Berland, n schoolchildren will participate there. At a meeting of the jury of the Olympiad it was decided that each of the n participants, depending on the results, will g

Codeforce 22B Bargaining Table

B. Bargaining Table Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n?×?m meters. Each square meter of the room is either occupied by so

poj2924--F - Knights of the Round Table(圆桌骑士,经典连通分量)

F - Knights of the Round Table Time Limit:7000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress, and drinking w

Code First Migrations: Making __MigrationHistory not a system table

https://blog.oneunicorn.com/2012/02/27/code-first-migrations-making-__migrationhistory-not-a-system-table/ Code First Migrations uses a table called __MigrationHistory as a place to store metadata about the migrations that have been applied to the da