uva 11649 - Home! Sweet Home!(贪心+优先队列)

D

Home! Sweet Home!

Input: Standard Input

Output: Standard Output

 

 

Own home is the sweetest place if it is really secured. People build wall around home to make their home more secured. Some pillars are required to make more strong wall. There is a shop named “Home! Sweet Home!”, which sells pillar specially made for building walls. This is really a busy shop. Everyday lot of contractors come, who are responsible for building walls, having a list of homes in their hands. Their list usually contains information of homes specially height of the home and number of pillars required to build that home.

They find pillars are arranged in a row in that shop. Every pillars in row are tagged with heights. Every contractors want to buy some pillars such that they can make walls for all those homes in their list. But unfortunately the shop has some limitation of pillars. So the owner of that shop decides to hire you to write a program for them, which can calculate the number of maximum possible home, for which the contractor can build wall using the pillars in that shop.

 

Note that, a pillar having less height than a home cannot be used to build wall in that home for security purpose.

 

Input

First line of input will contain a number T(1<=T<=200), which indicates the number of test cases. Each test case starts with a line having two integers NP and NH(1<=NP<=100000, 1<=NH<=100000), number of pillars in “Home! Sweet Home!” and number of homes in contractor's list. The next line will contain three integers A, B and C (1 <= A, B, C <= 10000). You can calculate height of the pillars of the shop by this way -

 

PH= C % 10000 + 1

PH2 = (A * PH1 + C) % 10000 + 1

PHi = (A * PHi-1 + B * PHi-2 + C) % 10000 + 1, where 3<=i<=NP.

 

Here PHi represents the height of ith pillar in the row of the shop.

 

The next line will contain six integers E, F, G, H, I and J. (1 <= E, F, G, H, I, J <= 10000). You can generate the height of the homes and number of pillars required for corresponding homes in the list by this way –

 

HH1 = G % 10000 + 1

PR1 = J % 100000 + 1

 

HHi = (E * HHi-1 + F * PRi-1 + G) % 10000 + 1, where 2<=i<=NH

PRi = (H * PRi-1 + I * HHi-1 + J) % 100000 + 1, where 2<=i<=NH

 

Here HHi represents height of ith home and PRi represents number of pillars required to build wall for ith home.

 

Output

For each test case, output a single line of the form “Case X: N”, where X denotes the case number and N denotes the maximum number home for which it is possible to make a wall.

                

Sample Input                              Output for Sample Input

1

10 4

2 10 19

1 2 1 1 1 1

Case 1: 2

 

Problem setter: Md. Arifuzzaman Arif , Special Thanks: Jane Alam Jan

 

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;

struct Home{
    int h , r;
    Home(int a = 0 , int b = 0){
        h = a , r = b;
    }
};
vector<Home> home;
vector<int> pillar;
int A , B , C , E , F , G , H , I , J , NP , NH;

bool cmp1(int a , int b){ return a<b;}

bool cmp2(Home h1 , Home h2){
    return h1.h<h2.h;
}

struct cmp{
    bool operator()(Home h1 , Home h2){
        return h1.r>h2.r;
    }
};

void initial(){
    home.clear();
    pillar.clear();
}

void readcase(){
    scanf("%d%d" , &NP , &NH);
    scanf("%d%d%d%d%d%d%d%d%d" , &A , &B , &C , &E , &F , &G , &H , &I , &J);
    pillar.push_back(C % 10000 + 1);
    if(NP >= 2) pillar.push_back((A * pillar[0] + C) % 10000 + 1);
    for(int i = 2; i < NP; i++){
        pillar.push_back((A * pillar[i-1] + B * pillar[i-2] + C) % 10000 + 1);
    }
    home.push_back(Home(G % 10000 + 1 , J % 100000 + 1));
    for(int i = 1; i < NH; i++){
        home.push_back(Home((E * home[i-1].h + F * home[i-1].r + G) % 10000 + 1 , (H * home[i-1].r + I * home[i-1].h + J) % 100000 + 1));
    }
    sort(pillar.begin() , pillar.end() , cmp1);
    sort(home.begin() , home.end() , cmp2);
}

void computing(){
    priority_queue<Home , vector<Home> , cmp> q;
    int h_index = 0 , p_index = 0 , ans = 0;
    while(p_index < NP){
        while(h_index < NH && home[h_index].h <= pillar[p_index]){
        	if(home[h_index].r <= NP-p_index) q.push(home[h_index]);
        	h_index++;
        }
        if(!q.empty()){
            Home thome = q.top();
            q.pop();
            thome.r--;
            if(thome.r <= 0){
            	ans++;
            }
            else q.push(thome);
        }
        p_index++;
    }
    printf("%d\n" , ans);
}

int main(){
    int T;
    scanf("%d" , &T);
    for(int i = 1; i <= T; i++){
        initial();
        readcase();
        printf("Case %d: " , i);
        computing();
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!
提供的源码资源涵盖了python应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值