VJ【规律题】

/*
Description
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.

Input
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.

Output
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 %I64d specifier.

Sample Input
Input
5 2
1 2
2 3
Output
5
Input
100 3
1 2
2 1
3 1
Output
4
Input
1 2
1 1
2 100
Output
100
Hint
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].
*/ 
#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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目C要求我们在一个网格图中求一条从起点到终点的路径,其中有障碍物阻挡了一些格子。这可以用回溯法进行求解。 我们可以用一个二维数组来表示网格图,其中0表示可以通过的格子,1表示障碍物阻挡的格子,2表示路径上的格子。我们可以从起点开始,不断尝试向右、向下、向左、向上四个方向前进,如果前进的下一个格子是可以通过的,则继续前进;如果下一个格子是障碍物或者已经走过,则回溯到上一个格子重新选择方向前进。当我们到达终点时,我们就找到了一条从起点到终点的路径。 这个过程可以用递归函数来实现。具体实现过程如下: 1. 定义一个递归函数`dfs(x, y)`,表示从坐标(x, y)开始寻找路径。 2. 检查当前坐标是否越界。如果越界,则返回false。 3. 检查当前坐标是否为终点。如果是,则返回true。 4. 检查当前坐标是否为障碍物或者已经走过。如果是,则返回false。 5. 将当前坐标标记为已经走过。 6. 尝试向右、向下、向左、向上四个方向前进。如果有一个方向前进成功,则返回true。 7. 如果四个方向都不能前进,则将当前坐标标记为未走过,并返回false。 8. 在主函数中,从起点开始调用递归函数`dfs(0, 0)`,如果返回true,则表示找到了一条路径,可以输出路径;否则表示不存在路径。 需要注意的是,在标记一个坐标为已经走过时,我们需要使用一个变量来记录是否已经走过,因为在回溯的过程中,我们需要将已经走过的坐标重新标记为未走过。 以上就是目C的建模过程,可以根据这个思路进行编程实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值