fruit.cpp

来自「NOIP2004问题标程」· C++ 代码 · 共 55 行

CPP
55 行
字号
#include <fstream>
#include <list>
#include <algorithm>
using namespace std;

ifstream fin("fruit.in");
ofstream fout("fruit.out");

int n;
list <int> a, b;

void init() {
	int p;

	fin >> n;
	for (int i = 0; i < n; i ++) {
		fin >> p;
		a.push_back(p);
	}
	a.sort();
}

int get() {
	int ans;

	if (a.empty()) {
		ans = b.front(); b.pop_front(); return ans;
	}
	if (b.empty()) {
		ans = a.front(); a.pop_front(); return ans;
	}
	if (a.front() < b.front()) {
		ans = a.front(); a.pop_front(); return ans;
	} 
	else {
		ans = b.front(); b.pop_front();	return ans;
	}
}
		
void work() {
	int p, sum = 0;
	for (int i = 0; i < n - 1; i ++) {
		p = get() + get();
		b.push_back(p);
		sum += p;
	}
	fout << sum << endl;
}

int main() {
	init();
	work();
	return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?