VJ【规律题】

来源:互联网 发布:一个域名对应多个网站 编辑:程序博客网 时间:2024/04/29 18:47
/*DescriptionLet'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.InputThe 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.OutputIn 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 %I64d specifier.Sample InputInput5 21 22 3Output5Input100 31 22 13 1Output4Input1 21 12 100Output100HintIn 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].*/ #include<stdio.h>#include<stdlib.h>#include<string.h> #include<math.h>int n, m, seg[2000010], p[100010];int cmp(const void *a, const void *b){return *(int *)b - *(int *)a;}int main(){int i, j, k;while(scanf("%d%d", &n, &m) != EOF){k = 0;for(i = 0; i < m; ++i){scanf("%d%d", &j, &p[i]);k += p[i];}qsort(p, m, sizeof(p[0]), cmp);int ans = sqrt(n*2);if(ans&1){if( (ans-1)*(ans-1)/2 + (ans+1)/2 > n)ans--;} else{if( (ans*ans)/2 + (ans+2)/2 <= n)ans++;}if(ans >= m)printf("%d\n", k);else{k = 0;for(j = 0; j < ans; ++j)k += p[j];printf("%d\n", k);}}return 0;} //题意:给出一个数组,要求这个数组中的任意2个数必定存在一种相邻的情况 ,这种数组称之为 美丽的数组//现在给出一个数组的长度,并且给你m个数字,每个数字都是独一无二的,要求你用这些数字来组成美丽数组,每一个数字都有一个价值//使用某个数需要付出对应的价值,并且该数可以无限使用,求构成该美丽数组需要的最大价值//思路:对于1个数字美丽数组的长度最短为1  对于2个数字美丽数组的长度最短为2  手推规律//发现当数字的个数为偶数时 需要构成的美丽数组的最短长度为m*m/2 //数字的个数为奇数时,美丽数组的长度最短为(m-1)*(m-1)/2 + (m+1)/2//根据这个规律来求当数组长度 为n时所需要的数字个数,然后求出最大价值 

0 0
原创粉丝点击