UVAlive 2322 (13.08.23)

本文探讨了如何通过排序及特定条件下的木棍处理过程,计算出最小的设置时间。通过对给定木棍长度和重量的序列进行排序,并遵循特定规则进行分组,最终找出最少的设置次数。

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

There is a pile of n wooden sticks. The length and weight ofeach stick are known in advance. The sticks areto be processed by a woodworking machine in one by one fashion. Itneeds some time, called setup time, forthe machine to prepare processing a stick. The setup times areassociated with cleaning operations andchanging tools and shapes in the machine. The setup times of thewoodworking machine are given as follows:

  • The setup time for the first woodenstick is 1 minute.
  • Right after processing a stick of length l and weight w, the machine will need no setup time for a stickof lengthl' and weight w' if l <=l'and w <=w' .
Otherwise, it will need 1 minute for setup.
You are to find the minimum setup time to process a given pile of nwooden sticks. For example, if you havefive sticks whose pairs of length and weight are (4,9), (5,2), (2,1), (3,5),and (1,4) then the minimum setuptime should be 2 minutes since there is a sequence of pairs (1,4),(3,5), (4,9), (2,1), (5,2).

Input

The input consists of T test cases. The number of test cases (T) is given in the first line of the input file. Eachtest case consists of two lines: The first line has an integern,1<=n<=5000, that represents the number ofwooden sticks in the test case, and the second line contains 2npositiveintegers l1, w1, l2,w2, ...,ln, wn,each of magnitude at most 10000, whereli and wiare the length and weight of thei th wooden stick,respectively. The 2n integers are delimited by one ormore spaces.

Output

The output should contain the minimum setup time in minutes, one perline.

Sample Input

3
5
4 9 5 2 2 1 3 5 1 4
3
2 2 1 1 2 2
3
1 3 2 2 3 1

Sample Output

2
1
3


题意:

有点说不清楚, 我简单点说就是排序, 拿样例的第一组来说

我们要做到: 下一根木棍的长度l和重量w 都要比 当前根来的大

看第一组样例最后排成的序列 (1,4),(3,5),(4,9),(2.1),(5.2)

显然前三个木棍是符合要求的, 然后(4,9) 和 (2,1) 明显不符合要求, 那么就停止, 前三根木棍算重新设置了一下, 编成了一组

然后 将(2,1),(5,2)再继续设置, 如此往复, 求最少设置了几次~?


做法:

按长度从小到大排序, 若长度相同, 则按重量从小到大排序(先按重量再按长度也行)

然后, 我们针对当前木棍, 与剩下的木棍比较, 满足 w1 <= w2 && l1 <= l2 的, 就更新一下当前棍, 并标记~

当所有木棍都被编组后 输出到底设置了几次~


AC代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>

using namespace std;

struct Point{
    int l;
    int w;
    int mark;
}point[5555];

int cmp(Point a, Point b) {
    if(a.l != b.l)
        return a.l < b.l;
    return a.w <= b.w;
}

int main() {
    int T;
    scanf("%d", &T);

    while(T--) {
        int n;
        scanf("%d", &n);

        int time = 0;
        memset(point, 0, sizeof(point));
        for(int i = 0; i < n; i++)
            scanf("%d %d", &point[i].l, &point[i].w);

        sort(point, point+n, cmp);

        int tmp;
        for(int i = 0; i < n; i++) {
            if(point[i].mark)
                continue;
            time++;
            tmp = point[i].w;
            for(int j = i; j < n; j++) {
                if(point[j].w >= tmp && point[j].mark == 0) {
                    tmp = point[j].w;
                    point[j].mark = 1;
                }
            }
        }
        printf("%d\n", time);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值