Codeforces Round #310(Div. 1) 전체 문제 해결

13057 단어 codeforces
A문제: 제목의 뜻을 이해한 후 1부터 연속되는 길이만 찾으면 된다. 다른 아기들은 모두 뜯어야 한다.
#include <iostream>
#include <queue> 
#include <stack> 
#include <map> 
#include <set> 
#include <bitset> 
#include <cstdio> 
#include <algorithm> 
#include <cstring> 
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 100005
#define maxm 2000005
#define eps 1e-7
#define mod 1000000007
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid 
#define rson o<<1 | 1, mid+1, R
#define pii pair<int, int>
#pragma comment(linker, "/STACK:16777216")
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
// head

int a[maxn];
int n, m;

void work()
{
	int k, t2 = 0;
	int ans = 0, t = 1;
	for(int i = 1; i <= m; i++) {
		scanf("%d", &k);
		for(int j = 1; j <= k; j++) scanf("%d", &a[j]);
		if(a[1] == 1) {
			for(int j = 2; j <= k; j++) {
				if(a[j] == a[j-1] + 1) t++;
				else {
					t2 = 1;
					break;
				}
			}
		}
	}
	ans = (n - t) - (m - 1) + (n - t);

	printf("%d
", ans); } int main() { while(scanf("%d%d", &n, &m)!=EOF) { work(); } return 0; <span style="font-size:24px;">}</span>

B:욕심이 많아서 n-1개의 다리의 길이를 미리 처리하고 각 다리의 구간을 [l,r]로 설정하면 l보다 크고 길이가 가장 작은 다리를 찾는 것이 가장 좋다는 것을 증명할 수 있다.증명은 다음과 같다.l[i]>l[i+1]r[i]#include <iostream> #include <queue> #include <stack> #include <map> #include <set> #include <bitset> #include <cstdio> #include <algorithm> #include <cstring> #include <climits> #include <cstdlib> #include <cmath> #include <time.h> #define maxn 200005 #define maxm 2000005 #define eps 1e-7 #define mod 1000000007 #define INF 0x3f3f3f3f #define PI (acos(-1.0)) #define lowbit(x) (x&(-x)) #define mp make_pair #define ls o<<1 #define rs o<<1 | 1 #define lson o<<1, L, mid #define rson o<<1 | 1, mid+1, R #define pii pair<int, int> #pragma comment(linker, "/STACK:16777216") typedef long long LL; typedef unsigned long long ULL; //typedef int LL; using namespace std; LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;} LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;} // head struct node { LL l, r; int id; }p[maxn]; set<pair<LL, int> > s; set<pair<LL, int> >::iterator it; LL l[maxn]; LL r[maxn]; int ans[maxn]; int n, m; int cmp(node a, node b) { if(a.r == b.r) return a.l > b.l; return a.r < b.r; } void work() { LL x; for(int i = 1; i <= n; i++) scanf("%I64d%I64d", &l[i], &r[i]); for(int i = 1; i <= m; i++) { scanf("%I64d", &x); s.insert(mp(x, i)); } for(int i = 1; i < n; i++) { p[i].l = l[i+1] - r[i]; p[i].r = r[i+1] - l[i]; p[i].id = i; } int ok = 1; sort(p+1, p+n, cmp); for(int i = 1; i < n; i++) { it = s.lower_bound(mp(p[i].l, 0)); if(it == s.end()) { ok = 0; break; } if(it->first > p[i].r) { ok = 0; break; } ans[p[i].id] = it->second; s.erase(it); } if(!ok) printf("No
"); else { printf("Yes
"); for(int i = 1; i < n; i++) printf("%d%c", ans[i], i == n - 1 ? '
' : ' '); } } int main() { while(scanf("%d%d", &n, &m)!=EOF) { work(); } return 0; }

C문제: 세그먼트 트리는 할 수 있다. 물론 set으로 해도 된다. U는 x가 그보다 큰 것을 찾고 L은 x가 그보다 작은 것을 찾는다.
#include <iostream>
#include <queue> 
#include <stack> 
#include <map> 
#include <set> 
#include <bitset> 
#include <cstdio> 
#include <algorithm> 
#include <cstring> 
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 200005
#define maxm 2000005
#define eps 1e-7
#define mod 1000000007
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid 
#define rson o<<1 | 1, mid+1, R
#define pii pair<int, int>
#pragma comment(linker, "/STACK:16777216")
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
// head

set<pii> s;
set<pii>::iterator it;
int x[maxn];
int y[maxn];
char ss[maxn];
int n, m;

void work()
{
	x[0] = y[0] = 0;
	s.insert(mp(0, m+1));
	s.insert(mp(n+1, m+1));
	for(int i = 1; i <= m; i++) {
		scanf("%d%d%s", &x[i], &y[i], ss);
		if(ss[0] == 'U') it = s.lower_bound(mp(x[i], -1));
		else {
			it = s.upper_bound(mp(x[i], m+1));
			it--;
		}
		if(it->first == x[i]) {
			printf("0
"); continue; } s.insert(mp(x[i], i)); if(ss[0] == 'U') { printf("%d
", y[i] - y[it->second]); y[i] = y[it->second]; } else { printf("%d
", x[i] - x[it->second]); x[i] = x[it->second]; } } } int main() { while(scanf("%d%d", &n, &m)!=EOF) { work(); } return 0; }

D문제: 어려운 수학 문제는 아니지만 상황이 많습니다.기본 사상은 매번 남은 선이 도달할 수 있는 가장 먼 곳을 찾아내고 매번 이렇게 하는 것이다.찾기는 이분으로 찾고, 순환이 있으면 모형을 취한다.
#include <iostream>
#include <queue> 
#include <stack> 
#include <map> 
#include <set> 
#include <bitset> 
#include <cstdio> 
#include <algorithm> 
#include <cstring> 
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 200005
#define maxm 2000005
#define eps 1e-7
#define mod 1000000007
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid 
#define rson o<<1 | 1, mid+1, R
#define pii pair<int, int>
#pragma comment(linker, "/STACK:16777216")
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
// head

map<int, int> mpp;
pii b[maxn];
int a[maxn];
int n, m;

void calc(int pos, int x, int dir)
{
	if(dir == 0 && a[pos] + x < a[pos+1]) {
		printf("%d
", b[pos].second); return; } if(dir == 1 && a[pos] - x > a[pos-1]) { printf("%d
", b[pos].second); return; } if(dir == 0) { int t, nxt; if(a[pos] + x >= a[n]) { t = a[n] - a[pos]; nxt = n; } else { nxt = upper_bound(a+1, a+n+1, a[pos]+x) - a - 1; t = a[nxt] - a[pos]; } int tt = x / t; if(tt % 2 == 0) calc(pos, x - tt * t, 0); else calc(nxt, x - tt * t, 1); } else { int t, nxt; if(a[pos] - x <= a[1]) { t = a[pos] - a[1]; nxt = 1; } else { nxt = lower_bound(a+1, a+n+1, a[pos]-x) - a; t = a[pos] - a[nxt]; } int tt = x / t; if(tt % 2 == 0) calc(pos, x - tt * t, 1); else calc(nxt, x - tt * t, 0); } } void work() { int t, x; for(int i = 1; i <= n; i++) scanf("%d", &a[i]), b[i] = mp(a[i], i); sort(b+1, b+n+1); sort(a+1, a+n+1); for(int i = 1; i <= n; i++) mpp[b[i].second] = i; while(m--) { scanf("%d%d", &t, &x); t = mpp[t]; if(n == 1) { printf("1
"); continue; } if(a[t] + x >= a[n]) calc(n, x - (a[n] - a[t]), 1); else { int nxt = upper_bound(a+1, a+n+1, a[t]+x) - a - 1; if(nxt == t && nxt == 1) { printf("%d
", b[1].second); continue; } t = a[nxt] - a[t]; calc(nxt, x - t, 1); } } } int main() { while(scanf("%d%d", &n, &m)!=EOF) { work(); } return 0; }

E문제: 먼저 변쌍연통의 축소점을 사용한다. 왜냐하면 한 변쌍연통분량에 대해 그 중의 변의 방향을 바꾸면 반드시 이 쌍연통을 강연통분량으로 바꿀 수 있기 때문이다.그러면 이변쌍련통은 이 문제에 완전히 부합된다.그래서 우리는 좀 줄여야 한다.축소점 이후 모든 연결 블록에 대해 하나의 나무로 각 나무의lca를 미리 처리한다.모든 s, t, s에서 t까지의 방향은 반드시 확정적이다.즉 나무 위의 점 하나하나가 나가는 방향이 확실하다는 것이다.up,down으로 방향을 기록하고, 모든 문의는 표기법으로 처리합니다.마지막으로 모든 트리 dfs에 대해 점 up과down이 값이 있다면 이것은 합법적이지 않습니다.
#include <iostream>
#include <queue> 
#include <stack> 
#include <map> 
#include <set> 
#include <bitset> 
#include <cstdio> 
#include <algorithm> 
#include <cstring> 
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 200005
#define maxm 400005
#define eps 1e-7
#define mod 1000000007
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid 
#define rson o<<1 | 1, mid+1, R
#define pii pair<int, int>
#pragma comment(linker, "/STACK:16777216")
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
// head

struct Edge
{
	int v, next;
	Edge(int v = 0, int next = 0) : v(v), next(next) {} 
}E[maxm << 1];

const int M = 20;

stack<int> s;
pii tpair[maxn];
int H[maxn], cntE, h[maxn];
bool used[maxn];
int bcc[maxn];
int block[maxn];
int dep[maxn];
int dfn[maxn];
int vis[maxn];
int low[maxn];
int anc[maxn][M];
int up[maxn];
int down[maxn];
int n, m, q, ok, dfs_clock, T, bcc_cnt;

void addedges(int u, int v, int *HH)
{
	E[cntE] = Edge(v, HH[u]);
	HH[u] = cntE++;
}

void init()
{
	T = 0;
	dfs_clock = cntE = bcc_cnt = 0;
	memset(H, -1, sizeof H);
	memset(h, -1, sizeof h);
	memset(vis, 0, sizeof vis);
	memset(dfn, 0, sizeof dfn);
	memset(used, 0, sizeof used);
}

void tarjan(int u)
{
	dfn[u] = low[u] = ++dfs_clock;
	s.push(u), block[u] = T;
	for(int e = H[u]; ~e; e = E[e].next) if(!used[e / 2]) {
		used[e / 2] =true;
		int v = E[e].v;
		if(!dfn[v]) {
			tarjan(v);
			low[u] = min(low[u], low[v]);
		}
		else low[u] = min(low[u], dfn[v]);
	}
	if(low[u] == dfn[u]) {
		int t;
		bcc_cnt++;
		do {
			t = s.top();
			bcc[t] = bcc_cnt;
			s.pop();
		}while(t != u);
	}
}

void dfs(int u, int fa)
{
	vis[u] = true;
	anc[u][0] = fa;
	for(int e = h[u]; ~e; e = E[e].next) {
		int v = E[e].v;
		if(vis[v]) continue;
		dep[v] = dep[u] + 1;
		dfs(v, u);
	}
}

void DFS(int u)
{
	vis[u] = true;
	for(int e = h[u]; ~e; e = E[e].next) {
		int v = E[e].v;
		if(vis[v]) continue;
		DFS(v);
		up[u] += up[v];
		down[u] += down[v];
	}
	if(up[u] && down[u]) ok = 0;
}

int to(int u, int d)
{
	for(int i = M - 1; i >= 0; i--) if(dep[anc[u][i]] >= d) u = anc[u][i];
	return u;
}

int lca(int u, int v)
{
	if(dep[u] < dep[v]) swap(u, v);
	u = to(u, dep[v]);
	for(int i = M - 1; i >= 0; i--) if(anc[u][i] != anc[v][i]) u = anc[u][i], v = anc[v][i];
	return u == v ? u : anc[u][0];
}

void work()
{
	int u, v;
	for(int i = 1; i <= m; i++) {
		scanf("%d%d", &u, &v);
		tpair[i] = mp(u, v);
		addedges(u, v, H);
		addedges(v, u, H); 
	}
	for(int i = 1; i <= n; i++) if(!dfn[i]) {
		T++;
		tarjan(i);
	}

	for(int i = 1; i <= m; i++) {
		u = bcc[tpair[i].first];
		v = bcc[tpair[i].second];
		if(u == v) continue;
		addedges(u, v, h);
		addedges(v, u, h);
	}
	
	for(int i = 1; i <= bcc_cnt; i++) if(!vis[i]) dfs(i, i);
	for(int i = 1; i < M; i++)
		for(int j = 1; j <= n; j++)
			anc[j][i] = anc[anc[j][i-1]][i-1];
	ok = 1;
	for(int i = 1; i <= q; i++) {
		scanf("%d%d", &u, &v);
		if(block[u] != block[v]) ok = 0;
		u = bcc[u];
		v = bcc[v];
		int c = lca(u, v);
		up[u]++;
		up[c]--;
		down[v]++;
		down[c]--;
	}
	memset(vis, 0, sizeof vis);
	for(int i = 1; i <= bcc_cnt; i++) if(!vis[i]) DFS(i);
	printf("%s
", ok ? "Yes" : "No"); } int main() { while(scanf("%d%d%d", &n, &m, &q)!=EOF) { init(); work(); } return 0; }

좋은 웹페이지 즐겨찾기