5.2日训练

1.abc_248C Dice Sum

题意:给定数n,m,k,问n个不超过m的数相加,结果不超过k的方案数有多少

这道题其实是比较明显的背包dp,但是因为对dp的不熟悉,结果硬是没有写出来,好失败。

思路:将整数的长度作为dp的第一维,将k作为第二维,f[i][j]表示整数个数为i的时候可以达到j的方案个数;

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
using namespace std;
typedef long long ll;
const int mod = 998244353;
const int N = 55,M= N* N;
int f[N][M]; //表示长度为n时,不超过M的答案是多少
int n,m,k;
int cnt,sum;

int main()
{
    //ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);

    cin >> n >> m >> k;

    for(int i = 1;i <= m;i++)f[1][i] = 1;
    for(int i = 1;i <= n;i++)
    {
        for(int j = 0;j <= k;j++)
        {
            for(int s = 1;s <= m;s++)
            {
                f[i+1][j+s] = (f[i+1][j+s] + f[i][j])%mod;
            }
        }
    }
    int res = 0;
    for(int i = 0;i <= k;i++) res = (res + f[n][i])%mod;
    cout << res << endl;
    return 0;
}

题目2:abc_248D Range Count Query

 

 题目大意是给定一个序列,然后q个查询,给出区间和一个数字x,问x在区间内的次数;

思路:这道题注意看数据的范围是2*10^5,要注意对数据的敏感度,可以存一个二维数组,以Ai作为第一维,存储Ai出现的位置,当查询x的时候只需要二分查找a[x]中L,R的位置即可

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = 200010;
int a[N];
int n,q;
vector<int> vec[N];
int main()
{
    //ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin >> n;
    for(int i = 1;i <= n;i++)
    {
        int x;
        scanf("%d",&x);
        vec[x].push_back(i);
    }
    scanf("%d",&q);
    while(q--)
    {
        int L,R,x;
        int cnt = 0;
        scanf("%d%d%d",&L,&R,&x);
        int pos1 = lower_bound(vec[x].begin(),vec[x].end(),L)-vec[x].begin();
        int pos2 = upper_bound(vec[x].begin(),vec[x].end(),R)-vec[x].begin();
        printf("%d\n",pos2-pos1);
    }
    return 0;
}

3.K-colinear Line  abc_248E

题意:给定n个点,问有多少在一条支线上的点超过k,如果有无数种输出Infinity

思路:当k等于1的时候,显然有无数种方案,当k不等于的时候暴力枚举即可,先确定两个点,然后枚举其他点,判断完之后将这条直线标记一下,避免后面重复标记;

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = 310;
struct Edge
{
    int x,y;
} edges[N];
bool flag[N][N];
bool judge(int a,int b,int c)
{
    int v1 = (edges[b].y - edges[a].y)*(edges[c].x - edges[a].x);
    int v2 = (edges[b].x - edges[a].x)*(edges[c].y - edges[a].y);
    return (v1 == v2);
}
int main()
{
    //ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int n,MAX;
    cin >> n >> MAX;
    for(int i = 0; i < n; i++)
    {
        scanf("%d%d",&edges[i].x,&edges[i].y);
    }
    if(MAX == 1)
    {
        puts("Infinity");
        return 0;
    }
    memset(flag,true,sizeof flag);
    int cnt = 0;
    for(int i = 0; i < n; i++)
    {
        for(int j = i+ 1; j < n; j++)
        {
            if(flag[i][j])
            {
                vector<int> temp;
                temp.push_back(i);
                temp.push_back(j);
                for(int k = j + 1; k < n; k++)
                {
                    if(judge(i,j,k))
                    {
                        temp.push_back(k);
                    }
                }
                if(temp.size() >= MAX)cnt++;
                for(int k = 0; k < temp.size(); k++)
                {
                    for(int s = k+1;s < temp.size();s++)
                    {
                        flag[temp[k]][temp[s]] = false;
                    }
                }
            }
        }
    }
    cout << cnt << endl;
    return 0;
}

4.Keep Connect abc_248F

待补

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值