二分 【离散化】

L - 二分

Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit

Status
Description
Vasiliy likes to rest after a hard work, so you may often meet him in some bar nearby. As all programmers do, he loves the famous drink “Beecola”, which can be bought in n different shops in the city. It’s known that the price of one bottle in the shop i is equal to xi coins.

Vasiliy plans to buy his favorite drink for q consecutive days. He knows, that on the i-th day he will be able to spent mi coins. Now, for each of the days he want to know in how many different shops he can buy a bottle of “Beecola”.

Input
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of shops in the city that sell Vasiliy’s favourite drink.

The second line contains n integers xi (1 ≤ xi ≤ 100 000) — prices of the bottles of the drink in the i-th shop.

The third line contains a single integer q (1 ≤ q ≤ 100 000) — the number of days Vasiliy plans to buy the drink.

Then follow q lines each containing one integer mi (1 ≤ mi ≤ 109) — the number of coins Vasiliy can spent on the i-th day.

Output
Print q integers. The i-th of them should be equal to the number of shops where Vasiliy will be able to buy a bottle of the drink on the i-th day.

Sample Input
Input
5
3 10 8 6 11
4
1
10
3
11

Output
0
4
1
5

Hint

On the first day, Vasiliy won’t be able to buy a drink in any of the shops.

On the second day, Vasiliy can buy a drink in the shops 1, 2, 3 and 4.

On the third day, Vasiliy can buy a drink only in the shop number 1.

Finally, on the last day Vasiliy can buy a drink in any shop.


  1. 题意:一个人每一天的零花钱不同,每个商店的酒价也不一样,给定所有商店酒价,给出每一天的零花钱,问这一天他能到多少商店买酒;
  2. 思路:把所有酒价排序,找出第一个比零花钱多的位置,那么前面的酒都可以买;
  3. 失误:不知道哪里用到离散化了???
  4. 代码如下:

#include<cstdio>
#include<algorithm>
using namespace std;

int a[100000+33];

int main()
{
    int N,M,i,Q;
    while(~scanf("%d",&N))
    {
        for(i=1;i<=N;++i)
        {
            scanf("%d",&a[i]);
        }
        sort(a+1,a+N+1);
        scanf("%d",&Q);
        while(Q--)
        {
            scanf("%d",&M);
            int ans=upper_bound(a+1,a+N+1,M)-a;//找到第一个比他的钱多的 那么前面的都能买 
            printf("%d\n",ans-1); 
         } 
    }
    return 0;
 } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
采样数据离散化是将连续数据离散化离散数值或类别的过程。离散化的目的是将连续的数据转化为离散的类别,便于分类、聚类和统计分析等处理。 下面介绍两种常见的离散化方法:等宽离散化和等频离散化。 1. 等宽离散化 等宽离散化是指将连续的数值划分为若干个等宽的区间,然后将每个区间用一个整数或字符串标识。等宽离散化的方法如下: ``` python import pandas as pd # 读取Excel文件 data = pd.read_excel('your_file_path.xlsx') # 提取第一列第二行之后的数据 samples = data.iloc[1:, 0].tolist() # 将采样数据等宽离散化为10个类别 labels = pd.cut(samples, bins=10, labels=False) # 打印离散化后的结果 print(labels) ``` 上述代码中,使用pd.cut()函数对采样数据进行等宽离散化,将采样数据分为10个区间,每个区间的间隔相等。函数的第一个参数是要离散化的数据,第二个参数bins是划分区间的个数,第三个参数labels=False表示返回的结果用整数表示。 2. 等频离散化 等频离散化是指将连续的数值划分为若干个区间,使每个区间中的数据个数相同,然后将每个区间用一个整数或字符串标识。等频离散化的方法如下: ``` python import pandas as pd # 读取Excel文件 data = pd.read_excel('your_file_path.xlsx') # 提取第一列第二行之后的数据 samples = data.iloc[1:, 0].tolist() # 将采样数据等频离散化为10个类别 labels = pd.qcut(samples, q=10, labels=False) # 打印离散化后的结果 print(labels) ``` 上述代码中,使用pd.qcut()函数对采样数据进行等频离散化,将采样数据分为10个区间,每个区间中的数据个数相同。函数的第一个参数是要离散化的数据,第二个参数q是划分区间的个数,第三个参数labels=False表示返回的结果用整数表示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值