30pt,为什么不能用每次合并掉能量最小的珠子来解
查看原帖
30pt,为什么不能用每次合并掉能量最小的珠子来解
371404
Amazing_llh楼主2024/11/19 19:55
#include <cstdio>
#include <algorithm>
using namespace std;

struct node {
	int head;
	node* last;
	node* next;
} a[105];

struct num {
	int raw;
	int data;
} sorted[105];

bool cmp(num a, num b) {
	return a.data < b.data;
}

int main() {
	int n, sum = 0;
	scanf("%d", &n);
	for (int i=1; i<=n; i++) {
		int head;
		scanf("%d", &head);
		a[i] = {head, &a[i-1], &a[i+1]};
		sorted[i] = {i, head};
	}
	a[1].last = &a[n];
	a[n].next = &a[1];
	sort(sorted + 1, sorted + 1 + n, cmp);
	for (int i=1; i<n; i++) {
		sum += (a[sorted[i].raw].last -> head) * a[sorted[i].raw].head * (a[sorted[i].raw].next -> head);
		a[sorted[i].raw].last -> next = a[sorted[i].raw].next;
		a[sorted[i].raw].next -> last = a[sorted[i].raw].last;
	}
	printf("%d", sum);
	return 0;
}
2024/11/19 19:55
加载中...