A - 简单计算器

模拟加栈。。写一写就好,从头到尾扫一遍,分两个栈存,一个存运算符,一个存中间结果,遇到乘除就先处理了,每次遇到加减就处理上一个加减的两个数,结果压进去。。。同时把这个运算符存进去。最后再来个循环把运算符清完。。

 #include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
int main()
{
int i;
double a,b;
char s[],c;
while(gets(s),strcmp(s,"")!=)
{
stack<char>s1;
stack<double>s2;
for(i=;s[i];i++)
{
if(s[i]>=''&&s[i]<='')
{
a=;
while(s[i]>=''&&s[i]<='')
{
a=a*+s[i]-'';
i++;
}
i--;
s2.push(a);
}
else if(s[i]=='-'||s[i]=='+')
{
if(!s1.empty())
{
c=s1.top();
s1.pop();
a=s2.top();
s2.pop();
b=s2.top();
s2.pop();
if(c=='+')
a+=b;
else
a=b-a;
s2.push(a);
s1.push(s[i]);
}
else
s1.push(s[i]);
}
else if(s[i]=='/')
{
b=;
i+=;
while(s[i]>=''&&s[i]<='')
{
b=b*+s[i]-'';
i++;
}
i--;
a=s2.top();
s2.pop();
a=a/b;
s2.push(a);
}
else if(s[i]=='*')
{
b=;
i+=;
while(s[i]>=''&&s[i]<='')
{
b=b*+s[i]-'';
i++;
}
i--;
a=s2.top();
s2.pop();
a=a*b;
s2.push(a);
}
}
while(!s1.empty())
{
c=s1.top();
s1.pop();
a=s2.top();
s2.pop();
b=s2.top();
s2.pop();
if(c=='+')
a+=b;
else
a=b-a;
s2.push(a);
}
printf("%.2f\n",s2.top());
}
return ;
}

B - ACboy needs your help again!

无脑队列,栈,刚开始WA了一发是因为没有清空栈队列。。

C - Ugly Numbers

这个题我本来是写扫一遍的算法。。但是那样很明显会超时,所以乘2,3,5?似乎可以拿优先队列来写……但我觉得用不着……

 #include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#define N 100000
#define ll long long
using namespace std;
bool check(ll x)
{
if(x<&&x!=) return false;
for(ll i=;i<floor(sqrt((double)x));++i)
if(x%i==) return false;
return true;
}
ll a[];
int main()
{
ll t=,q2=,q3=,q5=;a[]=;
//for(int i=1;i<=6;++i) a[i]=i;
while()
{
int n;
scanf("%d",&n);
if(n==) break;
while(t<n)
{
a[++t]=min(min(a[q2]*,a[q3]*),a[q5]*);
if(a[t]==a[q2]*) q2++;
if(a[t]==a[q3]*) q3++;
if(a[t]==a[q5]*) q5++;
}
printf("%lld\n",a[n]);
}
return ;
}

D - Rails

题目意思是给定一个火车出站序列,问能不能实现,由于其后进后出的模式,自然用栈。每次一个火车进站,都去看一看现在栈顶的火车能不能对的上当前应该出站的火车序号,能对的上就pop,最后全部push进去后,如果栈没空,则无法实现。简单的栈应用。

WA了两发是因为读入写残了emmmm

E - Black Box

这个题是要在向一个序列中添加元素的时候,输出第i小的数,是在输入过程中进行排序,用sort大概会炸吧。。其实用两个优先队列可以解决,一个存前i个数,大根堆,另一个小根堆存后面的数,这样可以保证大根堆的顶存的就是第i个小的数;

注意优先队列的写法。。orz

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#define N 100000
#define ll long long
using namespace std;
priority_queue<int,vector<int>,less<int> >q1;
priority_queue<int,vector<int>,greater<int> >q2;
int a[],b[];
void sock(int d)
{
while(q1.size()>d) {
q2.push(q1.top());q1.pop();
}
while(q1.size()<d)
{
q1.push(q2.top());q2.pop();
}
}
void add1(int x)
{
if(q1.empty()){ q1.push(x);return;
}
if(x>q1.top()) q2.push(x);
else q1.push(x);
}
int main()
{
int n,m;
cin>>n>>m;
for(int i=;i<=n;++i) scanf("%d",&a[i]);
for(int i=;i<=m;++i) scanf("%d",&b[i]);
int k=,get=;
for(int i=;i<=n;++i)
{
add1(a[i]);
while(b[get]==i&&get<=m)
{
get++;k++;
sock(k);
cout<<q1.top()<<endl;
}
}
}

F - Fence Repair

emmmme就是一个合并果子嘛。。刚开始想的鬼畜算法WA了,

G - Running Median

和E是一个道理,但是比E简单一点,因为只要求中位数就行了。。基本固定吧,

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#define N 100000
#define ll long long
using namespace std;
priority_queue<int,vector<int>,greater<int> > qa;//小根堆存比中位数大的数
priority_queue<int,vector<int>,less<int> > qb;//大根堆存小于中位数的数
void add(int x)
{
if(qa.empty())
{
qa.push(x);return;
}
if(x>qa.top()) qa.push(x);
else qb.push(x);
while(qa.size()<qb.size())
{
qa.push(qb.top());qb.pop();
}
while(qa.size()>qb.size()+)
{
qb.push(qa.top());qa.pop();
}
}
int a[];
int main()
{
int T,index,n,x;
cin>>T;
while(T--)
{
while(!qa.empty()) qa.pop();
while(!qb.empty()) qb.pop();
int t=;
scanf("%d %d",&index,&n);
for(int i=;i<=n;++i)
{
scanf("%d",&x);
add(x);
if(i%) a[++t]=qa.top();
}
printf("%d %d\n",index,(n+)/);
for(int i=;i<=t;++i)
{
printf("%d",a[i]);
if(i>&&i%==) printf("\n");
if(i%) printf(" ");
}
if(t%) printf("\n");
}
}

J - Train Problem I

和D一个道理,多了一个标记。

K - 看病要排队

    显然优先队列,优先队列中对于结构体需要自己手写重载。。我还是写带有友元函数的重载吧。。。总结里写

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#define N 100000
#define ll long long
using namespace std;
struct patient{
int cla,id;
bool operator < (const patient& p1) const
{
if(cla!=p1.cla) return cla<p1.cla;
else return id>p1.id;
}
};
int main()
{
int n;string comm;
while(~scanf("%d",&n))
{
priority_queue<patient>doctor[];
int id=;
for(int i=;i<=n;++i)
{
cin>>comm;
if(comm=="IN")
{
int x,y;
scanf("%d%d",&x,&y);
patient p1;
p1.cla=y;p1.id=id;
id++;
doctor[x].push(p1);
}
else
{
int x;
scanf("%d",&x);
if(doctor[x].empty()) printf("EMPTY\n");
else
{
printf("%d\n",doctor[x].top().id);
doctor[x].pop();
}
}
}
}
}

L - Team Queue

这个题用到了图啊。而我一点没想?WA一发是初始化出现了问题

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<map>
#define N 100000
#define ll long long
using namespace std;
int main()
{
int t,cas=;
while(cin>>t&&t)
{
int n,x;
queue<int> q1,q2[];
while(!q1.empty()) q1.pop();
for(int i=;i<;++i)
{
while(!q2[i].empty()) q2[i].pop();
}
cout<<"Scenario #"<<cas++<<endl;
map<int,int> team;
for(int i=;i<t;++i)
{
cin>>n;
while(n--)
{
cin>>x;team[x]=i;
}
}
string s;
for(;;)
{
cin>>s;
if(s[]=='S') break;
else if(s[]=='D')
{
int hh=q1.front();
cout<<q2[hh].front()<<endl;
q2[hh].pop();
if(q2[hh].empty()) q1.pop();
}
else if(s[]=='E')
{
cin>>x;
int hh=team[x];
if(q2[hh].empty()) q1.push(hh);
q2[hh].push(x);
}
}
cout<<endl;
}
}

M - Windows Message Queue

和K差不多吧。。WA一发是因为ID写错了。。

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#define N 100000
#define ll long long
using namespace std;
struct Msg{
string name;
int pa,pr,id;
friend bool operator < (Msg p1,Msg p2)
{
if(p1.pr!=p2.pr) return p1.pr>p2.pr;
else return p1.id>p2.id;
}
};
priority_queue<Msg> msg;
int main()
{
string s;
int id=;
while(cin>>s)
{
if(s=="GET")
{
if(msg.empty()) printf("EMPTY QUEUE!\n");
else
{
cout<<msg.top().name<<" "<<msg.top().pa<<endl;
msg.pop();
}
}
else
{
Msg p;
cin>>p.name>>p.pa>>p.pr;
p.id=id++;
msg.push(p);
}
}
}

冬训day3 简单数据结构的更多相关文章

  1. python学习总结----简单数据结构

    mini-web服务器 - 能够完成简单的请求处理 - 使用http协议 - 目的:加深对网络编程的认识.为后面阶段学习web做铺垫 简单数据结构 - 排列组合 import itertools # ...

  2. 牛客练习赛22-E.简单数据结构1(扩展欧拉定理降幂 +树状数组)

    链接:E.简单数据结构1 题意: 给一个长为n的序列,m次操作,每次操作: 1.区间加 2.对于区间,查询 ,一直到- 请注意每次的模数不同.   题解:扩展欧拉定理降幂 对一个数p取log(p)次的 ...

  3. [LOJ#2326]「清华集训 2017」简单数据结构

    [LOJ#2326]「清华集训 2017」简单数据结构 试题描述 参加完IOI2018之后就是姚班面试.而你,由于讨厌物理.并且想成为乔布斯一样的创业家,被成功踢回贵系. 转眼,时间的指针被指向201 ...

  4. 简单数据结构题(from 钟子谦——IOI2018集训队自选题)

    简单数据结构题(from 钟子谦--IOI2018集训队自选题) 试题描述 给一棵 \(n\) 个点的树,点权开始为 \(0\) ,有 \(q\) 次操作,每次操作是选择一个点,把周围一圈点点权 \( ...

  5. 简单数据结构———AVL树

    C - 万恶的二叉树 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64b ...

  6. NYOJ 简单数据结构

    NYOJ 2 括号配对问题 栈的简单应用.可使用STL. #include <iostream> #include <cstdio> #include <cstring& ...

  7. Test 6.29 T4 简单数据结构练习

    问题描述 费了一番功夫,神犇 CJK 终于完成了前三道题目."不错,不愧是新一代神犇啊!" JesseLiu 满意地说道,"不过,你在算法方面的功底固然不错.对于数据结构 ...

  8. [Contest20171102]简单数据结构题

    给一棵$n$个点的数,点权开始为$0$,有$q$次操作,每次操作选择一个点,把周围一圈点点权$+1$,在该操作后你需要输出当前周围一圈点点权的异或和. 由于输出量较大,设第$i$个询问输出为$ans_ ...

  9. UOJ228 简单数据结构练习题

    Description 传送门 维护一个数列, 有以下操作: 对[l,r]同时加上x 把[l,r]开根后下取整. 查询[l,r]之和 n,m \(\leq\)$ 100000, $\(a_i,x \l ...

随机推荐

  1. thinkphp 3.2.3 - App.class.php 解析

    class App { public static function init() { load_ext_file(COMMON_PATH); // { // /home/www/www.domain ...

  2. MongDB之各种修改操作

    接口IMongDaoUpdate: package com.net.test.mongdb.dao; import com.net.test.mongdb.entity.User; public in ...

  3. Python中列表的深浅拷贝

    copy_lst = [ ('py对象三要素',), ('== 比较运算符',), ('is 身份运算符',), ('小数据池',), ('列表的浅拷贝',), ('列表的深拷贝',), ] py对象 ...

  4. Leetcode 105. 从前序与中序遍历序列构造二叉树

    题目链接 题目描述 根据一棵树的前序遍历与中序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7] 中序遍历 inorder ...

  5. 【NopCommerce 3.1】asp.net mvc 利用jQuery from.js上传用户头像

    纯代码不解释. 在CusotmerControllers中添加上传方法 /// <summary> /// ajax上传用户头像 /// </summary> /// < ...

  6. leetcode 【 Merge k Sorted Lists 】python 实现

    题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...

  7. IOS开发学习笔记024-UIButton和UIImageView的区别

    一.UIButton和UIImageView的区别 1. UIImageView 默认只能显示一张图片(默认会填充整个ImageView) 设置方法:image/setImage: UIButton ...

  8. RSA进阶之共模攻击

    适用场景: 同一个n,对相同的m进行了加密,e取值不一样. e1和e2互质,gcd(e1,e2)=1 如果满足上述条件,那么就可以在不分解n的情况下求解m 原理 复杂的东西简单说: 如果gcd(e1, ...

  9. 使用android-junit-report.jar导出单元测试报告

    Android在使用脚本编译和测试时,使用默认的testrunner不会输出文件类型的单元测试报告,每次只能分析logcat的无法直观的看到单元测试结果和报告,这给编写自动化脚本带来了不少麻烦,虽然可 ...

  10. MySQL主从复制入门

    1.MySQL主从复制入门 首先,我们看一个图: MySQL 主从复制与读写分离概念及架构分析 影响MySQL-A数据库的操作,在数据库执行后,都会写入本地的日志系统A中. 假设,实时的将变化了的日志 ...