8.27总结
前两题签到,不写
T3
知道了公式,但不知道怎么推的
知道了在补上
有三种特殊情况,首先是p,k中有一个为1时,只要买一瓶
第二是k为0时不用买
第三是k<=p时要买k瓶
#include<bits/stdc++.h>
#define int long long
#define endl "\n"
using namespace std;
const int maxn=1e6+5,mod=1e9+7,inf=1e18;
int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0' && ch<='9')x=x*10+ch-'0',ch=getchar();return x*f;}
void write(int x){if(x<0){putchar('-'),x=-x;}if(x>9){write(x/10);}putchar(x%10+'0');return;}
int fpow(int a,int b,int p){if(b==0){return 1;}int res=fpow(a,b/2,p)%p;if(b%2==1){return((res*res)%p*a)%p;}else{return(res*res)%p;}}
int t,p,k;
void solve(){
cin>>p>>k;
if(p==1||k==1){
cout<<1<<endl;
return ;
}
if(k==0){
cout<<0<<endl;
return;
}
if(k<=p){
cout<<k<<endl;
return ;
}
cout<<k-k/p<<endl;
}
signed main(){
cin>>t;
while(t--){
solve();
}
return 0;
}
T4
可以发现,直接DP不可行,我们采用记忆化搜索,从n到0,可以除2,3,5
但是注意,要先判断是否能整除,不能整除则需要减去多余的部分
#include<bits/stdc++.h>
#define int long long
#define endl "\n"
using namespace std;
const int maxn=1e6+5,mod=1e9+7,inf=1e18;
int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0' && ch<='9')x=x*10+ch-'0',ch=getchar();return x*f;}
void write(int x){if(x<0){putchar('-'),x=-x;}if(x>9){write(x/10);}putchar(x%10+'0');return;}
int fpow(int a,int b,int p){if(b==0){return 1;}int res=fpow(a,b/2,p)%p;if(b%2==1){return((res*res)%p*a)%p;}else{return(res*res)%p;}}
int t,p,k;
void solve(){
cin>>p>>k;
if(p==1||k==1){
cout<<1<<endl;
return ;
}
if(k==0){
cout<<0<<endl;
return;
}
if(k<=p){
cout<<k<<endl;
return ;
}
cout<<k-k/p<<endl;
}
signed main(){
cin>>t;
while(t--){
solve();
}
return 0;
}
T5
我们找到一个子串
10 5 6 2
我们标记一个数5
,比5大的记为1,比5小的记为-1,发现,只要一个串内的数和大于等于0,就是有效的数对,可以使用前缀和优化
但是再观察,其实这个就是逆序对,我们使用树状数组优化即可
include<bits/stdc++.h>
define int long long
define endl "\n"
using namespace std;
const int maxn=1e6+5,mod=1e9+7,inf=1e18,betaa=100001;
int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch'-')f=-1;ch=getchar();}while(ch>='0' && ch<='9')x=x10+ch-'0',ch=getchar();return xf;}
void write(int x){if(x<0){putchar('-'),x=-x;}if(x>9){write(x/10);}putchar(x%10+'0');return;}
int fpow(int a,int b,int p){if(b0){return 1;}int res=fpow(a,b/2,p)%p;if(b%2==1){return((resres)%pa)%p;}else{return(res*res)%p;}}
int n,x,a[maxn],b[maxn],val[maxn],sum[maxn];
int lowbit(int x){
return x&(-x);
}
void update(int x,int v){
while(x<=210000){
val[x]+=v;
x+=lowbit(x);
}
}
int query(int x){
int res=0;
while(x){
res+=val[x];
x-=lowbit(x);
}
return res;
}
signed main(){
cin>>n>>x;
int ans=0;
for(int i=1;i<=n;i++){
cin>>a[i];
if(a[i]>=x){
b[i]=1;
}
else if(a[i]<x){
b[i]=-1;
}
}
update(0+betaa,1);
for(int i=1;i<=n;i++){
sum[i]=sum[i-1]+b[i];
int tmp=query(sum[i]+betaa);
ans+=tmp;
update(sum[i]+betaa,1);
}
cout<<ans<<endl;
return 0;
}
本人(KK_SpongeBob)蒟蒻,写不出好文章,但转载请注明原文链接:https://www.cnblogs.com/OIer-QAQ/p/19061653

浙公网安备 33010602011771号