CodeForces - 367C Sereja and the Arrangement of Numbers (图论&不懂)

CodeForces - 367C
Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u

 Status

Description

Let's call an array consisting of n integer numbers a1a2...anbeautiful 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 mcoupons, 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 cincout 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].

//读好长时间的题,没读懂,看大神的题意题解还是没理解(太菜了)。

题意:给出m个不同的数,并且每个数都有个费用,现在要在m个数中选择一些数,用这些数组成一个长度为n的数列,并且满足任意两个不同种类的数都相邻。问最大的费用是多少。

思路:不同种数的区别只有费用,不妨按费用从大到小排序,现在就是要在前几个数中选一些满足要求。可以把它想象成一个图,每个数代表一个顶点,两个数相邻代表两个顶点之间有条边,根据题中的要求,构造出来的图就是完全图。做题的时候没想那么多,找了找规律A掉了,AC后看题解才发现这题其实很巧妙。这个数列可以看成一个路径,每在当前数列后加一个数就相当于连了一条边,这样这题就变成了找一条欧拉路径!顶点为奇数的时候一定存在欧拉路径,因为每个顶点的度数都为偶数,顶点为偶数的时候要添加一些边来满足要求,添加的边数即为n/2-1。有趣的题~

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<map>
#include<math.h>
#define INF 0x3f3f3f3f
#define ull unsigned long long
#define ll long long
#define IN __int64
#define N 10010
#define M 1000000007
using namespace std;
ll a[N];
int w[N*10];
bool cmp(int x,int y)
{
	return x>y;
}
void init()
{
	for(int i=1;i<N;i++)
	{
		if(i&1)
			a[i]=i*(i-1)/2+1;
		else
			a[i]=i*i/2;
	}
}
int main()
{
	int t,n,m,i,j,k;
	init();
	scanf("%d%d",&n,&m);
	for(i=1;i<=m;i++)
		scanf("%d%d",&t,&w[i]);
	sort(w+1,w+m+1,cmp);
	int p;
	for(i=1;i<N;i++)
	{
		if(a[i]<=n)
			p=i;
		else
			break;
	}
	ll ans=0;
	for(i=1;i<=min(m,p);i++)
		ans+=w[i];
	printf("%lld\n",ans);
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值