Sponsors

Codeforces Round #380 (Div. 1, Rated, Based on Technocup 2017 – Elimination Round 2), problem: (F) Dirty plates Solution in C/C++

Hi Guys , I Just Tried to solve the Dirty plates problem , hope you might like it , please share if you’ve any better code .

 

#include <iostream>
#include <fstream>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <bitset>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <queue>

#define mp make_pair
#define pb push_back
typedef long long ll;
typedef long double ld;

using namespace std;
const int MAXN = 2100;

int s0[MAXN];
int s1[MAXN];
int s2[MAXN];
int pl[MAXN];
int c0, c1, c2;
vector<pair<int, int> > vv;
int a, b;
int n;

void ex() {
cout << “NO\n”;
exit(0);
}

void checks2() {
for (int i = 0; i < c2; ++i) {
if (s2[i] != n – i – 1)
ex();
}
}

void mv0(int x) {
if (x == 0)
return;
vv.push_back(make_pair(1, x));
for (int i = c0 – x; i < c0; ++i)
s1[c1++] = s0[i], pl[s0[i]] = 1;
c0 -= x;
}
vector<int> vp;

int main() {
scanf(“%d%d%d”, &n, &a, &b);
for (int i = 0; i < n; ++i)
scanf(“%d”, s0 + i), –s0[i];
reverse(s0, s0 + n);
c0 = n;
c1 = 0;
c2 = 0;
for (int i = n – 1; i >= 0; –i) {
if (pl[i] == 2)
continue;
if (pl[i] == 1) {
for (int j = 0; j < c1; ++j) {
if (s1[j] == i) {
if (c1 – j > b)
ex();
vv.push_back(make_pair(2, c1 – j));
for (int k = j; k < c1; ++k)
s2[c2++] = s1[k], pl[s1[k]] = 2;;
c1 = j;
checks2();
break;
}
}
}
else {
int pos = -1;
for (int j = 0; j < c0; ++j)
if (s0[j] == i) {
pos = j;
break;
}
int fl = 1;
while (fl) {
fl = 0;
int lst = -1;
if (c1)
lst = s1[c1 – 1];
for (int j = pos + 1; j < c0; ++j) {
if (s0[j] == lst – 1) {
if (c0 – j > a)
ex();
vv.push_back(make_pair(1, c0 – j));
for (int k = j; k < c0; ++k)
s1[c1++] = s0[k], pl[s0[k]] = 1;
fl = 1;
c0 = j;
break;
}
}
}
int mx = -1;
for (int j = 0; j < c1; ++j)
mx = max(mx, s1[j]);
int mn = n;
for (int j = pos; j < c0; ++j)
mn = min(mn, s0[j]);
if (mn < mx)
ex();
for (int j = c0 – 1; j > pos; –j) {
if (j == pos + 1 || s0[j] < s0[j – 1] – 1) {
vp.clear();
for (int k = c0 – 1; k >= j; –k)
if (k == c0 – 1 || s0[k + 1] > s0[k])
vp.push_back(k);
vp.push_back(j – 1);
if ((int)vp.size() == 2) {
int nd = c0 – j;
while (nd) {
int go = min(min(a, b), nd);
mv0(go);
nd -= go;
}
continue;
}
if ((int)vp.size() == 3 && s0[vp[0]] – 1 == s0[j]) {
int len = c0 – j;
int l2 = vp[0] – vp[1];
if (l2 <= a && len – l2 <= a && len <= b) {
mv0(l2);
mv0(len – l2);
continue;
}
int fl = 0;
for (int k = 0; k < len; ++k) {
if (k <= a && (len – k) <= a && abs(k – l2) <= b && len – abs(k – l2) <= b) {
fl = 1;
mv0(k);
mv0(len – k);
break;
}
}
if (fl)
continue;
}
for (int k = 0; k < (int)vp.size() – 1; ++k) {
if (vp[k] – vp[k + 1] > a)
ex();
}
int fl = 0;
for (int k = 0; k < (int)vp.size() – 2; ++k) {
if (s0[vp[k]] – 1 != s0[vp[k + 2] + 1]) {
fl = 1;
break;
}
}
if (!fl && c0 – j <= b) {
for (int k = 0; k < (int)vp.size() – 1; ++k)
mv0(vp[k] – vp[k + 1]);
}
else {
if (c0 – j > a)
ex();
mv0(c0 – j);
}
}
}
mv0(1);
vv.push_back(make_pair(2, 1));
s2[c2++] = s1[c1 – 1];
pl[s1[c1 – 1]] = 2;
–c1;
}

}
cout << “YES\n”;
cout << vv.size() << “\n”;
for (int i = 0; i < (int)vv.size(); ++i) {
cout << vv[i].first << ” ” << vv[i].second << “\n”;
}
return 0;
}

8 killed, 95 injured...

Tragic Escalation: US-Israeli Strikes on Iran’s B1 Bridge Claim...

Singapore Airlines Direct Rtn...

Experience Luxury for Less: Singapore Airlines Announces Direct Return...

UWEC’s Bullert named scholar...

Ava Bullert: A Legacy of Academic and Athletic Excellence...

First Nations rehabilitation programs...

Improving First Nations Rehabilitation Programs: A Path Forward In Australia,...

CyberPower PC – 7800X3D,...

Score Big with the CyberPower PC RTX 5070 Ti...

North Korea’s Kim Jong...

Kim Jong Un Oversees Advanced Rocket Engine and Tank...

8 killed, 95 injured as US-Israeli strikes hit Iran’s B1 Bridge

Tragic Escalation: US-Israeli Strikes on Iran’s B1 Bridge Claim Eight Lives The geopolitical landscape of the Middle East has taken a somber and dangerous turn...

Singapore Airlines Direct Rtn to Singapore Ex PER $595, DRW $613, BNE $822 MEL $838, SYD $846, CNS $859 & More @ BTF

Experience Luxury for Less: Singapore Airlines Announces Direct Return Flights from Australia Travelers looking for a premium experience at an affordable price are in luck....

UWEC’s Bullert named scholar athlete of the year

Ava Bullert: A Legacy of Academic and Athletic Excellence at UW-Eau Claire In the highly competitive world of collegiate athletics, finding a player who perfectly...

First Nations rehabilitation programs aren’t keeping people out of prison. Here’s what would help

Improving First Nations Rehabilitation Programs: A Path Forward In Australia, the incarceration rates of First Nations people remain a critical issue that demands urgent systemic...

CyberPower PC – 7800X3D, RTX 5070 ti, 32GB, 2TB SSD, B850-VC Pro, Corsair RM850 watt, 2Year Premium Warranty +free Mech Keyboard and Resident Evil...

Score Big with the CyberPower PC RTX 5070 Ti Gaming Rig Deal Finding a high-end gaming PC that balances cutting-edge components with a reasonable price...

North Korea’s Kim Jong Un inspects solid-fuel rocket engine, new battle tank as Pyongyang steps up military development

Kim Jong Un Oversees Advanced Rocket Engine and Tank DevelopmentsIn a significant display of military modernization, North Korean leader Kim Jong Un has personally...

Aurangabad horror: Uncle locks 3 children inside room, slits their throats, attempts suicide

Shocking Incident in Aurangabad: Uncle Kills Three Children over Family Feud A horrific incident has sent shockwaves through the Aurangabad district of Bihar, where a...

ついに明かされる”ペニーワイズ”の起源―。「IT/イット ウェルカム・トゥ・デリー “それ”が見えたら、終わり。<シーズン1>」Blu-ray BOX 6/3(水)発売決定!

The Nightmare Returns: IT: Welcome to Derry Season 1 Blu-ray Release Details Fans of Stephen King’s legendary horror saga are preparing to return to the...

Will too much AI weaken troops’ judgement?

The Pentagon's AI Integration: A Double-Edged Sword for Military PersonnelThe United States Department of Defense is moving at an unprecedented pace to integrate Large...