题解

535 条题解

  • 0
    @ 2016-03-12 16:00:18

    #include <iostream>
    #include <queue>
    using namespace std;
    int main(){

    priority_queue<int,vector<int>,greater<int> > Q;
    int n,k,i;
    cin>>n;
    for(i=0;i<n;i++){
    cin>>k;
    Q.push(k);
    }
    int js=0;
    for(i=0;i<n-1;i++){
    int a=Q.top();
    Q.pop();
    int b=Q.top();
    Q.pop();
    js+=a+b;
    Q.push(a+b);
    }
    cout<<js<<endl;
    return 0;

    }

  • 0
    @ 2016-03-11 16:53:55

    我是神!!!
    c++代码短
    堆+贪心!
    c++
    #include <iostream>
    #include <algorithm>
    #define ref(i,x,y)for(int i=x;i<=y;i++)
    using namespace std;
    int n,ans,a[10001];
    void put(int q)
    {
    while(1){
    int q1=q*2,q2=q1+1;
    if(q1>n)break;
    if(a[q2]<a[q1]&&q2<=n)q1=q2;
    if(a[q1]>a[q])break;
    swap(a[q],a[q1]);
    q=q1;
    }
    }
    int main()
    {
    cin>>n;
    ref(i,1,n)cin>>a[i];
    sort(a+1,a+n+1);
    while(1){
    a[1]+=n>2?min(a[2],a[3]):a[2];
    ans+=a[1];
    if (n==2)break;
    int q=a[2]>a[3]?3:2;
    a[q]=a[n];
    n--;
    put(q);
    put(1);
    }
    cout<<ans;
    }

  • 0
    @ 2016-03-06 14:33:41

    AC50纪念。。。
    测试数据 #0: Accepted, time = 125 ms, mem = 880 KiB, score = 10
    测试数据 #1: Accepted, time = 0 ms, mem = 876 KiB, score = 10
    测试数据 #2: Accepted, time = 0 ms, mem = 880 KiB, score = 10
    测试数据 #3: Accepted, time = 15 ms, mem = 884 KiB, score = 10
    测试数据 #4: Accepted, time = 15 ms, mem = 880 KiB, score = 10
    测试数据 #5: Accepted, time = 31 ms, mem = 880 KiB, score = 10
    测试数据 #6: Accepted, time = 125 ms, mem = 880 KiB, score = 10
    测试数据 #7: Accepted, time = 125 ms, mem = 884 KiB, score = 10
    测试数据 #8: Accepted, time = 140 ms, mem = 884 KiB, score = 10
    测试数据 #9: Accepted, time = 125 ms, mem = 880 KiB, score = 10
    Accepted, time = 701 ms, mem = 884 KiB, score = 100

    ···pascal
    type int=longint;
    var
    n,i,j,ans,t,head,k,m:int;
    a:array[0..20001]of int;

    procedure qsort(l,r:int);
    var
    i,j,m,p:int;
    begin
    i:=l; j:=r; m:=a[(l+r) div 2];
    repeat
    while a[i]<m do inc(i);
    while a[j]>m do dec(j);
    if i<=j then
    begin
    p:=a[i];
    a[i]:=a[j];
    a[j]:=p;
    inc(i);
    dec(j);
    end;
    until i>j;
    if l<j then qsort(l,j);
    if i<r then qsort(i,r);
    end;

    begin
    readln(n);
    for i:=1 to n do read(a[i]);
    ans:=0;
    qsort(1,n);
    a[n+1]:=maxlongint;
    head:=1;
    k:=0;
    m:=n;
    repeat
    inc(k);
    t:=a[head]+a[head+1];
    ans:=ans+t;
    inc(head,2);
    for i:=head to n+1 do if a[i]>t then
    begin
    for j:=n+2 downto i+1 do a[j]:=a[j-1];
    a[i]:=t;
    inc(n);
    break;
    end;
    until k=m-1;
    writeln(ans);
    end.
    ···

  • 0
    @ 2016-03-01 22:03:35

    其实本题可以水过
    var a:array[0..10000] of longint;
    n,total:longint;
    i:integer;

    procedure swap(var a,b:longint);
    var t:integer;
    begin
    t:=a; a:=b; b:=t
    end;

    procedure quick_sort(m,n:integer);
    var i,j,x:integer;
    begin
    i:=m; j:=n; x:=a[(i+j) div 2];
    repeat
    while a[i]>x do inc(i);
    while x>a[j] do dec(j);
    if i<=j then begin
    swap(a[i],a[j]);
    inc(i); dec(j)
    end;
    until i>j;
    if m<j then quick_sort(m,j);
    if i<n then quick_sort(i,n);
    end;

    procedure insert_sort(n:integer);
    var i,j:integer;
    begin
    i:=n-1; a[0]:=a[n];
    while a[i]<a[0] do begin
    a[i+1]:=a[i];
    dec(i)
    end;
    a[i+1]:=a[0]
    end;

    begin
    readln(n);
    for i:=1 to n do
    read(a[i]);
    quick_sort(1,n);
    for i:=1 to n-1 do begin
    a[n-i]:=a[n-i]+a[n-i+1];
    a[n-i+1]:=0;
    inc(total,a[n-i]);
    insert_sort(n-i)
    end;
    writeln(total);
    end.

  • 0
    @ 2016-02-21 21:05:19

  • 0
    @ 2016-02-19 09:34:32
    /* ***********************************************
    Author        :guanjun
    Created Time  :2016/2/19 8:40:51
    File Name     :vijosp1097.cpp
    ************************************************ */
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <stdio.h>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <iomanip>
    #include <list>
    #include <deque>
    #include <stack>
    #define ull unsigned long long
    #define ll long long
    #define mod 90001
    #define INF 0x3f3f3f3f
    #define maxn 10000+10
    #define cle(a) memset(a,0,sizeof(a))
    const ull inf = 1LL << 61;
    const double eps=1e-5;
    using namespace std;
    struct item{
        int v;
        bool operator <(const item &a)const{
            return v>a.v;
        }
    };
    
    bool cmp(int a,int b){
        return a>b;
    }
    priority_queue<item>pq;
    int main()
    {
        #ifndef ONLINE_JUDGE
        //freopen("in.txt","r",stdin);
        #endif
        //freopen("out.txt","w",stdout);
        int n;
        while(cin>>n){
            int a;
            for(int i=1;i<=n;i++){
                cin>>a;
                item w;
                w.v=a;
                pq.push(w);
            }
            ll ans=0;           
            item x,y,z;
            int sum=0;
            while(--n){
                x=pq.top();pq.pop();
                y=pq.top();pq.pop();
                sum=x.v+y.v;
                z.v=sum;
                pq.push(z);
                ans+=sum;
            }
            cout<<ans<<endl;
        }
        return 0;
    }
    
    
  • 0
    @ 2016-02-18 11:30:04
    #include<stdio.h>
    #include<queue>
    using namespace std;
    priority_queue<int,vector<int>,greater<int> > q;
    int n,ans=0;
    int main(){
        scanf("%d",&n);
        for(int x,i=0;i<n;++i){
            scanf("%d",&x);
            q.push(x);
        }
        while(--n){
            int x=q.top(); q.pop();
            int y=q.top(); q.pop();
            ans+=x+y;
            q.push(x+y);
        }
        printf("%d",ans);
    }
    
  • 0
    @ 2016-01-31 22:39:24

    优先队列解题无压力,讨论版上的哈夫曼树不过也是醉了
    #include<iostream>
    #include<cstdio>
    #include<functional>
    #include<queue>
    using namespace std;
    priority_queue<int, vector<int>, greater<int> >guozi;
    int m;
    int a,b;
    int main(){
    scanf("%d",&m);
    for(int i=1;i<=m;i++){
    scanf("%d",&a);
    guozi.push(a);
    }
    int sum=0;
    while(guozi.size()>1){
    sum=sum+guozi.top();
    b=guozi.top();

    guozi.pop();

    sum=sum+guozi.top();
    int k=guozi.top();
    guozi.pop();
    guozi.push(b+k);
    }
    printf("%d",sum);
    return 0;
    }

  • 0
    @ 2016-01-27 09:06:36

    评测结果
    编译成功

    foo.cpp: In function 'int main()':
    foo.cpp:16:19: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
    if(k2==tot||k1<n&&a[k1+1]<b[k2+1])
    ^
    foo.cpp:19:19: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
    if(k2==tot||k1<n&&a[k1+1]<b[k2+1])
    ^
    测试数据 #0: Accepted, time = 15 ms, mem = 644 KiB, score = 10
    测试数据 #1: Accepted, time = 0 ms, mem = 644 KiB, score = 10
    测试数据 #2: Accepted, time = 0 ms, mem = 644 KiB, score = 10
    测试数据 #3: Accepted, time = 0 ms, mem = 644 KiB, score = 10
    测试数据 #4: Accepted, time = 15 ms, mem = 644 KiB, score = 10
    测试数据 #5: Accepted, time = 15 ms, mem = 644 KiB, score = 10
    测试数据 #6: Accepted, time = 15 ms, mem = 640 KiB, score = 10
    测试数据 #7: Accepted, time = 15 ms, mem = 644 KiB, score = 10
    测试数据 #8: Accepted, time = 15 ms, mem = 644 KiB, score = 10
    测试数据 #9: Accepted, time = 15 ms, mem = 640 KiB, score = 10
    Accepted, time = 105 ms, mem = 644 KiB, score = 100

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    int a[10003],b[10003];
    int main()
    {
    int n,ans=0,x=0;
    cin>>n;
    for(int i=1;i<=n;i++)
    cin>>a[i];
    sort(a+1,1+a+n);
    int k1=0,k2=0,tot=0;
    for(int i=1;i<=n-1;i++)
    {
    if(k2==tot||k1<n&&a[k1+1]<b[k2+1])
    x=a[++k1];
    else x=b[++k2];
    if(k2==tot||k1<n&&a[k1+1]<b[k2+1])
    x+=a[++k1];
    else x+=b[++k2];
    ans+=x;
    b[++tot]=x;
    }
    cout<<ans;
    }

  • 0
    @ 2015-11-21 15:21:31

    手写堆,还是挺快的
    记录信息
    评测状态 Accepted
    题目 P1097 合并果子
    递交时间 2015-11-22 10:05:00
    代码语言 C++
    评测机 VijosEx
    消耗时间 45 ms
    消耗内存 316 KiB
    评测时间 2015-11-22 10:05:02
    评测结果
    编译成功

    测试数据 #0: Accepted, time = 0 ms, mem = 316 KiB, score = 10
    测试数据 #1: Accepted, time = 0 ms, mem = 316 KiB, score = 10
    测试数据 #2: Accepted, time = 0 ms, mem = 316 KiB, score = 10
    测试数据 #3: Accepted, time = 0 ms, mem = 316 KiB, score = 10
    测试数据 #4: Accepted, time = 0 ms, mem = 312 KiB, score = 10
    测试数据 #5: Accepted, time = 15 ms, mem = 312 KiB, score = 10
    测试数据 #6: Accepted, time = 0 ms, mem = 312 KiB, score = 10
    测试数据 #7: Accepted, time = 15 ms, mem = 316 KiB, score = 10
    测试数据 #8: Accepted, time = 15 ms, mem = 312 KiB, score = 10
    测试数据 #9: Accepted, time = 0 ms, mem = 316 KiB, score = 10
    Accepted, time = 45 ms, mem = 316 KiB, score = 100
    代码
    #include <iostream>
    #include <stdio.h>
    using namespace std;
    int heap[10010],cnt;
    void add(int x)
    { int p=++cnt;
    while(p!=1)
    {
    int t=p/2;
    if(heap[t]<=x)break;
    heap[p]=heap[t];
    p=t;
    }
    heap[p]=x;
    }
    void del()
    { if(cnt==0)return;
    int p=1,temp=heap[cnt--];
    while(1)
    { int a=p*2,b=p*2+1;
    if(a>cnt)break;
    if(heap[a]>heap[b]&&b<=cnt)a=b;
    if(temp<=heap[a])break;
    heap[p]=heap[a];
    p=a;
    }
    heap[p]=temp;
    }
    int main()
    { int n,ans=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    { int a;
    scanf("%d",&a);
    add(a);
    }
    for(int i=1;i<n;i++)
    { int a=heap[1];del();
    int b=heap[1];del();
    ans+=a+b;
    add(a+b);
    }
    printf("%d",ans);
    return 0;
    }

  • 0
    @ 2015-10-26 22:11:30

    #include<iostream>
    #include<cmath>
    #include<cstring>
    #include<cstdio>
    #include<queue>
    using namespace std;
    int n,x,y,z,ans;
    priority_queue<int,vector<int>,greater<int> >q;
    int main()
    {
    cin>>n;
    for(int i=1;i<=n;i++){
    cin>>x;q.push(x);
    }
    ans=0;
    while(q.size()>=2){
    y=q.top();q.pop();
    z=q.top();q.pop();
    x=y+z;ans=ans+x;
    q.push(x);
    }
    x=q.top();
    ans=ans;
    cout<<ans;
    }
    其实用优先队列最简单。。。。

    • @ 2016-01-31 11:13:14

      啊!!是这样的啊大神。。我编总是走远

  • 0
    @ 2015-10-26 14:54:02

    #include<iostream>

    #include<cstdio>

    #include<cstring>

    #include<algorithm>

    #include<cmath>

    using namespace std;

    int n,i,j,a[10002],ans,tot;

    int comp(int a,int b)

    {

    if(a<b) return 1;

    else return 0;

    }

    int main()

    {

    cin>>n;

    for(i=1;i<=n;i++) cin>>a[i];

    sort(a+1,a+n+1,comp);

    for(i=1;i<n;i++)
    {
    ans=a[i]+a[i+1];
    a[n+1]=ans;
    tot=tot+ans;
    for(j=i+1;j<=n;j++)
    {
    a[j]=a[j+1];
    if(a[j+1]>ans) { a[j]=ans; break;}

    }

    }

    cout<<tot;

    return 0;

    }

  • 0
    @ 2015-10-02 19:12:05

    用的最简单的方法,不过AC了……好高兴呢~
    program combine;
    var
    n,m,i,min1i,min2i,num,mun:integer;
    min1,min2,sum:longint;
    a:array[1..20000] of longint;
    begin
    readln(n);
    for i:=1 to n do
    read(a[i]);
    min1:=maxlongint;
    min2:=maxlongint;
    m:=n;
    sum:=0;
    repeat
    for i:=1 to m do
    if a[i]<min1 then
    begin
    min1:=a[i];
    min1i:=i;
    end;
    for i:=1 to m do
    if (a[i]<min2) and (a[i]>=min1) and (i<>min1i) then
    begin
    min2:=a[i];
    min2i:=i;
    end;
    if min1i<min2i then
    begin
    num:=min1i;
    mun:=min2i;
    end
    else
    begin
    num:=min2i;
    mun:=min1i;
    end;
    a[num]:=min1+min2;
    for i:=mun to m-1 do
    a[i]:=a[i+1];
    a[m]:=0;
    sum:=sum+min1+min2;
    min1:=maxlongint;
    min2:=maxlongint;
    m:=m-1;
    until m=1;
    write(sum);
    end.

  • 0
    @ 2015-10-01 22:19:53

    1、调用库,37行秒杀
    2、队列,3种情况判断
    3、堆(练手专用

  • 0
    @ 2015-09-24 02:34:17

    #include <iostream>
    #include <algorithm>
    #include <vector>

    using namespace std;

    int n;
    int i;
    int j;
    vector<int>apple;

    void llo(int f)
    {
    if(f==n-1)return;
    if(apple[f]>apple[f+1])
    {

    j=apple[f];
    apple[f]=apple[f+1];
    apple[f+1]=j;
    llo(f+1);
    }
    else return;
    }

    int main()
    {
    cin>>n;
    int he[n-1];
    for(i=0;i<n;++i)
    {
    cin>>j;
    apple.push_back(j);
    }
    sort(apple.begin(),apple.end());
    long int w=0;
    int f=0;
    for(i=0;i<n-1;i++)
    {
    j=apple[f]+apple[f+1];
    ++f;
    apple[f]=j;
    w+=apple[f];
    llo(f);
    }
    cout<<w;
    return 0;
    }

  • 0
    @ 2015-09-21 20:00:49

    AC 12 ti liu nian !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! hahahaahahahahaha !!!!!!!!!!!!!!!!!!!!!!!!!!!!

  • 0
    @ 2015-09-15 12:34:18

    #include<iostream>
    #include<cstring>
    #include<cmath>
    #include<cstdio>
    #include<algorithm>
    using namespace std;

    int a[10010];

    void change(int &a, int&b){
    int t = a;
    a = b;
    b = t;

    }

    int main()
    {
    int n;
    long long ans = 0;
    scanf("%d", &n);
    for(int i=1; i<=n; i++)
    scanf("%d", &a[i]);
    sort(&a[1], &a[1]+n);
    for(int i=1; i<n; i++){
    int j = i;
    while(a[j] > a[j+1]){
    if(j >= n)
    break;
    change(a[j], a[j+1]);
    j++;
    }
    ans += a[i] + a[i+1];
    a[i+1] += a[i];
    }
    printf("%lld", ans);
    system("pause");
    return 0;
    }
    注意每次可以在原排序的基础上修改

  • 0
    @ 2015-09-11 22:52:45

    #include <iostream>
    #include <algorithm>
    using namespace std;
    int ans,n;
    int fruit[10005];
    void input()
    {
    cin>>n;
    for(int i=1;i<=n;i++)cin>>fruit[i];
    return;
    }
    int main()
    {
    for(int i=1;i<=10004;i++)fruit[i]=2147483647;//初始化 防止诡异的越界
    input();//输入数据
    sort(fruit+1,fruit+1+n);//先排好升序
    for(int pos=1;pos<=n-1;pos++)
    {
    int temp=pos;
    while(fruit[temp]>fruit[temp+1])//重新排序的过程
    {
    swap(fruit[temp],fruit[temp+1]);
    temp++;
    }
    fruit[pos+1]+=fruit[pos];//合并到下一堆里去
    ans+=fruit[pos+1];//耗费体力
    }
    cout<<ans;
    return 0;
    }

  • 0
    @ 2015-09-01 16:22:19

    #include<iostream>
    using namespace std;
    main()
    {
    int z[10000],t,i,w,j[10000];
    cin>>i;
    for(int a=1;a<=i;a++)
    {
    cin>>z[a];
    }
    for(int a=1;a<=i;a++)
    {
    for(int b=1;b<=i;b++)
    {
    if(z[a]>=z[b])
    {
    t=z[b];
    z[b]=z[a];
    z[a]=t;
    }
    }
    }
    for(int a=i;a>=1;a--)
    {

    w=a;
    for(int b=w-2;b>=1;b--)
    {
    if(z[b]<=z[a-1])
    {
    t=z[b];
    z[b]=z[a-1];
    z[a-1]=t;
    }
    }
    z[a-1]=z[a]+z[a-1];
    z[a]=0;
    j[a-1]=z[a-1];
    }
    t=0;
    for(int a=1;a<=i-1;a++){
    t=j[a]+t;
    }
    cout<<t;
    }

  • 0
    @ 2015-09-01 14:31:28

    #include <iostream>
    #include <algorithm>
    #define MAXN 10001
    using namespace std;
    int heap_size=0;
    int heap[MAXN],input[MAXN];
    /*
    void put(int num){
    int now,next;
    heap[++heap_size]=num;
    now=heap_size;
    while(now>1){
    next=now>>1;
    if(heap[now]>=heap[next]) break;
    swap(heap[now],heap[next]);
    now=next;
    }
    }
    void get(){
    int now,next;
    heap[1]=heap[heap_size];
    heap_size--;
    now=1;
    while(now*2<=heap_size){
    next=now*2;
    if(next<heap_size && heap[next+1]<heap[next]) next++;
    if(heap[now]<=heap[next]) break;
    swap(heap[now],heap[next]);
    now=next;
    }
    }
    */
    void put(int num){
    heap[++heap_size]=num;
    push_heap(heap+1,heap+heap_size+1,greater<int>());
    }
    int get(){
    int res;
    res=heap[1];
    pop_heap(heap+1,heap+heap_size+1,greater<int>());
    heap_size--;
    return res;
    }
    int main(){
    int n,ans=0;
    cin>>n;
    for(int i=1;i<=n;i++){
    cin>>input[i];
    put(input[i]);
    }
    while(heap_size>1){
    int tmp1=get();
    int tmp2=get();
    int tmp=tmp1+tmp2;
    ans+=tmp;
    put(tmp);
    }
    cout<<ans<<endl;
    return 0;
    }
    /*
    10
    3 5 1 7 6 4 2 5 4 1
    */

    请记住,我叫傻蛋,抄了我的程序并AC后,请记得回踩哦!!!!!
    -----我是傻蛋 2015/9/1

    • @ 2015-09-01 19:28:03

      太棒了!傻蛋!你的程序好棒!!!
      赞一个!!!!!

信息

ID
1097
难度
6
分类
贪心 点击显示
标签
递交数
23906
已通过
6330
通过率
26%
被复制
41
上传者