AtCoder ABC174

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值