• 程序设计思维与实践 Week4 作业 (3/4/数据班)


    A - DDL 的恐惧

    ZJM 有 n 个作业,每个作业都有自己的 DDL,如果 ZJM 没有在 DDL 前做完这个作业,那么老师会扣掉这个作业的全部平时分。所以 ZJM 想知道如何安排做作业的顺序,才能尽可能少扣一点分。请你帮帮他吧!

    Input

    输入包含T个测试用例。输入的第一行是单个整数T,为测试用例的数量。每个测试用例以一个正整数N开头(1<=N<=1000),表示作业的数量。然后两行。第一行包含N个整数,表示DDL,下一行包含N个整数,表示扣的分。

    Output

    对于每个测试用例,您应该输出最小的总降低分数,每个测试用例一行。

    Sample Input
    3
    3
    3 3 3
    10 5 1
    3
    1 3 1
    6 2 3
    7
    1 4 6 4 2 4 3
    3 2 1 7 6 5 4
    
    Sample Output
    0
    3
    5
    

    Hint

    上方有三组样例。对于第一组样例,有三个作业它们的DDL均为第三天,ZJM每天做一个正好在DDL前全部做完,所以没有扣分,输出0。对于第二组样例,有三个作业,它们的DDL分别为第一天,第三天、第一天。ZJM在第一天做了第一个作业,第二天做了第二个作业,共扣了3分,输出3。

    问题分析

    该题目的原型,是poj1456 supermarket(超市销售商品问题)

    每天的任务,可以储存在一个结构体内,数据又两部分:价值(分值)和期限(时间),然后按照价值或者期限的顺序,排序。记录下最晚的期限,从那一天开始,向前一天安排任务,当一个任务被安排后,做标记,避免重复。优先安排价值大的项目。

    不同之处在于,这个题目要求输出总价值减去已选择的价值。

    #include <cstdio>
    #include <cstring>
    #include <queue>
     
    using namespace std;
     
    const int MAXN=10005;
     
    struct Node {
        int p,d;
     
        Node(int pp=0,int dd=0):p(pp),d(dd) {}
     
        bool operator < (const Node& a) const {
            return p<a.p||(p==a.p&&d<a.d);
        }
    }cur;
     
    int n,ans;
    int pre[MAXN],p,d,mxd;
    bool vis[MAXN];
    priority_queue<Node> q;
     
    int getPre(int a) {
        if(vis[pre[a]]) {
            return pre[a]=getPre(pre[a]);
        }
        return pre[a];
    }
     
    int main() {
    	int T;
    	int n;
    	scanf("%d",&T);
        while(T>0) {
        	T--;
        	scanf("%d",&n);
            ans=mxd=0;
            int sum=0;
            int a[2][n];
            for(int i=0;i<2;++i){
            	for(int j=0;j<n;++j){
            		scanf("%d",&a[i][j]);
    			}
    		}
            for(int i=0;i<n;++i) {
            	p=a[1][i];
            	d=a[0][i];
            	sum+=p;
                q.push(Node(p,d));
                if(mxd<d) {
                    mxd=d;
                }
            }
            vis[0]=false;
            for(int i=1;i<=mxd;++i) {
                pre[i]=i-1;
                vis[i]=false;
            }
            while(!q.empty()) {
                cur=q.top();
                q.pop();
                d=cur.d;
                if(vis[d]) {
                    d=getPre(d);
                }
                if(d!=0) {
                    ans+=cur.p;
                    vis[d]=true;
                }
            }
            printf("%d
    ",sum-ans);
        }
        return 0;
    }
    

    B - 四个数列

    ZJM 有四个数列 A,B,C,D,每个数列都有 n 个数字。ZJM 从每个数列中各取出一个数,他想知道有多少种方案使得 4 个数的和为 0。当一个数列中有多个相同的数字的时候,把它们当做不同的数对待。请你帮帮他吧!

    Input

    第一行:n(代表数列中数字的个数) (1≤n≤4000)接下来的 n 行中,第 i 行有四个数字,分别表示数列 A,B,C,D 中的第 i 个数字(数字不超过 2 的 28 次方)

    Output

    输出不同组合的个数。

    Sample Input
    6
    -45 22 42 -16
    -41 -27 56 30
    -36 53 -37 77
    -36 30 -75 -46
    26 -38 -10 62
    -32 -54 -6 45
    
    Sample Output
    5
    

    Hint

    样例解释: (-45, -27, 42, 30), (26, 30, -10, -46), (-32, 22, 56, -46),(-32, 30, -75, 77), (-32, -54, 56, 30).

    问题分析

    要避免超时,必须使用二分的方法。

    四个数列,先给前两个数列中,各选择1个元素,求和,得到一个新的数列v,使用sort() 对这个数列进行排序。然后考察后两个数列,仍然两两求和,用二分的方法,在数列v中找s的相反数如果找到了,由于这个数左边和右边相邻的仍有可能符合条件的,所以需要向左边和右边再找一下

    #include <iostream>
    #include <vector>
    #include <cstdio>
    #include <algorithm>
    using namespace std;
    
    int arr[4][4005];
    
    int main()
    {
        int n;
        int ans = 0;
        vector<int> v;
        scanf("%d", &n);
        for (int i = 0; i < n; ++i)
            scanf("%d%d%d%d", &arr[0][i], &arr[1][i], &arr[2][i], &arr[3][i]);
                
        for(int i=0;i<n;++i)
            for (int j = 0; j < n; ++j)
            {
                int s = arr[0][i] + arr[1][j];
                v.push_back(s);
            }
        sort(v.begin(), v.end());
        for(int i=0;i<n;++i)
            for (int j = 0; j < n; ++j)
            {
                int s = arr[2][i] + arr[3][j];
                s *= (-1);
                int left = 0,
                    right = v.size() - 1;
                while (left <= right)
                {
                    int mid = (left + right) / 2;
                    if (v[mid] == s)
                    {
                        ans++;
                        int tag = mid;
                        while ((v[--tag]) == s)
                            ans++;
                        tag = mid;
                        while ((v[++tag]) == s)
                            ans++;
                        break;
                    }
                    else if (v[mid] < s)
                        left = mid + 1;
                    else
                        right = mid - 1;
                }
            }
        printf("%d
    ", ans);
        return 0;
    }
    

    C - TT 的神秘礼物

    TT 是一位重度爱猫人士,每日沉溺于 B 站上的猫咪频道。
    有一天,TT 的好友 ZJM 决定交给 TT 一个难题,如果 TT 能够解决这个难题,ZJM 就会买一只可爱猫咪送给 TT。
    任务内容是,给定一个 N 个数的数组 cat[i],并用这个数组生成一个新数组 ans[i]。新数组定义为对于任意的 i, j 且 i != j,均有 ans[] = abs(cat[i] - cat[j]),1 <= i < j <= N。试求出这个新数组的中位数,中位数即为排序之后 (len+1)/2 位置对应的数字,'/' 为下取整。TT 非常想得到那只可爱的猫咪,你能帮帮他吗?

    Input

    多组输入,每次输入一个 N,表示有 N 个数,之后输入一个长度为 N 的序列 cat, cat[i] <= 1e9 , 3 <= n <= 1e5

    Output

    输出新数组 ans 的中位数

    Sample Input
    4
    1 3 2 4
    3
    1 10 2
    
    Sample Output
    1
    8
    

    问题分析

    首先可以确定结果的范围,将cat[] 从小到大排序,数组ans[]中的元素的最小值为0,最大值为 cat[N-1]-cat[0]。采用二分的思路,确定中位数。先假设中位数是最大值和最小值的一半,然后确定它的排名。这里使用了lower_bound(),对于每个可能的中位数,如果伫列中的第lower_bound(a,a+n,a[i]+x)-a个元素比中位数大,那么这个数之后的元素也满足条件,共有

    for(int i=0;i<n;++i)
        	tot+=n-(lower_bound(a,a+n,a[i]+x)-a);
    

    个元素满足条件。如果tot的值偏大,意味着所取的中位数偏小,取值区间的左端点就应该右移到之前所取的中位数处,继续计算。

    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    using namespace std;
    const int N = 0x7ffff;
    int a[N];
    bool solve(int x, int m, int n)
    {
        int tot=0;
        for(int i=0;i<n;++i)
        	tot+=n-(lower_bound(a,a+n,a[i]+x)-a);/*the seris which is about the first number greater than or equal to 
    		a[i]+x,All numbers after it satisfy this condition*/ 
        return tot>(m/2);/*Only when this condition is met, the larger number is more*/
        /*This means that m is too small,Interval needs to move to the right*/
    }
    int main()
    {
    	int m,n;
        while(scanf("%d",&n)!=EOF){
            m=(n*(n-1))/2;
            for(int i = 0; i<n;++i){
            	scanf("%d",&a[i]);
    		}
            sort(a,a+n);
            int L=0;
    		int R=a[n-1]-a[0]+1;
            while(R-L>1){
                int mid=(L+R)/2;
                if(solve(mid,m, n))
                    L=mid;
                else
                    R=mid;
            }
            printf("%d
    ",L);
        }
        return 0;
    }
    
  • 相关阅读:
    markdown语法及工具
    关于div的宽度或高度设置为100%时
    响应式css垂直居中
    JavaScript之闭包问题以及立即执行函数
    JavaScript和JQuery好书推荐
    数组中去重
    解决getImageData跨域问题
    js在for循环中绑定事件
    表格横竖颠倒
    上传按钮美化遇到的问题
  • 原文地址:https://www.cnblogs.com/master-cn/p/12522205.html
Copyright © 2020-2023  润新知