C Repsept
签到题
遍历乘积mod,到n以上就为-1
# -*- coding: utf-8 -*-
# @time : 2023/6/2 13:30
# @file : atcoder.py
# @software : PyCharm
import bisect
import copy
import sys
from itertools import permutations
from sortedcontainers import SortedList
from collections import defaultdict, Counter, deque
from functools import lru_cache, cmp_to_key
import heapq
import math
sys.setrecursionlimit(100010)
def main():
items = sys.version.split()
if items[0] == '3.10.6':
fp = open("in.txt")
else:
fp = sys.stdin
i = 1
k = int(fp.readline())
c = 7
while i <= k:
if c % k == 0:
print(i)
break
i += 1
c = (c * 10 + 7) % k
else:
print(-1)
if __name__ == "__main__":
main()
D - Alter Altar
手玩几遍后发现可以模拟
最后的R置换最前面的W
# -*- coding: utf-8 -*-
# @time : 2023/6/2 13:30
# @file : atcoder.py
# @software : PyCharm
import bisect
import copy
import sys
from itertools import permutations
from sortedcontainers import SortedList
from collections import defaultdict, Counter, deque
from functools import lru_cache, cmp_to_key
import heapq
import math
sys.setrecursionlimit(100010)
def main():
items = sys.version.split()
if items[0] == '3.10.6':
fp = open("in.txt")
else:
fp = sys.stdin
n = int(fp.readline())
s = fp.readline().strip()
sl = []
for c in s:
if c == 'W':
sl.append(0)
else:
sl.append(1)
i = 0
while i < n - 1:
if sl[i] == 0 and sl[i + 1] == 1:
break
i += 1
else:
print(0)
return
i = 0
while i < n - 1:
if sl[i] == 0:
break
i += 1
j = n - 1
while j >= 0:
if sl[j] == 1:
break
j -= 1
ans = 0
while i < n - 1 and j >= 0 and i < j:
sl[j] = 0
sl[i] = 1
ans += 1
i, j = i + 1, j - 1
while i < n - 1:
if sl[i] == 0:
break
i += 1
while j >= 0:
if sl[j] == 1:
break
j -= 1
print(ans)
if __name__ == "__main__":
main()
E - Logs
一开始理解错了Round Up的意义,以为是四舍五入
但是即使用浮点数往上取整,很容易有精度的问题,WA一个点
最后还是看了题解 整数去做
题目本身不难,这种求最大值里的最小值或者最小值里的最大值都可以用二分来进行求解
# -*- coding: utf-8 -*-
# @time : 2023/6/2 13:30
# @file : atcoder.py
# @software : PyCharm
import bisect
import copy
import sys
from itertools import permutations
from sortedcontainers import SortedList
from collections import defaultdict, Counter, deque
from functools import lru_cache, cmp_to_key
import heapq
import math
sys.setrecursionlimit(100010)
def main():
items = sys.version.split()
if items[0] == '3.10.6':
fp = open("in.txt")
else:
fp = sys.stdin
n, k = map(int, fp.readline().split())
a = list(map(int, fp.readline().split()))
lo, hi = 1, max(a) + 1
def check(val):
ret = 0
for x in a:
t = x // val - 1
if x % val:
t += 1
ret += t
if ret > k:
return False
return True
while lo < hi:
mi = (hi + lo) // 2
r = check(mi)
if r:
hi = mi
else:
lo = mi + 1
print(lo)
if __name__ == "__main__":
main()
F - Range Set Query
乍一看毫无头绪,总不能在线段树上开集合吧?
其实这种题目是有套路的
首先离线查询,按r前往后排序。
现在遍历颜色数组c,取pos[i]为当前color的最新位置,维护每个color的在区间内的个数(只有0或者1)。换句话说,每个color在要维护的数列里只出现一次并计数1,这样可以保证区间内color的数量就是区间color集合的元素个数
// atcoder.cpp :
//
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <map>
#include <set>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <vector>
#include <regex>
#include <queue>
#include <climits>
using namespace std;
typedef pair<int, int> pii;
typedef long long LL;
typedef vector<int> vi;
int N = 500000;
int n;
int pos[500005];
int col[500005];
int c[500005];
int ans[500005];
int Q;
struct Query {
int l, r, idx;
};
vector<Query> query;
bool cmp(Query& lhs, Query& rhs) {
return lhs.r < rhs.r;
}
inline int lowbit(int x) {
return x & -x;
}
void add(int x, int val) {
while (x <= N) {
c[x] += val;
x += lowbit(x);
}
}
int get(int x) {
int ret = 0;
while (x > 0) {
ret += c[x];
x -= lowbit(x);
}
return ret;
}
int main()
{
//freopen("in.txt", "r", stdin);
scanf("%d%d", &n, &Q);
for (int i = 1; i <= n; ++i) {
scanf("%d", col + i);
}
for (int i = 0; i < Q; ++i) {
int l, r;
scanf("%d%d", &l, &r);
Query q;
q.l = l, q.r = r, q.idx = i;
query.push_back(q);
}
sort(query.begin(), query.end(), cmp);
for (int i = 1, j = 0; i <= n; ++i) {
int idx = col[i];
add(i, 1);
if (pos[idx] != 0) {
add(pos[idx], -1);
}
pos[idx] = i;
while (j < query.size() && query[j].r == i) {
ans[query[j].idx] = get(query[j].r) - get(query[j].l - 1);
j++;
}
}
for (int i = 0; i < Q; ++i)
printf("%d\n", ans[i]);
return 0;
}