codeforces 620D D. Professor GukiZ and Two Arrays

D. Professor GukiZ and Two Arrays

Professor GukiZ has two arrays of integers, a and b. Professor wants to make the sum of the elements in the array a sa as close as possible to the sum of the elements in the array b sb. So he wants to minimize the value v = |sa - sb|.

In one operation professor can swap some element from the array a and some element from the array b. For example if the array a is[5, 1, 3, 2, 4] and the array b is [3, 3, 2] professor can swap the element 5 from the array a and the element 2 from the array b and get the new array a [2, 1, 3, 2, 4] and the new array b [3, 3, 5].

Professor doesn‘t want to make more than two swaps. Find the minimal value v and some sequence of no more than two swaps that will lead to the such value v. Professor makes swaps one by one, each new swap he makes with the new arrays a and b.

Input

The first line contains integer n (1 ≤ n ≤ 2000) — the number of elements in the array a.

The second line contains n integers ai ( - 109 ≤ ai ≤ 109) — the elements of the array a.

The third line contains integer m (1 ≤ m ≤ 2000) — the number of elements in the array b.

The fourth line contains m integers bj ( - 109 ≤ bj ≤ 109) — the elements of the array b.

Output

In the first line print the minimal value v = |sa - sb| that can be got with no more than two swaps.

The second line should contain the number of swaps k (0 ≤ k ≤ 2).

Each of the next k lines should contain two integers xp, yp (1 ≤ xp ≤ n, 1 ≤ yp ≤ m) — the index of the element in the array a and the index of the element in the array b in the p-th swap.

If there are several optimal solutions print any of them. Print the swaps in order the professor did them.

Sample test(s)

input

55 4 3 2 141 1 1 1

output

121 14 2

input

51 2 3 4 5115

output

00

input

51 2 3 4 541 2 3 4

output

113 1
#include<cstdio>
#include<cstring>
#include<map>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
map<ll,pair<int,int> >f;
int a[2][2005];
ll sum[2];
pair<int,int>ans[2];
int main()
{
    int n,m;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%d",&a[0][i]),sum[0]+=a[0][i];
    scanf("%d",&m);
    for(int i=0;i<m;i++)
        scanf("%d",&a[1][i]),sum[1]+=a[1][i];
    if(sum[0]==sum[1]){puts("0\n0");return 0;}
    ll bound=abs(sum[0]-sum[1]);
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        {
            ll tt=abs(sum[0]-2*(ll)a[0][i]+2*(ll)a[1][j]-sum[1]);
            if(tt<bound)ans[0].first=i+1,ans[0].second=j+1,bound=tt;
        }
    for(int i=0;i<n;i++)
        for(int j=i+1;j<n;j++)
            f[2*(ll)a[0][i]+2*(ll)a[0][j]]=make_pair(i+1,j+1);
    for(int i=0;i<m;i++)
        for(int j=i+1;j<m;j++)
        {
            ll tt=sum[0]-sum[1]+2*((ll)a[1][i]+(ll)(a[1][j]));
            map<ll,pair<int,int> >::iterator iter=f.lower_bound(tt);
            if((iter!=f.end()&&(abs(tt-iter->first)<bound))||(iter!=f.begin()&&(tt-(--iter)->first)<bound))
            {
                    ans[0]=make_pair(iter->second.first,i+1);
                    ans[1]=make_pair(iter->second.second,j+1);
                    bound=abs(iter->first-tt);
            }
        }
    printf("%I64d\n",bound);
    if(bound==sum[0]-sum[1]){puts("0");return 0;}
    if(!ans[1].first)cout<<"1\n"<<ans[0].first<<" "<<ans[0].second<<endl;
    else cout<<"2\n"<<ans[0].first<<" "<<ans[0].second<<endl<<ans[1].first<<" "<<ans[1].second<<endl;
    return 0;
}
时间: 2025-01-02 04:22:03

codeforces 620D D. Professor GukiZ and Two Arrays的相关文章

codeforces 620D Professor GukiZ and Two Arrays

1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 const int maxn = 2000 + 50; 6 7 const long long inf = 1e18; 8 9 int n, m; 10 11 long long suma, sumb; 12 13 int a[maxn], b[maxn]; 14 15 long long dbl_a[maxn], dbl_b[maxn]; 16 17 long long sum_d

Educational Codeforces Round 6

620A - Professor GukiZ's Robot    20171122 \(ans=max(\left | x2-x1 \right |,\left | y2-y1 \right |)\) #include<stdlib.h> #include<stdio.h> #include<math.h> #include<cstring> #include<iostream> #include<algorithm> using

codeforces 551 C GukiZ hates Boxes

--睡太晚了...脑子就傻了-- 这个题想的时候并没有想到该这样-- 题意大概是有n堆箱子从左往右依次排列,每堆ai个箱子,有m个人,最开始都站在第一个箱子的左边, 每一个人在每一秒钟都必须做出两种选择中的一种:1若他的位置有箱子则搬走一个箱子,2往右走一步. 问把所有箱子都搞掉的最少时间-- 很显然二分一下答案,若为x秒,则每个人都有x秒,一个一个排出去搬,看是否能够搬完-- 我竟然没想到-- #include<map> #include<string> #include<

Codeforces 551 D. GukiZ and Binary Operations

\(>Codeforces \space 551 D. GukiZ and Binary Operations<\) 题目大意 :给出 \(n, \ k\) 求有多少个长度为 \(n\) 的序列 \(a\) 满足 \((a_1\ and \ a_2)or(a_2\ and \ a_3)or..or(a_{n-1}\ and \ a_n) = k\) 且 \(a_i \leq k \leq 2^l\) 并输出方案数在$\mod m $ 意义下的值 \(0≤?n ≤?10^{18},\ 0?≤?k

Codeforces Round #307 (Div. 2) B. ZgukistringZ

Professor GukiZ doesn't accept string as they are. He likes to swap some letters in string to obtain a new one. GukiZ has strings a, b, and c. He wants to obtain string k by swapping some letters in a, so that k should contain as many non-overlapping

Codeforces551C:GukiZ hates Boxes(二分+贪心)

Professor GukiZ is concerned about making his way to school, because massive piles of boxes are blocking his way. In total there are n piles of boxes, arranged in a line, from left to right, i-th pile (1?≤?i?≤?n) containing ai boxes. Luckily, m stude

Codeforces551B:ZgukistringZ

Professor GukiZ doesn't accept string as they are. He likes to swap some letters in string to obtain a new one. GukiZ has strings a, b, and c. He wants to obtain string k by swapping some letters in a, so that k should contain as many non-overlapping

Codeforces 309C Memory for Arrays 二进制模拟进位

题目链接:点击打开链接 题意: 给定n个箱子m个物品 下面n个数字表示箱子的容量 下面m个数字b1-bm 表示物品体积为2^bi大 问最多有多少个物品可以放入箱子. 思路: 贪心,先放小的,小的不能放再放大的 显然我们把n个箱子拆成二进制,然后模拟二进制减法运算. 剩下就是简单模拟 #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include<m

Codeforces 893E - Counting Arrays

893E - Counting Arrays 思路:质因子分解. 对于每个质因子,假设它有k个,那么求把它分配到y个数上的方案数. 相当于把k个小球分配到y个盒子里的方案数. 这个问题可以用隔板法(插空法)解决,要把一段分成y段,需要y-1个隔板,那么有y-1+k个位置,选y-1个位置为隔板,剩下的都是小球,那么方案数为C(y-1+k,y-1). 如果全为正数,答案就是所有质因子方案数的积. 但是这道题目可以为负数,那么在这y个数里选偶数个变成负数 答案还要乘以C(y,0)+C(y,2)+C(y