• 埃森哲杯第十六届上海大学程序设计联赛春季赛暨上海高校金马五校赛 Wasserstein Distance


    题目链接:https://www.nowcoder.com/acm/contest/91/A

    思路:贪心(写复杂了)

    数据弱,还可以直接暴力

     1 #include<iostream>
     2 #include<string>
     3 #include<algorithm>
     4 #include<cmath>
     5 #include<string.h>
     6 #include<stdio.h>
     7 #include<queue>
     8 using namespace std;
     9 int a[100500];
    10 int b[100500];
    11 struct Node{
    12     int num;
    13     int value;
    14     bool operator < (const Node &a) const {
    15         return num>a.num;//最小值优先
    16     }
    17 };
    18 int main(){
    19     int T;
    20     cin>>T;
    21     while(T--){
    22         memset(a,0,sizeof(a));
    23         memset(b,0,sizeof(b));
    24         int n;
    25         cin>>n;
    26         for(int i=0;i<n;i++){
    27             scanf("%d",a+i);
    28         }
    29         for(int i=0;i<n;i++){
    30             scanf("%d",b+i);
    31         }
    32         priority_queue< Node > q1,q2;
    33         for(int i=0;i<n;i++){
    34             Node tmp;
    35             int value=a[i]-b[i];
    36             tmp.num=i;
    37             tmp.value=value;
    38             if(value==0){
    39                 continue;
    40             }else if(value<0){
    41                 q1.push(tmp);
    42             }else{
    43                 q2.push(tmp);
    44             }
    45         }
    46         long long  ans=0;
    47         while(!q2.empty()){
    48             Node tmp1=q1.top();
    49             Node tmp2=q2.top();
    50             int x,y;
    51             x=tmp1.value;
    52             y=tmp2.value;
    53             if(x+y==0){
    54                 ans+=y*abs(tmp1.num-tmp2.num);
    55                 q1.pop();
    56                 q2.pop();
    57             }else if(x+y<0){
    58                 ans+=y*abs(tmp1.num-tmp2.num);
    59                 q1.pop();
    60                 q2.pop();
    61                 tmp1.value=x+y;
    62                 q1.push(tmp1);
    63             }else{
    64                 ans+=(-x)*abs(tmp1.num-tmp2.num);
    65                 q1.pop();
    66                 q2.pop();
    67                 tmp2.value=x+y;
    68                 q2.push(tmp2);
    69             }
    70         }
    71         cout<<ans<<endl;
    72     }
    73     return 0;
    74 }
     1 /*暴力*/
     2 #include<iostream>
     3 #include<bits/stdc++.h>
     4 using namespace std;
     5 long long a[100500];
     6 long long b[100500];
     7 int main(){
     8     long long T;
     9     cin>>T;
    10     while(T--){
    11         long long n;
    12         cin>>n;
    13         long long cnt=0;
    14         for(long long i=0;i<n;i++){
    15             scanf("%lld",a+i);
    16         }
    17         for(long long i=0;i<n;i++){
    18             scanf("%lld",b+i);
    19         }
    20         for(long long i=0;i<n;i++){
    21             if(a[i]==b[i]){
    22                 continue;
    23             }
    24             else if(a[i]<b[i]){
    25                 for(long long j=i;j<n;j++){
    26                     if(a[i]==b[i]){
    27                         break;
    28                     }
    29                     if(a[j]>b[j]){
    30                         long long value=a[j]-b[j];
    31                         if(value==b[i]-a[i]){
    32                             cnt+=value*abs(i-j);
    33                             a[i]=b[i];
    34                             a[j]=b[j];
    35                         }else if(value > b[i]-a[i]){
    36                             cnt+=(b[i]-a[i])*abs(i-j);
    37                             a[j]=a[j]-(b[i]-a[i]);
    38                             a[i]=b[i];
    39                         }else{
    40                             cnt+=value*abs(i-j);
    41                             a[i]=a[i]+value;
    42                             a[j]=b[j];
    43                         }
    44                     }
    45                 }
    46             }else{
    47                 for(long long j=i;j<n;j++){
    48                     if(a[i]==b[i]){
    49                         break;
    50                     }
    51                     if(a[j]<b[j]){
    52                         long long value=b[j]-a[j];
    53                         if(value>a[i]-b[i]){
    54                             cnt+=(a[i]-b[i])*abs(i-j);
    55                             a[j]=a[j]+(a[i]-b[i]);
    56                             a[i]=b[i];
    57                         }else if(value<a[i]-b[i]){
    58                             cnt+=value*abs(i-j);
    59                             a[j]=b[j];
    60                             a[i]=a[i]-value;
    61                         }else{
    62                             cnt+=value*abs(i-j);
    63                             a[j]=b[j];
    64                             a[i]=b[i];
    65                         }
    66                     }
    67                 }
    68             }
    69         }
    70         cout<<cnt<<endl;
    71     }
    72     return 0;
    73 }
  • 相关阅读:
    整合SSH遇到的问题
    学习动态代理实现业务层的事务控制遇到的问题
    docker局域网独立IP外网访问配置
    第23次CSP-D题解法
    Nordic Bluetooth 切换到DFU Mode
    MySQL事务、锁和MVCC
    深拷贝和浅拷贝的区别是什么?
    python是如何进行内存管理的
    数据库的事务
    斐波那契数列
  • 原文地址:https://www.cnblogs.com/ISGuXing/p/8868067.html
Copyright © 2020-2023  润新知