Codeforces 458c elections bribe the chairman of the vote! Line Segment tree

Source: Internet
Author: User

Question link: Click the open link

Question:

Given n votes, each vote has two parameters. The first parameter indicates the selected person.

The second parameter indicates the cost of changing the vote to 0.


Q: The minimum cost of the 0-day vote is the highest (it cannot be the same as the 0-day vote ).

Count the final votes on the 0th

If the final ballot number 0 is known, some votes are larger than those of the 0, and those votes must be bought.

If the final vote is still not reached after the purchase, find the first K smaller ones from all the remaining votes.

Calculate the sum of the first K small numbers using the line segment tree, and then_ (: Too many rows) _


# Include <iostream> # include <cstdio> # include <cstring> # include <string> # include <algorithm> # include <vector> using namespace STD; # define n 100005 # define INF 100000000 # define L (x) (x <1) # define R (x) (x <1 | 1) # define sum (X) tree [X]. sum # define siz (x) tree [X]. siz # define lson (x) tree [X]. L # define rson (x) tree [X]. rinline int mid (int x, int y) {return (x + y)> 1;} struct node {int L, R, siz, sum ;} tree [10004*4]; void Pu Sh_up (int id) {sum (ID) = sum (L (ID) + sum (R (ID); siz (ID) = siz (L (ID )) + siz (R (ID);} void build (int l, int R, int ID) {lson (ID) = L, rson (ID) = R; sum (ID) = siz (ID) = 0; If (L = r) return; int mid = mid (L, R); Build (L, mid, L (ID); Build (Mid + 1, R, R (ID);} void updata (INT POs, int Val, int ID) {If (lson (ID) ==rson (ID) {siz (ID) + = val; sum (ID) + = POS * val; return ;} int mid = mid (lson (ID ), rson (ID )); If (Pos <= mid) updata (Pos, Val, L (ID); else updata (Pos, Val, R (ID); push_up (ID );} int query (int K, int ID) {If (k <= 0) return 0; If (siz (ID) <= k) {return sum (ID );} if (lson (ID) = rson (ID) {return min (siz (ID), k) * lson (ID );} int mid = mid (lson (ID), rson (ID); If (siz (L (ID) <= k) {return sum (L (ID )) + query (k-siz (L (ID), R (ID);} else query (K, L (ID ));} vector <int> A [n], G [N]; int N, cost, now; int W Ork (int x) {for (INT I = 0; I <G [X]. size (); I ++) {cost + = G [x] [I]; updata (G [x] [I],-1, 1 ); now ++;} int hehe = x-now; If (hehe <= 0) return cost; return cost + query (Hehe, 1);} void input () {build (0, 10004, 1); For (INT I = 0; I <n; I ++) A [I]. clear (), g [I]. clear (); For (INT I = 1; I <= N; I ++) {int U, V; scanf ("% d", & U, & V); A [u]. push_back (V); updata (V, 1, 1) ;}for (INT I = 1; I <n; I ++) sort (A [I]. beg In (), a [I]. end (); For (INT I = 0; I <n; I ++) {for (Int J = 0; j <A [I]. size (); j ++) {G [A [I]. size ()-J]. push_back (A [I] [J]) ;}} int main () {int I, j, U, V; while (~ Scanf ("% d", & N) {input (); int ans = 1e9 + 10; cost = 0; now = 0; for (I = N; i> = 0; I --) {int TMP = work (I); If (TMP =-1) break; ans = min (ANS, TMP );} cout <ans <Endl;} return 0 ;}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.