Educational Codeforces Round 115 (Rated for Div. 2)部分题解

本文详细探讨了四道编程竞赛题目,涉及路径规划、集合操作和数据处理。A题中,讨论了如何判断2×n网格中是否存在只经过0的路径;B题介绍了处理日期分配问题的策略;C题分析了如何计算删除两个元素后保持平均数不变的方案数;D题则讨论了物品选择方案,满足特定属性组合的计数方法。通过对题目的深入剖析,展示了多种算法思路和优化技巧。
摘要由CSDN通过智能技术生成

A题

题目陈述

大意:在一个 2 ∗ n 2*n 2n的长方形网格中,每一个叉点只有’0’或’1’,可以横着竖着斜着走,是否存在只经过0从左上角走到右下角的路径。

思路

  • 两个数相与为0即代表有答案,如果不为0,说明两个格子都为1,比如不能通过。
  • 时间复杂度 O ( n ) O(n) O(n)

代码实现

#include <bits/stdc++.h>
#include <bits/extc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;

#define debug(x) cerr << #x << ": " << x << '\n'
#define bd cerr << "----------------------" << el
#define el '\n'
#define cl putchar('\n')
#define pb push_back
#define eb emplace_back
#define x first
#define y second
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define loop(i, a, b) for (int i = (a); i < (b); i++)
#define dwn(i, a, b) for (int i = (a); i >= (b); i--)
#define ceil(a, b) (a + (b - 1)) / b
#define ms(a, x) memset(a, x, sizeof(a))
#define inf 0x3f3f3f3f
#define db double

typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<db, db> PDD;
typedef vector<int> vci;

inline int read()
{
	int f = 1, d = 0;
	char ch;
	ch = getchar();
	while(ch < '0' || ch > '9')
	{
		if(ch == '-') f = -1;
		ch = getchar();
	}
	while(ch >= '0' && ch <= '9')
		d = (d << 1) + (d << 3) + (ch ^ 48), ch = getchar();
	return d * f;
}

const int N = 1e5 + 10, M = 2e6 + 10, E = 1e3 + 10, md = 1e9 + 7;
const double PI = acos(-1), eps = 1e-8;

int T, n, m;
string a, b;

int main()
{
    cin.tie(0);
    cout.tie(0);
    cin>>T;
    while(T--)
    {
        cin >> n;
        cin >> a >> b;
        int flag = 1;
        for(int i = 0; i < n; i ++ )
        {
            if(a[i] == '1' && b[i] == '1')
            {
                flag = 0;
                break;
            }
        }
        if(flag)
        {
            cout << "YES" << el;
        }
        else cout << "NO" << el;

    }
}

B题

题目陈述

题意太长了,自己看一下链接把……原题链接

思路

  • 枚举两个星期几 i , j i,j i,j C 5 2 = 10 C_5^2=10 C52=10种,两者相或,如果为全1,并且 i i i中学生数量为 s i s_i si, j j j中名额的数量为 s j s_j sj
  • 即二者全集的大小为n,且各自大于等于 n / 2 n/2 n/2鸽巢原理得,必然有解
  • 如果所以方案都没有,则是NO
  • 时间复杂度 O ( n ) O(n) O(n)

代码实现

#include <bits/stdc++.h>
#include <bits/extc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;

#define debug(x) cerr << #x << ": " << x << '\n'
#define bd cerr << "----------------------" << el
#define el '\n'
#define cl putchar('\n')
#define pb push_back
#define eb emplace_back
#define x first
#define y second
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define loop(i, a, b) for (int i = (a); i < (b); i++)
#define dwn(i, a, b) for (int i = (a); i >= (b); i--)
#define ceil(a, b) (a + (b - 1)) / b
#define ms(a, x) memset(a, x, sizeof(a))
#define inf 0x3f3f3f3f
#define db double

typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<db, db> PDD;
typedef vector<int> vci;

inline int read()
{
    int f = 1, d = 0;
    char ch;
    ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
        d = (d << 1) + (d << 3) + (ch ^ 48), ch = getchar();
    return d * f;
}

const int N = 1e5 + 10, M = 2e6 + 10, E = 1e3 + 10, md = 1e9 + 7;
const double PI = acos(-1), eps = 1e-8;

int T, n, m;

bool a[1005][5];
int cnt =0;
int main()
{
    cin.tie(0);
    cout.tie(0);
    cin >> T;
    while (T--)
    {
        cin >> n;
        rep(i, 1, n)
        {
            rep(j, 1, 5)
            {
                cin >> a[i][j];
            }
        }
        int sa , sb;
        int flag = 0;
        rep(i, 1, 5)
        {
            rep(j, i + 1, 5)
            {
                sa = 0, sb = 0;
                cnt = 0;
                rep(k, 1, n)
                {
                    if(a[k][i] == 1 || a[k][j] == 1)
                    {
                        cnt ++ ;
                    }
                    if(a[k][i]) sa ++ ;
                    if(a[k][j]) sb ++ ;
                }
                if(cnt == n && sa >= n/2 && sb >= n/2)
                {
                    flag = 1;
                    goto JUMP;
                }
            }
        }
        JUMP:;
        if(flag == 1)
        {
            cout << "YES" <<el;

        }
        else cout << "NO" << el;
        
    }
}

C题

题目陈述

一个n个数的数组,删去2个数后,剩余的数的平均数跟原来数组的相同的方案数

思路

  • 设原来数组的平均数为 k k k
  • 易得,可以转化为选择两个数的平均数为 k k k的方案数
  • 即求解满足 x + y 2 = s u m n \cfrac{x+y}{2}=\cfrac{sum}{n} 2x+y=nsum的数对的个数,我们开一个map<LL,LL>的桶,来统计数值出现的次数
  • 因为是成对出现的,所以我们处理完毕 x x x之后,直接删除掉 x , y x,y x,y就可以避免重复计算。
  • 为了避免double带来的精度误差,将上面的式子移项得到 n x + n y = 2 ∗ s u m nx+ny=2*sum nx+ny=2sum,所以最后我将所有的 a [ i ] a[i] a[i]都乘以 n n n,再将 s u m ∗ 2 sum*2 sum2就可以避免精度误差
  • 如果 x ! = y x!=y x!=y则答案加上 m a p [ x ] ∗ m a p [ y ] map[x]*map[y] map[x]map[y],如果 x = = y x==y x==y代表同一个集合选两个,加上 m a p [ x ] ∗ ( m a p [ x − 1 ] ) / 2 map[x]*(map[x-1])/2 map[x](map[x1])/2,因为nx,map[]*map[],中途运算可能会爆LL,所以上面map用的LL
  • 时间复杂度 O ( n log ⁡ n ) O(n \log n) O(nlogn)

代码实现

#include <bits/stdc++.h>
#include <bits/extc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;

#define debug(x) cerr << #x << ": " << x << '\n'
#define bd cerr << "----------------------" << el
#define el '\n'
#define cl putchar('\n')
#define pb push_back
#define eb emplace_back
#define x first
#define y second
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define loop(i, a, b) for (int i = (a); i < (b); i++)
#define dwn(i, a, b) for (int i = (a); i >= (b); i--)
#define ceil(a, b) (a + (b - 1)) / b
#define ms(a, x) memset(a, x, sizeof(a))
#define inf 0x3f3f3f3f
#define db double

typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<db, db> PDD;
typedef vector<int> vci;

inline int read()
{
	int f = 1, d = 0;
	char ch;
	ch = getchar();
	while(ch < '0' || ch > '9')
	{
		if(ch == '-') f = -1;
		ch = getchar();
	}
	while(ch >= '0' && ch <= '9')
		d = (d << 1) + (d << 3) + (ch ^ 48), ch = getchar();
	return d * f;
}

const int N = 2e5 + 10, M = 2e6 + 10, E = 1e3 + 10, md = 1e9 + 7;
const double PI = acos(-1), eps = 1e-8;

int T, m;
LL n;
LL a[N];

map<LL, LL> mp;
LL sum;
LL ans;
int main()
{
    cin.tie(0);
    cout.tie(0);
    cin>>T;
    while(T--)
    {
        mp.clear();
        cin >> n;
        sum = ans = 0;
        rep(i, 1, n)
        {
            cin >> a[i];
            sum += a[i];
            a[i] *= n;
            
        }
        rep(i, 1, n)
        {
            mp[a[i]] ++ ;
        }
        sum *= 2;
        rep(i, 1, n)
        {
            if(mp[a[i]] == 0)
                continue;
            if(sum - a[i] != a[i])
            {
                ans += mp[sum - a[i]] * mp[a[i]];
                mp[sum - a[i]] = mp[a[i]] =0;
            }
            else {
                ans += mp[sum - a[i]] * (mp[sum - a[i]] - 1) / 2;
                mp[sum - a[i]] = 0;
            }
        }
        cout << ans << el;
    }
}

D题

题目陈述

  • n n n个物品,每个物品有两个属性 a a a b b b,要求在 n n n个物品中选取 3 3 3个物品满足以下性质之一的方案数
    • 性质一:三个物品的 a a a都不同
    • 性质二:三个物品的 b b b都不同

思路

  • 因为那时候刚刚打完CCPC网络赛,虽然ABC很快就秒了,是我CF的英文题面读假了,题面的意思原本是,我一直解读成为,结果调了半天样例还对不了……后来才发现我题意假了,把问题想难了很多hhh
  • 所以此处我可以拓展一下两个条件都满足的做法
  • 先对于原本的题意来解答

原题意:性质“或”

  • 显然这题的做法就是容斥原理()
  • a n s = 所 有 选 择 三 个 数 的 方 案 − 不 合 法 的 方 案 = 合 法 的 方 案 ans=所有选择三个数的方案-不合法的方案=合法的方案 ans==
  • 所有的选取三个数的方案数,为 C n 3 C_n^3 Cn3
  • 那不合法的方案数呢?仅有一种情况,第一个选取物品 i i i,第二个选取跟 i i i a a a相同(冲突矛盾),第三个选取跟 i i i b b b矛盾
  • 肯定有同学想问:“那三个都取相同a的呢?”,答案这种方案是合法的,因为题意的性质“”,既然数据保证了,不存在 a , b a,b a,b同时相同的数据,那么如果 a a a都相同, b b b一定不相同,即一定满足 b b b都不相同(性质二),故是合法的。同理对于三个全选择 b b b一样合法(性质一)。
  • 注意:我们的选择的是组合,是不分顺序的
  • 拿是否存在,选取了两个 a a a矛盾的,第三个数的 a , b a,b a,b都不跟前面两个数矛盾,那是否也合法?答案是肯定的,因为前两个数的 a a a相同,所以他们的 b b b不相同,又第三个数跟前两个不矛盾,所以三个数的 b b b都不矛盾(性质二)。同理对于仅有两个 b b b矛盾,依旧合法(满足性质一)。
  • 所以我们只需要扣去掉下面的方案数量即可

第一个选取物品 i i i,第二个选取跟 i i i a a a相同(冲突矛盾),第三个选取跟 i i i b b b矛盾

我们设

m a p a [ p i . a ] map_a[p_i.a] mapa[pi.a] i i i物品的 a a a属性的值的桶(出现次数), m a p b [ p i . b ] map_b[p_i.b] mapb[pi.b]代表 b b b属性的

  • 所以这类的方案数量为 t m p = ∑ i = 1 n ( m a p a [ p i . a ] − 1 ) ∗ ( m a p b [ p i . b ] − 1 ) tmp=\sum_{i=1}^n(map_a[p_i.a]-1)*(map_b[p_i.b]-1) tmp=i=1n(mapa[pi.a]1)(mapb[pi.b]1)

  • 即跟他同a的集合里面选一个数,跟它同b的集合里面选一个数,乘法原理累加

  • 当然,这样也不会有重复,为什么?如果存在存在重复的,即说明一个物品,即在另一个物品的a集合,也在它的b集合,说明跟他a,b都相同,与原来的题意相矛盾,故不存在重复计算的数对。

  • 最后的答案为 a n s = C n 3 − ∑ i = 1 n ( m a p a [ p i . a ] − 1 ) ∗ ( m a p b [ p i . b ] − 1 ) ans=C_n^3-\sum_{i=1}^n(map_a[p_i.a]-1)*(map_b[p_i.b]-1) ans=Cn3i=1n(mapa[pi.a]1)(mapb[pi.b]1)

代码实现

#include <bits/stdc++.h>
#include <bits/extc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;

#define debug(x) cerr << #x << ": " << x << '\n'
#define bd cerr << "----------------------" << el
#define el '\n'
#define cl putchar('\n')
#define pb push_back
#define eb emplace_back
#define x first
#define y second
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define loop(i, a, b) for (int i = (a); i < (b); i++)
#define dwn(i, a, b) for (int i = (a); i >= (b); i--)
#define ceil(a, b) (a + (b - 1)) / b
#define ms(a, x) memset(a, x, sizeof(a))
#define inf 0x3f3f3f3f
#define db double

typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<db, db> PDD;
typedef vector<int> vci;

inline int read()
{
    int f = 1, d = 0;
    char ch;
    ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
        d = (d << 1) + (d << 3) + (ch ^ 48), ch = getchar();
    return d * f;
}

const int N = 2e5 + 10, M = 2e6 + 10, E = 1e3 + 10, md = 1e9 + 7;
const double PI = acos(-1), eps = 1e-8;

int T, n, m;
map<int, LL> zt, nd;
LL ans, tmp, sum, tmp2;
PII b[N];

LL cn3(LL n)
{
    return n * (n - 1) * (n - 2) / 6;
}
int main()
{
    cin.tie(0);
    cout.tie(0);
    cin >> T;
    while (T--)
    {
        zt.clear();
        nd.clear();
        cin >> n;
        ans = cn3(n);
        rep(i, 1, n)
        {
            cin >> b[i].x >> b[i].y;
            zt[b[i].x]++;
            nd[b[i].y]++;
        }
        tmp =  0;
        rep(i, 1, n)
        {
            tmp += (zt[b[i].x] - 1) * (nd[b[i].y] - 1);
        }
        ans -= tmp;
        cout << ans << el;
    }
}

拓展题意:性质"与"

  • 即我看错的题意hhh
  • 同时满足两个性质的方案数量

性质一:三个物品的 a a a都不同
性质二:三个物品的 b b b都不同

  • 经过上面的""性质,相信身为巨佬的您一定学会了灵活变通
  • 式子如下 a n s = C n 3 − 1 2 ∑ i = 1 n ( m a p a [ p i . a ] − 1 ) ∗ ( n − m a p a [ p i . a ] ) − 1 2 ∑ i = 1 n ( m a p b [ p i . b ] − 1 ) ∗ ( n − m a p b [ p i . b ] ) + ∑ i = 1 n ( m a p a [ p i . a ] − 1 ) ∗ ( m a p b [ p i . b ] − 1 ) ans=C_n^3-\cfrac{1}{2}\sum_{i=1}^n(map_a[p_i.a]-1)*(n-map_a[p_i.a])-\cfrac{1}{2}\sum_{i=1}^n(map_b[p_i.b]-1)*(n-map_b[p_i.b])+\sum_{i=1}^n(map_a[p_i.a]-1)*(map_b[p_i.b]-1) ans=Cn321i=1n(mapa[pi.a]1)(nmapa[pi.a])21i=1n(mapb[pi.b]1)(nmapb[pi.b])+i=1n(mapa[pi.a]1)(mapb[pi.b]1)
  • 减去的部分,是选择了相同的a,或者b,乘以 1 2 \cfrac{1}{2} 21是因为,对称性要除以2,容斥原理重复计算了上面性质的方案两次,所以需要再加上一次

代码实现

#include <bits/stdc++.h>
#include <bits/extc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;

#define debug(x) cerr << #x << ": " << x << '\n'
#define bd cerr << "----------------------" << el
#define el '\n'
#define cl putchar('\n')
#define pb push_back
#define eb emplace_back
#define x first
#define y second
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define loop(i, a, b) for (int i = (a); i < (b); i++)
#define dwn(i, a, b) for (int i = (a); i >= (b); i--)
#define ceil(a, b) (a + (b - 1)) / b
#define ms(a, x) memset(a, x, sizeof(a))
#define inf 0x3f3f3f3f
#define db double

typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<db, db> PDD;
typedef vector<int> vci;

inline int read()
{
    int f = 1, d = 0;
    char ch;
    ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
        d = (d << 1) + (d << 3) + (ch ^ 48), ch = getchar();
    return d * f;
}

const int N = 2e5 + 10, M = 2e6 + 10, E = 1e3 + 10, md = 1e9 + 7;
const double PI = acos(-1), eps = 1e-8;

int T, n, m;
map<int, LL> zt, nd;
LL ans, tmp, sum, tmp2;
PII b[N];

LL cn3(LL n)
{
    return n * (n - 1) * (n - 2) / 6;
}
int main()
{
    cin.tie(0);
    cout.tie(0);
    cin >> T;
    while (T--)
    {
        zt.clear();
        nd.clear();
        cin >> n;
        ans = cn3(n);
        rep(i, 1, n)
        {
            cin >> b[i].x >> b[i].y;
            zt[b[i].x]++;
            nd[b[i].y]++;
        }
        tmp = 0;
        rep(i, 1, n)
        {
            tmp += (zt[b[i].x] - 1) * (n - zt[b[i].x]);
            tmp += (nd[b[i].y] - 1) * (n - nd[b[i].y]);
        }
        tmp /= 2;
        ans -= tmp;
        
        tmp = 0; //这部分在上面减了两遍,再加回来
        rep(i, 1, n)
        {
            tmp += (zt[b[i].x] - 1) * (nd[b[i].y] - 1);
        }
        ans += tmp;
        cout << ans << el;
    }
}

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值