Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

Блог пользователя mytheus

Автор mytheus, история, 8 месяцев назад, По-английски

I'm 13 years old and learning dynamic programming to improve my problem-solving abilities. Could someone please help me solve this problem? It has a dp kinda vibe, I learned how to memoize it, but I feel like I am not able to write the transition function, or the recursion statement in this properly. Can someone help me with my approach I have some mistake still Link: https://codeforces.me/contest/1343/problem/C

void solve(vector<ll>&a,ll idx,ll &maxSum,ll &sum,ll flag)
{
	if(idx == a.size()){
		maxSum = max(maxSum,sum);
		return ;
	}
	if(a[idx] < 0 && flag == 1)
	{
		sum += a[idx];
		solve(a,idx+1,maxSum,sum,0);
		sum -= a[idx];

		solve(a,idx+1,maxSum,sum,1);
	}
	else if(a[idx] > 0 && flag == 0)
	{
		sum += a[idx];
		solve(a,idx+1,maxSum,sum,1);
		sum -= a[idx];

		solve(a,idx+1,maxSum,sum,0);
	}
}
void solve(){
	ll n;
	cin >> n;
	vt<ll> a(n);
	cin >> a;
	ll sum = 0,maxSum = -1e9;
	solve(a,0,maxSum,sum,0);
	solve(a,0,maxSum,sum,1);
	cout << maxSum << endl;
}

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
8 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

When you overthinking in cp:

»
8 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

It is just a greddy problem you don't necessarily need dp to solve the problem. Div2 abc are generally greedy,implementation,math problems.Just be good at problem solving by practicing more problems.

»
8 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Even if you fixed the bugs in your solution, I'm quite sure it would be too slow. I don't think this problem is solvable with dp, at least not with a dp similar to yours. If you want to get better at problem-solving, you should try different techniques and not try to force dp just because the problem "has a dp kinda vibe". Reading this might be helpful: https://codeforces.me/blog/entry/106346