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

时间限制: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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值