题解

535 条题解

  • 0
    @ 2013-08-02 22:59:16

    var n,i,k,t,j:longint;a:array[1..10001]of qword; s:qword;
    procedure sort(l,r: longint);
    var
    i,j,x,y: longint;
    begin
    i:=l;
    j:=r;
    x:=a[(l+r) div 2];
    repeat
    while a[i]<x do
    inc(i);
    while x<a[j] do
    dec(j);
    if not(i>j) then
    begin
    y:=a[i];
    a[i]:=a[j];
    a[j]:=y;
    inc(i);
    j:=j-1;
    end;
    until i>j;
    if l<j then
    sort(l,j);
    if i<r then
    sort(i,r);
    end;
    begin
    readln(n);
    for i:=1 to n do
    read(a[i]);
    sort(1,n);
    for i:=1 to n-1 do
    begin
    t:=a[i]+a[i+1];
    s:=s+t;
    j:=i+2;
    while (a[j]<t)and(j<=n)do
    j:=j+1;
    for k:=i+1 to j-2 do
    a[k]:=a[k+1];
    a[j-1]:=t;
    end;
    writeln(s);
    end.

  • 0
    @ 2013-07-24 10:20:38

    双队列

    var a,b:array[1..10010] of longint;
    n,i,ans,heada,headb,tailb,ne:longint;
    procedure sort(x,y:longint);
    var xx,yy,mid,temp:longint;
    begin
    xx:=x;yy:=y;mid:=a[(x+y) shr 1];
    repeat
    while a[xx]<mid do inc(xx);
    while a[yy]>mid do dec(yy);
    if xx<=yy then begin
    temp:=a[xx];a[xx]:=a[yy];a[yy]:=temp;
    inc(xx);dec(yy);
    end;
    until xx>yy;
    if x<yy then sort(x,yy);
    if xx<y then sort(xx,y);
    end;
    begin
    readln(n);
    for i:=1 to n+10 do begin a[i]:=maxlongint;b[i]:=maxlongint;end;
    for i:=1 to n do read(a[i]);
    sort(1,n);
    if n=1 then begin write(a[1]);exit;end;
    heada:=1;headb:=1;tailb:=0;ans:=0;
    for i:=1 to n-1 do begin
    ne:=0;
    if a[heada]<=b[headb] then begin ne:=ne+a[heada];inc(heada);end
    else begin ne:=ne+b[headb];inc(headb);end;
    if a[heada]<=b[headb] then begin ne:=ne+a[heada];inc(heada);end
    else begin ne:=ne+b[headb];inc(headb);end;
    inc(tailb);b[tailb]:=ne;ans:=ans+ne;
    end;
    write(ans);
    end.

  • 0
    @ 2013-07-11 21:24:37

    var
    heap,b,c:array[0..100000] of longint;
    i,j,n,m,k,size,y,ans,s:longint;
    procedure down(x:longint);
    var
    k,l:longint;
    begin
    if heap[x*2]<heap[x*2+1] then k:=x*2 else k:=x*2+1;
    if (heap[k]<heap[x]) and (k<=size) then
    begin
    l:=heap[x];
    heap[x]:=heap[k];
    heap[k]:=l;
    down(k);
    end;
    end;
    procedure delete(x:longint);
    begin
    y:=y+heap[1];
    heap[1]:=heap[size];
    dec(size);
    down(1);
    end;
    procedure up(x:longint);
    var k:longint;
    begin
    if (heap[x]<heap[x div 2]) and (x div 2>0) then
    begin
    k:=heap[x];
    heap[x]:=heap[x div 2];
    heap[x div 2]:=k;
    up(x div 2);
    end;

    end;
    begin
    readln(n);
    size:=1; read(heap[1]);
    for i:=2 to n do
    begin
    inc(size);
    read(k);
    if k>heap[size div 2] then heap[size]:=k
    else begin heap[size]:=k; up(size); end;
    end;
    while size>1 do
    begin
    y:=0;
    delete(size);
    y:=y+heap[1];
    ans:=ans+y;
    heap[1]:=y;
    down(1);
    end;
    writeln(ans);
    end.

    {使用堆}

  • 0
    @ 2013-06-09 19:14:38

    const MAX=100000;
    type arr=array[1..MAX] of longint;
    procedure swap(var a,b:longint);
    var temp:longint;
    begin
    temp:=a;a:=b;b:=temp;
    end;
    procedure qsort(var a:arr;x,y:integer);
    var i,j,key:integer;
    begin
    key:=a[(x+y)div 2];i:=x;j:=y;
    repeat
    while a[i]<key do inc(i);
    while a[j]>key do dec(j);
    if i<=j then begin
    swap(a[i],a[j]);inc(i);dec(j);
    end;
    until i>j;
    if i<y then qsort(a,i,y);
    if j>x then qsort(a,x,j);
    end;
    var n,i:integer;
    cost:int64;
    a:arr;
    head,tail:integer;
    procedure adjust;
    var j,k:integer;
    temp:longint;
    begin
    j:=tail;
    while a[j]>a[head] do dec(j);
    temp:=a[head];
    for k:=head to j-1 do a[k]:=a[k+1];
    a[j]:=temp;
    end;
    begin
    readln(n);cost:=0;
    for i:=1 to n do read(a[i]);
    head:=1;tail:=n;
    qsort(a,head,tail);
    for i:=1 to n-1 do begin
    a[head+1]:=a[head]+a[head+1];
    cost:=cost+a[head+1];
    inc(head);
    adjust;
    end;
    writeln(cost);
    end.
    这个比较笨的写法 没有用堆这个结构
    测试数据 #0: Accepted, time = 156 ms, mem = 1124 KiB, score = 10
    测试数据 #1: Accepted, time = 0 ms, mem = 1128 KiB, score = 10
    测试数据 #2: Accepted, time = 0 ms, mem = 1128 KiB, score = 10
    测试数据 #3: Accepted, time = 0 ms, mem = 1124 KiB, score = 10
    测试数据 #4: Accepted, time = 31 ms, mem = 1124 KiB, score = 10
    测试数据 #5: Accepted, time = 46 ms, mem = 1124 KiB, score = 10
    测试数据 #6: Accepted, time = 156 ms, mem = 1124 KiB, score = 10
    测试数据 #7: Accepted, time = 156 ms, mem = 1128 KiB, score = 10
    测试数据 #8: Accepted, time = 156 ms, mem = 1128 KiB, score = 10
    测试数据 #9: Accepted, time = 156 ms, mem = 1124 KiB, score = 10

  • 0
    @ 2013-05-04 22:28:36

    #include<cstdio>
    #include<iostream>
    using namespace std;

    const int MAXN=10010;

    int heap[MAXN],n,tot=0;

    void UpOperation()
    {
    int t=tot;
    while (t>1 && heap[t/2]>heap[t])
    {
    int temp=heap[t];
    heap[t]=heap[t/2];
    heap[t/2]=temp;
    t=t/2;
    }
    }

    void DownOperation()
    {
    int t=1;
    while ( t<=tot/2 )
    {
    int y;
    if (t*2+1>tot) y=t*2;
    else if (heap[2*t]<heap[2*t+1]) y=t*2; else y=t*2+1;
    if (heap[t]<=heap[y]) break;
    int temp=heap[t];
    heap[t]=heap[y];
    heap[y]=temp;
    t=y;
    }
    }

    int main()
    {
    scanf("%d",&n);
    int data;
    for (int i=0;i<n;++i)
    {
    scanf("%d",&data);
    heap[++tot]=data;
    UpOperation();
    }
    int sum=0;
    for (int i=1;i<n;++i)
    {
    int min1,min2;

    min1=heap[1];
    heap[1]=heap[tot--];
    DownOperation();

    min2=heap[1];
    heap[1]=heap[tot--];
    DownOperation();

    int newFruit=min1+min2;
    sum+=newFruit;
    heap[++tot]=newFruit;
    UpOperation();
    }
    printf("%d\n",sum);
    return 0;
    }

    测试数据 #0: Accepted, time = 13 ms, mem = 508 KiB, score = 10

    测试数据 #1: Accepted, time = 1 ms, mem = 504 KiB, score = 10

    测试数据 #2: Accepted, time = 1 ms, mem = 504 KiB, score = 10

    测试数据 #3: Accepted, time = 2 ms, mem = 504 KiB, score = 10

    测试数据 #4: Accepted, time = 5 ms, mem = 504 KiB, score = 10

    测试数据 #5: Accepted, time = 2 ms, mem = 508 KiB, score = 10

    测试数据 #6: Accepted, time = 7 ms, mem = 508 KiB, score = 10

    测试数据 #7: Accepted, time = 7 ms, mem = 508 KiB, score = 10

    测试数据 #8: Accepted, time = 3 ms, mem = 508 KiB, score = 10

    测试数据 #9: Accepted, time = 7 ms, mem = 504 KiB, score = 10

    Accepted, time = 56 ms, mem = 508 KiB, score = 100

  • 0
    @ 2013-02-02 14:08:31

    看到这里,猛然觉得用了STL的好缺德。。。。T.T
    直接用STL中的优先队列维护,每次取出其中最小的两个进行合并,然后在把合成后的和入队
    测试数据 #0: Accepted, time = 31 ms, mem = 868 KiB, score = 10

    测试数据 #1: Accepted, time = 14 ms, mem = 716 KiB, score = 10

    测试数据 #2: Accepted, time = 15 ms, mem = 720 KiB, score = 10

    测试数据 #3: Accepted, time = 14 ms, mem = 720 KiB, score = 10

    测试数据 #4: Accepted, time = 15 ms, mem = 756 KiB, score = 10

    测试数据 #5: Accepted, time = 31 ms, mem = 800 KiB, score = 10

    测试数据 #6: Accepted, time = 29 ms, mem = 868 KiB, score = 10

    测试数据 #7: Accepted, time = 31 ms, mem = 868 KiB, score = 10

    测试数据 #8: Accepted, time = 31 ms, mem = 868 KiB, score = 10

    测试数据 #9: Accepted, time = 31 ms, mem = 868 KiB, score = 10

    Summary: Accepted, time = 242 ms, mem = 868 KiB, score = 100

  • 0
    @ 2012-11-02 10:49:20

    超详细题解传送门:

    http://user.qzone.qq.com/1304445713/blog/1351650297

    不设访问权限,没有动画,没有音乐,很整洁就一个博客而已!

  • 0
    @ 2012-10-26 22:05:41

    program Project1;

    var a:array[1..10005] of int64;

    n:longint;

    procedure shift(k,n:longint);

    var y:int64;

    begin

    while (k shl 1

  • 0
    @ 2012-10-17 16:41:19

    var

    a:array[0..100000] of longint;

    n,smallest,ans:longint;

    procedure swap(var a,b:longint);

    var i:longint;

    begin

    i:=a; a:=b; b:=i;

    end;

    procedure min(x:longint);

    var l,r:longint;

    begin

    smallest:=0;

    l:=x*2; r:=x*2+1;

    if (l

  • 0
    @ 2012-10-16 20:34:04

    http://blog.sina.com.cn/s/blog_72fc630a0100pbcf.html

    我写得麻烦了点儿,明明记得以前写过超短的,现在忘记了。双队列解法请看以上网址。虽然可能长,但是秒杀才是王道。

    冒泡超时什么的如果你写的对那么就是评测机的问题。堆么……

  • 0
    @ 2012-08-13 14:44:23

    有必要这么麻烦吗??

    我们相信快排,我们笃信堆排,我们仰信归排,但是我们还是老老实实的冒泡吧,反正耗的又不是我的电脑内存。

    冒泡万岁!菜鸟万岁!

  • 0
    @ 2012-08-06 21:28:35

    #include

    #include

    #include

    using namespace std;

    int n,heap[10010],t1,t2,sum,wh,ans=0;

    void adjust(int wh){

       int t=wh

  • 0
    @ 2012-07-31 14:56:24

    思路:合并当前最小的两个果子

    最简单的方法是使用优先队列,那个会自动管理当前优先级最高的元素

    什么?不会优先队列?其实我也不会……

    但是C++的STL中提供了priority_queue类型(注意值小的优先级高)

    不要重复发明轮子= =

  • 0
    @ 2012-07-29 16:51:23

    编译通过...

    ├ 测试数据 01:答案正确... 0ms

    ├ 测试数据 02:答案正确... 0ms

    ├ 测试数据 03:答案正确... 0ms

    ├ 测试数据 04:答案正确... 0ms

    ├ 测试数据 05:答案正确... 0ms

    ├ 测试数据 06:答案正确... 0ms

    ├ 测试数据 07:答案正确... 0ms

    ├ 测试数据 08:答案正确... 0ms

    ├ 测试数据 09:答案正确... 0ms

    ├ 测试数据 10:答案正确... 0ms

    ---|---|---|---|---|---|---|---|-

    Accepted 有效得分:100 有效耗时:0ms

    秒掉不解释啊……

    #include

    #include

    #include

    using namespace std;

    int n,heap[10010],t1,t2,sum,wh,ans=0;

    void adjust(int wh){

    int t=wh

  • 0
    @ 2010-07-17 21:23:25

    var

    a:array[1..10000]of longint;

    i,j,n,m,i1:integer;

    k:longint;

    procedure f(x,y:integer);

    var

    i,j,k:integer;

    begin

    i:=x; j:=y; k:=a[i];

    repeat

    while (i=k) do j:=j-1;

    if i

  • 0
    @ 2010-07-07 10:27:21

    快排+二分查找,然后插入。

  • 0
    @ 2010-04-14 22:35:01

    //

    #include

    #include

    using namespace std;

    int cmp(const void *a, const void b)

    {return(
    (long int *)a-*(long int *)b);}

    int main()

    {

    long int n;long int power=0;

    cin>>n;

    long int array=new long int[n];

    long int
    pn=array+n;

    for(int i=0;i>array[i];

    }

    qsort(array,n,sizeof(array[0]),cmp);

    long int* min1,*min2;

    long int* x=array;

    for(long int d=0;d

  • 0
    @ 2010-04-14 18:02:26

    编译通过...

    ├ 测试数据 01:答案正确... 119ms

    ├ 测试数据 02:答案正确... 0ms

    ├ 测试数据 03:答案正确... 0ms

    ├ 测试数据 04:答案正确... 0ms

    ├ 测试数据 05:答案正确... 0ms

    ├ 测试数据 06:答案正确... 0ms

    ├ 测试数据 07:答案正确... 41ms

    ├ 测试数据 08:答案正确... 119ms

    ├ 测试数据 09:答案正确... 41ms

    ├ 测试数据 10:答案正确... 119ms

    ---|---|---|---|---|---|---|---|-

  • 0
    @ 2010-04-08 16:38:18

    编译通过...

    ├ 测试数据 01:答案正确... 697ms

    ├ 测试数据 02:答案正确... 0ms

    ├ 测试数据 03:答案正确... 0ms

    ├ 测试数据 04:答案正确... 0ms

    ├ 测试数据 05:答案正确... 0ms

    ├ 测试数据 06:答案正确... 103ms

    ├ 测试数据 07:答案正确... 572ms

    ├ 测试数据 08:答案正确... 728ms

    ├ 测试数据 09:答案正确... 572ms

    ├ 测试数据 10:答案正确... 697ms

    ---|---|---|---|---|---|---|---|-

    Accepted 有效得分:100 有效耗时:3369ms

    先快排,再插序

    type ar=array [1..10001] of longint;

    var ai:ar;

    n,i,x,m,t,y:longint;

    procedure kp(var ai:ar;i,j:longint);

    var x,y,e,t:longint;

    begin

    x:=i;

    y:=j;

    e:=ai[x];

    repeat

    while (x=e) do y:=y-1;

    t:=ai[y];

    ai[y]:=ai[x];

    ai[x]:=t;

    while (y>x) and (ai[x]

信息

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