Codeforces Round #378 (Div. 2), problem: (E) Sleep in Class Solution in C

#include
#define ABS(a) ((a) < 0 ? (a) : (a))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define TRUE 1
#define FALSE 0

#define getchar_unlocked() getchar()

long long fast_in()
{
static long long j_fast;
static char c_fast;
j_fast = 0;
c_fast = getchar_unlocked();
while (c_fast < ‘0’ || c_fast > ‘9’)
c_fast = getchar_unlocked();
while (c_fast >= ‘0’ && c_fast <= ‘9’) {
j_fast = (j_fast << 1) + (j_fast << 3) + c_fast – ‘0’;
c_fast = getchar_unlocked();
}
return j_fast;
}

#define MAXN 1000006

long long n, a[MAXN], ans[MAXN], down[MAXN], up[MAXN], wdown[MAXN], wup[MAXN];

long long binary_search_up(long long req)
{
// search for last occurance
static long long f, l, m;
f = 1;
l = n + 1;
while ((l – f) > 1) {
m = ((l + f) >> 1);
if (up[m] >= req)
f = m;
else
l = m;
}
if (up[l] == req)
return l;
else
return f;
}
long long binary_search_down(long long req)
{
// search for first occurance
static long long f, l, m;
f = 0;
l = n;
while ((l – f) > 1) {
m = ((l + f) >> 1);
if (down[m] < req)
f = m;
else
l = m;
}
if (down[f] == req)
return f;
else
return l;
}
int main()
{
static long long i, considered, uptill, downtill;
static char c;
n = fast_in();
for (i = 1; i <= n; i++) {
c = getchar_unlocked();
if (c == ‘U’)
a[i] = 1;
else
a[i] = 0;
}
for (i = 1; i <= n; i++) {
if (a[i])
down[i] = down[i – 1];
else
down[i] = down[i – 1] + 1;
}
for (i = 0; i <= n; i++) {
if (a[i])
wdown[i] = wdown[i – 1];
else
wdown[i] = wdown[i – 1] + i;
}
for (i = n; i > 0; i–) {
if (a[i])
up[i] = up[i + 1] + 1;
else
up[i] = up[i + 1];
}
for (i = n; i > 0; i–) {
if (a[i])
wup[i] = wup[i + 1] + (n – i + 1);
else
wup[i] = wup[i + 1];
}
up[0] = up[1];
wup[0] = wup[1];
down[n + 1] = down[n];
wdown[n + 1] = wdown[n];
// for (i = 0; i <= n + 1; i++) {
// printf(“%lld %lld %lld %lld\n”, up[i], down[i], wup[i],
// wdown[i]);
// }
for (i = 1; i <= n; i++) {
considered = MIN(up[0] – up[i], down[n] – down[i]);
if (a[i]) {
downtill = binary_search_down(considered + down[i] + 1);
uptill = binary_search_up(considered + up[i]);
} else {
downtill = binary_search_down(considered + down[i]);
uptill = binary_search_up(considered + up[i] + 1);
}
ans[i] = (wdown[downtill] – wdown[i] –
i * (down[downtill] – down[i]));
ans[i] +=
(wup[uptill] – wup[i] – (n – i + 1) * (up[uptill] – up[i]));
ans[i] <<= 1;

//printf(“%lld considered %lld downtill %lld uptill %lld\n”, i,
// considered, downtill, uptill);
if (a[i]) {
if ((down[n] – down[i]) > (up[0] – up[i]))
ans[i] += i;
else
ans[i] += (n – i + 1);
} else {
if ((down[n] – down[i]) >= (up[0] – up[i]))
ans[i] += i;
else
ans[i] += (n – i + 1);
}
}
for (i = 1; i <= n; i++)
printf(“%lld “, ans[i]);
return 0;
}

Advertisement

spot_img

How Viral Kitchen Gadgets...

📢 As an Amazon Associate, I earn from qualifying...

Avengers: Armageddon #5 Preview:...

Tension Reaches a Breaking Point in Avengers: Armageddon #5 Marvel's...

7 Reasons This Viral...

📢 As an Amazon Associate, I earn from qualifying...

Can baby deer walk...

Witnessing a newborn animal take its very first steps...

Is the Anker MagGo...

The Long-Term Durability Assessment: 6 Months as a Daily...

Anker MagGo 3-in-1 vs...

The Heavyweight Showdown: Anker MagGo vs. Belkin BoostCharge Pro For...

How Viral Kitchen Gadgets Is Quietly Changing the Game

📢 As an Amazon Associate, I earn from qualifying purchases.Most products in this category disappoint within weeks. Cheap materials, vague instructions, and results that...

Avengers: Armageddon #5 Preview: Trust Issues Intensify

Tension Reaches a Breaking Point in Avengers: Armageddon #5 Marvel's mightiest heroes are once again staring down total catastrophe, but this time, the greatest danger...

7 Reasons This Viral Kitchen Gadgets Is Worth Every Single Penny

📢 As an Amazon Associate, I earn from qualifying purchases.I stumbled across this while desperately trying to fix a recurring problem in my routine....

Can baby deer walk right away?

Witnessing a newborn animal take its very first steps is one of the most heartwarming sights in the natural world. In the wild, however,...

Is the Anker MagGo 3-in-1 Worth $100? Long-Term Durability Review

The Long-Term Durability Assessment: 6 Months as a Daily Driver Spending $100 on a smartphone charging accessory is a significant consumer investment. When budget 3-in-1...

Anker MagGo 3-in-1 vs Belkin BoostCharge Pro 3-in-1: Which Is Better?

The Heavyweight Showdown: Anker MagGo vs. Belkin BoostCharge Pro For the past four years, if an Apple user wanted the absolute best 3-in-1 wireless charging...

Best Protective Travel Pouches and Cases for Anker MagGo 3-in-1

Protecting Your Premium Travel Power Investment The Anker MagGo 3-in-1 Foldable Charging Station is engineered for life on the move. With its collapsible form factor,...

How Fast Does Anker MagGo Charge iPhone 15 and 16 from 0 to 100%?

Putting Qi2 15W Speed Claims to the Test When wireless charging standards make technological leaps, consumer skepticism is natural. For years, marketing promises of "fast...

Does the Anker MagGo 3-in-1 Come with a 40W USB-C Power Brick and Cable?

The Hidden Cost of Wireless Chargers: What’s Really in the Box? In modern consumer electronics packaging, a frustrating cost-cutting trend has become commonplace: the omitted...