「解题报告」NIKKEI Prog. Contest 2019-2

又是签到走人的一天……

A. Sum of Two Integers

Source

签到题。

1
2
3
4
5
6
7
8
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
int n; scanf("%d",&n);
if(n % 2) n += 1; n /= 2; printf("%d",n - 1);
return 0;
}

B. Counting of Trees

Source

签到题。考虑每一层的方案数,直接乘法原理就好了。

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
#include<iostream>
#include<cstdio>
using namespace std;
#define LL long long
const int CN = 1e5+5;
const LL R = 998244353;
int n,fir; LL s[CN];
LL qpow(LL a,LL b){
LL rec = 1;
while(b){
if(b & 1) (rec *= a) %= R;
(a *= a) %= R; b >>= 1;
}
return rec;
}
int main(){
scanf("%d%d",&n,&fir);
for(int i=2;i<=n;i++){
int x; scanf("%d",&x);
if(!x) {fir = 1; break;}
s[x]++;
}

for(int i=1;i<=n;i++) if(s[i]) s[0] = i;
for(int i=1;i<=s[0];i++) if(!s[i]) fir = 1;

if(fir) printf("0");
else{
LL ans = 1;
for(int i=2;i<=s[0];i++) (ans *= qpow(s[i-1], s[i])) %= R;
printf("%lld",ans);
}

return 0;
}

C 不会,skip 。

D. Shortest Path on a Line

Source

这题把我卡了一个多小时还没想出来,当时一直在想建个线段树搞线段覆盖……

实际上一段区间里面任意两点间有等距的有向边,这个东西等价于把这段区间连成一个简单环:节点顺序连边,然后仅有一条边有边权为 ci ,其余为 0 。那么如果我们逆着这个环的方向去求最短路,就能得到这个长度总是 ci ,因为没法直接到达,就总要经过那条边权为 ci 的边。

一张图解:

剩下的问题跑 DJ 就好了。

代码:

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
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
using namespace std;

#define LL long long

const int CN = 1e5+5;
const LL INF = 0x3f3f3f3f3f3f3f2f;

LL read(){
LL s = 0,ne = 1; char c = getchar();
for(;c<'0'||c>'9';c=getchar()) if(c=='-') ne=-1;
for(;c>='0'&&c<='9';c=getchar()) s=(s<<1)+(s<<3)+c-'0';
return s*ne;
}

class fs{
public: int to,nxt; LL di; void init(int t,int n,LL d) {to=t;nxt=n;di=d;}
}E[CN * 51];
int hd[CN],ecnt = 0;
void add(int x,int y,LL z) {E[++ecnt].init(y, hd[x], z); hd[x] = ecnt;}

int n,m;

/* DJ */
class DJ{
public: int id; LL v; bool operator < (const DJ& a)const {return v > a.v;}
};
priority_queue<DJ> Q;
LL d[CN]; bool vis[CN];
LL SP(int st,int ed){
memset(vis, 0, sizeof(vis));
memset(d, 0x3f, sizeof(d));
Q.push((DJ){st, d[st] = 0});
while(!Q.empty()){
int u = Q.top().id; Q.pop();
if(vis[u]) continue; vis[u] = true;
for(int k = hd[u]; k; k = E[k].nxt){
int v = E[k].to;
if(d[v] > d[u] + E[k].di){
d[v] = d[u] + E[k].di;
Q.push((DJ){v, d[v]});
}
}
}
return d[ed] < INF ? d[ed] : -1;
}

int main()
{
n = read(); m = read();
while(m--){
int u = read(),v = read(); LL c = read();
add(u, v, c);
}
for(int i=1;i<n;i++) add(i + 1, i, 0);

printf("%lld", SP(1, n));

return 0;
}
作者

ce-amtic

发布于

2019-11-10

更新于

2023-04-15

许可协议

CC BY-NC-SA 4.0

评论

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×