Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

贴个c++代码,分桶

Posted by a280920481 at 2018-11-21 16:04:11 on Problem 2104
#include <iostream>
#include <algorithm>
using namespace std;

const int BSize = 1000;

int a[100005];

int sorta[100005];

int bucket[101][BSize];

int main()
{
	int n, m, bAmount, left, right, k, fb, lb, fs, ls;//数据规模 操作次数 需要桶的个数 左端点 右端点 序数 首个完整的桶 末个完整的桶 首个下标 末个下标

	cin >> n >> m;

	bAmount = n / BSize;

	for (int i = 0; i < n; i++)
	{
		//cin >> a[i];
		scanf_s("%d", &a[i]);
	}

	int i1 = 0, i2 = 0;

	for (int i = 0; i < n; i++)
	{
		bucket[i1][i2] = sorta[i] = a[i];
		i2++;
		if (i2 == BSize)
		{
			i1++;
			i2 = 0;
		}
	}

	for (int i = 0; i < bAmount; i++)
	{
		sort(bucket[i], bucket[i] + BSize);
	}
	sort(sorta, sorta + n);

	for (int so = 0; so < m; so++)
	{
		//cin >> left >> right >> k;
		scanf_s("%d%d%d", &left, &right, &k);

		left--;
		right--;

		fb = (left + 999) / BSize;
		lb = (right + 1) / BSize - 1;
		
		int lp = -1, rp = n - 1, mp, c;

		while (rp - lp > 1)
		{
			mp = (lp + rp) >> 1;
			c = 0;

			fs = left;
			ls = right;

			while ((fs < (fb * BSize)) && (fs <= ls))
			{
				if (a[fs] <= sorta[mp])
				{
					c++;
				}
				fs++;
			}
			while ((ls >= ((lb + 1)*BSize)) && (fs <= ls))
			{
				if (a[ls] <= sorta[mp])
				{
					c++;
				}
				ls--;
			}

			for (int i = fb; i <= lb; i++)
			{
				int lw = -1, rw = BSize, mw;
				while (rw - lw > 1)
				{
					mw = (lw + rw) >> 1;
					if (bucket[i][mw] > sorta[mp])
					{
						rw = mw;
					}
					else
					{
						lw = mw;
					}
				}
				c += rw;
			}

			if (c < k)
			{
				lp = mp;
			}
			else
			{
				rp = mp;
			}
		}

		cout << sorta[rp] << '\n';

	}

	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator