抗击疫情 从我做起 训练赛一

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;
}

I: 序列 I

题目描述
有一个整数序列,它的每个数各不相同,我们不知道它的长度是多少(即整数个数),但我们知道在某些区间中间至少有多少个整数,用区间(Li,Ri,Ci) 来描述,表示这个整数序列中至少有Ci个数来自区间[Li,Ri],给出若干个这样的区间,问这个整数序列的长度最少能为多少?
输入
第一行一个整数N,表示区间个数;接下来N行,每行三个整数(Li,Ri,Ci),描述一个区间。N<=1000,0<=Li<=Ri<=1000,1<=Ci<=Ri-Li+1
输出
仅一个数,表示该整数序列的最小长度。

样例输入

4
4 5 1
6 10 3
7 10 3
5 6 1

样例输出
4

先把N个右端点从小到大排序,查找的时候从右端点开始

#include <bits/stdc++.h>

using namespace std;
const int maxn = 1e3 + 10;
int n, vis[maxn], ans;

struct node {
    int l, r, c;

    bool operator<(const node &node) {
        return r <  node.r;//r从小到大,再排l
        //return node.r < r;先排r,从大到小,再排l,从小到大
    }
} mp[maxn];

int main() {
    ios::sync_with_stdio(0);
    cin >> n;
    for (int i = 0; i < n; i++) cin >> mp[i].l >> mp[i].r >> mp[i].c;
    sort(mp, mp + n);//这个不要忘了

    for (int i = 0; i < n; i++) {
        int cnt = 0;
        for (int j = mp[i].l; j <= mp[i].r; j++)
            if (vis[j]) cnt++;

        for (int j = mp[i].r; j >= mp[i].l; j--)//要倒着写,比如[6,10][7,10]都是3 先找[7,10]
            if (cnt < mp[i].c && !vis[j]) {
                vis[j] = 1;
                cnt++;
                ans++;
            }
    }
    cout << ans;
    return 0;
}

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;
}
发布了90 篇原创文章 · 获赞 4 · 访问量 9477

猜你喜欢

转载自blog.csdn.net/xcfkaixin/article/details/104176759