D - Triangles
排序后two pointer
# -*- coding: utf-8 -*-
# @time : 2023/6/2 13:30
# @author : yhdu@tongwoo.cn
# @desc :
# @file : atcoder.py
# @software : PyCharm
import bisect
import copy
import sys
from sortedcontainers import SortedList
from collections import defaultdict, Counter, deque
from functools import lru_cache, cmp_to_key
import heapq
import math
sys.setrecursionlimit(50050)
def main():
items = sys.version.split()
if items[0] == '3.10.6':
fp = open("in.txt")
else:
fp = sys.stdin
n = int(fp.readline())
a = list(map(int, fp.readline().split()))
a.sort()
ans = 0
for i in range(n - 2):
j = i + 1
k = j + 1
while j < n - 1:
while k < n:
if a[i] + a[j] <= a[k]:
break
k += 1
if k == j + 1 and a[i] + a[j] <= a[k]:
pass
else:
ans += k - j - 1
j += 1
print(ans)
if __name__ == "__main__":
main()
E - Travel by Car
稍微有点绕
单纯floyd求最短路径是不行的,因为如一条路径是2,3,2,从点1到点3最短路径是7,而l=4时从点1到点4要加两次油。第二个图中如果路径为2,2,3, 最短路径还是7,但是只需要加一次油。
问题的关键是,第一个图中点1到点3是无法一次到达,而图2中可以。
因此修改原最短路径图为
如果点s到点t可以一次到达,点s到点t的距离设为1,否则设为INF
再求一次最短路径就是答案
# -*- coding: utf-8 -*-
# @time : 2023/6/2 13:30
# @author : yhdu@tongwoo.cn
# @desc :
# @file : atcoder.py
# @software : PyCharm
import bisect
import copy
import sys
from sortedcontainers import SortedList
from collections import defaultdict, Counter, deque
from functools import lru_cache, cmp_to_key
import heapq
import math
sys.setrecursionlimit(50050)
def main():
items = sys.version.split()
if items[0] == '3.10.6':
fp = open("in.txt")
else:
fp = sys.stdin
n, m, l = map(int, fp.readline().split())
dist = [[10 ** 18] * n for _ in range(n)]
for i in range(m):
a, b, c = map(int, fp.readline().split())
a, b = a - 1, b - 1
dist[a][b] = dist[b][a] = c
for i in range(n):
for j in range(n):
for k in range(n):
if dist[j][i] + dist[i][k] < dist[j][k]:
dist[j][k] = dist[i][k] + dist[j][i]
cnt = [[10 ** 18] * n for _ in range(n)]
for i in range(n):
for j in range(n):
if dist[i][j] <= l:
cnt[i][j] = 1
for i in range(n):
for j in range(n):
for k in range(n):
if cnt[j][i] + cnt[i][k] < cnt[j][k]:
cnt[j][k] = cnt[j][i] + cnt[i][k]
q = int(fp.readline())
for i in range(q):
s, t = map(int, fp.readline().split())
s, t = s - 1, t - 1
if cnt[s][t] >= 10 ** 18:
print(-1)
else:
print(cnt[s][t] - 1)
if __name__ == "__main__":
main()
F - Distinct Numbers
本题需要证明单调性质
待写
# -*- coding: utf-8 -*-
# @time : 2023/6/2 13:30
# @author : yhdu@tongwoo.cn
# @desc :
# @file : atcoder.py
# @software : PyCharm
import bisect
import copy
import sys
from sortedcontainers import SortedList
from collections import defaultdict, Counter, deque
from functools import lru_cache, cmp_to_key
import heapq
import math
sys.setrecursionlimit(50050)
def main():
items = sys.version.split()
if items[0] == '3.10.6':
fp = open("in.txt")
else:
fp = sys.stdin
n = int(fp.readline())
a = list(map(int, fp.readline().split()))
cnt = [0] * (n + 1)
b = [0] * (n + 1)
s = [0] * (n + 1)
for i in range(n):
cnt[a[i]] += 1
b[cnt[a[i]]] += 1
for i in range(1, n + 1):
s[i] = b[i] + s[i - 1]
for k in range(1, n + 1):
hi, lo = n + 1, 1
while lo < hi:
mid = (lo + hi) >> 1
if mid * k <= s[mid]:
lo = mid + 1
else:
hi = mid
print(lo - 1)
if __name__ == "__main__":
main()