20220323华为笔试

1

#include <vector>
#include <queue>
#include <iostream>

using namespace std;

int help(vector<int>& num){
    vector<bool> is_used(num.size(), false);
    queue<int> q;
    q.push(0);
    is_used[0] = true;
    int cnt = 0;

    while(q.size()){
        int s = q.size();
        cnt ++;
        while(s --){
            int t = q.front();
            q.pop();
            for(int i=t; i<= t+num[t]; i++){
                if(i == num.size() - 1) return cnt;
                if(! is_used[i]) {
                    q.push(i);
                    is_used[i] = true;
                }
            }
        }
    }
    return cnt;
}


int main()
{
    int n; scanf("%d", &n);
    vector<int> vec;
    for(int i=0; i<n; i++){
        int t; 
        scanf("%d", &t); 
        vec.push_back(t);
    }


    int ans = help(vec);

    int k; scanf("%d", &k);
    if(ans > k) ans = -1;
    printf("%d\n", ans);
    return 0;
}

/*

5
2 0 1 0 3
2

 6
 2 1 5 6 2 3
 3



 */

2

#include <vector>
#include <queue>
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;


int to_int(char &a, char &b){

    int x, y;
    if('A' <= a && a <= 'F') x = a - 'A' + 10;
    else x = a - '0';

    if('A' <= b && b <= 'F') y = b - 'A' + 10;
    else y = b - '0';

//    cout << a << " " << b << " " << x * 16 + y << endl;
    return x * 16 + y;
}

int main(){
    string s; cin >> s;
    int n; cin >> n;

    vector<int> num(n, 0);
    for(int i=0; i<n; i++){
        cin >> num[i];
    }

    sort(num.begin(), num.end());

    int idx = 0;
    for(int i=0; i<num.size(); i++){
        if(idx + 1 < s.size()
            && to_int(s[idx], s[idx+1]) == num[i]){
                idx += 2;
                int len = to_int(s[idx], s[idx+1]);
                idx += 2;
                int offs = idx / 2;
                idx += len * 2;
                cout << len << " " << offs << "\n";
        }else{
            cout << "0" << " " << "0\n";
        }
    }

}

/*

0F04ABABABAB
1
15


0F04ABABABAB1001FF
2
15
17

 */

3

#include <vector>
#include <queue>
#include <iostream>
#include <string>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>

using namespace std;


struct ItemA{
    int id;
    int vol;
    unordered_set<int> fset;

    ItemA(int id, int vol) : id(id), vol(vol){}

    bool operator < (const ItemA& oth) const{
        if(fset.size() < oth.fset.size()) return true;
        else if(fset.size() > oth.fset.size()) return false;
        else{
            if(vol < oth.vol) return false;
            else return true;
        }

        return true;
    }

    bool operator > (const ItemA& oth) const{
        if(fset.size() < oth.fset.size()) return false;
        else if(fset.size() > oth.fset.size()) return true;
        else{
            if(vol < oth.vol) return true;
            else return false;
        }

        return true;
    }
};

struct ItemB{
    int id;
    int cate;
    ItemB(int id, int cate):id(id), cate(cate){}
};


unordered_map<string, int> cateory;
int cidx = 0;

int main(){
    int m, n, x; cin >> m >> n >> x;

    priority_queue<ItemA, vector<ItemA>, greater<ItemA>> ppp1;
    priority_queue<ItemA, vector<ItemA>, greater<ItemA>> ppp2;


    for(int i=0; i<m; i++){
        ItemA t(i, x);

        string s;
        while(cin >> s){
            if(cateory.count(s) == 0) cateory.insert({s, ++cidx});

            t.fset.insert(cateory[s]);
            ppp1.push(t);

            if(cin.get() == '\n') break;
        }
    }

    vector<ItemB> vb;
    for(int i=0; i<n; i++){
        string s; cin >> s;

        if(cateory.count(s) == 0){
            cout << false << endl;
            return 0;
        }

        ItemB t(i, cateory[s]);
        vb.push_back(t);
    }

    vector<vector<int>> ans(m, vector<int>(n, 0));

    for(int i=0; i<vb.size(); i++){
        ItemB &x = vb[i];

        bool flag = false;

        while(ppp1.size()){
            ItemA y = ppp1.top();
            ppp1.pop();
            if(y.fset.count(x.cate)) {
                ans[y.id][x.id] = 1;
                y.vol -= 1;

                if(y.vol > 0) ppp2.push(y);
                flag = true;
                break;
            }else{
                ppp2.push(y);
            }
        }

        if(flag == false) {
            cout << "false" << endl;
            return 0;
        }

        while(ppp2.size()){
            ItemA t = ppp2.top(); ppp2.pop(); ppp1.push(t);
        }
    }

    cout << "true\n";

    for(int i=0; i<m; i++){
        for(int j=0; j<n; j++){
            cout << ans[i][j] << " ";
        }
        cout << "\n";
    }
}
// 每次取种类数最小,剩余数最大的数字


/*

4 6 4
jav cpp py
py
cpp jav
py
jav
py
cpp
py
cpp
jav

 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ColaForced

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值