結果

問題 No.324 落ちてた閉路グラフ
ユーザー koyumeishikoyumeishi
提出日時 2015-12-17 11:01:04
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 37 ms / 5,000 ms
コード長 1,377 bytes
コンパイル時間 885 ms
コンパイル使用メモリ 82,000 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-15 19:30:25
合計ジャッジ時間 2,203 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 34
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘std::istream& operator>>(std::istream&, std::vector<int>&)’:
main.cpp:13:79: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   13 | istream& operator >> (istream& is, vector<int>& vec){ for(int& val: vec) scanf("%d", &val); return is;}
      |                                                                          ~~~~~^~~~~~~~~~~~
main.cpp: In function ‘int main()’:
main.cpp:19:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   19 |         scanf("%d%d", &n,&m);
      |         ~~~~~^~~~~~~~~~~~~~~
main.cpp:22:28: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   22 |         for(int& x:w) scanf("%d", &x);
      |                       ~~~~~^~~~~~~~~~

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <set>
using namespace std;

istream& operator >> (istream& is, vector<int>& vec){ for(int& val: vec) scanf("%d", &val); return is;}
template<class T> istream& operator >> (istream& is, vector<T>& vec){ for(T& val: vec) is >> val; return is;}

int main(){
	int n,m;
	//cin >> n >> m;
	scanf("%d%d", &n,&m);
	vector<int> w(n);
	//cin >> w;
	for(int& x:w) scanf("%d", &x);
	
	if(m==0){
		cout << 0 << endl;
		return 0;
	}
	
	const int inf = 100000000;
	
	vector<vector<vector<int>>> dp (2,vector<vector<int>>(2,vector<int>(m+1, -inf)));
	vector<vector<vector<int>>> dp_(2,vector<vector<int>>(2,vector<int>(m+1, -inf)));
	
	dp[0][0][0] = 0;
	dp[1][1][1] = 0;
	
	for(int i=1; i<n; i++){
		for(int start=0; start<2; ++start)
		for(int last=0; last<2; ++last)
		for(int k=max(0,m-(n-i)); k<=min(i,m); ++k){
			if(dp[start][last][k] <= -inf) continue;
			dp_[start][0][k] = max(dp_[start][0][k], dp[start][last][k]);
			if(k+1<=m){
				dp_[start][1][k+1] = max(dp_[start][1][k+1], dp[start][last][k]+(last?w[i-1]:0) );
			}
			dp[start][last][k] = -inf;
		}
		swap(dp, dp_);
	}
	int ans = max({dp[0][0][m], dp[0][1][m], dp[1][0][m], dp[1][1][m]+w[n-1]});
	printf("%d\n", ans);
	//cout << ans << endl;
	return 0;
}
0