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

原创 2014年03月28日 22:57:30

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;
}


蓝桥杯 算法提高 排队打水问题(贪心排序+优先队列)

算法提高 排队打水问题   时间限制:1.0s   内存限制:256.0MB      问题描述   有n个人排队到r个水龙头去打水,他们装满水桶的时间t1、t2………...
  • Sterben_Da
  • Sterben_Da
  • 2016年01月22日 15:50
  • 2269

POJ1456Supermarket(贪心+优先队列)

Supermarket Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9103   Ac...
  • u010372095
  • u010372095
  • 2014年08月11日 22:47
  • 1235

UVA 1422 - Processor(二分+优先队列+贪心)

题目链接:点击打开链接 题意:有n个任务, 每个任务必须在[r,d]时间段内完成, 任务量为w, 求最小执行速度, 使得可以在规定时间内完成所有任务。 思路:二分通常要和贪心搭配, 显然, d越小...
  • weizhuwyzc000
  • weizhuwyzc000
  • 2016年02月25日 12:37
  • 1061

nyoj 1100 WAJUEJI which home strong!( BFS+优先队列)

在一个山沟里,姐弟俩同时考上了大学。但由于家里拮据,所以这并不是什么好消息。父亲对孩子说:我就是砸锅卖铁也要把你们姐俩供出来。 当时的姐姐已经决定放弃上学的机会。 没想到第二天天还没亮,弟弟就偷偷带著...
  • qqchanmingdexiaji
  • qqchanmingdexiaji
  • 2017年05月13日 11:35
  • 95

NYOJ - 1100 - WAJUEJI which home strong!(BFS变形,优先队列)

输入 第一个数T,T组测试数据。 两个数 n, m; ( 0 接下来n行每行m 个字符。 ‘s’ 表示弟弟目前所在位置。 ‘# ’表示此处为一座山。为了节省体力,不从此处通行。 从‘A’-...
  • qq_34594236
  • qq_34594236
  • 2016年10月16日 10:36
  • 191

NYOJ 1100 WAJUEJI which home strong!(BFS+优先队列)

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=1100 解题思路:广搜+优先队列 自定义优先级用优先队列存最小花费,更新的时候是加s[...
  • Jane_JXR
  • Jane_JXR
  • 2016年11月06日 14:15
  • 187

uva10954Add All(贪心+优先队列)

题目链接:http://vjudge.net/contest/view.action?cid=50494#problem/D Add All Input: standard input Ou...
  • u013790563
  • u013790563
  • 2014年07月22日 16:03
  • 453

(advanced) UVA 优先队列+贪心 1316 Supermarket

A supermarket has a set Prod of products on sale. It earns a profit px for each product x in Prod so...
  • wsx1754175
  • wsx1754175
  • 2014年04月10日 15:11
  • 430

Uva-7423-Assigning Workstations(贪心+优先队列)

链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&pr...
  • qq978874169
  • qq978874169
  • 2017年04月04日 14:18
  • 239

UVa 757 / POJ 1042 / East Central North America 1999 Gone Fishing (枚举&贪心&想法题&优先队列)

757 - Gone Fishing Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinej...
  • synapse7
  • synapse7
  • 2013年09月27日 08:17
  • 1081
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:uva 11649 - Home! Sweet Home!(贪心+优先队列)
举报原因:
原因补充:

(最多只允许输入30个字)