zoukankan      html  css  js  c++  java
  • 抗击疫情 从我做起 训练赛一

    D: 找素数

    题目描述
    素数又称质数,是指一个大于 1 的正整数,如果除了 1 和它本身以外,不能再被其它的数整除, 例如:2、3、5、97 等都是素数。2 是最小的素数。
    现在,给你 n 个数字,请你从中选取一部分,用它们拼出一个最大的素数。
    注意:某个数字出现多少次你就可以用多少次,6 与 9 不能混用。

    输入
    输入共 2 行:
    第 1 行,1 个整数 n,表示所给你的数字的个数。
    第 2 行,n 个数字,用一个空格隔开,其含义如题目所述。

    输出
    输出共 1 行,1 个整数,为找到的最大素数。若无法拼出素数,输出-1。
    样例输入

    3 2 7 9
    

    样例输出
    97
    提示
    对于 30%的数据:n ≤ 3;
    对于 60%的数据:n ≤ 4;
    对于 100%的数据:n ≤ 5。

    这个题考试做的时候没问题,过了,现在做的时候不知道怎么了,六神无主,时间太晚了?太累了?I don’t know.

    #include <bits/stdc++.h>
    using namespace std;
    int n,a[6],ans = -1;
    int vis[6];
    int ju(int n){
        if(n < 2) return 0;
        for(int i = 2; i <= sqrt(n); i++){
            if(n % i == 0)
                return 0;
        }
        return 1;
    }
    void dfs(int be,int gs,int s){
        if(ju(s)) ans = max(ans,s);
        if(gs > n) return;
        for(int i = 0; i < n; i++) {
            if(!vis[i]) {
                vis[i] = 1;
                dfs(be + 1, gs + 1,s * 10 + a[i]);
                vis[i] = 0;
            }
        }
    }
    int main(){
        //freopen("in","r",stdin);
        ios::sync_with_stdio(0);
        cin >> n;
        for(int i = 0; i < n; i++) cin >> a[i];
        sort(a,a+n);
        dfs(0,0,0);
        cout << ans;
        return 0;
    }
    View Code

    M: Prison

    题目描述
    We have N ID cards, and there are M gates.
    We can pass the i-th gate if we have one of the following ID cards: the Li-th, (Li+1)-th, …, and Ri-th ID cards.
    How many of the ID cards allow us to pass all the gates alone?

    Constraints
    ·All values in input are integers.
    ·1≤N≤105
    ·1≤M≤105
    ·1≤Li≤Ri≤N

    输入
    Input is given from Standard Input in the following format:

    N M
    L1 R1
    L2 R2

    LM RM

    输出
    Print the number of ID cards that allow us to pass all the gates alone.
    样例输

    4 2
    1 3
    2 4
    

    样例输出
    2
    提示
    Two ID cards allow us to pass all the gates alone, as follows:
    ·The first ID card does not allow us to pass the second gate.
    ·The second ID card allows us to pass all the gates.
    ·The third ID card allows us to pass all the gates.
    ·The fourth ID card does not allow us to pass the first gate.

    题意:n张卡片,m扇门,能打开第i扇门的是第Li,Li+1,Li+ 2…Ri;
    求能通过所有门的卡片的张数
    what?刚看到这个题,一头雾水,后来看别人写的,发现只要求出左右边界即可。要打开所有的门,左边界也就是输入的左面最大的那一个,同理,右边界是最小的那一个
    当时做题的时候忽略了一个重要条件Li≤Ri

    #include <bits/stdc++.h>
    
    using namespace std;
    const int maxn = 1e3 + 10;
    int n,m,l,r,L,R;
    
    int main() {
        ios::sync_with_stdio(0);
        cin >> n >> m;
        R = n;
        while(m--) {
            cin >> l >> r;
            L = max(L,l);
            R = min(R,r);
        }
        cout << max(R - L + 1,0);
        return 0;
    }
    

    N: Integer Cards

    题目描述
    You have N cards. On the i-th card, an integer Ai is written.
    For each j=1,2,…,M in this order, you will perform the following operation once:
    Operation: Choose at most Bj cards (possibly zero). Replace the integer written on each chosen card ith Cj.

    Find the maximum possible sum of the integers written on the N cards after the M operations.

    Constraints
    ·All values in input are integers.
    ·1≤N≤105
    ·1≤M≤105
    ·1≤Ai,Ci≤109
    ·1≤Bi≤N
    输入
    Input is given from Standard Input in the following format:

    N M
    A1 A2 … AN
    B1 C1
    B2 C2

    BM CM

    输出
    Print the maximum possible sum of the integers written on the N cards after the M operations.
    样例输入

    3 2
    5 1 4
    2 3
    1 5
    

    样例输出
    14
    提示
    By replacing the integer on the second card with 5, the sum of the integers written on the three cards becomes 5+5+4=14, which is the maximum result.

    题意:n张卡片,每张卡片上的数字是A…
    有B张卡片,每张上的数字是C,这些卡片可以替换A里面的卡片,求替换之后,n张数字之和最大是多少
    思路:一堆卡片里面,求n个最大的和

    #include <bits/stdc++.h>
    using namespace std;
    #define int long long
    map<int,int> mp;
    int n,m,a,b,c,ans;
    signed main() {
        //freopen("in","r",stdin);
        ios::sync_with_stdio(0);
        cin >> n >> m;
       for(int i = 0; i < n; i++){
          cin >> a;
          mp[a]++;
       }
        for(int i = 0; i < m; i++){
            cin >> b >> c;
            mp[c] += b;
        }
        for(auto it = -- mp.end();;it--){
            ans += min(n,it->second) * it->first;
            n -= it->second;//不能保证最后n为0
            if(n <= 0)//it != mp.begin();这个不用写了
                break;
        }
        cout << ans;
        return 0;
    }
    
  • 相关阅读:
    eclipse添加xsd
    Ibatis 后台打印完整的sql语句
    eclipse 将文件夹作为sourcefolder
    git:hook declined FATAL: W refs/heads DENIED by fallthru error
    单点登陆CAS安装过程中可能遇到的问题
    单点登录的原理与CAS技术的研究
    【转载】软件开发模式对比(瀑布、迭代、螺旋、敏捷)
    UML工具选择
    UML 用例图,时序图,活动图的定义以及区别
    基于UML的需求分析和系统设计个人体会
  • 原文地址:https://www.cnblogs.com/xcfxcf/p/12301577.html
Copyright © 2011-2022 走看看