Codeforces Round #215 (Div. 1) C. Sereja and the Arrangement of Numbers(欧拉图)
来源:互联网 发布:手表品牌排行榜 知乎 编辑:程序博客网 时间:2024/06/14 07:12
Let's call an array consisting of n integer numbers a1, a2, ..., an, beautiful if it has the following property:
- consider all pairs of numbers x, y (x ≠ y), such that number x occurs in the array a and number y occurs in the array a;
- for each pair x, y must exist some position j (1 ≤ j < n), such that at least one of the two conditions are met, either aj = x, aj + 1 = y, or aj = y, aj + 1 = x.
Sereja wants to build a beautiful array a, consisting of n integers. But not everything is so easy, Sereja's friend Dima has m coupons, each contains two integers qi, wi. Coupon i costs wi and allows you to use as many numbers qi as you want when constructing the array a. Values qi are distinct. Sereja has no coupons, so Dima and Sereja have made the following deal. Dima builds some beautiful array a of n elements. After that he takes wi rubles from Sereja for each qi, which occurs in the array a. Sereja believed his friend and agreed to the contract, and now he is wondering, what is the maximum amount of money he can pay.
Help Sereja, find the maximum amount of money he can pay to Dima.
The first line contains two integers n and m (1 ≤ n ≤ 2·106, 1 ≤ m ≤ 105). Next m lines contain pairs of integers. The i-th line contains numbers qi, wi (1 ≤ qi, wi ≤ 105).
It is guaranteed that all qi are distinct.
In a single line print maximum amount of money (in rubles) Sereja can pay.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64dspecifier.
5 21 22 3
5
100 31 22 13 1
4
1 21 12 100
100
In the first sample Sereja can pay 5 rubles, for example, if Dima constructs the following array: [1, 2, 1, 2, 2]. There are another optimal arrays for this test.
In the third sample Sereja can pay 100 rubles, if Dima constructs the following array: [2].
分析:将每个数字看成点,两个数字相邻就连一条无向边,这样每个点的度数为 n-1,如果n为奇数那么原图就是欧拉图,这样至少要连n*(n-1)/2条边;如果n为偶数那么每个点的度数都为奇数,我们需要再至少添加 n/2 - 1条边,才能保证图中存在一条欧拉通路,这样至少要连n*(n-1)/2 + n/2 - 1条边。
然后我们就可以二分最多加几个数字进来了,因为数字是什么无关紧要,所以我们将数字按权值排好序之后取大的就可以了。
#include<iostream>#include<string>#include<algorithm>#include<cstdlib>#include<cstdio>#include<set>#include<map>#include<vector>#include<cstring>#include<stack>#include<queue>#define INF 4557430888798830400ll#define eps 1e-9#define MAXN 0x3f#define N 100005using namespace std;long long ans;int n,m,q[N],w[N];long long f(int x){if(x & 1) return 1ll*x*(x-1)/2 + 1;else return 1ll*x*(x-1)/2 + x/2;}int main(){scanf("%d%d",&n,&m);for(int i = 1;i <= m;i++) scanf("%d%d",&q[i],&w[i]);int s = 1,t = m;while(s != t){int mid = ((s+t)>>1) + 1;if(f(mid) <= n) s = mid;else t = mid - 1;}sort(w+1,w+1+m);for(int i = 1;i <= s;i++) ans += w[m-i+1];cout<<ans<<endl; }
- Codeforces Round #215 (Div. 1) C. Sereja and the Arrangement of Numbers(欧拉图)
- Codeforces Round #215_div2_E. Sereja and the Arrangement of Numbers
- Sereja and the Arrangement of Numbers -------Codeforce Round # 215 C
- CodeForces - 367C Sereja and the Arrangement of Numbers (图论&不懂)
- CF 367C Sereja and the Arrangement of Numbers(欧拉路径)
- Codeforces Round #243 (Div. 1) C Sereja and Two Sequences
- Codeforces Round #223 (Div. 1) C. Sereja and Brackets
- Codeforces Round #223 (Div. 1) C. Sereja and Brackets
- Sereja and Algorithm -----Codeforces Round #215 div.1 A
- 【瞎搞】 Codeforces Round #215 (Div. 1) A Sereja and Algorithm
- CodeForces Round 223 Div 2 C Sereja and Prefixes
- Codeforces Round #243 (Div. 2) C. Sereja and Swaps
- Codeforces Round #243 (Div. 2) C. Sereja and Swaps
- Codeforces Round #243 (Div. 2)-C. Sereja and Swaps(multiset)
- Codeforces Round #223 (Div. 2)-C. Sereja and Prefixes
- Codeforces Round #215 (Div. 2) -A. Sereja and Coat Rack
- Codeforces Round #215 (Div. 2) -B. Sereja and Suffixes
- Codeforces Round #215 (Div. 2) -B. Sereja and Suffixes
- 缓冲与非缓冲
- java web 基础知识
- 如何使用大数据 SQL 语句
- Caffe源码中common文件分析
- Qt学习笔记
- Codeforces Round #215 (Div. 1) C. Sereja and the Arrangement of Numbers(欧拉图)
- java中的几种对象(PO,VO,DAO,BO,POJO)
- 程序员的元宵灯谜
- Android操作SQL数据库
- linux之修改dns以及用netmask修改ip和网关
- {题解}[jzoj3366]【NOI2012】随机数生成器
- USCAO Sorting a Three-Valued Sequence
- 指针 的基础知识
- jsp九大内置对象包括request response session application out page config exception pageContent