QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#207242#7560. Computer Networkw4p3rML 1ms9648kbC++202.1kb2023-10-08 12:19:432023-10-08 12:19:43

Judging History

你现在查看的是最新测评结果

  • [2023-10-08 12:19:43]
  • 评测
  • 测评结果:ML
  • 用时:1ms
  • 内存:9648kb
  • [2023-10-08 12:19:43]
  • 提交

answer

#include<bits/stdc++.h>
#define inf 2e9
#define eps 1e-6
#define FOR(i, a, b) for(int i = a; i <= b; i ++)
#define REP(i, a, b) for(int i = a; i >= b; i --)
#define pb push_back
#define fr first
#define sd second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline ll read()
{
	char ch = getchar();
	ll s = 0, w = 1;
	while (ch < '0' || ch > '9') {if (ch == '-')w = -1; ch = getchar();}
	while (ch >= '0' && ch <= '9') {s = s * 10 + ch - '0'; ch = getchar();}
	return s * w;
}
#define N 1000010
int ans = inf;
int n;
int a[N], b[N];
int pa[N], qa[N], ta[N];
int lowbit(const int x) {return x & (-x);}
int qry(int l, int r)
{
	if (l > r)return inf;
	else return min(__builtin_popcount(l), qry(l + lowbit(l), r));
}
void upd(int K)
{
	FOR(i, 1, n)
	{
		qa[i] = qa[i] | ((pa[i] & 1) << K);
		pa[i] >>= 1;
	}
}
int main()
{
	// freopen("C.in", "r", stdin);
	// freopen("C.out", "w", stdout);
	n = read();
	FOR(i, 1, n)a[i] = read();
	FOR(i, 1, n)b[i] = read();
	FOR(i, 1, n)pa[i] = a[i];
	FOR(K, 0, 30)
	{
		map<int, vector<int>>ma;
		FOR(i, 1, n)ta[i] = pa[i];
		int d = ta[1] - b[1], num = 0;
		FOR(i, 1, n)num += ((ta[i] - b[i]) == d);
		FOR(i, 1, n)if (qa[i])ma[(1 << K) - qa[i]].push_back(i);
		// cout << "GG:" << K << endl;
		// FOR(i, 1, n)cout << pa[i] << ' '; cout << endl;
		// FOR(i, 1, n)cout << qa[i] << ' '; cout << endl;
		int lst = 0;
		for (auto [w, v] : ma)
		{
			if (d <= 0 && num == n)ans = min(ans, -d + K + qry(lst, w - 1));
			for (int x : v)
			{
				if (x > 1)
				{
					num -= ((ta[x] - b[x]) == d);
					ta[x] ++;
					num += ((ta[x] - b[x]) == d);
				}
				else
				{
					ta[x] ++;
					d = ta[x] - b[x], num = 0;
					FOR(i, 1, n)num += ((ta[i] - b[i]) == d);
				}
			}
			// cout << "EEEEE:" << w << " " << d << ' ' << num << endl;
			// FOR(i, 1, n)cout << ta[i] << ' '; cout << endl;
			lst = w;
		}
		if (d <= 0 && num == n)ans = min(ans, -d + K + qry(lst, (1 << K) - 1));
		upd(K);
	}
	if (ans < inf)cout << ans << '\n';
	else cout << "-1\n";
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 9648kb

input:

5
1 2 3 4 5
6 6 6 6 7

output:

9

result:

ok 1 number(s): "9"

Test #2:

score: 0
Accepted
time: 1ms
memory: 9576kb

input:

3
2 3 4
1 2 3

output:

-1

result:

ok 1 number(s): "-1"

Test #3:

score: 0
Accepted
time: 0ms
memory: 9568kb

input:

2
65536 65537
1 2

output:

32

result:

ok 1 number(s): "32"

Test #4:

score: -100
Memory Limit Exceeded

input:

1
0
28

output:


result: