結果

問題 No.258 回転寿司(2)
ユーザー vjudge1
提出日時 2025-10-05 11:06:55
言語 C++17(clang)
(17.0.6 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 719 bytes
コンパイル時間 2,631 ms
コンパイル使用メモリ 171,080 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2025-10-05 11:07:09
合計ジャッジ時間 6,673 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 67
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:6:8: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
    6 |         int v[n+1],dp[n+1];
      |               ^~~
main.cpp:6:8: note: read of non-const variable 'n' is not allowed in a constant expression
main.cpp:4:6: note: declared here
    4 |         int n;
      |             ^
main.cpp:6:16: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
    6 |         int v[n+1],dp[n+1];
      |                       ^~~
main.cpp:6:16: note: read of non-const variable 'n' is not allowed in a constant expression
main.cpp:4:6: note: declared here
    4 |         int n;
      |             ^
main.cpp:23:9: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
   23 |         int ch[n+1];
      |                ^~~
main.cpp:23:9: note: read of non-const variable 'n' is not allowed in a constant expression
main.cpp:4:6: note: declared here
    4 |         int n;
      |             ^
3 warnings generated.

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin>>n;
	int v[n+1],dp[n+1];
	for(int i=1;i<=n;i++){
		cin>>v[i];
	}
	if(n==1){
		cout<<v[1]<<endl<<1;
		return 0;
	}
	if(n==0){
		cout<<0<<endl;
		return 0;
	}
	dp[0]=0;
	dp[1]=v[1];
	for(int i=2;i<=n;i++){
		dp[i]=max(dp[i-1],dp[i-2]+v[i]);
	}
	int ch[n+1];
	for(int i=1;i<=n;i++){
		ch[i]=0;
	}
    int i=n;
    while(i>0){
        if(i==1){
            ch[i]=1;
            break;
        }
        if(dp[i]==dp[i-1]){
        	ch[i]=0;
            i--;
        }
		else{
            ch[i]=1;
            i-=2;
        }
    }
	cout<<dp[n]<<endl;
    for(int i=1;i<=n;i++){
    	if(ch[i]==1){
    		cout<<i<<" ";
		}
	}
    cout<<endl;
	return 0;
} 
0