【二分】Codeforces 706B Interesting drink

题目链接:

  http://codeforces.com/problemset/problem/706/B

题目大意

  n (1 ≤ n ≤ 100 000)个商店卖一个东西,每个商店的价格Ai,你有m(1≤m≤100 000)天,每天有Cj的钱,问每天可以负的起的商店数。

题目思路:

  【二分】

  排个序,二分。

 

 1 //
 2 //by coolxxx
 3 //
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<string>
 7 #include<iomanip>
 8 #include<memory.h>
 9 #include<time.h>
10 #include<stdio.h>
11 #include<stdlib.h>
12 #include<string.h>
13 //#include<stdbool.h>
14 #include<math.h>
15 #define min(a,b) ((a)<(b)?(a):(b))
16 #define max(a,b) ((a)>(b)?(a):(b))
17 #define abs(a) ((a)>0?(a):(-(a)))
18 #define lowbit(a) (a&(-a))
19 #define sqr(a) ((a)*(a))
20 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
21 #define eps (1e-8)
22 #define J 10000000
23 #define MAX 0x7f7f7f7f
24 #define PI 3.1415926535897
25 #define N 100004
26 using namespace std;
27 typedef long long LL;
28 int cas,cass;
29 int n,m,lll,ans;
30 int a[N];
31 bool cmp(int aa,int bb)
32 {
33     return aa<bb;
34 }
35 int work(int v)
36 {
37     int l=0,r=n,mid;
38     while(l<r)
39     {
40         mid=(l+r+1)>>1;
41         if(v>=a[mid])l=mid;
42         else r=mid-1;
43     }
44     return r;
45 }
46 int main()
47 {
48     #ifndef ONLINE_JUDGE
49 //    freopen("1.txt","r",stdin);
50 //    freopen("2.txt","w",stdout);
51     #endif
52     int i,j,x,v;
53 //    for(scanf("%d",&cas);cas;cas--)
54 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
55 //    while(~scanf("%s",s))
56     while(~scanf("%d",&n))
57     {
58         for(i=1,x=0;i<=n;i++)
59         {
60             scanf("%d",&a[i]);
61             x=max(a[i],x);
62         }
63         sort(a+1,a+1+n,cmp);
64         scanf("%d",&m);
65         for(i=1;i<=m;i++)
66         {
67             scanf("%d",&v);
68             if(v>=x)printf("%d\n",n);
69             else printf("%d\n",work(v));
70         }
71     }
72     return 0;
73 }
74 /*
75 //
76     
77 //
78 */
View Code

 

posted @ 2016-08-13 21:40  Cool639zhu  阅读(246)  评论(0编辑  收藏  举报