浙江大学软件学院2020年保研真题Distance of Triples (25 分)

这是一个关于算法的题目,要求找出三个整数集合中所有可能的三元组(a, b, c)的最小距离,并输出最小距离及对应的三元组。输入包含三个整数集合的大小和元素,通过排序和双指针的方法找到最小距离的解决方案。如果存在多个解,输出最大值的三元组。代码中首先读取并存储三个集合,然后对集合进行排序,接着使用三个指针遍历集合,计算当前三元组的距离并与当前最小距离比较更新结果。最后输出最小距离及对应的三元组。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

时间限制:300 ms 内存限制:64 MB

The distance of triples (三元组) (a,b,c) is defined as D(a,b,c)=∣a−b∣+∣b−c∣+∣c−a∣. Now given three non-empty integer sets S1, S2 and S3, you are supposed to find the minimum distance of all the possible triples (a,b,c) where a∈S1, b∈S2 and c∈S3.

Input Specification:
Each input file contains one test case. For each case, the first line gives three positive integers N1, N2 and N3 (all no more than 104), which are the sizes of S1, S2 and S3, respectively. Then the members of the three sets are given in the following three lines, respectively. All the numbers are integers in the range [−104,104], and they are distinct within each set. The numbers in a line are separated by spaces.

Output Specification:
For each case, print in a line MinD(a, b, c) = d, where (a, b, c) are the triples that has the minimum distance, and d is the corresponding distance. If the solution is not unique, output the largest triples.

Sample Input:
4 4 6
0 9 -1 11
10 -25 11 -10
9 2 41 17 12 30
Sample Output:
MinD(11, 11, 12) = 2
Hint:
Notice that there are two solutions. The other one is MinD(9, 10, 9) = 2. Since (11, 11, 12) is larger, this one must be printed out.

这是一道408真题,可能比较典型吧,看了题解以后发现没有比较清楚的,直接用的王道书上的思想写了一遍

#include<bits/stdc++.h>
using namespace std;
struct node {
    int aa;
    int bb;
    int cc;
};
vector<int> a,b,c;
int find_min(int a,int b,int c) {
    if (a <= b && a <= c) {
        return true;
    } else {
        return false;
    }
}
int la,lb,lc;
vector<vector<int>> ans;
int main(void) {
    freopen("pataTrain//in.txt","r",stdin);
    cin>>la>>lb>>lc;
    for(int i = 0;i < la;i++) {
        int num;
        cin>>num;
        a.push_back(num);
    }
    for(int i = 0;i < lb;i++) {
        int num;
        cin>>num;
        b.push_back(num);
    }
    for(int i = 0;i < lc;i++) {
        int num;
        cin>>num;
        c.push_back(num);
    }
    int dis = 0;
    int mindis = INT_MAX;;
    int pa,pb,pc;
    sort(a.begin(),a.end());
    sort(b.begin(),b.end());
    sort(c.begin(),c.end());
    for(pa = 0,pb = 0,pc = 0;pa < la && pb < lb && pc < lc; ) {
        int na = a[pa],nb = b[pb],nc = c[pc];
        dis = abs(na - nb) + abs(nb - nc) + abs(nc - na);
        if (dis < mindis) {
            mindis = dis;
            ans.clear();
            ans.push_back({na,nb,nc});
        } else if(dis == mindis) {
            ans.clear();
            ans.push_back({na,nb,nc});
        }
        if (find_min(na,nb,nc)) {
            pa++; 
        } else if(find_min(nb,na,nc)) {
            pb++;
        } else {
            pc++;
        }
    }
    printf("MinD(%d, %d, %d) = %d",ans[0][0],ans[0][1],ans[0][2],mindis);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值