【Codeforces】Testing Round #12

本文详细分析了Codeforces Testing Round #12的三个问题:A题要求找出区间内能被k整除的数,解决方案忽略了负数;B题是关于不重叠区间的最大选择数,采用贪心策略按R值排序;C题求解排列中长度为k+1的上升子序列数量,利用树状数组解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【A. Divisibility】
【题目大意】输出[a,b]中能被k整除的数。

【分析】……我没看到负数……
代码:

#include <cstdio>
#include <algorithm>
using namespace std;

int main()
{
    long long a,b,k;
    scanf("%I64d%I64d%I64d",&k,&a,&b); 
    if (a>0 && b>0) printf("%I64d",b/k-(a-1)/k);
    if (a<=0 && b>=0) printf("%I64d",b/k-a/k+1);
    if (a<0 && b<0) printf("%I64d",(b+1)/k-a/k);
}

【B. Restaurant】
【题目大意】有n个区间,第i 个区间的范围为[Li,Ri]问最多能选择几个不重叠的区间。

【分析】贪心,以R值从小到大排序,如果L比上一个R大,那么该区间就可以选择。一时脑残用了优先队列。
代码:

#include <cstdio>
#include <queue>
#include <vector>
#include <functional>
using namespace std;
typedef pair<int, int> pii;
priority_queue < pii, vector <pii>, greater<pii> > q;
pii a[500010];

using namespace std;

int main()
{
    int n;
    scanf("%d",&n);
    for (int i=1; i<=n; i++) scanf("%d%d",&a[i].second,&a[i].first),q.push(a[i]);
    int nowr=q.top().second-1,ans=0;
    while (!q.empty())
    {
        int l=q.top().second,r=q.top().first; q.pop();
        if (l<=nowr) continue;
        ans++;
        nowr=r;
    }
    printf("%d\n",ans);
}

【C. Subsequences】
【题目大意】给出1-n的一个排列,问长度为k+1的上升子序列的数量。

【分析】树状数组区间查询点修改。
代码:

#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL;
inline int lowbit(int x) { return x&(-x); }

LL t[200020][12];
int n,m,k,a;
inline LL Sum(int x, int y) { LL res=0; for (x; x>0; x-=lowbit(x)) res+=t[x][y]; return res; }
void Change(int x, int y, LL d) { for (x; x<=100010; x+=lowbit(x)) t[x][y]+=d; }

int main()
{
    scanf("%d%d",&n,&k);
    for (int i=1; i<=n; i++) 
    {
        scanf("%d",&a); m=max(m,a);
        Change(a,1,1);
        if (a==1) continue; 
        for (int j=2; j<=k+1; j++)
        {
            LL tt=Sum(a-1,j-1);
            Change(a,j,tt); 
        }
    }
    printf("%I64d\n",Sum(100010,k+1));
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值