首页 > 其他分享 >Codeforces Round 260 (Div. 1)A. Boredom(dp)

Codeforces Round 260 (Div. 1)A. Boredom(dp)

时间:2024-02-08 18:22:18浏览次数:41  
标签:int max rep Codeforces long 260 Boredom dp define

image


最开始写了一发贪心wa了,然后这种选和不选的组合优化问题,一般是考虑动态规划

\(dp[i][0]:\)表示第i个数不选的最大值
\(dp[i][1]:\)表示第i个数选的最大值

考虑转移:
\(dp[i][0]=max(dp[i-1][1],dp[i-1][0])\)
\(dp[i][1]=dp[i-1][1]+a[i] * i\)
需要将每一个数用一个桶统计次数
因为n比较小。
最后答案在\(dp[n][0]和dp[n][1]\)两者中取一个最大值即可


#include <bits/stdc++.h> 
#define int long long
#define rep(i,a,b) for(int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(int i = (a); i >= (b); --i)
#define pii pair<int, int>
#define pll pair<long long, long long>
#define ll long long
#define db double
#define endl '\n'
#define x first
#define y second
#define pb push_back

using namespace std;

const int N=1e6+10,mod=1e9+7;
int dp[N][2];
int a[N];
void solve()
{
	int n;cin>>n;
	int m=0;
	rep(i,1,n){
		int x;cin>>x;
		a[x]++;
		m=max(m,x);
	}	
	
	rep(i,1,m){
		dp[i][0]=max(dp[i-1][0],dp[i-1][1]);
		dp[i][1]=dp[i-1][0]+a[i]*i;
	}
	cout<<max(dp[m][0],dp[m][1])<<endl;	
}

signed main(){
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
//   	freopen("1.in", "r", stdin);
  	int _;
//	cin>>_;
//	while(_--)
	solve();
	return 0;
}

标签:int,max,rep,Codeforces,long,260,Boredom,dp,define
From: https://www.cnblogs.com/cxy8/p/18011999

相关文章

  • Codeforces Round 923 (Div. 3)
    A.MakeitWhiteA题不多说B.FollowingtheString题目一开始没看懂,后面发现数字是指字母出现的次数,读懂题目后就好做了,先把26个字母放在一个数组里全部初始化为0,然后用1次就加1,然后要根据数字来选数的话就可以遍历数组当满足就break;也可以通过集合。C.ChoosetheDifferent......
  • Codeforces Round 909 (Div. 3)
    题目链接A.若n是3的倍数,那么输出第二,因为不管先手如何操作,后手只要跟先手出的相反那么先手就永远不会赢其他情况,先手都能赢#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongconstintN=1e5+10;voidsolve(){intn;cin>>n;if((n+1)%3==0|......
  • Codeforces-Round-917
    比赛链接A.LeastProduct假如序列中有\(0\),那么答案是\(0\)。否则如果有奇数个负数,只需要让每个数的绝对值最大:不操作是最优的。否则如果有偶数个负数,乘积只会\(\ge0\),为了达到最小值\(0\),只需要将任意一个数改成\(0\)。时间复杂度\(\mathcal{O}(n)\)。codeforA......
  • Codeforces-Pinely-Round-3
    比赛链接A.DistinctButtons扣掉一个方向就是只能走到相邻两个象限以及和这两个象限相邻的坐标轴上,判一下是不是所有的点都满足就行。判的时候只需要判是否所有\(x\le0\)(落到二、三象限),或者所有\(x\ge0\)(四、一象限),或者所有\(y\le0\)或者所有\(y\ge0\)就行,......
  • Codeforces Round 923 (Div. 3)
    CodeforcesRound923(Div.3)A-MakeitWhite分析在字符串中找到第一个B的位置l和最后一个B的位置r,打印r-l+1即可如果找不到B打印-1code#include<bits/stdc++.h>#defineintlonglong#defineyescout<<"YES"<<'\n'#defineno cout<<"NO"......
  • Codeforces Round 921 (Div. 2)
    https://codeforces.com/contest/1925恶心round,从C题开始每题都是一眼看出做法但是细节挺多的题,烦的一比。A.WeGotEverythingCovered!*800给定\(n,k\),若所有长度为\(n\)且仅由字母表中前\(k\)个小写字母组成的串都是\(s\)的子序列,则称\(s\)是"EverythingCover......
  • Codeforces Round 923 (Div. 3)(A~F)
    目录ABCDEFA#include<bits/stdc++.h>#defineintlonglong#definerep(i,a,b)for(inti=(a);i<=(b);++i)#definefep(i,a,b)for(inti=(a);i>=(b);--i)#definepiipair<int,int>#definepllpair<longlong,longlong>#de......
  • Codeforces-Hello-2024-Round
    比赛链接A.WalletExchange签到,某个人操作的时候只要一方有金币就行,所以最终赢的应该是拿到最后一个硬币的人,当\(a+b\equiv1\pmod2\)的时候Alice获胜,否则Bob获胜。时间复杂度\(\mathcal{O}(1)\)。codeforA#include<bits/stdc++.h>usingnamespacestd;inli......
  • Codeforces Round 920 (Div. 3)(A~F)
    目录ABCDEFA按题意模拟即可#include<bits/stdc++.h>#defineintlonglong#definerep(i,a,b)for(inti=(a);i<=(b);++i)#definefep(i,a,b)for(inti=(a);i>=(b);--i)#definepiipair<int,int>#definepllpair<longlong,longlong......
  • Codeforces div2 C题补题
    Codeforcesdiv2C题补题1922C.ClosestCitiesC.ClosestCities很容易看出,端点的两个城市的最近城市就是他的直接后继和直接前驱,而中间的城市的最近城市就是他前驱和后继中距离绝对值最小的那个,因此我们可以先预处理出每个城市对应的最近城市,用map存储。然后因为区间可以从......