Educational Codeforces Round 6 D. Professor GukiZ and Two Arrays(二分 | two pointers)

题意:

N2×103,0k22,使|sumasumb|

分析:

k,,k=0k=1  
k=2,2C2n  
2ai,bj,asuma=sumaai+bj,bsumb=sumb+aibj  
dif=sumasumb=sumasumb+2(ai+bj)  

然后尺取法求解,最小值一定在大于0和小于0之间

#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define L(i) i<<1
#define R(i) i<<1|1
#define INF  0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-9
#define maxn 100100
#define MOD 1000000007

int n,m;
long long a[2020],b[2020];
struct node
{
    int x,y;
    long long val;
    bool operator < (const node & a)const
    {
        return val < a.val;
    }
}aa[4000040],bb[4000040];
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int t,C = 1;
    //scanf("%d",&t);
    while(scanf("%d",&n) != EOF)
    {
        long long sum1 = 0,sum2 = 0;
        for(int i = 0; i < n; i++)
        {
            scanf("%lld",&a[i]);
            sum1 += a[i];
        }
        scanf("%d",&m);
        for(int i = 0; i < m; i++)
        {
            scanf("%lld",&b[i]);
            sum2 += b[i];
        }
        long long Min = abs(sum1-sum2);
        int ans1 = -1,ans2 = -1;
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
        {
            long long k1 = sum1 - a[i] + b[j];
            long long k2 = sum2 - b[j] + a[i];
            if(abs(k1-k2) < Min)
            {
                Min = abs(k1-k2);
                ans1 = i+1;
                ans2 = j+1;
            }
        }
        int pos1 = 0,pos2 = 0;
        for(int i = 0; i < n; i++)
            for(int j = i + 1; j < n; j++)
            {
                aa[pos1].x = i+1;
                aa[pos1].y = j+1;
                aa[pos1++].val = a[i] + a[j];
            }
        sort(aa,aa+pos1);
        for(int i = 0; i < m; i++)
            for(int j = i + 1; j < m; j++)
            {
                bb[pos2].x = i+1;
                bb[pos2].y = j+1;
                bb[pos2++].val = b[i] + b[j];
            }
        sort(bb,bb+pos2);
        int index1 = 0,index2 = 0;
        long long sum = sum1 - sum2;
        int anss1 = -1,anss2 = -1;
        while(index1 < pos1 && index2 < pos2)
        {
            long long cnt = sum - 2 * aa[index1].val + 2 * bb[index2].val;
            if(abs(cnt) < Min)
            {
                Min = abs(cnt);
                anss1 = index1;
                anss2 = index2;
            }
            if(cnt < 0)
                index2++;
            else
                index1++;
        }
        printf("%lld\n",Min);
        if(anss1 != -1)
            printf("2\n%d %d\n%d %d\n",aa[anss1].x,bb[anss2].x,aa[anss1].y,bb[anss2].y);
        else if(ans1 != -1)
            printf("1\n%d %d\n",ans1,ans2);
        else
            printf("0\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值