地址:https://nanti.jisuanke.com/t/30996
就是一个模拟题,n^2会超时,需要用维护一个最小值线段树,复杂度变成了nlog(n)
注意:被坑的要命的地方,一定要好好理解题意,一次可能会有多个房间的灯泡被换掉,
不是一次一个,有时不要被样例给迷惑了
先预处理1~100000月的值,然后按照询问输出
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int N = 1e5 + 5;
int ans[N][2];
int tree[N << 2];
int arr[N];
void build(int root,int l,int r)
{
if(l == r)
{
tree[root] = arr[l];
return;
}
int mid = (l + r) / 2;
build(root * 2 + 1,l,mid);
build(root * 2 + 2,mid + 1,r);
tree[root] = min(tree[root * 2 + 1],tree[root * 2 + 2]);
}
void updateone(int root,int l,int r,int index,int val)
{
if(l == r){
if(l == index){
tree[root] = val;
}
return ;
}
int mid = (l + r) / 2;
if(index <= mid){
updateone(root * 2 + 1,l,mid,index,val);
}else{
updateone(root * 2 + 2,mid + 1,r,index,val);
}
tree[root] = min(tree[root * 2 + 1],tree[root * 2 + 2]);
}
int cur;
void Find(int root,int l,int r,int pl,int pr,int val)
{
//cout << root << " " << tree[root] << " " << l << " " << r << " " << val << endl;
//cout << tree[root] << " " << vis[root] << endl;
if(l == r)
{
if(tree[root] <= val){
cur = min(cur,l);
}
return ;
}
int mid = (l + r) / 2;
if(pl <= l && r <= pr){
if(tree[root * 2 + 1] <= val){
Find(root * 2 + 1,l,mid,pl,pr,val);
}else{
if(tree[root * 2 + 2] <= val){
Find(root * 2 + 2,mid + 1,r,pl,pr,val);
}
}
return ;
}
if(pl <= mid){
Find(root * 2 + 1,l,mid,pl,pr,val);
}
if(pr > mid){
Find(root * 2 + 2,mid + 1,r,pl,pr,val);
}
}
int main()
{
int n,m;
while(~scanf("%d %d",&n,&m))
{
for(int i = 1;i <= n;++i)
{
scanf("%d",&arr[i]);
}
build(1,1,n);
//cout << tree[1] << endl;
memset(ans,0,sizeof(ans));
int sum = m;
for(int i = 1;i <= 100000;++i)
{
//当没有房间需要换,每个月就不需要购买灯泡了
if(tree[1] == inf){
for(int j = i;j <= 100000;++j)
{
ans[j][0] = ans[i - 1][0];
ans[j][1] = ans[i - 1][1];
}
break;
}
if(tree[1] > sum){
//当前灯泡数不够更新房间
ans[i][0] = ans[i - 1][0];
ans[i][1] = sum;
}else{
int p = 1;
//用一个循环解决一次换掉多个房间的情况
while(tree[1] <= sum){
cur = inf;
// cout << 0 << endl;
Find(1,1,n,1,n,sum);
//cout << cur << endl;
ans[i][0] = ans[i - 1][0] + p++;
sum -= arr[cur];
updateone(1,1,n,cur,inf);
}
ans[i][1] = sum;
}
sum += m;
//cout << "root:" << tree[1] << endl;
//cout << ans[i][0] << " " << ans[i][1] << endl;
}
int q;
scanf("%d",&q);
for(int i = 1;i <= q;++i)
{
int x;
scanf("%d",&x);
printf("%d %d\n",ans[x][0],ans[x][1]);
}
}
return 0;
}