std & random

Laffey 2022-04-30 9:40:17

std

#include <cstdio>
#include <queue>
using namespace std;

priority_queue<int> heap1;
priority_queue<int, vector<int>, greater<int>> heap2;

int main()
{
	int n;
	scanf("%d", &n);
	heap1.push(-0x3f3f3f3f);
	heap2.push(0x3f3f3f3f);
	for (int i = 1; i <= n; i++) {
		int t;
		scanf("%d", &t);
		if (t >= heap1.top()) {
			heap2.push(t);
		}
		else {
			heap1.push(t);
		}
		while (heap2.size() < (i + 2) / 2) {
			heap2.push(heap1.top());
			heap1.pop();
		}
		while (heap2.size() > (i + 2) / 2) {
			heap1.push(heap2.top());
			heap2.pop();
		}
		if (i & 1) {
			printf("%d\n", heap1.top());
		}
	}
	return 0;
}

random

#include <cstdio>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    char in[] = ".\\data\\median0.in";
    char ans[] = ".\\data\\median0.ans";
    int all[] = {0, 100, 3000, 30000, 70000, 100000};
    srand(time(0));
    for (int i = 1; i <= 5; i++) {
        in[13]++, ans[13]++;
        freopen(in, "w", stdout);
        int n = all[i];
        printf("%d\n", n);
        for (int i = 1; i <= n; i++) {
            printf("%d ", rand() * rand() % n);
        }

        freopen(in, "r", stdin);
        freopen(ans, "w", stdout);
        system(".\\2.exe");
    }
    return 0;
}

共 1 条回复

Laffey

update on 2022.4.30: 两个 while 循环其实可以换成 if-else if 也能过。

(出题当天修改标程是个什么意思啊喂)