0032求固定窗口的最大和
题目描述
有一个N个整数的数组 和一个长度为M的窗口 窗口从数组内的第一个数开始滑动 直到窗口不能滑动为止 每次滑动产生一个窗口 和窗口内所有数的和 求窗口滑动产生的所有窗口和的最大值
输入描述
第一行输入一个正整数N 表示整数个数 0
输出描述
窗口滑动产生所有窗口和的最大值
样例
输入
6
12 10 20 30 15 23
3
输出
68
c++ 代码
#include
#include
#include
#include
using namespace std;
int main()
{
int n,m;
cin>>n;
vector<int>arr(n);
string s;
getchar();
getline(cin,s);
istringstream stemp(s);
int temp;
int i=0;
while(stemp>>temp){
arr[i++]=temp;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
0033-简易内存池的分配
题目描述
有一个简易内存池,内存按照大小粒度分类
每个粒度有若干个可用内存资源用户会进行一系列内存申请 需要按需分配内存池中的资源返回申请结果成功失败列表
分配规则如下
1.分配的内存要大于等于内存的申请量存在满足需求的内存就必须分配 优先分配粒度小的,但内存不能拆分使用
2.需要按申请顺序分配 先申请的先分配,有可用内存分配则申请结果为true 没有可用则返回false
注释:不考虑内存释放
输入描述
输入为两行字符串
第一行为内存池资源列表
包含内存粒度数据信息,粒度数据间用逗号分割
一个粒度信息内用冒号分割
冒号前为内存粒度大小,冒号后为数量
资源列表不大于1024 每个粒度的数量不大于4096
第二行为申请列表 申请的内存大小间用逗号分割,申请列表不大于100000
如
64:2,128:1,32:4,1:128
50,36,64,128,127
输出描述
输出为内存池分配结果
如true,true,true,false,false
输入样例
64:2,128:1,32:4,1:128
50,36,64,128,127
输出样例
true,true,true,false,false
C++ 代码
#include
#include
#include
#include
using namespace std;
int main(){
string ss;
map<int ,int>tmap;
vector<pair<string,string>>arr;
getline(cin,ss);
int k=0;
{
while (k < ss.length()) {
string s1;
while (ss[k] != ',' && k < ss.length() && ss[k] != ':') {
s1 += ss[k++];
}
k++;
string s2;
while (ss[k] != ',' && k < ss.length() && ss[k] != ':') {
s2 += ss[k++];
}
k++;
pair<string, string> stemp(s1, s2);
arr.push_back(stemp);
}
for (int i = 0; i < arr.size(); i++) {
int temp1 = stoi(arr[i].first);
int temp2 = stoi(arr[i].second);
tmap[temp1] = temp2;
}
}
cin.clear();
cin.sync();
ss.clear();
getline(cin,ss);
k=0;
queue<int>q;
while(k<ss.length()){
string stemp;
while(k<ss.length()&&ss[k]!=','){
stemp+=ss[k++];
}
k++;
int temp=stoi(stemp, nullptr,10);
q.push(temp);
}
vector<string>res;
while(!q.empty()){
int temp=q.front();
q.pop();
auto x= tmap.lower_bound(temp);
if(x!=tmap.end()&&x->second>0){
res.push_back("true");
x->second--;
if(x->second==0){
tmap.erase(x);
}
}else{
res.push_back("false");
}
}
for(int i=0;i<res.size()-1;++i){
cout<<res[i]<<",";
}
cout<<res[res.size()-1];
return 0;
}

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78