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\n", 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大的且长度最小的桥是最优的。证明如下:1.设第l[i] > l[i+1] r[i] < r[i+1],那么先满足第i个桥的条件必然是最优的。2.l[i] < l[i+1] r[i] < r[i+1],画个图就知道找比l[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\n");
else {
printf("Yes\n");
for(int i = 1; i < n; i++) printf("%d%c", ans[i], i == n - 1 ? '\n' : ' ');
}
}
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\n");
continue;
}
s.insert(mp(x[i], i));
if(ss[0] == 'U') {
printf("%d\n", y[i] - y[it->second]);
y[i] = y[it->second];
}
else {
printf("%d\n", 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\n", b[pos].second);
return;
}
if(dir == 1 && a[pos] - x > a[pos-1]) {
printf("%d\n", 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\n");
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\n", 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\n", ok ? "Yes" : "No");
}
int main()
{
while(scanf("%d%d%d", &n, &m, &q)!=EOF) {
init();
work();
}
return 0;
}