Sponsors

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; }

Why is Tesla Up...

Understanding the Recent $28 Surge in Tesla StockTesla investors...

Candace Owens Responds to...

Candace Owens Criticizes Erika Kirk Over TPUSA Event Cancellation The...

Passive component demand splits...

The Shift in Passive Component Demand: AI and Automotive...

Cuba’s president warns US...

Cuba's President Issues Stern Warning to United States Over...

Samsung’s compact speakers bring...

Samsung Redefines Home Audio with Compact Wireless Speakers Samsung has...

Bihar hostel horror: Canteen...

Shocking Incident: Bihar Hostel Guard Arrested for Murder and...

Why is Tesla Up $28

Understanding the Recent $28 Surge in Tesla StockTesla investors have plenty to celebrate as the company’s stock recently witnessed a significant $28 jump. This...

Candace Owens Responds to Erika Kirk Pulling Out of Event With VP JD Vance

Candace Owens Criticizes Erika Kirk Over TPUSA Event Cancellation The political commentary landscape is heating up following a public disagreement between two prominent figures. Candace...

Passive component demand splits in 2026; AI and autos hold firm

The Shift in Passive Component Demand: AI and Automotive Drive 2026 Growth As we approach 2026, the global passive components sector is witnessing a significant...

Cuba’s president warns US against attacking island or trying to depose him

Cuba's President Issues Stern Warning to United States Over Regime Change Threats In a recent and forceful address, Cuban President Miguel Diaz-Canel has sent a...

Samsung’s compact speakers bring wireless home theater sound anywhere

Samsung Redefines Home Audio with Compact Wireless Speakers Samsung has officially released its highly anticipated line of compact lifestyle speakers, bringing a new level of...

Bihar hostel horror: Canteen guard held for rape, murder of minor student, ‘claims he acted to defame institution’

Shocking Incident: Bihar Hostel Guard Arrested for Murder and Rape of MinorIn a chilling turn of events that has sparked widespread outrage across the...

A Harry Potter Star’s Lowest Rated Movie Just Became Free to Stream

The Post-Potter Evolution of Daniel Radcliffe For most of the world, Daniel Radcliffe will always be the boy with the lightning-shaped scar. For over a...

VAPORESSO Strengthens Global Anti-Counterfeiting Drive, Reaffirms Unwavering Commitment to Consumer Safety

Protecting Consumers: VAPORESSO's Global Stand Against Counterfeits In an era where the vaping industry continues to evolve at a rapid pace, the threat of counterfeit...

FM Sitharaman: India’s Strong Fiscal Health Gives RBI Greater Policy Flexibility – startup business stories

Nirmala Sitharaman Highlights Synergy Between Fiscal Strength and RBI Policy Finance Minister Nirmala Sitharaman recently underscored a pivotal aspect of India's current economic landscape: the...