Codeforces1061D TV Shows

本文介绍了一种解决电视节目观看最小花费问题的算法,通过线段求交、STL和二分搜索技术,实现了对电视节目开始和结束时间的有效管理,以最小化观看所有节目的成本。

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

标签:线段求交,STL,二分

题目

[题目传送门](http://codeforces.com/contest/1061/problem/D)

题意

给出要看的 n n n个电视节目,和每个节目开始结束时间分别为 l i l_i li r i r_i ri,每次申请一个新的电视的花费为 X + ( r i − l i ) × Y X+(r_i-l_i)\times Y X+(rili)×Y,如果两个节目有交集,就必须用两个电视看,现在询问看完所有电视节目的最小花费

分析

先排序,后二分

二分判断是重新开一个电视划算,还是延续上一个划算

code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define mem(x,num) memset(x,num,sizeof x)
#define reg(x) for(int i=last[x];i;i=e[i].next)
using namespace std;
inline ll read(){
    ll f=1,x=0;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
//******head by yjjr******
const int maxn=1e5+6;
const int mod=1e9+7;
struct node{ll a,b;}c[maxn];
inline bool operator < (node p,node q){return p.a==q.a?p.b<q.b:p.a<q.a;}
set<ll>st;
map<ll,int>mp; 
int n;
ll X,Y,ans=0;
int main(){
    n=read(),X=read(),Y=read();
    rep(i,1,n)c[i]=(node){read(),read()};
    sort(c+1,c+1+n);
    st.insert(c[1].b);
    mp[c[1].b]++;
    ans=(ans+X+(c[1].b-c[1].a)*Y%mod)%mod;
    set<ll>::iterator it;
    rep(i,2,n){
        it=st.lower_bound(c[i].a);
        if(it==st.begin()){
            ans=(ans+X+(c[i].b-c[i].a)*Y%mod)%mod;
            mp[c[i].b]++;
        }else{
            it--;
            ll value=*it;
            if(c[i].a>value&&(c[i].a-value)*Y<=X){
                mp[value]--;
                if(mp[value]==0)st.erase(value);
                ans=(ans+(c[i].b-value)*Y%mod)%mod;
                mp[c[i].b]++;
            }else{
                ans=(ans+X+(c[i].b-c[i].a)*Y%mod)%mod;
                mp[c[i].b]++;
            }
        }
        st.insert(c[i].b);
    }
    cout<<ans<<endl;
    return 0;
}
### Codeforces 1487D Problem Solution The problem described involves determining the maximum amount of a product that can be created from given quantities of ingredients under an idealized production process. For this specific case on Codeforces with problem number 1487D, while direct details about this exact question are not provided here, similar problems often involve resource allocation or limiting reagent type calculations. For instance, when faced with such constraints-based questions where multiple resources contribute to producing one unit of output but at different ratios, finding the bottleneck becomes crucial. In another context related to crafting items using various materials, it was determined that the formula `min(a[0],a[1],a[2]/2,a[3]/7,a[4]/4)` could represent how these limits interact[^1]. However, applying this directly without knowing specifics like what each array element represents in relation to the actual requirements for creating "philosophical stones" as mentioned would require adjustments based upon the precise conditions outlined within 1487D itself. To solve or discuss solutions effectively regarding Codeforces' challenge numbered 1487D: - Carefully read through all aspects presented by the contest organizers. - Identify which ingredient or component acts as the primary constraint towards achieving full capacity utilization. - Implement logic reflecting those relationships accurately; typically involving loops, conditionals, and possibly dynamic programming depending on complexity level required beyond simple minimum value determination across adjusted inputs. ```cpp #include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for(int i=0;i<n;++i){ cin>>a[i]; } // Assuming indices correspond appropriately per problem statement's ratio requirement cout << min({a[0], a[1], a[2]/2LL, a[3]/7LL, a[4]/4LL}) << endl; } ``` --related questions-- 1. How does identifying bottlenecks help optimize algorithms solving constrained optimization problems? 2. What strategies should contestants adopt when translating mathematical formulas into code during competitive coding events? 3. Can you explain why understanding input-output relations is critical before implementing any algorithmic approach? 4. In what ways do prefix-suffix-middle frameworks enhance model training efficiency outside of just tokenization improvements? 5. Why might adjusting sample proportions specifically benefit models designed for tasks requiring both strong linguistic comprehension alongside logical reasoning skills?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值