↓ Skip to main content
  1. Posts/

codeforces 456 C. Boredom

·457 words·1 min
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

http://codeforces.com/contest/456/problem/C 题意:给出n(1E5)个数(1E5),每次可以选一个数a[k]并删掉a[k],a[k]-1,a[k]+1得到a[k]分,问最多能得到的分数。 思路:裸dp.f[i]表示选到数i的时候能达到的最大分数。开一个计数数组cnt[x]表示数字x出现的次数。那么显然有f[0]=0,f[1]=cnt[1],f[i(i>=2)] = max(f[i-1],f[i-2]+f[i]*cnt[i]);答案为f[max(a[i])],注意要开long long

代码实现
 1/* ***********************************************
 2Author :111qqz
 3Created Time :2015年12月25日 星期五 18时17分05秒
 4File Name :code/cf/problem/455A.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=1E5+7;
34int n ;
35int a[N];
36LL cnt[N];
37LL dp[N];
38
39bool cmp( int a,int b)
40{
41    return a>b;
42}
43int main()
44{
45	#ifndef  ONLINE_JUDGE
46	freopen("code/in.txt","r",stdin);
47  #endif
48	cin>>n;
49	ms(cnt,0);
50	for ( int i = 0 ; i < n ; i++)
51	{
52	    scanf("%d",&a[i]);
53	    cnt[a[i]]++;
54	}
55
56	dp[0] = 0 ;  //dp[i]表示到数字i时能得到的最多分数
57	dp[1] = cnt[1];
58	for ( int i = 2 ; i <= 1E5 ; i++)
59	{
60	    dp[i] = max(dp[i-1],dp[i-2]+i*cnt[i]);
61	}
62	cout<<dp[100000]<<endl;
63
64
65  #ifndef ONLINE_JUDGE
66  fclose(stdin);
67  #endif
68    return 0;
69}

Related

zoj 3634 Bounty hunter(dp,已经想明白)

·846 words·2 mins
M - Bounty hunter **Time Limit:**5000MS **Memory Limit:**65536KB 64bit IO Format:%lld & %llu Submit Status Description Bounty hunter is a hero who always moves along cities to earn money by his power. One day he decides to N cities one by one At the beginning ,Bounty hunter has X money and Y points of Attack force. At day 1, he will goes to city 1, then city 2 at day 2, city 3 at day 3, … At last ,he goes to city N at day N and leaves it at day N+1. In each city, he can increase his attack force by money and earn some money by accepting a task. In the city i, it costs him ai money to increase one point of attack force. And he can gets bi*yi money after finishing the task in city i while yi is his attack force after his increasing at city i.

bestcoder #56 div 2 B Clarke and problem(dp)

·437 words·1 min
果然dp還是弱項啊啊啊啊.. 不過比最開始的完全無從下手強了不少應該... 至少dp狀態表示相對了....轉移方程沒想出來嗚嗚嗚 官方題解:设d(i, j)d(i,j)表示前ii个数,模pp为jj的方案数,则容易得到d(0, 0)=1, d(i, j)=d(i-1, j)+sum_{j=0}^{p-1} d(i-1, (j-a[i]) mod p)d(0,0)=1,d(i,j)=d(i−1,j)+∑j=0p−1d(i−1,(j−a[i]) mod p),很多人没1a是因为没注意|a_i| le 10^9∣ai∣≤109

HUST team contest #E A Mountain Road||poj 3846 (dp)

·840 words·2 mins
比赛的时候以为是贪心… 想了好久. 不过最后没敢写,因为没办法证明正确性,只是直觉== 最后剩下的时间给队友改另一道题了.. 果然明智… 蠢的人的直觉真心不靠谱..