Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

WA到死的看这里,讲两个坑点,贴一个AC代码

Posted by algoriiiiithm at 2020-11-09 20:12:19 on Problem 3253
1.结果要用long long类型
2.不要printf("%d\n", ans);数据类型是long long和%d不匹配的,改成cout就可以

用priority_queue的代码:

#include <queue>
#include <vector>
#include <functional>
#include <iostream>
using namespace std;

int main(void)
{
	int n, i;
	long long ans = 0;
	priority_queue<int, vector<int>, greater<int>> pQ;
	cin >> n;
	for(i = 0; i < n; i++){
		int a;
		cin >> a;
		pQ.push(a);
	}
	while(pQ.size() >= 2){
		int x, y;
		x = pQ.top();
		pQ.pop();
		y = pQ.top();
		pQ.pop();
		ans += (x + y);
		pQ.push(x + y);
	}
	cout << ans << endl;
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator