Codeforces Round #379 (Div. 2), problem: (F) Anton and School Solution in C/C++

Affiliate Disclosure: This post contains Amazon affiliate links. If you purchase through these links, eBlogarithm may earn a commission at no extra cost to you. Prices and availability are subject to change.

Hey guys I just tried the Anton and School problem , hope you might like my solution feel free to comment better solution .

#include <stdio.h>

#define N 200000
#define B 30

int main() {
int i, j, n;
long long sum;
static int aa[N], bb[N], cc[N], kk[B];

scanf(“%d”, &n);
sum = 0;
for (i = 0; i < n; i++) {
scanf(“%d”, &bb[i]);
sum += bb[i];
}
for (i = 0; i < n; i++) {
scanf(“%d”, &cc[i]);
sum += cc[i];
}
if (sum % (2 * n) != 0) {
printf(“-1\n”);
return 0;
}
sum /= 2 * n;
for (i = 0; i < n; i++) {
int a = bb[i] + cc[i];

if (a < sum) {
printf(“-1\n”);
return 0;
}
a -= sum;
if (a % n != 0) {
printf(“-1\n”);
return 0;
}
aa[i] = a / n;
}
for (i = 0; i < n; i++)
for (j = 0; j < B; j++)
if ((aa[i] & 1 << j) > 0)
kk[j]++;
for (i = 0; i < n; i++) {
long long b, c;

b = c = 0;
for (j = 0; j < B; j++)
if ((aa[i] & 1 << j) > 0) {
b += (long long) (1 << j) * kk[j];
c += (long long) (1 << j) * n;
} else {
b += (long long) (1 << j) * 0;
c += (long long) (1 << j) * kk[j];
}
if (b != bb[i] || c != cc[i]) {
printf(“-1\n”);
return 0;
}
}
for (i = 0; i < n; i++)
printf(“%d “, aa[i]);
printf(“\n”);
return 0;
}

📦 Looking for Codeforces Round 379 Div Problem Anton? Check the best deals on Amazon.

🛒 Shop Codeforces Round 379 Div Problem Anton on Amazon

As an Amazon Associate, eBlogarithm earns from qualifying purchases. Prices and availability are subject to change.

Affiliate Disclosure: This post contains Amazon affiliate links. If you purchase through these links, eBlogarithm may earn a commission at no extra cost to you. Prices and availability are subject to change.

Hey guys I just tried the Anton and School problem , hope you might like my solution feel free to comment better solution .

#include <stdio.h>

#define N 200000
#define B 30

int main() {
int i, j, n;
long long sum;
static int aa[N], bb[N], cc[N], kk[B];

scanf(“%d”, &n);
sum = 0;
for (i = 0; i < n; i++) {
scanf(“%d”, &bb[i]);
sum += bb[i];
}
for (i = 0; i < n; i++) {
scanf(“%d”, &cc[i]);
sum += cc[i];
}
if (sum % (2 * n) != 0) {
printf(“-1\n”);
return 0;
}
sum /= 2 * n;
for (i = 0; i < n; i++) {
int a = bb[i] + cc[i];

if (a < sum) {
printf(“-1\n”);
return 0;
}
a -= sum;
if (a % n != 0) {
printf(“-1\n”);
return 0;
}
aa[i] = a / n;
}
for (i = 0; i < n; i++)
for (j = 0; j < B; j++)
if ((aa[i] & 1 << j) > 0)
kk[j]++;
for (i = 0; i < n; i++) {
long long b, c;

b = c = 0;
for (j = 0; j < B; j++)
if ((aa[i] & 1 << j) > 0) {
b += (long long) (1 << j) * kk[j];
c += (long long) (1 << j) * n;
} else {
b += (long long) (1 << j) * 0;
c += (long long) (1 << j) * kk[j];
}
if (b != bb[i] || c != cc[i]) {
printf(“-1\n”);
return 0;
}
}
for (i = 0; i < n; i++)
printf(“%d “, aa[i]);
printf(“\n”);
return 0;
}

📦 Looking for Codeforces Round 379 Div Problem Anton? Check the best deals on Amazon.

🛒 Shop Codeforces Round 379 Div Problem Anton on Amazon

As an Amazon Associate, eBlogarithm earns from qualifying purchases. Prices and availability are subject to change.

More from author

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related posts

Advertismentspot_img

Latest posts

7 Reasons This Viral Kitchen Gadgets Is Worth Every Single Penny

📢 As an Amazon Associate, I earn from qualifying purchases.After putting the Viral Kitchen Gadgets through a proper 30-day real-world test, I can tell...

How long should I bleach my live rock?

When resetting an aquarium or dealing with persistent pests like bubble algae, aiptasia, and unwanted parasites, bleaching live rock is often the most reliable...

Is Viral Kitchen Gadgets Worth Your Money Here Is Our Full Review

📢 As an Amazon Associate, I earn from qualifying purchases.After putting the Viral Kitchen Gadgets through a proper 30-day real-world test, I can tell...

Want to stay up to date with the latest news?

We would love to hear from you! Please fill in your details and we will stay in touch. It's that simple!