Blackops

初心易得,始终难守

0%

CF #460 Div.2 A B C D

A.Supermarket

题意告诉你买$a_i$元可以获得$b_i$重量的物品,你要买$m$重量,由于物品可以拆分,选最高性价比的物品就行了。

代码:

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
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);

int main(void)
{
int n, m, i;
scanf("%d%d", &n, &m);
double ans = 1e9;
for (i = 1; i <= n; ++i)
{
int a, b;
scanf("%d%d", &a, &b);
ans = min(a * 1.0 / b * m * 1.0, ans);
}
printf("%.8f\n", ans);
return 0;
}

B.Perfect Number

题意就是叫你找出第$k$小的数位和为$10$的数字(把1W范围看成10W,打表发现打不动,心态爆炸,写不来写不来),for一遍暴力就行就行。

代码:

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
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 1e5 + 7;
int arr[N];

inline int sg(int n)
{
int r = 0;
while (n)
{
r += (n % 10);
n /= 10;
}
return r;
}
int main(void)
{
int sz = 0;
register int i;
int k;
cin >> k;
for (int i = 19; i <= 10800100; ++i)
{
int j = i;
int s = sg(i);
if (s == 10)
--k;
if (!k)
{
cout << i << endl;
return 0;
}
}
return 0;
}

C.Seat Arrangements

题意就是给你一幅$n*m$的地图,找出左右或者上下连续的$k$个位置,输出这种位置有几个。
k=1的时候横竖是一样的,答案要除以2,B题没写这题又被hack,心态爆炸

代码:

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
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 2010;
char s[N][N];
int pre[N][N];
int up[N][N];

int main(void)
{
int n, m, i, j, k;
scanf("%d%d%d", &n, &m, &k);
for (i = 1; i <= n; ++i)
scanf("%s", s[i] + 1);
for (i = 1; i <= n; ++i)
{
for (j = 1; j <= m; ++j)
{
pre[i][j] = pre[i][j - 1] + (s[i][j] == '.' ? 1 : 0);
}
}
int ans = 0;
for (i = 1; i <= n; ++i)
{
for (j = 1; j <= m - k + 1; ++j)
{
if (pre[i][j + k - 1] - pre[i][j - 1] == k)
++ans;
}
}
if (k == 1)
{
printf("%d\n", ans);
return 0;
}
for (j = 1; j <= m; ++j)
{
for (i = 1; i <= n; ++i)
{
up[i][j] = up[i - 1][j] + (s[i][j] == '.' ? 1 : 0);
}
}
for (j = 1; j <= m; ++j)
{
for (i = 1; i <= n - k + 1; ++i)
{
if (up[i + k - 1][j] - up[i - 1][j] == k)
++ans;
}
}
cout << ans << endl;
return 0;
}

D.Substring

题意就是给你一幅点带权有向图,然后求一条某一个字母出现次数最多的路径,输出这个最多出现次数,若可以有无穷多个则输出$-1$
显然存在环肯定是$-1$,先$tarjan$或者$dfs$一发判环,然后这题还有个坑点可能存在自环,因此如果出现一条边起点和终点一样,也算有环。
然后留下的图是一个$DAG$,正解应该是做一遍$DAG$上的$DP$,燃鹅$DP$菜到抠脚的我就喜欢用二维$SPFA$作死,T了无数发。不过可以说一下$SPFA$建图,感觉挺好玩的。
最复杂的情况原图由多个$DAG$构成,且子图可能存在多起点,多终点的情况,那么构造源点$S$(记为$0$),终点$T$(记为$n+1$),$S$连向所有入度为$0$的点,所有出度为$0$的点连向$T$,跑一遍$S$到$T$的二维最长路,$Max{dis[T][i]},0 \le i \le 25$就是答案。

正解的$DP$感觉挺巧妙的,先处理子结点,再转移到自己,复杂度是$O(N)$

代码:

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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 3e5 + 7;
struct edge
{
int to, nxt;
edge() {}
edge(int _to, int _nxt): to(_to), nxt(_nxt) {}
} E[N];
int head[N], tot;
int ch[N], cycle, d[N][26], vis[N], Ans;
char s[N];

void init()
{
CLR(head, -1);
}
inline void add(int s, int t)
{
E[tot] = edge(t, head[s]);
head[s] = tot++;
}
namespace tarjan {
int dfn[N], low[N], st[N], sc, top, sccnt, scnum[N];
bool ins[N];
void tar(int u)
{
if (cycle)
return ;
dfn[u] = low[u] = ++sc;
st[top++] = u;
ins[u] = true;
register int v;
for (register int i = head[u]; ~i; i = E[i].nxt)
{
v = E[i].to;
if (!dfn[v])
{
tar(v);
low[u] = min(low[u], low[v]);
}
else if (ins[v])
low[u] = min(low[u], dfn[v]);
}
if (low[u] == dfn[u])
{
++sccnt;
do
{
v = st[--top];
ins[v] = false;
if (++scnum[sccnt] > 1)
{
cycle = 1;
return ;
}
}
while (u != v);
}
}
}
void dfs(int u)
{
vis[u] = 1;
d[u][ch[u]] = 1;
for (register int i = head[u]; ~i; i = E[i].nxt)
{
int v = E[i].to;
if (!vis[v])
dfs(v);
for (register int k = 0; k < 26; ++k)
{
d[u][k] = max(d[u][k], d[v][k] + (k == ch[u]));
Ans = max(Ans, d[u][k]);
}
}
}
int main(void)
{
init();
register int n, m, i, a, b;
scanf("%d%d", &n, &m);
scanf("%s", s + 1);
for (i = 1; i <= n; ++i)
ch[i] = s[i] - 'a';
for (i = 0; i < m; ++i)
{
scanf("%d%d", &a, &b);
if (cycle)
continue;
add(a, b);
if (a == b)
cycle = 1;
}
if (cycle)
{
return puts("-1");
}
for (i = 1; i <= n && !cycle; ++i)
{
if (!tarjan::dfn[i])
{
tarjan::tar(i);
}
}
if (cycle)
{
return puts("-1");
}
for (i = 1; i <= n; ++i)
{
if (!vis[i])
dfs(i);
}
for (i = 1; i <= n; ++i)
for (a = 0; a < 26; ++a)
Ans = max(Ans, d[i][a]);
cout << Ans << endl;
return 0;
}