HHHOJ周赛01

A. 「JOISC 2014」巴士走读

考虑预处理出第 $t$ 时刻到 $x$ 从起点出发的最大时刻,用 $f[x][t]$ 表示。

记一条边 $(x,y,a,b)$ 表示从 $x$ 到 $y$ ,第 $a$ 时刻开向 $b$ 时刻。让所有边按照 $b$ 升序排序。

对于一条边,找到从上一条边转移过来的最大时刻。

显然,如果 $a<b,f[x][a]>f[x][b]$ ,那么 $b$ 是没有意义的。

我们要对每个结点维护一个序列,二分查找,和从后面加入数。用 vector 解决。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define maxn 300005
#define put() putchar('\n')
#define Tp template<typename Ty>
#define Ts template<typename Ty,typename... Ar>
using namespace std;
inline void read(int &x){
int f=1;x=0;char c=getchar();
while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
while (c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}
x*=f;
}
namespace Debug{
Tp void _debug(char* f,Ty t){cerr<<f<<'='<<t<<endl;}
Ts void _debug(char* f,Ty x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);}
Tp ostream& operator<<(ostream& os,vector<Ty>& V){os<<"[";for(auto& vv:V) os<<vv<<",";os<<"]";return os;}
#define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__)
}using namespace Debug;
struct yyy{
int x,y,a,b;
}a[maxn];
inline bool cmp(yyy x,yyy y) {return x.b<y.b;}
vector<int>ans1[maxn],ans2[maxn];
int n,m;
signed main(void){
int i,x,y,tmp1,tmp2,tmp;
read(n);read(m);
for (i=1;i<=m;i++) read(a[i].x),read(a[i].y),read(a[i].a),read(a[i].b);
sort(a+1,a+1+m,cmp);
for (i=1;i<=n;i++) ans1[i].push_back(-1),ans2[i].push_back(-1);
for (i=1;i<=m;i++) {
x=a[i].x,y=a[i].y,tmp1=a[i].a,tmp2=a[i].b;
if (x==1) tmp=tmp1;
else tmp=ans2[x][upper_bound(ans1[x].begin(),ans1[x].end(),tmp1)-ans1[x].begin()-1];
if (tmp>ans2[y][ans2[y].size()-1]) ans2[y].push_back(tmp),ans1[y].push_back(tmp2);
}
int Q;
read(Q);
while (Q--) {
read(x);
int id=upper_bound(ans1[n].begin(),ans1[n].end(),x)-ans1[n].begin()-1;
if (id==0) puts("-1");
else printf("%d\n",ans2[n][id]);
}
return 0;
}

B.「JOISC 2014 Day1」有趣的家庭菜园

有一个非常愚蠢的想法。枚举最后最大值所在的位置,分别正反做两遍逆序对加起来求最小。这样是不对的。

考虑贪心。将 $h$ 升序排序。每次将在 $h$ 前面的个数和在 $h$ 后面的个数取最小值。表示放左边或者放右边产生的贡献。

$h$ 相同的情况特殊处理。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<bits/stdc++.h>
#define int long long
#define ull unsigned long long
#define maxn 300005
#define put() putchar('\n')
#define Tp template<typename Ty>
#define Ts template<typename Ty,typename... Ar>
using namespace std;
inline void read(int &x){
int f=1;x=0;char c=getchar();
while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
while (c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}
x*=f;
}
namespace Debug{
Tp void _debug(char* f,Ty t){cerr<<f<<'='<<t<<endl;}
Ts void _debug(char* f,Ty x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);}
Tp ostream& operator<<(ostream& os,vector<Ty>& V){os<<"[";for(auto& vv:V) os<<vv<<",";os<<"]";return os;}
#define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__)
}using namespace Debug;
int n,cnt,f[maxn],g[maxn],ff[maxn],ans=1e18,a[maxn],b[maxn];
struct BIT{
#define lowbit(x) ((x)&(-x))
int f[maxn];
inline void add(int x,int y) {for (;x<=n;x+=lowbit(x)) f[x]+=y;}
inline int query(int x) {int sum=0;for (;x;x-=lowbit(x)) sum+=f[x];return sum;}
}t;
vector<int>O[maxn];
signed main(void){
int i,j,x;
read(n);
for (i=1;i<=n;i++) read(a[i]),g[++cnt]=a[i];
sort(g+1,g+1+cnt);
cnt=unique(g+1,g+1+cnt)-g-1;
for (i=1;i<=n;i++) a[i]=lower_bound(g+1,g+1+cnt,a[i])-g,O[a[i]].push_back(i);
int nums=0,ans=0;
for (i=cnt;i>=1;i--) {
int len=O[i].size();
for (j=0;j<len;j++) {

x=t.query(O[i][j]);
ans+=min(x,nums-x);
}
for (j=0;j<len;j++) t.add(O[i][j],1),nums++;
}
// for (i=1;i<=n;i++) {
// f[i]=f[i-1]+(i-1)-t.query(a[i]);
// t.add(a[i],1);
// }
// memset(t.f,0,sizeof(t.f));
// for (i=1;i<=n;i++) b[n-i+1]=a[i];
// for (i=1;i<=n;i++) {
// g[i]=g[i-1]+(i-1)-t.query(b[i]);
// t.add(b[i],1);
// }
// for (i=1;i<=n;i++) ans=min(ans,f[i-1]+g[n-i+1]);
printf("%lld\n",ans);
return 0;
}

C. 「JOISC 2014」历史研究

回滚莫队经典题。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<bits/stdc++.h>
#define maxn 100005
#define int long long
#define put() putchar('\n')
using namespace std;
inline void read(int &x){
int f=1;x=0;char c=getchar();
while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
while (c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}
x*=f;
}
int n,m,block;
int t[maxn],a[maxn],g[maxn],w[maxn];
struct yyy{
int l,r,id,bl;
}q[maxn];
inline bool cmp(yyy x,yyy y){
if (x.bl==y.bl) return x.r<y.r;
return x.bl<y.bl;
}
int stac[maxn],tot,ans,Ans[maxn];
inline void add(int x) {
t[a[x]]++;ans=max(ans,t[a[x]]*w[x]);
}
signed main(void){
int l=1,r=0,last_block=0,ql,qr,j,i;
read(n);read(m);block=n/sqrt(m)+1;
for (i=1;i<=n;i++) read(a[i]),g[i]=w[i]=a[i];
sort(g+1,g+1+n);int tot=unique(g+1,g+1+n)-g-1;
for (i=1;i<=n;i++) a[i]=lower_bound(g+1,g+1+tot,a[i])-g-1;
for (i=1;i<=m;i++) {
read(q[i].l);read(q[i].r);
q[i].bl=(q[i].l-1)/block+1;
q[i].id=i;
}
sort(q+1,q+1+m,cmp);
for (i=1;i<=m;i++) {
ql=q[i].l,qr=q[i].r;
if (last_block!=q[i].bl){
l=q[i].bl*block+1;r=l-1;
for (j=0;j<=n;j++) t[j]=0;
tot=0;ans=0;last_block=q[i].bl;
}//printf("%lld %lld %lld %lld %lld\n",ans,l,r,ql,qr);
if ((q[i].l-1)/block==(q[i].r-1)/block) {
for (j=q[i].l;j<=q[i].r;j++) t[a[j]]++;
for (j=q[i].l;j<=q[i].r;j++) ans=max(ans,t[a[j]]*w[j]);
for (j=q[i].l;j<=q[i].r;j++) t[a[j]]=0;
Ans[q[i].id]=ans;ans=0;
}
else {
while (r<qr) add(++r);
while (l>ql) stac[++tot]=ans,add(--l);
Ans[q[i].id]=ans;
while (tot) {
ans=stac[tot];
t[a[l]]--;l++;tot--;
}
}
}
for (i=1;i<=m;i++) printf("%lld\n",Ans[i]);
return 0;
}

D. 「JOISC 2014」水壶

bfs 建最小生成树,查询 lca 跑路径之间最大值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define maxn 200005
#define maxm 4005
#define put() putchar('\n')
#define Tp template<typename Ty>
#define Ts template<typename Ty,typename... Ar>
using namespace std;
inline void read(int &x){
int f=1;x=0;char c=getchar();
while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
while (c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}
x*=f;
}
namespace Debug{
Tp void _debug(char* f,Ty t){cerr<<f<<'='<<t<<endl;}
Ts void _debug(char* f,Ty x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);}
Tp ostream& operator<<(ostream& os,vector<Ty>& V){os<<"[";for(auto& vv:V) os<<vv<<",";os<<"]";return os;}
#define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__)
}using namespace Debug;
int a[maxm][maxm],n,m,p,T;
int fa[maxn][21],Max[maxn][21],lg[maxn],deep[maxn];
struct zyq{
int to,w;
zyq(int a=0,int b=0) {to=a;w=b;}
};
int ecnt;
vector<zyq>to[200005];
struct node{
int x,y,id;
node (int a=0,int b=0,int c=0) {
x=a;y=b;id=c;
}
}g[maxn];
vector<node>e[4000005];
inline void dfs(int x,int pre,int w) {
int i;deep[x]=deep[pre]+1;fa[x][0]=pre;Max[x][0]=w;
for (i=1;i<=20;i++) {
fa[x][i]=fa[fa[x][i-1]][i-1];
Max[x][i]=max(Max[x][i-1],Max[fa[x][i-1]][i-1]);
}
for (auto tmp:to[x]) if (tmp.to^pre) dfs(tmp.to,x,tmp.w);
}
inline int query(int x,int y) {
int ans=0,i;
if (deep[x]<deep[y]) swap(x,y);
while (deep[x]>deep[y]) ans=max(ans,Max[x][lg[deep[x]-deep[y]]]),x=fa[x][lg[deep[x]-deep[y]]];
if (x==y) return ans;
for (i=lg[deep[x]];i>=0;i--) if (fa[x][i]^fa[y][i]) {
ans=max(ans,max(Max[x][i],Max[y][i]));
x=fa[x][i],y=fa[y][i];
}
return max(ans,max(Max[x][0],Max[y][0]));
}
int fx[5]={0,1,-1,0,0},fy[5]={0,0,0,1,-1};
int vis[maxm][maxm],dis[maxm][maxm];
int father[maxn];
inline int getfa(int x) {return x==father[x]?x:father[x]=getfa(father[x]);}
inline bool cmp(node x,node y) {return x.id<y.id;}
queue<node>q;
signed main(void){
int i,j,k,xx,yy,x,y,z;char ch;
read(n);read(m);read(p);read(T);
for (i=1;i<=n;i++) {
ch=getchar();while (ch!='.'&&ch!='#') ch=getchar();
for (j=1;j<=m;j++) a[i][j]=(ch=='#'),ch=getchar();
}
for (i=2;i<=p;i++) lg[i]=lg[i/2]+1;
for (i=1;i<=p;i++) read(g[i].x),read(g[i].y),g[i].id=i,q.push(node(g[i].x,g[i].y,i)),vis[g[i].x][g[i].y]=i;
for (i=1;i<=p;i++) father[i]=i;
while (!q.empty()) {
auto tmp=q.front();q.pop();
// if (dis[tmp.x][tmp.y]>n+m) continue;
for (i=1;i<=4;i++) {
xx=tmp.x+fx[i];yy=tmp.y+fy[i];
if (xx>=1&&xx<=n&&yy>=1&&yy<=m&&!a[xx][yy]) {
if (vis[xx][yy]) {
x=tmp.id,y=vis[xx][yy];
// if (dis[xx][yy]+dis[tmp.x][tmp.y]>4000000) printf("%d %d\n",dis[xx][yy],dis[tmp.x][tmp.y]);
assert(dis[xx][yy]+dis[tmp.x][tmp.y]<=4000000);
e[dis[xx][yy]+dis[tmp.x][tmp.y]].push_back(node(x,y));
}
else vis[xx][yy]=tmp.id,dis[xx][yy]=dis[tmp.x][tmp.y]+1,q.push(node(xx,yy,tmp.id));
}
}
}
for (i=0;i<=n*m;i++) {
for (auto tmp:e[i]) {
x=tmp.x,y=tmp.y,z=i;
xx=getfa(x),yy=getfa(y);
if (xx^yy) {
to[x].push_back(zyq(y,z));
to[y].push_back(zyq(x,z));
father[xx]=yy;
}
}
}
for (i=1;i<=p;i++) if (father[i]==i) dfs(i,0,0);
while (T--) {
read(x);read(y);
if (getfa(x)!=getfa(y)) puts("-1");
else printf("%d\n",query(x,y));
}
return 0;
}

E. 「JOISC 2014」邮戳拉力赛

目前还不太会

F. 「JOISC 2014」稻草人

先按 $x$ 排序后,考虑分治。

分治的意义在于我们每次只需要经过中间的区间,也就是在左边的点集和右边的点集分别找一个,使区间合法。

考虑一个区间 $[L,R]$ 其分为两个区间 $[L,M],[M+1,R]$,两个区间内的点分别按 $y$ 降序排序。

枚举左区间内的点,计算过程是显然的。(不画图感觉很难简单的解释,但是没有数位板比较懒)分别用两个单调栈维护。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define maxn 200005
#define put() putchar('\n')
#define Tp template<typename Ty>
#define Ts template<typename Ty,typename... Ar>
using namespace std;
inline void read(int &x){
int f=1;x=0;char c=getchar();
while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
while (c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}
x*=f;
}
namespace Debug{
Tp void _debug(char* f,Ty t){cerr<<f<<'='<<t<<endl;}
Ts void _debug(char* f,Ty x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);}
Tp ostream& operator<<(ostream& os,vector<Ty>& V){os<<"[";for(auto& vv:V) os<<vv<<",";os<<"]";return os;}
#define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__)
}using namespace Debug;
struct node{
int x,y;
}a[maxn],b[maxn];
ll ans;
int n;
inline bool cmp(node x,node y) {return x.x<y.x;}
inline bool cmp2(node x,node y) {return x.y>y.y;}
int stac1[maxn],tot1,stac2[maxn],tot2,s1[maxn],s2[maxn];
inline void add1(int x,int y) {
while (tot1&&stac1[tot1]<=x) tot1--;
stac1[++tot1]=x;
s1[tot1]=y;
}
inline void add2(int x,int y) {
while (tot2&&stac2[tot2]>=x) tot2--;
stac2[++tot2]=x;
s2[tot2]=y;
}
inline void solve(int L,int R) {
if (L==R) return ;
int mid=L+R>>1,i;

for (i=L;i<=R;i++) b[i]=a[i];
sort(b+L,b+1+mid,cmp2);
sort(b+mid+1,b+R+1,cmp2);
int it1,it2,now=mid;
s1[0]=1e9;tot1=tot2=0;
for (i=L;i<=mid;i++) {
while (now<R&&b[now+1].y>=b[i].y) now++,add2(b[now].x,b[now].y);
add1(b[i].x,b[i].y);
it1=lower_bound(s2+1,s2+1+tot2,s1[tot1-1],[](int x,int y){return x>y;})-s2;
it2=lower_bound(s2+1,s2+1+tot2,s1[tot1],[](int x,int y){return x>y;})-s2;
ans+=it2-it1;
// gdb(i,now,it1,it2,stac1[tot1-1],stac1[tot1]);
}
solve(L,mid);
solve(mid+1,R);
}
signed main(void){
freopen("1.in","r",stdin);
// int c[3]={3,2,1};
// cout<<lower_bound(c,c+3,0,[](int x,int y){return x>y;})-c<<'\n';
int i;
read(n);
for (i=1;i<=n;i++) read(a[i].x),read(a[i].y);
sort(a+1,a+1+n,cmp);
solve(1,n);
printf("%lld\n",ans);
return 0;
}

G.「JOISC 2014」电压

bzoj4238

二分图。我好像写过题解了。

H.「JOISC 2014」挂饰

看上去是个平凡的背包题。

实际上也是。为了避免背包出现没有挂钩的情况。先将物品按挂钩个数从大到小排序。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<bits/stdc++.h>
#define int long long
#define ull unsigned long long
#define maxn 2005
#define put() putchar('\n')
#define Tp template<typename Ty>
#define Ts template<typename Ty,typename... Ar>
using namespace std;
inline void read(int &x){
int f=1;x=0;char c=getchar();
while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
while (c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}
x*=f;
}
namespace Debug{
Tp void _debug(char* f,Ty t){cerr<<f<<'='<<t<<endl;}
Ts void _debug(char* f,Ty x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);}
Tp ostream& operator<<(ostream& os,vector<Ty>& V){os<<"[";for(auto& vv:V) os<<vv<<",";os<<"]";return os;}
#define gdb(...) _debug(#__VA_ARGS__,__VA_ARGS__)
}using namespace Debug;
struct node{
int x,y;
}a[maxn];
int n;
int f[maxn][maxn],ans;
inline bool cmp(node x,node y) {return x.x>y.x;}
signed main(void){
// freopen("1.in","r",stdin);
int i,j;
memset(f,-0x3f,sizeof(f));
read(n);
for (i=1;i<=n;i++) read(a[i].x),read(a[i].y);
sort(a+1,a+1+n,cmp);
f[0][1]=0;
for (i=1;i<=n;i++) {
for (j=0;j<=n;j++) f[i][j]=max(f[i-1][max(0ll,j-a[i].x)+1]+a[i].y,f[i-1][j]);
}
for (j=0;j<=n;j++) ans=max(ans,f[n][j]);
printf("%lld",ans);
return 0;
}