二分+尺取

题意:https://vjudge.net/contest/336100#problem/B

HDU6231

  1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
  2 #include <cstdio>//sprintf islower isupper
  3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
  4 #include <iostream>//pair
  5 #include <fstream>//freopen("C:\\Users\\13606\\Desktop\\草稿.txt","r",stdin);
  6 #include <bitset>
  7 //#include <map>
  8 //#include<unordered_map>
  9 #include <vector>
 10 #include <stack>
 11 #include <set>
 12 #include <string.h>//strstr substr
 13 #include <string>
 14 #include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
 15 #include <cmath>
 16 #include <deque>
 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
 18 #include <vector>//emplace_back
 19 //#include <math.h>
 20 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
 21 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
 22 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
 23 //******************
 24 int abss(int a);
 25 int lowbit(int n);
 26 int Del_bit_1(int n);
 27 int maxx(int a,int b);
 28 int minn(int a,int b);
 29 double fabss(double a);
 30 void swapp(int &a,int &b);
 31 clock_t __STRAT,__END;
 32 double __TOTALTIME;
 33 void _MS(){__STRAT=clock();}
 34 void _ME(){__END=clock();__TOTALTIME=(double)(__END-__STRAT)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
 35 //***********************
 36 #define rint register int
 37 #define fo(a,b,c) for(rint a=b;a<=c;++a)
 38 #define fr(a,b,c) for(rint a=b;a>=c;--a)
 39 #define mem(a,b) memset(a,b,sizeof(a))
 40 #define pr printf
 41 #define sc scanf
 42 #define ls rt<<1
 43 #define rs rt<<1|1
 44 typedef long long ll;
 45 const double E=2.718281828;
 46 const double PI=acos(-1.0);
 47 //const ll INF=(1LL<<60);
 48 const int inf=(1<<30);
 49 const double ESP=1e-9;
 50 const int mod=(int)1e9+7;
 51 const int N=(int)1e6+10;
 52 
 53 int er[17]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536};
 54 int a[N],b[N];
 55 
 56 template<class _Ty = void>
 57 struct more:public binary_function<_Ty, _Ty, bool>
 58 {
 59     bool operator()(const _Ty& _Left, const _Ty& _Right) const
 60     {
 61         return (_Left > _Right);
 62     }
 63 };
 64 
 65 ll get(int x,int n,int k)
 66 {
 67     ll sum=0;
 68     int rk=0;
 69     int r=0;
 70     for(int i=1;i<=n;++i)
 71     {
 72         if(i!=1)
 73         {
 74             if(a[i-1]>=x)
 75                 rk--;
 76         }
 77         while(rk<k&&r+1<=n)
 78         {
 79             r++;
 80             if(a[r]>=x)rk++;
 81         }
 82         if(rk>=k)sum+=n-r+1;
 83     }
 84     return sum;
 85 }
 86 
 87 void solve()
 88 {
 89     int n,k;
 90     ll m;
 91     sc("%d%d%lld",&n,&k,&m);
 92     for(int i=1;i<=n;++i)
 93         sc("%d",&a[i]),b[i]=a[i];
 94     sort(b+1,b+1+n);
 95     int temp=0;
 96     for(int i=16;i>=0;--i)
 97     {
 98         if(temp+er[i]<=n&&get(b[temp+er[i]],n,k)>=m)
 99             temp+=er[i];
100     }
101     pr("%d\n",b[temp]);
102 }
103 
104 int main()
105 {
106     int T;
107     sc("%d",&T);
108     while(T--)solve();
109     return 0;
110 }
111 
112 /**************************************************************************************/
113 
114 int maxx(int a,int b)
115 {
116     return a>b?a:b;
117 }
118 
119 void swapp(int &a,int &b)
120 {
121     a^=b^=a^=b;
122 }
123 
124 int lowbit(int n)
125 {
126     return n&(-n);
127 }
128 
129 int Del_bit_1(int n)
130 {
131     return n&(n-1);
132 }
133 
134 int abss(int a)
135 {
136     return a>0?a:-a;
137 }
138 
139 double fabss(double a)
140 {
141     return a>0?a:-a;
142 }
143 
144 int minn(int a,int b)
145 {
146     return a<b?a:b;
147 }

猜你喜欢

转载自www.cnblogs.com/--HPY-7m/p/11706937.html