# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
369163 | Alma | Handcrafted Gift (IOI20_gift) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include "gift.h"
using namespace std;
int construct(int n, int r, vector<int>& a, vector<int>& b, vector<int>& x) {
vector<pair<int, pair<int, int>>> requirements (r);
for (int i = 0; i < r; i++) {
requirements[i] = make_pair(x[i], make_pair(a[i], b[i]));
}
sort(requirements.begin(), requirements.end());
vector<int> necklace (n, -1); // blue = 0, red = 1
bool isPossible = true;
for (int i = 0; i < r; i++) {
if (!isPossible) break;
int colour = requirements[i].first;
int start = requirements[i].second.first;
int end = requirements[i].second.second;
if (colour == 1) {
if (necklace[start] == 0) {
start++;
while (start <= end) {
if (necklace[start] == 1) {
isPossible = false;
break;
}
necklace[start] = 0;
start++;
}
} else { // necklace[start] == 1 or -1 (no colour)
while (start <= end) {
if (necklace[start] == 0) {
isPossible = false;
break;
}
necklace[start] = 1;
start++;
}
}
} else { // colour = 2
int numBeads = end - start + 1;
while (start <= end) {
if (necklace[start] == 0)
numBeads--;
else if (necklace[start] == 1)
numBeads++;
start++;
}
if (numBeads == 0 || numBeads == (end - start) * 2)
isPossible = false;
}
}
string s (n, ' ');
char prevColour = 'B';
for (int i = 0; i < n; i++) {
if (necklace[i] == 0) {
s[i] = 'B';
prevColour = 'B';
} else if (necklace[i] == 1) {
s[i] = 'R';
prevColour = 'R';
} else { // necklace[i] == -1
if (prevColour == 'B') s[i] = 'R';
else s[i] = 'B';
prevColour = s[i];
}
}
craft(s);
}