关于输入造成的MLE
查看原帖
关于输入造成的MLE
519384
Link_Cut_Y楼主2021/10/10 19:41

MLE的CODE

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>

using namespace std;

const int N = 120;
int f[N][N][N][N];
int score[400], card;
int g[5];
int n, m;

int main()
{
	
	cin >> n >> m;
	
	for (int i = 1; i <= n; i ++ ) cin >> score[i];
	for (int i = 1; i <= m; i ++ ) cin >> card, g[card] ++ ;
	
	f[0][0][0][0] = score[1];
	int maxn, u;
	for (int a = 0; a <= g[1]; a ++ )
		for (int b = 0; b <= g[2]; b ++ )
			for (int c = 0; c <= g[3]; c ++ )
				for (int d = 0; d <= g[4]; d ++ )
				{
					if (!a && !b && !c && !d) continue;
					maxn = 0, u = 1 + a + b * 2 + c * 3 + d * 4;
					if (a) maxn = max(maxn, f[a - 1][b][c][d] + score[u]);
					if (b) maxn = max(maxn, f[a][b - 1][c][d] + score[u]);
					if (c) maxn = max(maxn, f[a][b][c - 1][d] + score[u]);
					if (d) maxn = max(maxn, f[a][b][c][d - 1] + score[u]);
					
					f[a][b][c][d] = max(maxn, f[a][b][c][d]);
				}
				
	cout << f[g[1]][g[2]][g[3]][g[4]];
	
	return 0;
}

AC的CODE

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>

using namespace std;

const int N = 120;
int f[N][N][N][N];
int score[400], card;
int g[5];
int n, m;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	
	cin >> n >> m;
	
	for (int i = 1; i <= n; i ++ ) cin >> score[i];
	for (int i = 1; i <= m; i ++ ) cin >> card, g[card] ++ ;
	
	f[0][0][0][0] = score[1];
	int maxn, u;
	for (int a = 0; a <= g[1]; a ++ )
		for (int b = 0; b <= g[2]; b ++ )
			for (int c = 0; c <= g[3]; c ++ )
				for (int d = 0; d <= g[4]; d ++ )
				{
					if (!a && !b && !c && !d) continue;
					maxn = 0, u = 1 + a + b * 2 + c * 3 + d * 4;
					if (a) maxn = max(maxn, f[a - 1][b][c][d] + score[u]);
					if (b) maxn = max(maxn, f[a][b - 1][c][d] + score[u]);
					if (c) maxn = max(maxn, f[a][b][c - 1][d] + score[u]);
					if (d) maxn = max(maxn, f[a][b][c][d - 1] + score[u]);
					
					f[a][b][c][d] = max(maxn, f[a][b][c][d]);
				}
				
	cout << f[g[1]][g[2]][g[3]][g[4]];
	
	return 0;
}

没错,加上ios::sync_with_stdio(false); cin.tie(0), cout.tie(0);就可以了

2021/10/10 19:41
加载中...