1 条题解
-
0
C :
#include<stdio.h> #include<string.h> int main() { long n,t,i,j,m1,m2,y1,y2; long x[10005]; int b[10005]; scanf("%d",&n); for(i=0;i<n;i++){ scanf("%d",&x[i]); b[i]=0;} if(n==1){printf("%d\n",x[0]);return 0;} t=0; for(i=0;i<n-1;i++) { m1=m2=2000000000;y1=y2=0; for(j=0;j<n;j++) if(b[j]==0){ if(m1>x[j]){m2=m1;y2=y1;m1=x[j];y1=j;} else if(m2>x[j]){m2=x[j];y2=j;} } b[y1]=1;x[y2]+=x[y1]; t+=x[y2]; } printf("%d\n",t); return 0; }
C++ :
#include<iostream> #include<queue> using namespace std; struct G{ int v; bool operator<(G a)const { return v>a.v; } G operator+(G a) { v+=a.v; return *this; } }; priority_queue<G>q; int main() { int ret = 0,n; G t; cin>>n; while (n--) { cin>>t.v; q.push(t); } G a,b; while (q.size()>1) { a = q.top(); q.pop(); b = q.top(); q.pop(); ret += a.v+b.v; q.push(a+b); } // ret +=q.top().v; cout<<ret<<endl; return 0; }
Pascal :
var a:Array[0..10000]of longint;n,x,s,i,p,j:longint; procedure kp(l,r:int64); var i,j,mid,t:int64; begin i:=l;j:=r; mid:=a[(i+j) div 2]; repeat while(a[i]<mid)do inc(i); while(a[j]>mid)do dec(j); if i<=j then begin t:=a[i];a[i]:=a[j];a[j]:=t; inc(i);dec(j); end; until i>j; if l<j then kp(l,j); if i<r then kp(i,r); end; begin readln(n); for i:=1 to n do read(a[i]); kp(1,n); x:=0; i:=1; repeat inc(i); a[i]:=a[i-1]+a[i]; s:=a[i]; p:=n; for j:=i+1 to n do if s>a[j] then a[j-1]:=a[j] else begin p:=j-1;break;end; a[p]:=s; x:=x+s; until i=n; writeln(x); end.
Java :
import java.util.PriorityQueue; import java.util.Queue; import java.util.Scanner; public class Main { public static int MAX_N = 105; public static int a[] = new int[MAX_N]; public static int dp1[] = new int[MAX_N]; public static int dp2[] = new int[MAX_N]; public static void main(String args[]) { Scanner in = new Scanner(System.in); int n = in.nextInt(); Queue<Integer> q = new PriorityQueue<>(); for(int i=0;i<n;i++){ int v = in.nextInt(); q.add(v); } int ans = 0; while(q.size()>1){ int num = q.poll() + q.poll(); q.add(num); ans+=num; } System.out.println(ans); } }
- 1
信息
- ID
- 235
- 时间
- 1000ms
- 内存
- 125MiB
- 难度
- 10
- 标签
- 递交数
- 2
- 已通过
- 1
- 上传者