URAL 1105 Observers Coloring (构造法)

解决一个数学竞赛问题,通过构造法确定考场内特定时间段仅有单一染色监考员的最长时间,确保该时间不短于考生答题时间的三分之二。

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

大体题意:

你在一个考场里做题,做题时间是t0~t1,有n (n <= 10000)个监考员,告诉你每一个监考员的入场时间和出场时间,现在你可以给一定量的监考员进行染色,使得考场内只有一个且染色监考员的总时间不小于 你做题时间的2/3,如果可行输出你染色的监考员,如果不可行输出0!  输入保证在你做题时间至少有一个监考员!

思路:

构造法!

知道了每个监考员的入场时间和出场时间,我们可以把它看成一个区间!  最后要求你保留一些区间,使得这个区间只有一个线段的数值总和不小于考试时间 的2/3.

我们把 去掉后没有空白的区间给删掉!这样最后给剩下的区间按照左端点进行排序,那么第i 个区间和第i+1个区间一定相交,并且第i 个区间一定不和第i+2个区间相交,否则第i+2个区间或者第i+1个区间可以被删掉!

这样我们选出所有奇数号的区间,这些区间一定都不相交,加起来如果大于2/3  输出!

如果上述情况不合适   那么我们在加偶数号区间,这些区间同样也都不想交 ,加起来如果大于2/3  输出!

如果还是不大于2/3

那么我们来看看全选的时间!

令a =  奇数号的区间时间总和!

令b = 偶数号区间的时间总和!

令T 为你的做题时间!

如果全选,那么剩下的“单独时间”  的总和为   T-a  + T - b(这个式子画个图就很明了了!)

现在a < 2*T/3    b  < 2*T/3

那么单独时间  为2*T - (a+b) >= 2*T/3

所以说 如果上述奇偶都不合理的情况下   全选一定有解! 所以此题一定有解!

因为是浮点数  注意好精度控制就好了!

贴个图 帮助大家理解:


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
const double eps = 1e-10;
const double inf = 1e18;
const int maxn = 10000 + 7;
double t0,t1;
double dcmp(double a,double b){ // 比较浮点数大小
    if (fabs(a-b) < eps) return 0;
    if (a > b) return 1;
    return -1;
}
struct Node{
    double l,r;
    int id;
    void read() { scanf("%lf %lf",&l, &r);}
    bool operator < (const Node& rhs) const {
        return dcmp(l,rhs.l) == -1 || (dcmp(l,rhs.l) == 0 && dcmp(r,rhs.r) == 1);
    }
}p[maxn];
vector<int>ans;
int main(){
    while(~scanf("%lf %lf",&t0, &t1)){
        int n;
        ans.clear();
        double T = 2.0*(t1-t0)/3.0; // 总时间值!
        scanf("%d",&n);
        for (int i = 0; i < n; ++i){
            p[i].read();
            p[i].id = i+1;
        }
        sort(p,p+n);
        int len = 0;
        for (int i = 0; i < n; ++i){
            bool ok = 0;
            if (len == 0){
                ans.push_back(i);
                ok = 1;
                len++;
            }
            else if (len == 1){
                int id = ans[0];
                if (dcmp(p[i].l,p[id].l) == 1 && dcmp(p[i].r,p[id].r) == 1) ans.push_back(i),ok = 1,++len;
            }
            else {
                int laa = ans[len-2];
                int la = ans[len-1];
                if (dcmp(p[laa].r,p[i].l) == -1 && dcmp(p[i].r,p[la].r) == 1){
                    ans.push_back(i);
                    ++len;
                    ok = 1;
                }
                else if (dcmp(p[laa].r,p[i].l) != -1 && dcmp(p[i].r,p[la].r) != -1){
                    ans.pop_back();
                    ans.push_back(i);
                    ok = 1;
                }

            }
            if (!ok) continue;
            int cur = i;
            while(i+1 < n && dcmp(p[i+1].l,p[cur].l) == 0)++i;
        }
        double sum1 = 0,sum2 = 0; //  分奇数偶数的形式来讨论!
        int cnt1=0,cnt2=0;
        for (int i = 0; i < ans.size(); i+=2){
            int id = ans[i];
            sum1 += p[id].r - p[id].l;
            ++cnt1;
        }
        if (dcmp(sum1,T) != -1) {
            printf("%d\n",cnt1);
            for (int i = 0; i < ans.size(); i+=2){
                int id = ans[i];
                printf("%d\n",p[id].id);
            }
        }
        else {
            for (int i = 1; i < ans.size(); i+=2){
                int id = ans[i];
                sum2 += p[id].r - p[id].l;
                ++cnt2;
            }
            if (dcmp(sum2,T) != -1) {
                printf("%d\n",cnt2);
                for (int i = 1; i < ans.size(); i+=2){
                    int id = ans[i];
                    printf("%d\n",p[id].id);
                }
            }
            else {
                printf("%d\n",ans.size());
                for (int i = 0; i < ans.size(); ++i){
                    printf("%d\n",p[ans[i]].id);
                }
            }
        }
    }
    return 0;
}
/**
0 6
3
0 3
2 5 4 6
**/

1105. Observers Coloring

Time limit: 0.5 second
Memory limit: 64 MB
Nikifor told us that once he solved problem at mathematical tournament of S.Petersburg's secondary school N239 in 1994. Nikifor said that he solved a problem from the moment  T 0 to the moment  T 1. He remembers that  N observers appeared in the room. The  i-th observer entered the room at the moment t 0, i and went out at the moment  t 1, i. At every moment there was at least one observer in the room.
When the tournament was finished, Nikifor claimed that it is possible to color some observers, and the summary time when there was only one colored observer in the room is not less than 2/3 of the time when Nikifor solved problem.
You are to answer whether Nikifor right or not.

Input

The first line of input contains real numbers  T 0 and  T 1 ( T 0 <  T 1). The second line contains number N — number of observers ( N < 10000). Next  N lines contain real numbers  t 0, i and  t 1, i ( T 0 ≤ t 0, i <  t 1, i ≤  T 1).

Output

If Nikifor is not right output should contain the only number 0. If Nikifor is right you should write to the first line the quantity of colored observers, and next lines should contain their numbers. Do not write more than one number in a line. You may write these numbers in any order. If there are more than one solution exist you may find any of them.

Sample

input output
0.0 20.0
7
1.0 1.5
0.0 10.0
9.0 10.0
18.0 20.0
9.0 18.0
2.72 3.14
19.0 20.0
3
2
5
7
Problem Author: Dmitry Filimonenkov feat Igor Goldberg
Problem Source: Tetrahedron Team Contest May 2001


资源下载链接为: https://pan.quark.cn/s/1bfadf00ae14 “STC单片机电压测量”是一个以STC系列单片机为基础的电压检测应用案例,它涵盖了硬件电路设计、软件编程以及数据处理等核心知识点。STC单片机凭借其低功耗、高性价比和丰富的I/O接口,在电子工程领域得到了广泛应用。 STC是Specialized Technology Corporation的缩写,该公司的单片机基于8051内核,具备内部振荡器、高速运算能力、ISP(在系统编程)和IAP(在应用编程)功能,非常适合用于各种嵌入式控制系统。 在源代码方面,“浅雪”风格的代码通常简洁易懂,非常适合初学者学习。其中,“main.c”文件是程序的入口,包含了电压测量的核心逻辑;“STARTUP.A51”是启动代码,负责初始化单片机的硬件环境;“电压测量_uvopt.bak”和“电压测量_uvproj.bak”可能是Keil编译器的配置文件备份,用于设置编译选项和项目配置。 对于3S锂电池电压测量,3S锂电池由三节锂离子电池串联而成,标称电压为11.1V。测量时需要考虑电池的串联特性,通过分压电路将高电压转换为单片机可接受的范围,并实时监控,防止过充或过放,以确保电池的安全和寿命。 在电压测量电路设计中,“电压测量.lnp”文件可能包含电路布局信息,而“.hex”文件是编译后的机器码,用于烧录到单片机中。电路中通常会使用ADC(模拟数字转换器)将模拟电压信号转换为数字信号供单片机处理。 在软件编程方面,“StringData.h”文件可能包含程序中使用的字符串常量和数据结构定义。处理电压数据时,可能涉及浮点数运算,需要了解STC单片机对浮点数的支持情况,以及如何高效地存储和显示电压值。 用户界面方面,“电压测量.uvgui.kidd”可能是用户界面的配置文件,用于显示测量结果。在嵌入式系统中,用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值