codeforces Gym - 101485 A (2015-2016 Northwestern European Regional Contest (NWERC 2015))

题目描述:

点击打开链接

题意还是比较清晰的,有n个工人,每个工人有自己的到达工作站的时间和工作的时间,每个工人工作都需要一台机器,你可以认为工作站有足够多的机器,对于每台机器,需要操作人员来开启。如果一个工人用完就立即关闭,那么每来一个工人,操作员都需要开启一台机器,但是现在操作员想要偷懒,他让每个工人离开时不关闭机器,这样如果这时有工人来的话就不需要他来开启机器就可以直接使用,但是工作站的机器有一个限制,如果机器没有人使用的话,经过m分钟就会自动关闭,现问操作人员这样偷懒可以减少多少次操作次数。
这道题我问不少人用优先队列写的,但是我优先队列没写清白,我总想着如果一台机器如果有工人接着用的话,那么它使用的结束时间就需要更新,优先队列我就是不清楚怎样对应更新,一开始采取了vector数组的写法,但是这样排序就成了一个问题,最后就采取了multiset的写法,因为multiset可以自动排序还是十分好用的。
解题思路就是,我先按每个工人的到达工作站的时间排序,然后从第一个工人开始扫,如果在这个工人到达时的m分钟之前到现在之间有正好使用完毕的机器,那么就可以省下一次操作次数,如果找不到,那么就得新开一台机器,如果找到的机器的使用结束的时间要大于当前这个工人来的时间的话那也得开一台机器,就这样扫一遍就可以了。
AC代码:
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;
const int MAXM=300010;
const int bit=4;
const long long INF=0x3f3f3f3f3f3f3f3f;
const int maxm=60*30;
const long long MOD=998244353;
struct person
{
    int a,s,ed;
};

int n,m;
struct person p[MAXM];

bool cmp(person p1,person p2)
{
    return p1.a<p2.a;
}
int main()
{
    while( scanf("%d%d",&n,&m)!=EOF)
    {
    for (int i=0;i<n;i++)
        scanf("%d%d",&p[i].a,&p[i].s),p[i].ed=p[i].a+p[i].s;
    sort(p,p+n,cmp);
    multiset<int > st;
    multiset<int >::iterator it;
    st.clear();
    int ans=0;
    for (int i=0;i<n;i++)
    {
        int tmp=p[i].a-m;
        int ed=p[i].ed;
        if (st.size()==0)
        {
            st.insert(ed);
            ans++;
            continue;
        }
        it=st.lower_bound(tmp);
        if (it==st.end())
        {
            st.insert(ed);
            ans++;
        }
        else
        {
            if ((*it)>p[i].a)
            {
                st.insert(ed);
                ans++;
            }
            else
            {
                st.erase(it);
                st.insert(ed);
            }
        }
        //cout<<ans<<endl;
    }
    ans=n-ans;
    printf("%d\n",ans);
    }
    return 0;
}



### Codeforces Problem 976C Solution in Python For solving problem 976C on Codeforces using Python, efficiency becomes a critical factor due to strict time limits aimed at distinguishing between efficient and less efficient solutions[^1]. Given these constraints, it is advisable to focus on optimizing algorithms and choosing appropriate data structures. The provided code snippet offers insight into handling string manipulation problems efficiently by customizing comparison logic for sorting elements based on specific criteria[^2]. However, for addressing problem 976C specifically, which involves determining the winner ('A' or 'B') based on frequency counts within given inputs, one can adapt similar principles of optimization but tailored towards counting occurrences directly as shown below: ```python from collections import Counter def determine_winner(): for _ in range(int(input())): count_map = Counter(input().strip()) result = "A" if count_map['A'] > count_map['B'] else "B" print(result) determine_winner() ``` This approach leverages `Counter` from Python’s built-in `collections` module to quickly tally up instances of 'A' versus 'B'. By iterating over multiple test cases through a loop defined by user input, this method ensures that comparisons are made accurately while maintaining performance standards required under tight computational resources[^3]. To further enhance execution speed when working with Python, consider submitting codes via platforms like PyPy instead of traditional interpreters whenever possible since they offer better runtime efficiencies especially important during competitive programming contests where milliseconds matter significantly.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值