玲珑杯 - 我也不是B 倍增+二分


传送门

1112 - 我也不是B

Time Limit:2s Memory Limit:512MByte

Submissions:687Solved:83

DESCRIPTION

小 L 有一串 QQ 个数排成一排,从左往右是 A1A1 开始一直到 AQAQ,它一直很喜欢这串数。

有一天,小 L 去高考报名了,小 J 和小 N 过来用这串数玩游戏。

一开始,小 N 手上有一个空序列,定义变量C:=0C:=0。小 J 从左往右依次把序列中的数取出,放到小 N 手上序列的最右边,如果放完之后,整个序列的混乱度超过了 MM,小 N 就会把手上的所有数全部扔掉,然后令 C:=C+1C:=C+1

定义一个长度为 KK 序列的混乱度 S=Ki=1Bi×ViS=∑i=1KBi×Vi,其中BiBi 表示序列中第 ii 小的数,VV 为一个给定的序列。

小 J 和小 N 想知道,加入每个数之后,CC 是多少。

INPUT
第一行两个整数 QQ MM接下来一行 QQ个整数, 第 ii个整数表示 AiAi接下来一行 QQ个整数, 第 ii个整数表示 ViVi
OUTPUT
一行 QQ个整数, 第 ii个整数表示加入第 ii个数之后的 CC, 相邻两个整数之间用空格隔开, 注意最后一个数后不要输出空格输出完请换行
SAMPLE INPUT
5 11 3 2 5 41 1 1 1 1
SAMPLE OUTPUT
0 1 2 3 4
HINT
1Q300000,0M1018,1Ai108,1Vi1041≤Q≤300000,0≤M≤1018,1≤Ai≤108,1≤Vi≤104对于所有的 1iQ1,ViVi+11≤i≤Q−1,Vi≤Vi+1

由于v[i+1]>=v[i],所以我们可以发现加入一个数以后混乱度是肯定会增加的,一次我们的一个想法就是,对于当前的起点,我们二分查找混乱度大于M的第一个点,然后在那个点C的值会加一,在那个点之前的点的C都是一样的.

但是我们这样的话复杂度是O(nnlog(n))的,是不够的, 因此我们可以先倍增,对于当前的起点x,我们找到一个区间[x+2^(k-1),x+2^k],使得混乱度大于M的点在这个区间内, 然后我们再在这个区间里二分,这样的话复杂度就降为(nlog(n)log(n))

//china no.1
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <functional>
#include <stdlib.h>
#include <time.h>
#include <bitset>
using namespace std;

#define pi acos(-1)
#define s_1(x) scanf("%d",&x)
#define s_2(x,y) scanf("%d%d",&x,&y)
#define s_3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define s_4(x,y,z,X) scanf("%d%d%d%d",&x,&y,&z,&X)
#define S_1(x) scan_d(x)
#define S_2(x,y) scan_d(x),scan_d(y)
#define S_3(x,y,z) scan_d(x),scan_d(y),scan_d(z)
#define PI acos(-1)
#define endl '\n'
#define srand() srand(time(0));
#define me(x,y) memset(x,y,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0); cin.tie(0);
#define FOR(x,n,i) for(int i=x;i<=n;i++)
#define FOr(x,n,i) for(int i=x;i<n;i++)
#define fOR(n,x,i) for(int i=n;i>=x;i--)
#define fOr(n,x,i) for(int i=n;i>x;i--)
#define W while
#define sgn(x) ((x) < 0 ? -1 : (x) > 0)
#define bug printf("***********\n");
#define db double
#define ll long long
#define mp make_pair
#define pb push_back
typedef long long LL;
typedef pair <int, int> ii;
const int INF=0x3f3f3f3f;
const LL LINF=0x3f3f3f3f3f3f3f3fLL;
const int dx[]={-1,0,1,0,1,-1,-1,1};
const int dy[]={0,1,0,-1,-1,1,-1,1};
const int maxn=4e3+10;
const int maxx=3e5+10;
const double EPS=1e-8;
const double eps=1e-8;
const int mod=1e9+7;
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}
template <class T>
inline bool scan_d(T &ret){char c;int sgn;if (c = getchar(), c == EOF){return 0;}
while (c != '-' && (c < '0' || c > '9')){c = getchar();}sgn = (c == '-') ? -1 : 1;ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0' && c <= '9'){ret = ret * 10 + (c - '0');}ret *= sgn;return 1;}

inline bool scan_lf(double &num){char in;double Dec=0.1;bool IsN=false,IsD=false;in=getchar();if(in==EOF) return false;
while(in!='-'&&in!='.'&&(in<'0'||in>'9'))in=getchar();if(in=='-'){IsN=true;num=0;}else if(in=='.'){IsD=true;num=0;}
else num=in-'0';if(!IsD){while(in=getchar(),in>='0'&&in<='9'){num*=10;num+=in-'0';}}
if(in!='.'){if(IsN) num=-num;return true;}else{while(in=getchar(),in>='0'&&in<='9'){num+=Dec*(in-'0');Dec*=0.1;}}
if(IsN) num=-num;return true;}

void Out(LL a){if(a < 0) { putchar('-'); a = -a; }if(a >= 10) Out(a / 10);putchar(a % 10 + '0');}
void print(LL a){ Out(a),puts("");}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );
//cerr << "run time is " << clock() << endl;

int a[maxx], v[maxx];
int ans[maxx];
ll m;
int n;
set<ii>st;
map<int,int>cnt;

ll calc()
{
	int now = 1;
	ll ret = 0;
	for(auto it:st)
	{
		ret += 1ll * it.first * v[now++];
	}
	return ret;
}

int main()
{
	while(scanf("%d%lld",&n,&m) != EOF)
	{
		me(ans,0);
		st.clear();
		cnt.clear();
		FOR(1,n,i) s_1(a[i]);
		FOR(1,n,i) s_1(v[i]);
		int now = 1;
		while(now <= n)
		{
			if(now == n)
			{
				if(a[now] * v[1] > m)
					ans[now] = 1;
				break;
			}
			st.clear();
			cnt.clear();

			st.insert(mp(a[now],++cnt[a[now]]) );

			int k = 1;
			int r = now, lst = now;
			ll ret = calc();
			while(ret <= m)
			{
				r = now + k;
				if(r > n)
					r = n;
				k<<=1;
				for(int i = lst + 1; i <= r; ++i)
				{
					st.insert(mp(a[i],++cnt[a[i]]));
				}
				lst = r;
				ret = calc();
				if(r == n)
					break;
			}
			k>>=1;
			if(r == n && ret <= m)
				break;
			int L = now + k/2, R = now + k;
			if(R > n)
				R = n;
			while(R > L)
			{
				int mid = (R + L) / 2;
				int sz = st.size();
				if(mid - now + 1 > sz)
				{
					//add
					for(int i = now + sz; i <= mid; ++i)
					{
						st.insert(mp(a[i],++cnt[a[i]]));
					}
				}
				else
				{
					//erase
					for(int i = now + sz - 1; i > mid; --i)
					{
						st.erase(mp(a[i],cnt[a[i]]--));
						if(cnt[a[i]] == 0)
							cnt.erase(a[i]);
					}
				}
				ll v = calc();
				if(v > m)
					R = mid;
				else
					L = mid + 1;
			}
			ans[L] = 1;
			now = L+1;
		}
		FOR(1,n,i)
		ans[i] += ans[i-1];
		FOR(1,n-1,i) printf("%d ",ans[i]);
		printf("%d\n",ans[n]);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值