結果

問題 No.2145 Segment +-
コンテスト
ユーザー askr58
提出日時 2026-07-19 20:30:57
言語 C++23
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 15 ms / 2,000 ms
+ 765µs
コード長 920 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,090 ms
コンパイル使用メモリ 184,764 KB
実行使用メモリ 5,888 KB
最終ジャッジ日時 2026-07-19 20:31:00
合計ジャッジ時間 3,031 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24
権限があれば一括ダウンロードができます
コンパイルメッセージ
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/vector:67,
                 from main.cpp:5:
In function '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = long long int*; _ForwardIterator = long long int*]',
    inlined from 'constexpr _ForwardIterator std::__uninitialized_copy_a(_InputIterator, _Sentinel, _ForwardIterator, allocator<_Tp>&) [with _InputIterator = long long int*; _Sentinel = long long int*; _ForwardIterator = long long int*; _Tp = long long int]' at /home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/bits/stl_uninitialized.h:635:32,
    inlined from 'constexpr std::vector<_Tp, _Alloc>& std::vector< <template-parameter-1-1>, <template-parameter-1-2> >::operator=(const std::vector< <template-parameter-1-1>, <template-parameter-1-2> >&) [with _Tp = long long int; _Alloc = std::allocator<long long int>]' at /home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/bits/vector.tcc:257:35,
    inlined from 'int main()' at main.cpp:42:6:
/home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/bits/stl_uninitialized.h:273:31: warning: 'void* __builtin_memcpy(void*, const void*, long unsigned int)' writing between 1 and 400 bytes into a region of size 0 overflows the destination [-Wstringop-overflow=]
  273 |               __builtin_memcpy(std::__niter_base(__result),
      |               ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  274 |                                std::__niter_base(__first),
      |                                ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  275 |                                __n * sizeof(_ValT));
      |                                ~~~~~~~~~~~~~~~~~~~~
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/x86_64-pc-linux-gnu/bits/c++allocator.h:33,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/bits/allocator.h:46,
                 from

ソースコード

diff #
raw source code

#include <iostream>
#include <string>
#include <ranges>
#include <algorithm>
#include <vector>
using namespace std;
using ll=long long;
int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int n;
	string s;
	cin>>n>>s;
	ll inf=1e18;
	int b=50;
	vector<ll> dp(b,-inf);
	dp[0]=0;
	for(int i=0;i<n;i++){
		vector<ll> ndp(b,-inf);
		if(s[i]!='-'){
			for(int j=0;j<b;j++){
				if(dp[j]<0)continue;
				if(j%2==0){
					ndp[j]=max(ndp[j],dp[j]+1);
				}else{
					if(dp[j]>0)ndp[j]=max(ndp[j],dp[j]-1);
					if(j<b-1)ndp[j+1]=max(ndp[j+1],dp[j]+1);
				}
			}
		}
		if(s[i]!='+'){
			for(int j=0;j<b;j++){
				if(dp[j]<0)continue;
				if(j%2==1){
					if(dp[j]>0)ndp[j]=max(ndp[j],dp[j]+1);
				}else{
					if(dp[j]>0)ndp[j]=max(ndp[j],dp[j]-1);
					if(j<b-1)ndp[j+1]=max(ndp[j+1],dp[j]+1);
				}
			}
		}
		dp=ndp;
	}
	for(int i=0;i<b;i++){
		if(dp[i]>=0){
			cout<<(i+1)/2<<endl;
			return 0;
		}
	}
}
				
					
0