A:
冒泡排序,最坏情况下严格递减,需要n*(n-1)/2次
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define re register
#define ls (o<<1)
#define rs (o<<1|1)
#define pb push_back
const double PI= acos(-1.0);
const int M = 1e5+7;
/*
int head[M],cnt=1;
void init(int n){cnt=1;for(int i=0;i<=n;i++)head[i]=0;}
struct EDGE{int to,nxt,w;}ee[M*2];
void add(int x,int y,int w){ee[++cnt].nxt=head[x],ee[cnt].w=w,ee[cnt].to=y,head[x]=cnt;}
*/
int a[M];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin>>T;
while(T--){
int n;
cin>>n;
bool f= true;
for(int i=1;i<=n;i++){
cin>>a[i];
if(i > 1&& a[i]>=a[i-1])f=false;
}
if(!f)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
B:显然,同一位时 ,1&1> 1^1 1 & 0 < 1^0
所以只有最高位相同才会大于
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define re register
#define ls (o<<1)
#define rs (o<<1|1)
#define pb push_back
const double PI= acos(-1.0);
const int M = 1e5+7;
/*
int head[M],cnt=1;
void init(int n){cnt=1;for(int i=0;i<=n;i++)head[i]=0;}
struct EDGE{int to,nxt,w;}ee[M*2];
void add(int x,int y,int w){ee[++cnt].nxt=head[x],ee[cnt].w=w,ee[cnt].to=y,head[x]=cnt;}
*/
int a[M],b[M];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin>>T;
while(T--){
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
b[i]=-1;
for(int j=0;j<=31;j++)
{
if((a[i]>>j) & 1)b[i]=j;
}
}
ll ans=0;
for(int i=0;i<=31;i++)
{
int nm=0;
for(int j=1;j<=n;j++)
{
if(b[j]==i)nm++;
}
ans+=(ll)nm*(nm-1)/2;
}
cout<<ans<<endl;
}
return 0;
}
C1:
很容易发现并证明:
所选数大小满足是W型的折线。
结果为:\sum {max(0,a[i+1]-a[i])}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define re register
#define ls (o<<1)
#define rs (o<<1|1)
#define pb push_back
const double PI= acos(-1.0);
const int M = 3e5+7;
/*
int head[M],cnt=1;
void init(int n){cnt=1;for(int i=0;i<=n;i++)head[i]=0;}
struct EDGE{int to,nxt,w;}ee[M*2];
void add(int x,int y,int w){ee[++cnt].nxt=head[x],ee[cnt].w=w,ee[cnt].to=y,head[x]=cnt;}
*/
int a[M],b[M];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin>>T;
while(T--){
int n,q,id=0;
ll ans=0;
cin>>n>>q;
for(int i=1;i<=n;i++)
{
cin>>a[i];
b[i]=a[i]-a[i-1];
if(b[i]>=0)id=i;
// else ans-=b[i];
}
for(int i=1;i<=id;i++)
{
if(b[i]<0)ans-=b[i];
}
ans+=a[id];
cout<<ans<<endl;
}
return 0;
}
C2:
每次交换两个数差分数组最多改变4个。记录第一次的值,每次改变差值即可,(比赛时写麻烦了,没必要树状数组)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define re register
#define ls (o<<1)
#define rs (o<<1|1)
#define pb push_back
const double PI= acos(-1.0);
const int M = 3e5+7;
/*
int head[M],cnt=1;
void init(int n){cnt=1;for(int i=0;i<=n;i++)head[i]=0;}
struct EDGE{int to,nxt,w;}ee[M*2];
void add(int x,int y,int w){ee[++cnt].nxt=head[x],ee[cnt].w=w,ee[cnt].to=y,head[x]=cnt;}
*/
int a[M],b[M],n;
ll c[M];
void add(int x,int d){
// cout<<" -= = "<<x<<" "<<d<<endl;
while(x<=n){
c[x]+=d;
x+=x&(-x);
}
}
ll qu(int x)
{
ll ans=0;
while(x){
ans+=c[x];
x-=x&(-x);
}
return ans;
}
set<int>z;
void ea(int x)
{
if(x>n)return ;
if(b[x]>=0)z.erase(x);
else add(x,-b[x]);
}
void in(int x)
{
if(x>n)return ;
if(b[x]>=0)z.insert(x);
else add(x,b[x]);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin>>T;
while(T--){
int q,id=0;
ll ans=0;
cin>>n>>q;
z.clear();
for(int i=1;i<=n;i++)c[i]=0;
for(int i=1;i<=n;i++)
{
// c[i]=0;
cin>>a[i];
b[i]=a[i]-a[i-1];
if(b[i]>=0)id=i,z.insert(i);
else add(i,b[i]);
}
// cout<<id<<" - "<<qu(6)-qu(5)<<" "<<qu(7)<<endl;
ans=-qu(id)+a[id];
cout<<ans<<endl;;
while(q--){
int x,y;
cin>>x>>y;
if(x>y)swap(x,y);
ea(x),ea(x+1);if(y>x+1)ea(y);ea(y+1);
swap(a[x],a[y]);
b[x]=a[x]-a[x-1];b[x+1]= a[x+1]-a[x];
b[y]= a[y]-a[y-1];b[y+1]=a[y+1]-a[y];
in(x),in(x+1);if(y>x+1)in(y);in(y+1);
if(z.size())id=*(--z.end());
else id=0;/*
for(int i=1;i<=n;i++)cout<<a[i]<<" ";
cout<<" =================- -- "<<endl;
for(int i=1;i<=n;i++)cout<<b[i]<<" ";
cout<<" - -- "<<id<<" "<<qu(id)<<endl;*/
ans=-qu(id)+a[id];
cout<<ans<<endl;
}
}
return 0;
}
D:
显然,类比扫描线从前往后,在某个数消失之前,计算选它的方案数即可。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define re register
#define ls (o<<1)
#define rs (o<<1|1)
#define pb push_back
const double PI= acos(-1.0);
const int M = 1e6+7;
const int mod = 998244353 ;
/*
int head[M],cnt=1;
void init(int n){cnt=1;for(int i=0;i<=n;i++)head[i]=0;}
struct EDGE{int to,nxt,w;}ee[M*2];
void add(int x,int y,int w){ee[++cnt].nxt=head[x],ee[cnt].w=w,ee[cnt].to=y,head[x]=cnt;}
*/
struct node{
int x,opt;
}p[M];
bool cmp(node a,node b){
if(a.x==b.x)return a.opt<b.opt;
return a.x<b.x;
}
ll c[M];
ll qpow(ll a,ll b){
ll ans=1;
while(b){
if(b&1)ans=ans*a%mod;
a=a*a%mod;
b/=2;
}
return ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n,k,sz=0,l,r;
cin>>n>>k;
for(int i=1;i<=n;i++){
cin>>l>>r;
p[++sz]=node{l,1};
p[++sz]=node{r+1,-1};
}
sort(p+1,p+1+sz,cmp);
ll ans=0,nm=0;
c[k-1]=1;
/*
*/
for(int i=k;i<=n;i++){
c[i]=c[i-1]*i%mod*qpow(i-(k-1),mod-2)%mod;
// cout<<i<<" - "<<c[i]<<endl;
}
for(int i=1;i<=sz;i++)
{
nm+=p[i].opt;
if(p[i].opt==-1){
ans+=c[nm];
ans%=mod;
// cout<<i<<" "<<nm<<endl;
}
// if(p[i].x==p[i+1].x)continue;
// cout<<" == = = "<<nm<<" "<<i<<endl;
}
cout<<ans<<endl;
return 0;
}