训练赛部分题解

补题赛

http://cichengzi.cn/contest/3

题解

冒险家:
https://blog.csdn.net/qq_43321732/article/details/104457567
第k短路和感染(low):
https://www.cnblogs.com/SwiftAC/p/12349038.html
目标学校:

1e5,1e6的数据不可能暴力。先对所有的学校按分数线从小到大排序,然后二分查找。可以手写二分,也可以用lower_bound函数,找出第一个大于等于t的数a[i]。a[i]是大于等于t的数,a[i-1]是小于t的数。
比较abs(a[i]-t)和abs(t-a[i-1])的大小。加起来就好了。
还要考虑两种种特殊情况,x比最高分数线高,x比最低分数线低。

参考代码:

#include<algorithm>
#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
int a[100005];
int main()
{
	int n, m, i, t, ans = 0;
	scanf("%d%d", &n, &m);
	for (i = 1; i <= n; i++)
		scanf("%d", &a[i]);
	sort(a + 1, a + n + 1);
	for (i = 1; i <= m; i++)
	{
		scanf("%d", &t);
		if (t < a[1])ans += (a[1] - t);
		//特判比最小值小的情况
		else
		{
			int L;
			L = lower_bound(a + 1, a + 1 + n, t) - a;
			ans += min(abs(a[L] - t), abs(t - a[L - 1]));
			//当t比最大值大的时候,L为n+1,a[L]=0,a[L-1]=a[n]
			//t-a[L-1]肯定更小,就不用再特判了。
		}
	}
	printf("%d\n", ans);
	return 0;
}

cxk不会二进制:

s = x + y * 2^k
y乘2的k次方就相当于把y左移k 例如 1 * 2 二进制是10 1 * 2^2 二进制是 100
然后贪心匹配就行了:找到y串最后一个1的位置t,因为只能左移,所以 在x串中找到对应位置左边的第一个1 ,因为加一下就可以把那个位置变成0,然后 答案就是 他们两个的位置的距离

#include <stdio.h>
#include <string.h>
char a[100005];
char b[100005];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s%s",a,b);
        int x,y;
        int na=strlen(a);
        int nb=strlen(b);
        for(int i=nb-1;i>=0;i--)
        {
            if(b[i]=='1')
            {
                y=i;
                break;
            }
        }
        for(int i=na-nb+y;i>=0;i--)
        {
            if(a[i]=='1')
            {
                x=i;
                break;
            }
        }
        printf("%d\n",na-x-nb+y);
    }
}

感染mid,hight和画圈游戏:
2D:https://blog.csdn.net/nuoyanli/article/details/104458430
3D:https://blog.csdn.net/nuoyanli/article/details/104458686
3A:https://blog.csdn.net/nuoyanli/article/details/104458865

发布了311 篇原创文章 · 获赞 226 · 访问量 13万+

猜你喜欢

转载自blog.csdn.net/nuoyanli/article/details/104479610