A 小沙的好客
题目:
思路:
考思维;
代码如下:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,q,k,x,a[100005],sum[100005]={0};
int main(){
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>a[i];
}
sort(a+1,a+n+1);//只用排一次序更省时
for(int i=1;i<=n;i++){
sum[i]=sum[i-1]+a[i];//动归思路提前计算
}
for(int i=0;i<q;i++){
cin>>k>>x;
ll l=1,r=n;
while(l<=r){//二分查找所需要x的下标
ll mid=(l+r)/2;
if(x<a[mid])r=mid-1;
else l=mid+1;
}
if(r-k<=0)cout<<sum[r]<<endl;
else cout<<sum[r]-sum[r-k]<<endl;
}
return 0;
}
I小沙の金银阁
题目:
思路:
考思维;想不到......焦虑
代码如下:
# include<bits/stdc++.h>
using namespace std;
# define int long long
signed main(){
int n,m;
cin>>n>>m;
vector<int> s(n+2);
s[n] = m;
for(int i = n;i>1;--i){
if(s[i]>=2){
s[i-1] = s[i]/2;
s[i] -= s[i-1];
}
}
for(int i = 1;i <= n;++i){
if(s[i] == 0) {
cout<<-1<<endl;
return 0;
}
}
for(int i = 1;i <= n;++i){
cout<<s[i]<<" \n"[i == n];
}
return 0;
}