On Segment's Own Points(模拟)

本文探讨了大学宿舍中晾衣绳的合理分配问题,通过数学建模帮助Alexey找到最大化的个人晾衣区域,避免与其他学生衣物产生接触。详细介绍了输入输出规范及解决策略,适合计算机科学领域的读者深入理解。

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

A. On Segment's Own Points
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Our old friend Alexey has finally entered the University of City N — the Berland capital. Alexey expected his father to get him a place to live in but his father said it was high time for Alexey to practice some financial independence. So, Alexey is living in a dorm.

The dorm has exactly one straight dryer — a 100 centimeter long rope to hang clothes on. The dryer has got a coordinate system installed: the leftmost end of the dryer has coordinate 0, and the opposite end has coordinate 100. Overall, the university has nstudents. Dean's office allows i-th student to use the segment (li, ri) of the dryer. However, the dean's office actions are contradictory and now one part of the dryer can belong to multiple students!

Alexey don't like when someone touch his clothes. That's why he want make it impossible to someone clothes touch his ones. So Alexey wonders: what is the total length of the parts of the dryer that he may use in a such way that clothes of the others (n - 1) students aren't drying there. Help him! Note that Alexey, as the most respected student, has number 1.

Input

The first line contains a positive integer n (1 ≤ n ≤ 100). The (i + 1)-th line contains integers li and ri (0 ≤ li < ri ≤ 100) — the endpoints of the corresponding segment for the i-th student.

Output

On a single line print a single number k, equal to the sum of lengths of the parts of the dryer which are inside Alexey's segment and are outside all other segments.

Sample test(s)
input
3
0 5
2 8
1 6
output
1
input
3
0 10
1 5
7 15
output
3
Note

Note that it's not important are clothes drying on the touching segments (e.g. (0, 1) and (1, 2)) considered to be touching or not because you need to find the length of segments.

In the first test sample Alexey may use the only segment (0, 1). In such case his clothes will not touch clothes on the segments (1, 6)and (2, 8). The length of segment (0, 1) is 1.

In the second test sample Alexey may dry his clothes on segments (0, 1) and (5, 7). Overall length of these segments is 3.

 

    题意:

    给出N(1 ~ 100),后给出 N 条线段,以第一条线段为标准,问不被以下 N - 1 条线段覆盖的长度为多少。

 

    思路:

    num 数组标记每段的覆盖次数,最后数第一段覆盖次数仅为 1 的有多少即可,注意区间要左开右闭。

 

    AC:

#include<stdio.h>
#include<string.h>
int num[105];
int main()
{
    int n,x,y;
    scanf("%d",&n);
    memset(num,0,sizeof(num));
    for(int i = 1;i <= n;i++)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        for(int j = l + 1;j <= r;j++)
            num[j]++;
        if(i == 1)
        {
            x = l;
            y = r;
        }
    }
    int sum = 0;
    for(int i = x + 1;i <= y;i++)
    {
        if(num[i] == 1) sum++;
    }
    printf("%d\n",sum);
    return 0;
}

 

 

优化这段代码,窗口1、3、5、9、11、17、33,输入3200个点,输出也要3200个点,边缘输出原值:#include "SmoothProfileOnXAxisMean.h" #define MAX_WINDOW_SIZE 33 #define HALF_MAX_WINDOW 16 #define II 1 // 每周期处理一个数据 // 使用18位定点数(16位整数 + 8位小数)替代浮点数 typedef ap_fixed<24, 12> fixed_t; typedef ap_fixed<32, 16> fixed_cache; void SmoothProfileOnXAxisMeanOptimize(hls::stream<float>& points_in_z, hls::stream<float>& smoothed_z, ap_uint<6> mean_win_size, float invalid_z ) { #pragma HLS PIPELINE II=1 #pragma HLS INTERFACE ap_ctrl_none port=return #pragma HLS INTERFACE axis port=points_in_z #pragma HLS INTERFACE axis port=smoothed_z #pragma HLS INTERFACE ap_none port=mean_win_size #pragma HLS INTERFACE ap_none port=invalid_z // 循环缓冲区 - 使用定点数提高时序性能 static fixed_t buffer[MAX_WINDOW_SIZE]; #pragma HLS ARRAY_PARTITION variable=buffer complete dim=1 static ap_uint<1> win[MAX_WINDOW_SIZE]; #pragma HLS ARRAY_PARTITION variable=win complete dim=1 static ap_uint<1> invalid_flag0; static ap_uint<1> invalid_flag1; static ap_uint<1> invalid_flag2; // 读取输入并转换为定点数 float in_val_float = points_in_z.read(); fixed_t in_val = in_val_float; fixed_t invalid_z_fixed = invalid_z; // std::cout << "out1_data:" << in_val << " " << invalid_z_fixed << " " << invalid_z <<std::endl; for (int i = MAX_WINDOW_SIZE-1; i > 0; i--){ #pragma HLS UNROLL buffer[i] = buffer[i - 1]; win[i] = win[i-1]; } buffer[0] = (in_val==invalid_z_fixed)?(fixed_t)0:in_val; win[0] = (in_val==invalid_z_fixed)?0:1; static fixed_cache sum_buffer0[11]; static fixed_cache sum_win0[11]; for (int i = 0; i < 11; i=i+1) { #pragma HLS UNROLL sum_buffer0[i] = buffer[i * 3] + buffer[i * 3+1] + buffer[i * 3+2]; sum_win0[i] = win[i * 3] + win[i * 3+1] + win[i * 3+2]; } invalid_flag0 = win[16]; static fixed_cache sum_buffer1[3]; sum_buffer1[0] = sum_buffer0[0] + sum_buffer0[1] + sum_buffer0[2] + sum_buffer0[3]; sum_buffer1[1] = sum_buffer0[4] + sum_buffer0[5] + sum_buffer0[6] + sum_buffer0[7]; sum_buffer1[2] = sum_buffer0[8] + sum_buffer0[9] + sum_buffer0[10]; static fixed_cache sum_win1[3]; sum_win1[0] = sum_win0[0] + sum_win0[1] + sum_win0[2] + sum_win0[3]; sum_win1[1] = sum_win0[4] + sum_win0[5] + sum_win0[6] + sum_win0[7]; sum_win1[2] = sum_win0[8] + sum_win0[9] + sum_win0[10]; invalid_flag1 = invalid_flag0; static fixed_cache sum_buffer2; sum_buffer2 = sum_buffer1[0] + sum_buffer1[1] + sum_buffer1[2]; static fixed_cache sum_win2; sum_win2 = sum_win1[0] + sum_win1[1] + sum_win1[2]; invalid_flag2 = invalid_flag1; // std::cout << "out2_data:" << in_val << " " << sum_buffer2 << " " << sum_win2 <<std::endl; // 除法优化 - 使用移位和乘法避免硬件除法器 fixed_t result; if (invalid_flag2 == 0) { result = invalid_z_fixed; } else { // 使用倒数乘法代替除法 ap_ufixed<24, 8> reciprocal = 1.0 / sum_win2.to_int(); result = fixed_t(sum_buffer2 * reciprocal); } // std::cout << "out2_data:" << in_val << " " << result << " " << sum_buffer2 << " " << sum_win2 << " " << std::endl; smoothed_z.write((float)result); }
最新发布
07-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值