답안 #792876

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
792876 2023-07-25T10:14:32 Z IvanJ Fireworks (APIO16_fireworks) C++17
19 / 100
17 ms 724 KB
#include<bits/stdc++.h>

#define pb push_back
#define x first
#define y second
#define all(a) (a).begin(), (a).end()

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;

const int maxn = 305;

int n, m;
int dp[maxn][maxn];
vector<ii> adj[maxn];

int rek(int x, int len) {
	if(dp[x][len] != -1)
		return dp[x][len];
	if(adj[x].size() == 0)
		return dp[x][len] = (len == 0) ? 0 : 1e9;
	
	int ret = 0;
	for(auto p : adj[x]) {
		int reti = 1e9;
		for(int i = 0;i <= len;i++) {
			int l = len - i;
			reti = min(reti, rek(p.x, i) + abs(p.y - l));
		}
		ret += reti;
	}
	return dp[x][len] = ret;
}

int main() {
	scanf("%d%d", &n, &m);
	for(int i = 1;i < n + m;i++) {
		int p, c;
		scanf("%d%d", &p, &c);
		p--;
		adj[p].pb({i, c});
	}
	if(n == 1) {
		vector<int> v;
		for(auto p : adj[0]) v.pb(p.y);
		sort(all(v));
		int med = v[(m - 1) / 2];
		ll ans = 0;
		for(int i : v) ans += (ll)(i - med);
		printf("%lld\n", ans);
		return 0;
	}
	
	memset(dp, -1, sizeof dp);
	int ans = 1e9;
	for(int i = 0;i < maxn;i++) 
		ans = min(ans, rek(0, i));
	printf("%d\n", ans);
	return 0;
}

Compilation message

fireworks.cpp: In function 'int main()':
fireworks.cpp:38:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |  scanf("%d%d", &n, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~
fireworks.cpp:41:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |   scanf("%d%d", &p, &c);
      |   ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Incorrect 0 ms 316 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 596 KB Output is correct
2 Correct 3 ms 596 KB Output is correct
3 Correct 4 ms 596 KB Output is correct
4 Correct 5 ms 596 KB Output is correct
5 Correct 8 ms 596 KB Output is correct
6 Correct 9 ms 676 KB Output is correct
7 Correct 9 ms 596 KB Output is correct
8 Correct 12 ms 676 KB Output is correct
9 Correct 10 ms 676 KB Output is correct
10 Correct 12 ms 596 KB Output is correct
11 Correct 13 ms 676 KB Output is correct
12 Correct 15 ms 724 KB Output is correct
13 Correct 15 ms 596 KB Output is correct
14 Correct 16 ms 708 KB Output is correct
15 Correct 16 ms 596 KB Output is correct
16 Correct 17 ms 668 KB Output is correct
17 Correct 16 ms 676 KB Output is correct
18 Correct 16 ms 596 KB Output is correct
19 Correct 16 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Incorrect 0 ms 316 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Incorrect 0 ms 316 KB Output isn't correct
3 Halted 0 ms 0 KB -