Codeforces Round 988 (Div. 3)
Codeforces Round 988 (Div. 3) 总结
A
没啥好说的,用桶存出现次数。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
using namespace std;
typedef long long ll;
const int N=25;
int n;
int a[N],c[N];
void solve()
{
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i],c[a[i]]++;
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        ans+=c[i]/2;
        c[i]=0;
    }
    cout<<ans<<'\n';
}
int main ()
{
    #ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
    #endif
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int T;
    cin>>T;
    while(T--) solve();
    return 0;
}
B
依据题意有 \(n \times m+2=k\),则 \(n \times m=k-2\),将 \(k-2\) 分解,检验是否出现在 \(a\) 中即可。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
using namespace std;
typedef long long ll;
const int N=2e5+5;
int k;
int a[N],c[N];
void solve()
{
    cin>>k;
    for(int i=1;i<=k;i++) cin>>a[i],c[i]=0;
    for(int i=1;i<=k;i++) c[a[i]]++;
    int x=k-2;
    for(int i=1;1ll*i*i<=x;i++)
        if(x%i==0&&c[i]&&c[x/i])
        {
            cout<<i<<' '<<x/i<<'\n';
            return ;
        }
}
int main ()
{
    #ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
    #endif
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int T;
    cin>>T;
    while(T--) solve();
    return 0;
}
C
首先容易想到,奇数相加可行,偶数相加可行,而最小的奇合数为 \(9=4+5\),所以 \(n \ge 5\) 时有解,\(n=5\) 时为 \(1,3,5,4,2\)。\(n>5\) 只需要将偶数添加到队尾,奇数添加到队头即可。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
using namespace std;
typedef long long ll;
const int N=2e5+5;
int n;
int a[N];
void solve()
{
    cin>>n;
    if(n<=4)
    {
        cout<<-1<<'\n';
        return ;
    }
    int cnt=0;
    for(int i=7;i<=n;i+=2) cout<<i<<' ';
    cout<<"1 3 5 4 2 ";
    for(int i=6;i<=n;i+=2) cout<<i<<' ';
    cout<<'\n';
}
int main ()
{
    #ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
    #endif
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int T;
    cin>>T;
    while(T--) solve();
    return 0;
}
D
考虑贪心,首先能不捡就不捡,遇到障碍跳不过去时,才返回去捡加速器,要求捡的次数最小,所以回去捡的加速器要先捡大的。
因此枚举障碍 \(i\),要跳过去必须满足 \(k \ge r_i-l_i+2\),用大根堆存加速器的信息,如果全部取出来加到 \(k\) 上仍不满足,说明无法完成。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
using namespace std;
typedef long long ll;
const int N=2e5+5;
int n,m,L;
int l[N],r[N];
int x[N],v[N];
priority_queue<int> q;
void solve()
{
    cin>>n>>m>>L;
    for(int i=1;i<=n;i++) cin>>l[i]>>r[i];
    for(int i=1;i<=m;i++) cin>>x[i]>>v[i];
    int k=1,id=1,ans=0;
    for(int i=1;i<=n;i++)
    {
        int len=r[i]-l[i]+2;
        while(x[id]<l[i]&&id<=m) q.push(v[id]),id++;
        while(q.size()&&k<len) k+=q.top(),ans++,q.pop();
        if(k<len)
        {
            cout<<-1<<'\n';
            return ;
        }
    }
    while(q.size()) q.pop();
    cout<<ans<<'\n';
}
int main ()
{
    #ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
    #endif
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int T;
    cin>>T;
    while(T--) solve();
    return 0;
}
E
交互题,记得用 endl 或者 cout.flush() 清空缓存区。
询问次数最大为 \(n\),设 \(k_i=f(1,i)\) \((2 \le i \le n)\)。
先考虑无解的情况,显然是 \(k_n=0\) 时不能确定答案。
然后遍历 \(k\),考虑如果 \(s_i=0\),则一定有 \(k_i=k_{i-1}\)。所以当 \(k_i>k{i-1}\) 时,\(s_i=1\)。但这样有一个问题,如果 \(s\) 为 \(\mathtt{111\dots000\dots1\dots}\),那么开头的 \(1\) 也都不会有贡献。所以找到第一个不等于 \(0\) 的 \(k_i\),说明 \([1,i-1]\) 中有 \(k_i\) 个 \(0\),所以开头就有 \(i-1-k_i\) 个 \(1\)。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
using namespace std;
typedef long long ll;
const int N=1e4+5;
int n;
int s[N];
int query(int l,int r)
{
    int x;
    cout<<"? "<<l<<" "<<r<<' '<<endl;
    cin>>x;
    return x;
}
int k[N];
void solve()
{
    cin>>n;
    for(int i=1;i<=n;i++) s[i]=0;
    for(int i=2;i<=n;i++) k[i]=query(1,i);
    if(k[n]==0)
    {
        cout<<"! IMPOSSIBLE "<<endl;
        return ;
    }
    for(int i=2;i<=n;i++) if(k[i]>k[i-1]) s[i]=1;
    for(int i=2;i<=n;i++)
        if(s[i])
        {
            for(int j=1;j<=i-1-k[i];j++) s[j]=1;
            break;
        }
    cout<<"! ";
    for(int i=1;i<=n;i++) cout<<s[i];
    cout<<' '<<endl;
}
int main ()
{
    #ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
    #endif
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int T;
    cin>>T;
    while(T--) solve();
    return 0;
}
F
二分答案,设攻击了 \(k\) 次。
因为只能是在同一个位置 \(p\) 攻击,所以第 \(i\) 个敌人每次攻击需要受到至少 \(t_i=\lceil\frac{h_i}{k}\rceil\) 的伤害,因此 \(p\) 的范围就是 \([x_i-(m-t[i]),x_i+(m-t[i])]\),若 \(m < t_i\) 则一定无法击败第 \(i\) 个敌人。
这样就转化成了 \(n\) 个区间的覆盖问题,问是否有一个点被覆盖了至少 \(k\) 次。
离散化加差分轻松搞定,复杂度为 \(O(n\log(n)\log(V))\)。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
using namespace std;
typedef long long ll;
const int N=2e5+5,inf=1e9;
int n,m,k;
int h[N],x[N],t[N];
int tot;
int b[N],c[N];
bool check(int k)
{
    tot=0;
    for(int i=1;i<=n;i++)
    {
        t[i]=(h[i]+k-1)/k;
        if(t[i]<=m)
        {
            c[++tot]=x[i]-(m-t[i]);
            c[++tot]=x[i]+(m-t[i])+1;
        }
    }
    sort(c+1,c+tot+1);
    tot=unique(c+1,c+tot+1)-c-1;
    for(int i=1;i<=tot;i++) b[i]=0;
    for(int i=1;i<=n;i++)
    {
        if(t[i]<=m)
        {
            int l=lower_bound(c+1,c+tot+1,x[i]-(m-t[i]))-c;
            int r=lower_bound(c+1,c+tot+1,x[i]+(m-t[i])+1)-c;
            b[l]++,b[r]--;
        }
    }
    for(int i=1;i<=tot;i++) b[i]+=b[i-1];
    for(int i=1;i<=tot;i++) if(b[i]>=k) return 1;
    return 0;
}
void solve()
{
    cin>>n>>m>>k;
    for(int i=1;i<=n;i++) cin>>h[i];
    for(int i=1;i<=n;i++) cin>>x[i];
    int l=1,r=inf,mid,ans=-1;
    while(l<=r)
    {
        mid=l+r>>1;
        if(check(mid)) r=mid-1,ans=mid;
        else l=mid+1;
    }
    cout<<ans<<'\n';
}
int main ()
{
    #ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
    #endif
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int T;
    cin>>T;
    while(T--) solve();
    return 0;
}
G
dp 加容斥。
逐步推导,设 \(f_i\) 为从 \(1\) 到 \(i\) 的方案数,按题意模拟就有:
\]
\]
这样做是 \(O(n^2)\) 的。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
const int N=2e5+5,mod=998244353,M=1e6+5;
int n;
int a[N];
int f[N];
int gcd(int x,int y)
{
    return y ? gcd(y,x%y) : x;
}
vector<int> g[M],h[N];
set<int> v;
void solve()
{
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    f[1]=1;
    for(int i=2;i<=n;i++)
        for(int j=1;j<=i-1;j++)
            if(gcd(a[i],a[j])!=1)
				f[i]=(f[i]+f[j])%mod;
    cout<<f[n]<<'\n';
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
    #endif
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    solve();
    return 0;
}
考虑优化,不难发现我们其实并不在意具体的 \(\gcd(a_i,a_j)\) 是啥,只要求不为 \(1\),将 \(a_i\) 质因数分解,\(a_i\) 与 \(a_j\) 是通过相同的质因数产生联系的。每次分解 \(a_i\),找前面的位置进行转移。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
const int N=2e5+5,mod=998244353,M=1e6+5;
int n;
int a[N],f[N];
vector<int> g[M],h[N];
set<int> v;
void solve()
{
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    int x=a[1];
    for(int j=2;1ll*j*j<=n;j++)
    {
        if(x%j==0)
        {
            g[j].push_back(1);
            while(x%j==0) x/=j;
        }
    }
    if(x>1) g[x].push_back(1);
    f[1]=1;
    for(int i=2;i<=n;i++)
    {
        int x=a[i];
        for(int j=2;1ll*j*j<=x;j++)
        {
            if(x%j==0)
            {
                for(int k:g[j])
                {
                    if(v.count(k)==0) f[i]=(f[i]+f[k])%mod;
                    v.insert(k);
                }
                g[j].push_back(i);
                while(x%j==0) x/=j;
            }
        }
        if(x>1)
        {
            for(int k:g[x])
            {
                if(v.count(k)==0) f[i]=(f[i]+f[k])%mod;
                v.insert(k);
            }
            g[x].push_back(i);
        }
        v.clear();
    }
    cout<<f[n]<<'\n';
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
    #endif
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    solve();
    return 0;
}
但这样还是过不了。因为这最坏还是 \(O(n^2)\) 的,随便就能卡掉。
然后不妨按质因数划分集合,设 \(b_x\) 为质因数为 \(x\) 的数产生的贡献。这样每次 \(a_i\) 分解出 \(x\) \(f_i\) 只需要加上 \(b_x\) 就行了。但是仅仅如此的话连样例都过不了。
考虑这样一组数据 \(\mathtt{6,6}\)。\(i=1\),\(f_1=1\),更新 \(b\),有 \(b_2=1,b_3=1\)。\(i=2\),\(f_2=b_2+b_3=2\),但实际上是 \(f_2=1\)。
这里就是 \(2\) 和 \(3\) 重复算了,因为这里显然还要减去 \(6\) 出现的次数 \(1\)。
如何避免这样情况呢?用容斥原理。
将 \(a_i\) 质因数分解后,每个质因数只取一次相乘得到 \(y\),取 \(y\) 的所有约数放在动态数组 g[a[i]] 中。比如 g[12]={2,3,6},g[6]={2,3,6},这时候可以计算上面例子,\(i=1\),有 \(b_2=b_3=b_6=1\),\(i=2\),有 \(f_2=b_2+b_3-b_6=1\)。
也就是说在每次更新 \(f_i\) 时对于 g[a[i]] 中的每个元素 \(x\),如果 \(x\) 的质因数个数为奇数,说明要加上贡献,否则减去贡献。同样 \(b_x\) 的更新也需要取 g[a[i]] 中的每个元素 \(x\)。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
using namespace std;
typedef long long ll;
const int N=2e5+5,M=1e6+5,mod=998244353;
int n;
int a[N],b[M],f[N];
vector<int> g[M],h[M];
void divide1(int x,int id)
{
    for(int i=2;1ll*i*i<=x;i++)
    {
        if(x%i==0)
        {
            h[id].push_back(i);
            while(x%i==0) x/=i;
        }
    }
    if(x>1) h[id].push_back(x);
}
void divide2(int x,int id)
{
    x=1;
    for(auto t:h[id]) x*=t;
    for(int i=2;1ll*i*i<=x;i++)
    {
        if(x%i==0)
        {
            g[id].push_back(i);
            if(x!=i) g[id].push_back(x/i);
        }
    }
    if(x>1) g[id].push_back(x);
}
void solve()
{
    cin>>n;
    int mx=0;
    for(int i=1;i<=n;i++) cin>>a[i],mx=max(mx,a[i]);
    for(int i=2;i<=mx;i++) divide1(i,i),divide2(i,i);
    f[1]=1;
    for(int i=1;i<=n;i++)
    {
        for(auto x:g[a[i]])
        {
            if(h[x].size()&1) f[i]=(f[i]+b[x])%mod;
            else f[i]=(f[i]+mod-b[x])%mod;
        }
        for(auto x:g[a[i]]) b[x]=(b[x]+f[i])%mod;
    }
    cout<<f[n]<<'\n';
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
    #endif
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    solve();
    return 0;
}
Codeforces Round 988 (Div. 3)的更多相关文章
- Codeforces Round #486 (Div. 3) F. Rain and Umbrellas
		Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ... 
- Codeforces Round #486 (Div. 3) E. Divisibility by 25
		Codeforces Round #486 (Div. 3) E. Divisibility by 25 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ... 
- Codeforces Round #486 (Div. 3) D. Points and Powers of Two
		Codeforces Round #486 (Div. 3) D. Points and Powers of Two 题目连接: http://codeforces.com/group/T0ITBvo ... 
- Codeforces Round #486 (Div. 3) A. Diverse Team
		Codeforces Round #486 (Div. 3) A. Diverse Team 题目连接: http://codeforces.com/contest/988/problem/A Des ... 
- Codeforces Round #366 (Div. 2) ABC
		Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ... 
- Codeforces Round #354 (Div. 2) ABCD
		Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ... 
- Codeforces Round #368 (Div. 2)
		直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ... 
- cf之路,1,Codeforces Round #345 (Div. 2)
		cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ... 
- Codeforces Round #279 (Div. 2) ABCDE
		Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ... 
- Codeforces Round #262 (Div. 2) 1003
		Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ... 
随机推荐
- Gradio.NET 支持 .NET 8 简化 Web 应用开发
			前言 Gradio.NET 是 Gradio 在 .NET 平台上的移植版本.Gradio 是一个开源的 Python 包,用于快速构建机器学习模型.API 或任意 Python 函数的演示或 Web ... 
- C#  导出datatable数据到excel
			第一步:下载两个需要的NUGET包 1.org.in2bits.MyXls:2.NPOI 第二步:关键类OutExcel. using System; using System.Linq; using ... 
- 使用 SSH 转义代码来控制连接
			OpenSSH 最常被忽视的一个非常有用的功能是能够从连接内部控制会话的某些方面.通过使用 SSH 转义代码,我们能够在会话内部与本地 SSH 软件进行交互. 强制从客户端断开连接(如何退出卡住或冻结 ... 
- 【转】 Vue中import from的来源:省略后缀与加载文件夹
			原文地址 Vue中import from的来源:省略后缀与加载文件夹_超频化石鱼的博客-CSDN博客 ,原文地址排版格式可能更好,建议看原文,本文只是为了转载记录 Vue使用import ... fr ... 
- pyinstaller 打包成 exe
			生成 spec 和可执行文件: pyinstaller --onefile your_file.py 如果需要修改生成的spec文件,则后续可以这样运行: pyinstaller -F your_fi ... 
- vim命令之多行注释
			vim常用命令之多行注释和多行删除 vim中多行注释和多行删除命令,这些命令也是经常用到的一些小技巧,可以大大提高工作效率. 1.多行注释: 1. 首先按esc进入命令行模式下,按下Ctrl + v, ... 
- VS code常用插件安装【持续更新】
			Auto Close Tag 自动添加HTML/XML关闭标签.例如,在输入<div>时,输入完最后一个尖括号>时,会自动添加对应的闭合标签</div> Auto Ren ... 
- httpclient调用接口
			有时候会将参数(返回结果)压缩(解压),加密(解密) 将json参数通过GZip压缩 Base64加密 1 public static String gzipAndEncryption(String ... 
- 暑假集训SCP提高模拟10
			我(看着百度百科):我已经知道这场谁组的题了 CTH: 谁 我:你想想,能在模拟赛里塞四道数学题还玩邦的,还能有谁 CTH: 我不知道 我:我不知道 CTH: 我知道了 我:我知道了 我:我是 Bob ... 
- laravel框架中保留条件搜索
			前段代码 <form action="admin_index" method="get"> <input type="text&qu ... 
