别人的代码:
# include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair <int, int> pii;
int n, t[100005];
int x, y, s, sum, maxn;
queue <int> a, q;
inline int read()
{
int cns = 0, f = 1;
char ch = getchar();
while (ch > '9' || ch < '0')
{
if (ch == '-')
{
f = -1;
}
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
cns = cns * 10 + ch - '0';
ch = getchar();
}
return cns * f;
}
main()
{
n=read();
for (int i = 0; i < n; ++ i)
x=read(),++ t[x], maxn = max (maxn, x);
for (int i = 1; i <= maxn; ++ i)
while (t[i] --)
a.push (i);
while (-- n)
{
if (a.empty ())
x = q.front (), q.pop ();
else if (q.empty ())
x = a.front (), a.pop ();
else if (a.front () < q.front ())
x = a.front (), a.pop ();
else
x = q.front (), q.pop ();
if (a.empty ())
y = q.front (), q.pop ();
else if (q.empty ())
y = a.front (), a.pop ();
else if (a.front () < q.front ())
y = a.front (), a.pop ();
else
y = q.front (), q.pop ();
s = x + y;
q.push (s);
sum += s;
}
cout << sum;
return 0;
}
可以过,我的却不行!!!
不都是快读吗?? :
#include"bits/stdc++.h"
using namespace std;
long long n,n2,ans,sum,chuchun;
inline int read()
{
int cns = 0, f = 1;
char ch = getchar();
while (ch > '9' || ch < '0')
{
if (ch == '-')
{
f = -1;
}
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
cns = cns * 10 + ch - '0';
ch = getchar();
}
return cns * f;
}
inline void print(register const int &x)
{
if (x > 9)
{
print(x / 10);
}
putchar(x % 10 + '0');
}
main()
{
long long i=0,j=0;
n=read();
long long a[n+10],a2[n+10];
memset(a,127,sizeof(a));
memset(a2,127,sizeof(a2));
for(int i=0;i<n;i++)
a[i]=read();
sort(a,a+n);
for(int l=1;l<n;l++)
{
sum=a[i]<a2[j]?a[i++]:a2[j++];
sum+=a[i]<a2[j]?a[i++]:a2[j++];
a2[n2++]=sum;
ans=ans+sum;
}
cout<<ans;
return 0;
}