题解

6 条题解

  • 2
    @ 2020-11-22 11:57:01
    #include <cmath>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    #include <deque>
    using namespace std;
    
    namespace dts
    {
        typedef long long ll;
        
        ll n,a[1<<19],b[1<<19],c[1<<20];
        
        void main()
        {
            scanf("%lld",&n);
            for (ll i=0;i<n;i++)
                scanf("%lld",&a[i]);
            for (ll i=0;i<n;i++)
                scanf("%lld",&b[i]);
            for (ll i=0;i<n;i++)
                c[i]=a[i]-=llabs((n-1)/2-i),c[i+n]=b[i]-=llabs((n-1)/2-i);
            sort(&c[0],&c[n<<1]);
            ll mid=(c[n-1]+c[n])>>1,ans=0;
            for (ll i=0;i<(n<<1);i++)
                ans+=llabs(c[i]-mid);
            printf("%lld\n",ans);
        }
    }
    
    int main()
    {
        dts::main();
    }
    
  • 1
    @ 2014-10-06 11:37:45

    令c[i]=
    {
    t-i+1 1<=i<=n/2+1
    i-t+1 n/2+1<i<=n
    }
    将a[i]和b[i]-=c[i]之后,问题就转化成了将a和b的所有元素变成同一个值得最小代价,
    求中位数即可
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<cmath>
    #include<iostream>
    #include<vector>
    #include<set>
    #include<map>
    #include<string>
    #include<algorithm>
    #include<queue>
    #include<list>
    #define FOR(i,a,b) for(i=(a);i<=(b);i++)
    #define ROF(i,a,b) for(i=(a);i>=(b);i--)
    #define mmt(a,b) memset(a,b,sizeof(a))
    #define pb push_back
    #define mp make_pair
    #define y1 fuck
    #define N 700010
    using namespace std;
    typedef long long LL;
    typedef long double LD;
    int n,a[N],b[N],c[N];
    int main()
    {
    scanf("%d",&n);int i;
    FOR(i,1,n) scanf("%d",&a[i]);
    FOR(i,1,n) scanf("%d",&b[i]);
    int t=n/2+1;
    FOR(i,1,t) a[i]-=t-i+1;
    FOR(i,t+1,n) a[i]-=i-t+1;
    FOR(i,1,t) b[i]-=t-i+1;
    FOR(i,t+1,n) b[i]-=i-t+1;
    FOR(i,1,n) c[i]=a[i];
    FOR(i,n+1,2*n) c[i]=b[i-n];
    sort(c+1,c+1+2*n);
    LL ans=0;
    FOR(i,1,2*n) ans+=abs(c[i]-c[n]);
    printf("%lld\n",ans);
    return 0;
    }

  • 0
    @ 2014-10-22 21:30:26

    program vijos1882;
    var
    a,b:array[1..300010] of int64;
    c:array[1..600010] of int64;
    i,j:longint;
    k,m,n,mid:int64;
    procedure qp(l,r:int64);
    var
    i,j,x,y:int64;
    begin
    i:=l; j:=r;
    x:=c[(l+r)div 2];
    repeat
    while c[i]<x do inc(i);
    while c[j]>x do dec(j);
    if i<=j then
    begin
    y:=c[i]; c[i]:=c[j]; c[j]:=y;
    inc(i); dec(j);
    end;
    until i>j ;
    if j>l then qp(l,j);
    if i<r then qp(i,r);
    end;
    begin
    {assign(input,'hah.in');
    assign(output,'hah.out');
    reset(input);
    rewrite(output);}

    readln(n);
    for i:=1 to n do
    read(a[i]);
    readln;
    for i:=1 to n do
    read(b[i]);
    mid:=(n+1) div 2;
    for i:=1 to mid-1 do
    begin
    a[mid-i]:=a[mid-i]-i;
    a[mid+i]:=a[mid+i]-i;
    b[mid-i]:=b[mid-i]-i;
    b[mid+i]:=b[mid+i]-i;
    end;
    for i:=1 to n do
    c[i]:=a[i];
    for i:=1 to n do
    c[i+n]:=b[i];
    qp(1,2*n);
    k:=c[n];
    for i:=1 to n do
    m:=m+k-c[i];
    for i:=n+1 to 2*n do
    m:=m+c[i]-k;
    writeln(m);

    { close(input);
    close(output); }
    end.

  • 0
    @ 2014-10-06 11:45:23

    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    long long s[300010];
    long long m[300010];
    long long o[600010];
    long long ans,alpha;
    int main()
    {
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    scanf("%I64d",&s[i]);
    for(int i=1;i<=n;i++)
    scanf("%I64d",&m[i]);
    int mid=(n+1)/2;
    for(int i=1;i<=mid-1;i++)
    {
    s[mid-i]-=i;
    s[mid+i]-=i;
    m[mid-i]-=i;
    m[mid+i]-=i;
    }
    for(int i=1;i<=n;i++)
    o[i]=s[i];
    for(int i=1;i<=n;i++)
    o[n+i]=m[i];
    sort(o+1,o+2*n+1);
    alpha=o[n];
    for(int i=1;i<=n;i++)
    ans+=(alpha-o[i]);
    for(int i=n+1;i<=2*n;i++)
    ans+=(o[i]-alpha);
    printf("%I64d\n",ans);
    return 0;
    }

    -------------以上贪心算法来自'1756500824',orz orz--------------------------------
    -------------以上贪心算法来自'1756500824',orz orz--------------------------------
    -------------以上贪心算法来自'1756500824',orz orz--------------------------------
    我现在是打击报复。。。。

  • 0
    @ 2014-10-06 11:26:22

    include<cstdio>
    include<cstdlib>
    include<cstring>
    include<iostream>
    include<algorithm>
    using namespace std;
    long long s[300010];
    long long m[300010];
    long long o[600010];
    long long ans,alpha;
    int main()
    {
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    scanf("%I64d",&s[i]);
    for(int i=1;i<=n;i++)
    scanf("%I64d",&m[i]);
    int mid=(n+1)/2;
    for(int i=1;i<=mid-1;i++)
    {
    s[mid-i]-=i;
    s[mid+i]-=i;
    m[mid-i]-=i;
    m[mid+i]-=i;
    }
    for(int i=1;i<=n;i++)
    o[i]=s[i];
    for(int i=1;i<=n;i++)
    o[n+i]=m[i];
    sort(o+1,o+2n+1);
    alpha=o[n];
    for(int i=1;i<=n;i++)
    ans+=(alpha-o[i]);
    for(int i=n+1;i<=2n;i++)
    ans+=(o[i]-alpha);
    printf("%I64d\n",ans);
    return 0;
    }
    -------------以上贪心算法来自1756500824,orz orz--------------------------------
    好吧我是有一点追求完美。。。。。。

  • 0
    @ 2014-10-06 11:18:32

    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    long long s[300010];
    long long m[300010];
    long long o[600010];
    long long ans,alpha;
    int main()
    {
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    scanf("%I64d",&s[i]);
    for(int i=1;i<=n;i++)
    scanf("%I64d",&m[i]);
    int mid=(n+1)/2;
    for(int i=1;i<=mid-1;i++)
    {
    s[mid-i]-=i;
    s[mid+i]-=i;
    m[mid-i]-=i;
    m[mid+i]-=i;
    }
    for(int i=1;i<=n;i++)
    o[i]=s[i];
    for(int i=1;i<=n;i++)
    o[n+i]=m[i];
    sort(o+1,o+2*n+1);
    alpha=o[n];
    for(int i=1;i<=n;i++)
    ans+=(alpha-o[i]);
    for(int i=n+1;i<=2*n;i++)
    ans+=(o[i]-alpha);
    printf("%I64d\n",ans);
    return 0;
    }
    -------------以上贪心算法来自1756500824,orz orz--------------------------------

  • 1

信息

ID
1882
难度
6
分类
(无)
标签
(无)
递交数
357
已通过
107
通过率
30%
被复制
2
上传者