Codeforces 458C - Elections

458C - Elections

思路:

三分凹形函数极小值域

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))

const int N=1e5+5;
const int INF=0x7f7f7f7f;
vector<int>g[N],vc;
int n;
int cost(int x){
    int need=x-g[0].size(),ans=0;
    vc.clear();
    for(int i=1;i<N;i++){
        for(int j=0;j<g[i].size();j++){
            if(g[i].size()-j>=x){
                need--;
                ans+=g[i][j];
            }
            else vc.pb(g[i][j]);
        }
    }
    sort(vc.begin(),vc.end());
    for(int i=0;i<need;i++)ans+=vc[i];
    return ans;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int a,b;
    cin>>n;
    for(int i=0;i<n;i++)cin>>a>>b,g[a].pb(b);
    for(int i=0;i<N;i++)sort(g[i].begin(),g[i].end());
    int l=g[0].size(),r=n,m1=(l+l+r)/3,m2=(l+r+r)/3;
    while(l<r){
        if(cost(m1)>cost(m2)){
            if(l==m1)break;
            else l=m1;
        }
        else {
            if(r==m2)break;
            else r=m2;
        }
        m1=(l+l+r)/3;
        m2=(l+r+r)/3;
        //cout<<l<<‘ ‘<<r<<‘ ‘<<m1<<‘ ‘<<m2<<endl;
    }
    int ans=INF;
    for(int i=l;i<=r;i++)ans=min(ans,cost(i));
    cout<<ans<<endl;
    return 0;
}

原文地址:https://www.cnblogs.com/widsom/p/8485663.html

时间: 2024-10-03 06:58:46

Codeforces 458C - Elections的相关文章

Codeforces 458C Elections 贿赂选票抢主席! 线段树

题目链接:点击打开链接 题意: 给定n张选票,每张选票有2个参数,第一个参数表示这张选票选的人 第二个参数表示如果让这张选票改为选0号 的花费 问:使得0号的选票是最高的(不能有和0号相同)的最小花费 枚举0号的最终选票 那么已知0号最终选票,则有些人选票比0号大的,那些票都要买下来. 如果买完了还是达不到 最终选票,就从所有剩下的选票里找前k小的. 用线段树求前k小的数的和,然后_(:зゝ∠)_就可以了 #include<iostream> #include<cstdio> #i

[Codeforces 258B &amp; 259 D]Little Elephant and Elections 数位dp+dfs

http://codeforces.com/problemset/problem/258/B 题目大意: 说七个party选择数字(各不相同) 而规定的小象的party选择的数字之中所拥有的数字4和7的个数要比其他六个party拥有的个数之和还要严格多,询问方案数. 如m=7时其余的随意选择至少会拥有一个4或7,与题意矛盾,故方案数为0 m=8时,7 1 2 3 5 6 8是一种合法方案 思路: 由于小象的party选到的数字所含4和7的个数至多和m的位数一样多,则枚举小象的party所含4和7

CodeForces 258B Little Elephant and Elections 数位DP

前面先用数位DP预处理,然后暴力计算组合方式即可. #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include

Little Elephant and Elections CodeForces - 258B

Little Elephant and Elections CodeForces - 258B 题意:给出m,在1-m中先找出一个数x,再在剩下数中找出6个不同的数y1,...,y6,使得y1到y6中数字4和7出现的总次数严格小于x中数字4和7出现的总次数.求方案数. 方法:先数位dp分别预处理出:1到m之间,数字4和7出现的总次数为0到9的数.(总共最多10个数字,第一个最大1,因此4和7出现的总次数最多9次)然后枚举x,再暴力dfs枚举6个数,统计方案数. 问题(细节): 1.(13行)数位

在青岛穷游打的cf codeforces Round #318 (Div. 2) A.Bear and Elections

这场cf是暑假集训后在青岛旅游打的一场,好累..... 题意:给出一个序列,要使a[1]大于a[2]~a[n],a[1]每次可以增加一,这个一从a[2]到a[[n]里面任意一个数减一扣除,求最少的步数 思路:要不断地修改值,并从中找出最大的,可以想到优先队列,还要保存下该数的编号,要知道在队首时是a[1].还有处里一种特殊情况,详见代码 总结:这道题并不难,我用了40多分钟,主要前面太急了,一开始并没有想到是优先队列,是一些其他的想法,只要合适一个样例就下手去做,导致有很明显的bug我竟然敲完代

Codeforces 570 A. Elections

click here ~~ ***A. Elections*** The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate. The electoral system in the country is pretty un

Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) A. Bear and Elections(优先队列)

Limak is a grizzly bear who desires power and adoration. He wants to win in upcoming elections and rule over the Bearland. There are n candidates, including Limak. We know how many citizens are going to vote for each candidate. Now i-th candidate wou

CodeForces - 369C - Valera and Elections

369C - Valera and Elections 思路:dfs,对于搜索到的每个节点,看他后面有没有需要修的路,如果没有,那么这个节点就是答案. 代码: #include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back const int N=1e5+5; vector<int>g[N]; vector<int>edge[N]; vector<int&

Codeforces Round #216 (Div. 2)---C. Valera and Elections

The city Valera lives in is going to hold elections to the city Parliament. The city has n districts and n?-?1 bidirectional roads. We know that from any district there is a path along the roads to any other district. Let's enumerate all districts in