贝壳找房 2019校招 研发类试卷C++ 编程题 2018.08.19

贝壳找房 2019校招 研发类试卷编程题 C++ 2018.08.19

道路修建
遍历的同时求出最小值min 和 村庄高度和sum,sum-min即可

#include <iostream>

using namespace std;

int main()
{
    int n, MIN = 10001, sum=0;
    cin >> n;
    for(int i=0; i<n; i++){
        int temp;
        cin >> temp;
        if(temp < MIN)
            MIN = temp;
        sum += temp;
    }
    cout << sum-MIN << endl;
    return 0;
}

取消社团预约1
取消社团预约2

根据结束时间排序,贪心
赛码网样例感觉不好,自己写了些样例,应该没什么问题

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct node{
    int id;
    int start;
    int end;
};
bool cmp(node a, node b){
    if(a.end == b.end)
        return a.start < b.start;
    return a.end < b.end;
}
int main()
{
    vector<node> arr;
    int n;
    cin >> n;
    for(int i=1; i<=n; i++){
        node temp;
        temp.id = i;
        cin >> temp.start >> temp.end;
        arr.push_back(temp);
    }
    sort(arr.begin(), arr.end(), cmp);


    int countt = 0;//计算冲突数量
    vector<int> res;//存放需要删除的社团
    for(int i=1; i<arr.size(); i++){
        if(arr[i].start < arr[i-1].end){
            countt++;//冲突增加
            if(countt == 1){//第一次冲突
                res.push_back(arr[i-1].id);
                if(i>=2 && arr[i].start<arr[i-2].end)
                    res.pop_back();
            }
            if(countt==2 && res[res.size()-1] == arr[i-1].id && arr[i].start>=arr[i-2].end){//第二次冲突与第一次相连
                if(res.size() == 2){
                    res[0] = res[1];
                    res.pop_back();
                }
                continue;
            }
            if(countt == 3){//过多冲突 无法安排
                cout << 0 << endl;
                return 0;
            }
            res.push_back(arr[i].id);//注意顺序
        }
    }
    //for(int i=0; i<res.size(); i++)
    //   cout << res[i] << "+";
    //cout << endl;
    sort(res.begin(), res.end());
    if(countt == 0){//无冲突
        cout << n << endl;
        for(int i=1; i<=n; i++)
            cout << i << " ";
        cout << endl;
    }else if(countt == 1){//1冲突
        cout << res.size() << endl;
        for(int i=0; i<res.size(); i++)
            cout << res[i] << " ";
        cout << endl;
    }else if(countt == 2 && res.size()==1){//2冲突 去除中间的社团
        cout << 1 << endl;
        cout << res[0] <<endl;
    }else{//过多冲突 无法安排
        cout << 0 << endl;
    }
    return 0;
}
/*
0个冲突
3
1 3
3 4
5 6

4
1 3
5 6
3 4
6 9

1个冲突
4
1 3
2 4
5 6
7 8

4
1 3
4 6
5 8
8 9

4
1 3
4 6
6 8
5 9

4
1 3
4 6
6 8
7 9

2个冲突 能解决
4
1 3
2 4
3 5
5 6

5
1 2
4 6
5 10
8 15
20 30

5
1 2
4 6
3 10
8 15
20 30

2个冲突 不能解决
5
1 2
4 6
3 10
5 15
20 30

5
1 4
3 6
8 10
9 15
20 30

不能解决
3
1 5
2 6
3 7
*/

扑克牌
贴上别人的代码链接1
贴上别人的AC代码链接2
直接输出4通过36%。。。。赛码网样例有点easy
这道题的原题好像是 洛谷 P2668 ,直接百度搜这两个关键字就行

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <bitset>
//#include <unistd.h>

using namespace std;

#define LL long long
#define pb push_back
#define mk make_pair
#define pill pair<int, int>
#define mst(a, b) memset(a, b, sizeof a)
#define lson (rt << 1)
#define rson ((rt << 1) | 1)

const int qq = 1e5 + 300;
const int INF = 1e9 + 10;
const int MOD = 1e9 + 7;
int num[20];
char st[50];
int minx, n = 14;
void dfs(int card, int cnt) {
    for (int i = 1; i <= 9; ++i) {
        bool f = true;
        int ct = i;
        while (num[ct] > 0 && ct <= 13) {
            ct++;
            if (ct - i >= 5) {
                for (int j = i; j < ct; ++j) {
                    num[j]--;
                }   
                dfs(card - (ct - i), cnt + 1);
                for (int j = i; j < ct; ++j) {
                    num[j]++;
                }
            }
        }
    }
    for (int i = 1; i <= 13; ++i) {
        if (num[i] >= 4) {
            num[i] -= 4;
            for (int j = 1; j <= 13; ++j) {
                for (int k = j; k <= 13; ++k) {
                    if (j == k && num[j] >= 2) {
                        num[j] -= 2;
                        dfs(card - 6, cnt + 1);
                        num[j] += 2;
                    } else if (j != k && num[j] > 0 && num[k] > 0) {
                        num[j]--, num[k]--;
                        dfs(card - 6, cnt + 1);
                        num[j]++, num[k]++;
                    }
                }
            }
            for (int j = 1; j <= 13; ++j) {
                if (num[j] > 0) {
                    num[j]--;
                    dfs(card - 5, cnt + 1);
                    num[j]++;
                }
            }
            dfs(card - 4, cnt + 1);
            num[i] += 4;
        }
    }
    for (int i = 1; i <= 13; ++i) {
        if (num[i] >= 3) {
            num[i] -= 3;
            for (int j = 1; j <= 13; ++j) {
                if (num[j] > 0) {
                    num[j]--;
                    dfs(card - 4, cnt + 1);
                    num[j]++;
                }
            }
            dfs(card - 3, cnt + 1);
            num[i] += 3;
        } 
    }
    int tmp = 0;
    for (int i = 1; i <= 13; ++i) {
        tmp += (num[i] + 1) / 2;
    }
    minx = min(minx, tmp + cnt);
}

int main() {
    minx = INF;
    scanf("%s", st);
    int len = strlen(st);
    for (int i = 0; i < len; ++i) {
        if (isalpha(st[i])) {
            if (st[i] == 'A') {
                num[1]++;
            } else if (st[i] == 'T') {
                num[10]++;
            } else if (st[i] == 'J') {
                num[11]++;
            } else if (st[i] == 'Q') {
                num[12]++;
            } else if (st[i] == 'K') {
                num[13]++;
            }
        } else {
            num[st[i] - '0']++;
        }
    }
    // for (int i = 1; i <= 13; ++i) {
    //  printf("%d ", num[i]);
    // }
    // puts("");
    dfs(20, 0);
    printf("%d\n", minx);
    return 0;
}

极差
这道题我没有碰到,贴上别人题解链接

using namespace std;
int main() {
    int n;
    cin >> n;
    if (n <= 1) {
        cout << 0;
        return 0;
    }
    vector<long long>a(n);
    for (int i = 0; i < n; i++)
        cin >> a[i];
    stack<pair<int, int>> s;
    vector<int> c(n), d(n);
    for (int i = 0; i < n; i++) {
        while (!s.empty() && s.top().first > a[i])
            s.pop();
        if (s.empty())
            c[i] = -1;
        else
            c[i] = s.top().second;
        s.push({ a[i], i });
    }
    s = stack<pair<int, int>>();
    for (int i = n - 1; i >= 0; i--) {
        while (!s.empty() && s.top().first >= a[i])
        s.pop();
        if (s.empty())
            d[i] = n;
        else
            d[i] = s.top().second;
        s.push({ a[i], i });
    }
    long long res = 0;
    for (int i = 0; i < n; i++) {
        res -= a[i] * (i - c[i]) * (d[i] - i);
    }
    s = stack<pair<int, int>>();
    for (int i = 0; i < n; i++) {
        while (!s.empty() && s.top().first <= a[i])
            s.pop();
        if (s.empty())
            c[i] = -1;
        else
            c[i] = s.top().second;
        s.push({ a[i], i });
    }
    s = stack<pair<int, int>>();
    for (int i = n - 1; i >= 0; i--) {
        while (!s.empty() && s.top().first < a[i])
            s.pop();
        if (s.empty())
            d[i] = n;
        else
            d[i] = s.top().second;
        s.push({ a[i], i });
    }
    for (int i = 0; i < n; i++) {
        res += a[i] * (i - c[i]) * (d[i] - i);
    }
    cout << res;
    return 0;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值