2021杭电多校(MINIEYE)第六场补题

1001 Yes, Prime Minister

解题思路

显然 r > 0*, r* l,如果 l 0,此时 [l, r] 区间和 = [[l + 1*, r*] 的区间和,且 r ≥ −**l + 1 > 0。

故对于任意一个区间,都可以找到一个对应的 r l > 0 的区间与之对应,区间和相同。

对于下式在这里插入图片描述

l > 0 l > 0 l>0 时,若 r − l ≥ 2 r -l ≥ 2 rl2,则$(l+r)/2 $ 和 ( r − l + 1 ) / 2 (r-l+1) /2 rl+1)/2之中必有一个是大于 1 的整数。区间和必然能被拆分成两个因数的乘积。所以这样的满足和为素数的区间长度必然不长于 2。
预处理出比 [ 2 , 2 × 1 0 7 ] [2, 2 × 10^7] [2,2×107]略大范围内的质数,埃氏筛或者线筛均可。

如果 x ≤ 0 x≤0 x0,则候选答案为:

• 最小的 y ( y ≥ 1 − x ) y(y ≥ 1-x) y(y1x),满足 y y y 是质数,区间为 [ − y + 1 , y ] [ -y + 1, y] [y+1,y]

• 最小的 z ( z ≥ 2 − x ) z(z ≥ 2 -x) z(z2x),满足 2 z − 1 2z -1 2z1 是质数,区间为 [ − z + 2 , z ] [ -z + 2, z] [z+2,z]

如果 x > 0 x > 0 x>0,则候选答案为:

[ x , x ] [x, x] [x,x]

[ x , x + 1 ] [x, x + 1] [x,x+1]

[ x − 1 , x ] [x -1, x] [x1,x]

• 最小的 y ( y ≥ x ) y(y ≥ x) y(yx),满足 y y y是质数,区间为 [ y + 1 , y ] [ y + 1, y] [y+1,y]

• 最小的 z ( z ≥ x ) z(z ≥ x) z(zx),满足 2 z − 1 2z -1 2z1 是质数,区间为 [ − z + 2 , z ] [ -z + 2, z] [z+2,z]

二分查找即可,时间复杂度为 O ( T l o g ( ∣ x ∣ ) + ∣ x ∣ ) O(T log(|x|) + |x|) O(Tlog(x)+x)

代码

#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
#include<queue>
#define maxn (2 * 10000005)
#define mem memset
using namespace std;
int prime[maxn] = {0};
int visit[maxn] = {0};
void Prime() {
    for (int i = 2; i <= maxn; i++) {
        if (!visit[i]) {
            prime[++prime[0]] = i;
        }
        for (int j = 1; j <= prime[0] && i * prime[j] <= maxn; j++) {
            visit[i * prime[j]] = 1;
            if (i % prime[j] == 0) {
                break;
            }
        }
    }
}
int main()
{
    int t;
    bool isfu = false;
    long long res= 1;
    scanf("%d", &t);
    Prime();
    while (t--)
    {
        int n;
        int cur;
        res = 1;
        isfu = false;
        scanf("%d", &n);
        if (n == 0) {printf("3\n"); continue;}
        else if(n==1){printf("2\n"); continue;}
        else if(n < 0){
            n = -n;
            res = 2 * n + 1;
            n = n+1;
            isfu = true;
            res++;
        }
        // cout << res << " " << n << endl;
        // cout << visit[7] << endl;

        if(visit[n] == 0) {cout << res << endl; continue;}
        else if( !isfu && (visit[n+n+1] ==0 || visit[n+n-1] == 0)) {cout << 2 << endl; continue;}
        else if(isfu && visit[n+n+1] == 0) {cout << res + 1 << endl; continue;}
        else{
            if(!isfu) res += 2 * n;
            else  res += 1;
            cur = n + 1;
            while(true){
                if(visit[cur] == 0){
                    res += 1;
                    break;
                }else if(visit[cur + cur + 1] == 0){
                    res += 2;
                    break;
                }else{
                    res += 2;
                    cur = cur + 1;
                }
            }
        }
        cout << res << endl;
    }
    return 0;
}

1004 Decomposition

解题思路

相当于对一个无向完全图构造一个欧拉回路。而从欧拉回路的性质我们可以知道,对于每个点的度数都是偶数的情况,必定存在欧拉回路。

不难发现我们需要找到一种方式遍历这个完全图。
首先考虑n-1的情况。
我们可以选择一个点x,然后选顺时针一个位置的,然后选逆时针两个位置的,然后选时针三个位置的…于是我们就遍历完了这n个点。创造出了一个链
但是这是去掉n这个点的情况,我们在这个链的两段加上n,就把这些链连起来了。

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void work(){
	int n, k;
	scanf("%d%d",&n,&k);
	vector<int> v;
	v.push_back(n);
	for (int i = 1 ; i <= n / 2; i++) {
        int pos = i - 1;
        for (int j = 1; j < n; j++) {
            v.push_back(((pos  % (n - 1) + n - 1) % (n - 1) + 1));
            if (j & 1)
                pos += j;
            else
                pos -= j;
        }
        v.push_back(n);
	}
	int id = 0;
	while (k--) {
		int t; scanf("%d",&t);
		t++;
		while (t--) {
			printf("%d%c",v[id++],t == 0 ? '\n' : ' ');
		}
		id--;
	}
	 
}
int main() {
	int T; scanf("%d",&T);
	for (int i = 1; i <= T; i++) {
		printf("Case #%d:\n", i);
		work();
	}	
	return 0;
}

1008 Command and Conquer: Red Alert 2

解题思路

考虑范围为 k k k的狙击手,可以攻击的范围是一个以狙击手为中心、边长为 2 k 2k 2k的正方体。那我们不妨重新陈述一下题面,称狙击手从 ( x , y , z ) (x,y,z) x,y,z可以攻击的范围是所有满足 x ≤ x ′ ≤ x + 2 k , y ≤ y ′ ≤ y + 2 k , z ≤ z ′ ≤ z + 2 k x≤x′≤x+2k , y≤y′≤y+2k , z≤z′≤ z+2k xxx+2k,yyy+2k,zzz+2k ( x ′ , y ′ , z ′ ) (x′,y′,z′) x,y,z

那么我们就会发现:如果我们的狙击手的某一维度的坐标小于剩余点的那一维度的最小坐标,我们就应该把狙击手移动的那一维度的最小坐标,因为这样只会让我们能狙击的目标增多。在我们三个维度都是那一维度的最小坐标时,狙击手就必须将三个维度中某一维度最小坐标的所有敌军都消灭,才能继续移动。

换一个更清楚的讲法:
在这里插入图片描述
下面,我们可以用贪心不断扩大k来删掉下一个待删的点,每次将狙击手置于三个坐标分别最小的位置,一直这样下去直到删掉所有点,最终答案为 a n s / 2 ans/2 ans/2。复杂度 O ( n ∗ l o g n ) O(n*logn) O(nlogn)

代码

#include <algorithm>
#include <cstdio>
#include <map>
#include <queue>
using namespace std;
const int maxn = 5e5 + 7;
int t, n, tot, ans;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> Q[3];
int p[maxn][4];
int main()
{
    for (scanf("%d", &t); t--;)
    {
        for (int i = 0; i < 3; ++i)
            while (Q[i].size()) Q[i].pop();
        int o[3];
        ans = 0;
        scanf("%d", &n);
        for (int i = 1; i <= n; ++i)
        {
            for (int j = 0; j < 3; ++j)
                scanf("%d", &p[i][j]), Q[j].push({p[i][j], i}), o[j] = min(o[j], p[i][j]);
            p[i][3] = 1;
        }
    while (Q[0].size())
        {
            int tmp[4] = {0, 0, 0, (int)3e9};
            for (int i = 0; i < 3; ++i)
            {
                while (Q[i].size() && p[Q[i].top().second][3] == 0) Q[i].pop();
                if (Q[i].size()) o[i] = Q[i].top().first;
            }
            if (!Q[0].size()) break;
            for (int i = 0; i < 3; ++i)
            {
                int no = Q[i].top().second;
                tmp[i] = max(p[no][(i + 1) % 3] - o[(i + 1) % 3], 
				p[no][(i + 2) % 3] - o[(i + 2) % 3]);
                tmp[3] = min(tmp[3], tmp[i]);
            }
            ans = max(ans, tmp[3]);
            for (int i = 0; i < 3; ++i)
                if (tmp[3] == tmp[i])
                    p[Q[i].top().second][3] = 0, Q[i].pop();
        }
        printf("%d\n", (ans + 1) >> 1);
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值