/*
\\ //
\\ //
\\ //
##DDDDDDDDDDDDDDDDDDDDDD##
## DDDDDDDDDDDDDDDDDDDD ##
## hh /\ ***** hh ##
## hh //\\ ** hh ##
## hh //__\\ ** hh ##
## hh// \\ ***** hh ##
## hh wwww hh ##
## hh hh ##
## MMMMMMMMMMMMMMMMMMMM ##
##MMMMMMMMMMMMMMMMMMMMMM##
\/ \/
*/
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<math.h>
#include<map>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define fora(i,a,b) for (int i = (a); i < (b); i++)
#define fors(i,a,b,s) for (int i = (a); i < (b); i=i+(s))
int a[100005],b[100005];
// 你好,我叫赵师哥
int main(){
int n,t,m;
cin>>t;
while(t--){
cin>>n;
memset(b,0,sizeof(b));
for(int i=0;i<n;i++){
cin>>a[i];
}
int ans=0;
sort(a,a+n);
for(int i=0,j=n-1;i<j;i++,j--){
b[ans]=a[i];
ans++;
b[ans]=a[j];
ans++;
}
if(n%2){
b[ans]=a[n/2]; //憨批这里从0开始!!!!
}
for(int i=n-1;i>=0;i--){
cout<<b[i]<<" ";
}
cout<<endl;
}
return 0;
}
/*
\\ //
\\ //
\\ //
##DDDDDDDDDDDDDDDDDDDDDD##
## DDDDDDDDDDDDDDDDDDDD ##
## hh /\ ***** hh ##
## hh //\\ ** hh ##
## hh //__\\ ** hh ##
## hh// \\ ***** hh ##
## hh wwww hh ##
## hh hh ##
## MMMMMMMMMMMMMMMMMMMM ##
##MMMMMMMMMMMMMMMMMMMMMM##
\/ \/
*/
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<math.h>
#include<map>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define fora(i,a,b) for (int i = (a); i < (b); i++)
#define fors(i,a,b,s) for (int i = (a); i < (b); i=i+(s))
// 你好,我叫赵师哥
ll a[100005];
ll sum[50];
int main()
{
sum[0]=1;
for(ll j=1;j<=40;j++)
{
sum[j]=(1LL<<j)+sum[j-1];
}
int t;
scanf("%d",&t);
while(t--)
{
ll maxx=0-1e10;
ll ans=0;
int n;
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
scanf("%lld",&a[i]);
maxx=max(maxx,a[i]);
if(a[i]<maxx)
{
for(ll j=0; j<=40; j++)
{
//cout<<((1LL)<<j)+a[i]<<endl;
if(sum[j]+a[i]>=maxx)
{
ans=max(ans,j+1);
break;
}
}
}
}
printf("%lld\n",ans);
}
return 0;
}
/*
\\ //
\\ //
\\ //
##DDDDDDDDDDDDDDDDDDDDDD##
## DDDDDDDDDDDDDDDDDDDD ##
## hh /\ ***** hh ##
## hh //\\ ** hh ##
## hh //__\\ ** hh ##
## hh// \\ ***** hh ##
## hh wwww hh ##
## hh hh ##
## MMMMMMMMMMMMMMMMMMMM ##
##MMMMMMMMMMMMMMMMMMMMMM##
\/ \/
*/
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<math.h>
#include<map>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define fora(i,a,b) for (int i = (a); i < (b); i++)
#define fors(i,a,b,s) for (int i = (a); i < (b); i=i+(s))
// 你好,我叫赵师哥
int a[100005];
int main(){
int n,m,t,x;
cin>>t;
while(t--){
cin>>n>>x;
fora(i,0,n){
cin>>a[i];
}
int l=n,r;
long long sum=0;
int ans=0;
sort(a,a+n);
for(int i=n-1;i>=0;i--){
if(a[i]-x>=0){
sum+=(a[i]-x);
ans++;
}
else{
break;
}
}
if(ans==0){
cout<<0<<endl;
continue;
}
for(int i=n-ans;i>=0;i--){
if(x-a[i]>0){
if(sum>=x-a[i]){
sum-=(x-a[i]);
ans++;
}
else
break;
}
}
cout<<ans<<endl;
}
return 0;
}
/*
\\ //
\\ //
\\ //
##DDDDDDDDDDDDDDDDDDDDDD##
## DDDDDDDDDDDDDDDDDDDD ##
## hh /\ ***** hh ##
## hh //\\ ** hh ##
## hh //__\\ ** hh ##
## hh// \\ ***** hh ##
## hh wwww hh ##
## hh hh ##
## MMMMMMMMMMMMMMMMMMMM ##
##MMMMMMMMMMMMMMMMMMMMMM##
\/ \/
*/
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<math.h>
#include<map>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define fora(i,a,b) for (int i = (a); i < (b); i++)
#define fors(i,a,b,s) for (int i = (a); i < (b); i=i+(s))
// 你好,我叫赵师哥
ll a[300001], b[300001], c[300001];
int main (){
int T;
scanf("%d", &T);
while(T--){
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%lld%lld", &a[i], &b[i]);
for (int i = 2; i <= n; i++)
c[i] = max(0ll, a[i] - b[i - 1]);
c[1] = max(0ll, a[1] - b[n]);
ll sum = 0, Min = 9e18;
for (int i = 1; i <= n; i++)
sum += c[i];
for (int i = 1; i <= n; i++)
Min = min(Min, sum - c[i] + a[i]);
printf("%lld\n", Min);
}
return 0;
}
/*
\\ //
\\ //
\\ //
##DDDDDDDDDDDDDDDDDDDDDD##
## DDDDDDDDDDDDDDDDDDDD ##
## hh /\ ***** hh ##
## hh //\\ ** hh ##
## hh //__\\ ** hh ##
## hh// \\ ***** hh ##
## hh wwww hh ##
## hh hh ##
## MMMMMMMMMMMMMMMMMMMM ##
##MMMMMMMMMMMMMMMMMMMMMM##
\/ \/
*/
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<math.h>
#include<map>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define fora(i,a,b) for (int i = (a); i < (b); i++)
#define fors(i,a,b,s) for (int i = (a); i < (b); i=i+(s))
// 你好,我叫赵师哥
int t, n, a[100010], b[100010], cnt[2];
int main(){
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=1;i<=n;++i){
scanf("%d",a+i);
if(i<n){
if(a[i]==1)
cnt[1]++;
if(a[i]==-1)
cnt[0]++;
}
}
for(int i=1;i<=n;++i)
scanf("%d",b+i);
bool flag=1;
for(int i=n; i>=1;--i){
if(a[i]==b[i]){
if(a[i-1]==1)
cnt[1]--;
if(a[i - 1] == -1)
cnt[0]--;
}
if(a[i] > b[i]){
if(!cnt[0]){
flag = 0;
break;
}
if(a[i-1]==1)
cnt[1]--;
if(a[i-1]==-1)
cnt[0]--;
}
else if(a[i]<b[i]){
if(!cnt[1]){
flag=0;
break;
}
if(a[i-1]==1)
cnt[1]--;
if(a[i-1]==-1)
cnt[0]--;
}
}
if(!flag)
puts("NO");
else
puts("YES");
memset(cnt,0,sizeof(cnt));
}
return 0;
}
原文:https://www.cnblogs.com/pioneerjiesen/p/12989566.html