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;
}
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';
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";
}
}
}
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";
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)