sgu 551. Preparing Problem 优先队列或数学方法?

本文介绍了一个竞赛编程问题,探讨了两名程序员如何合作完成至少n个不同的编程解决方案。通过使用优先队列来模拟工作流程,文章提供了一种高效算法,用于确定最终完成的解决方案总数及其完成时间。

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

551. Preparing Problem
Time limit per test: 1 second(s)
Memory limit: 262144 kilobytes
input: standard
output: standard



It is not easy to prepare a problem for a programming contest. Petya and Vasya decided that problem "A+B" needs at least n distinct solutions to be written. It doesn't matter how many solutions each of them will write, they need to write at least n solutions in total. We know that Petya needs t1 units of time to write a solution, and Vasya needs t2 units of time. They start to work simultaneously at time 0. Thus, for example, Petya finishes writing his first solution at time t1, his second solution at 2 · t1 and so on.

Petya and Vasya are working by the same algorithm. Each time Petya (Vasya) finishes writing a solution, he checks on how many solutions have already been written up to the current time moment t. Ready solutions are the solutions that have been fully written by this time. The solutions that were fully finished exactly at time t are also considered ready. If the number of such solutions is strictly less than n, then Petya (Vasya) starts writing the next solution. If a member of the jury began working on a problem, he doesn't stop working under any circumstances, and he will surely finish it.

Petya and Vasya realize that if they act on this algorithm, they will not necessarily write exactly n solutions in total. Maybe they'll write more solutions.

Considering that Petya and Vasya work non-stop, find, how many solutions they wrote in total and the moment when the latest solution was finished. The latest solution is one which was finished last.

Input
The only input line contains three integers nt1 and t2 (1 ≤ nt1t2 ≤ 5000).

Output
Print two integers — m and f, where m is the number of written solutions, and f is the moment when the last solution was finished.

Example(s)
sample input
sample output
5 2 3
5 6

sample input
sample output
5 2 4
6 8

sample input
sample output
3 30 50
4 100


模拟生产过程优先队列中取出用时少的,处理数据并决定是否再次加入队列。


#include <bits/stdc++.h>

using namespace std;

struct node{
    int t;
    int s;
};

bool operator<(node x,node y){
    return x.t>y.t;
}

priority_queue<node> q;

int a[2];
void solve(){
    int n;
    scanf("%d%d%d",&n,&a[0],&a[1]);
    int ans=0;
    int tt=0;
    node x;
    x.s=0;
    x.t=a[0];
    q.push(x);
    x.s=1;
    x.t=a[1];
    q.push(x);
    while(!q.empty()){
        node temp=q.top();
        q.pop();
        ans++;
        if(q.top().t==temp.t&&(ans+1)==n){
            ans=n;
            tt=temp.t;
            break;
        }
        tt=temp.t;
        if(ans<n){
            temp.t+=a[temp.s];
            q.push(temp);
        }
    }
    printf("%d %d",ans,tt);
}

int main(){
    solve();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值