C++,Lutece 99 Fly Through

/*
Migrated from Lutece 99 Fly Through
Description
The home of flower fairies is being devastated by a monster called Littlefatcat. 
They have to leave the place where their generations lived with no other choices.
CC, the greatest investigator of flower fairies, 
found a paradise in the west and will lead all the flower fairies there. 
This paradise is full of flowers and safe from attack of Littlefatcat. 
However, there are lots of huge rocks on the way to the paradise.

Flower fairies are of different levels which are determined by their power. 
Fairies of higher level will fly higher. 
A fairy will persist in flying at the height corresponding to his or her level 
for honor and self-respect. Also, rocks on the way have specific height. 
When the route of a fairy hit a rock, the fairy will have to use magic 
to fly through the rock. 
Being aware of the height of all rocks and the specific height each fairy can fly at, 
CC want to know how many rocks each fairy will fly through.

Input
The first line will be N M, representing for the numbers of rocks and fairies.

The following line will give N numbers, giving the height of the rocks from the east to west.
Then M lines followed. On the i_th line, 
a number representing the specific height of fairy numbered i can fly at will be given.
All numbers are positive and not bigger than 100000.
Output
Please output the number of rocks each fairy has to fly through in order to get to the paradise in a single line.
*/

//高度相同的石头也是要用魔法穿过的
#include <iostream>
#include <vector>
#include <algorithm>
int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    const int MAX_HEIGHT = 100000;
    int numRocks,numFairies;std::cin>>numRocks>>numFairies;
    std::vector<int> ansRecorder(MAX_HEIGHT+1,0);
    //ansRecoder[i]标识高度为i的精灵需要穿过的石头数
    int highestRock =0,lowestRock = 100000;
    //首先记录每个高度的石头数
    for(int i = 0; i<numRocks; i++){
        int rockHeight;std::cin>>rockHeight;
        ++ansRecorder[rockHeight];
        highestRock = std::max(highestRock,rockHeight);
        lowestRock = std::min(lowestRock,rockHeight);
    }
    //从最高高度向下累加,使得ansRecorder[i]的值为所有高度大于等于i的石头数
    for(int i = highestRock; i>lowestRock; --i){
        ansRecorder[i-1]+=ansRecorder[i];
    }
    for(int i = 0; i<numFairies; ++i){
        int fairyHeight;std::cin>>fairyHeight;
        if(fairyHeight>highestRock){
            std::cout<<0<<std::endl;
        }else if(fairyHeight<=lowestRock){
            std::cout<<numRocks<<std::endl;
        }else{
            std::cout<<ansRecorder[fairyHeight]<<std::endl;
        }
    }
    return 0;
}
posted @ 2025-03-25 15:10  Kazuma_124  阅读(35)  评论(0)    收藏  举报