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

注意到了long long int结果还是WA了,是贪心算法思路不对吗

Posted by 1204987890 at 2021-08-09 17:43:25 on Problem 3253
#include<stdio.h>

int l[20001];

int main()
{
	//freopen("stat.in", "r", stdin);
	int n,i,j;
	long long int sum = 0;
	scanf("%d", &n);
	if (n == 1) {
		printf("%d\n",0);
		return 0;
	}
	for (i = 0; i < n; i++)
	{
		scanf("%d", &l[i]);
	}
	int t = 1, mi = 1;
	while ((mi*=2)<n)
	{
		t++;
	}
	if (mi == n) mi *= 2;
	for ( i = 0; i < n; i++)
	{
		for ( j = i+1; j < n; j++)
		{
			if (l[i]>l[j])
			{
				int temp = l[i];
				l[i] = l[j];
				l[j] = temp;
			}
		}
	}
	for ( i = 0; i < mi/2; i++)
	{
		sum = sum + (l[i] * t);
	}
	for ( i = mi/2; i < n; i++)
	{
		sum = sum + (l[i] * (t-1));
	}
	printf("%lld\n", sum);
	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