이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "gift.h"
using namespace std;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef tuple<int,int,int> ti;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<ld,ld> pld;
#define pb push_back
#define popb pop_back()
#define pf push_front
#define popf pop_front
#define ff first
#define ss second
#define MOD (int)(1e8)
#define INF (ll) (1e18)
#define all(v) (v).begin(),(v).end()
#define LSOne(S) ((S) & -(S))
ll gcd(ll a , ll b) {return b ? gcd(b , a % b) : a ;}
ll lcm(ll a , ll b) {return (a * b) / gcd(a , b);}
ld pointdist(ld x, ld y, ld point) { return ((x-point)*(y-point))/abs(x-y); }
ld dist(ld x, ld y, ld a, ld b){ return sqrt((x-a)*(x-a) + (y-b)*(y-b)); }
const int nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0}; //East, West, South, North+
////////////******SOLUTION******\\\\\\\\\\\
int sz;
int req;
vi A, B, X;
vi p, rnk, setsz;
int findset(int i) {return p[i] == i? i: findset(p[i]);}
void unite(int i, int j)
{
if(findset(i) == findset(j))
return ;
int x= findset(i);
int y = findset(j);
if(rnk[x] > rnk[y]) swap(x,y);
p[x] = y;
if(rnk[x] == rnk[y]) rnk[y]++;
setsz[y] += setsz[x];
}
int construct(int n, int r, vi a, vi b, vi x)
{
string s(n, 'R');
bool all_one = true;
bool all_two = true;
for(int i = 0; i <r; i ++)
{
if(x[i] != 1)
all_one = false;
if(x[i] != 2)
all_two = false;
}
if(all_one)
{
craft(s);
return 1;
}
else if(all_two)
{
for(int i = 0; i <r; i ++)
{
if(a[i] == b[i] || n == 1)
return 0;
}
for(int j = 1; j < n; j += 2)
s[j] = 'B';
craft(s);
return 1;
}
else
{
vector<pair<pii,int>> query;
for(int i = 0; i <r; i ++)
query.pb({{a[i],b[i]},x[i]});
sort(all(query));
vector<pii> comps;
for(int j = 0; j < r; j ++)
{
if(query[j].ss == 2)
continue;
if(query[j].ff.ff > comps[comps.size()-1].ss)
{
comps.pb({query[j].ff.ff,query[j].ff.ss});
}
else
comps[comps.size()-1].ss = max(comps[comps.size()-1].ss,query[j].ff.ss);
}
string ans(n,'X');
char c = 'R';
for(int k = 0; k < comps.size(); k ++)
{
for(int j= comps[k].ff; j <= comps[k].ss; j ++)
ans[j] = c;
c = c == 'R'? 'B': 'R';
for(int u = comps[k].ss + 1; u < k == comps.size()-1? n: comps[k+1].ff; u ++)
{
ans[u] = c;
c = c == 'R'? 'B': 'R';
}
}
int sz = n;
int pref[n+1];
pref[0] = 0;
for(int i = 0; i < sz; i ++)
{
pref[i+1] = pref[i] + (ans[i] == 'R');
}
for(int j = 0; j < req; j ++)
{
int rs = pref[b[j]+1] - pref[a[j]];
int len = b[j] + 1 - a[j];
if(x[j] == 1)
{
if(rs == len || rs == 0)
continue;
else
return 0;
}
else
{
if(rs < len && rs > 0)
continue;
else
return 0;
}
}
craft(ans);
return 1;
}
return 0;
}
/*
Identify problem diagram: Brute force, Greedy, Dynamic Programming, Divide and Conquer
Reformulate the problem into something more theoretical
!!!!! IMPLICIT GRAPH ??????
!!!!! PAY ATTENTION TO THE CONSTRAINTS: DP nD ? BF ? BITMASKING ?
!!!!! SOLVE THE SUBTASKS FIRST: IT'S TOTALLY OK NOT TO SOLVE THE PROBLEM ENTIRELY
Search for multiple approaches: select the seemingly optimal one
Remember that you're the king of CP
Change your approach
Imagine Corner cases before submitting
Don't spend too much time on the problem: move on !
*/
컴파일 시 표준 에러 (stderr) 메시지
gift.cpp:28:1: warning: multi-line comment [-Wcomment]
28 | ////////////******SOLUTION******\\\\\\\\\\\
| ^
gift.cpp: In function 'int construct(int, int, vi, vi, vi)':
gift.cpp:95:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | for(int k = 0; k < comps.size(); k ++)
| ~~^~~~~~~~~~~~~~
gift.cpp:100:44: warning: suggest parentheses around comparison in operand of '==' [-Wparentheses]
100 | for(int u = comps[k].ss + 1; u < k == comps.size()-1? n: comps[k+1].ff; u ++)
| ~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |