冬训day3 简单数据结构

A - 简单计算器

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

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<stack>
 4 using namespace std;
 5 int main()
 6 {
 7    int i;
 8    double a,b;
 9    char s[250],c;
10    while(gets(s),strcmp(s,"0")!=0)
11    {
12        stack<char>s1;
13        stack<double>s2;
14        for(i=0;s[i];i++)
15        {
16            if(s[i]>='0'&&s[i]<='9')
17            {
18                a=0;
19                while(s[i]>='0'&&s[i]<='9')
20                {
21                    a=a*10+s[i]-'0';
22                    i++;
23                }
24                i--;
25                s2.push(a);
26            }
27            else if(s[i]=='-'||s[i]=='+')
28            {
29                if(!s1.empty())
30                {
31                    c=s1.top();
32                    s1.pop();
33                    a=s2.top();
34                    s2.pop();
35                    b=s2.top();
36                    s2.pop();
37                    if(c=='+')
38                        a+=b;
39                    else
40                        a=b-a;
41                    s2.push(a);
42                    s1.push(s[i]);
43                }
44                else
45                    s1.push(s[i]);
46            }
47            else if(s[i]=='/')
48            {
49                b=0;
50                i+=2;
51                while(s[i]>='0'&&s[i]<='9')
52                {
53                    b=b*10+s[i]-'0';
54                    i++;
55                }
56                i--;
57                a=s2.top();
58                s2.pop();
59                a=a/b;
60                s2.push(a);
61            }
62            else if(s[i]=='*')
63            {
64                b=0;
65                i+=2;
66                while(s[i]>='0'&&s[i]<='9')
67                {
68                    b=b*10+s[i]-'0';
69                    i++;
70                }
71                i--;
72                a=s2.top();
73                s2.pop();
74                a=a*b;
75                s2.push(a);
76            }
77        }
78        while(!s1.empty())
79        {
80            c=s1.top();
81            s1.pop();
82            a=s2.top();
83            s2.pop();
84            b=s2.top();
85            s2.pop();
86            if(c=='+')
87                a+=b;
88            else
89                a=b-a;
90            s2.push(a);
91        }
92        printf("%.2f
",s2.top());
93    }
94    return 0;
95 }
View Code

B - ACboy needs your help again!

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

C - Ugly Numbers

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

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<algorithm>
 5 #define N 100000
 6 #define ll long long
 7 using namespace std;
 8 bool check(ll x)
 9 {
10     if(x<10&&x!=7) return false;
11     for(ll i=2;i<floor(sqrt((double)x));++i)
12       if(x%i==0) return false;
13       return true;
14 }
15 ll a[1505];
16 int main()
17 {  
18     ll t=1,q2=1,q3=1,q5=1;a[1]=1;
19     //for(int i=1;i<=6;++i) a[i]=i;
20     while(1)
21     {
22         int n;
23         scanf("%d",&n);
24         if(n==0) break;
25         while(t<n)
26         {
27             a[++t]=min(min(a[q2]*2,a[q3]*3),a[q5]*5);
28             if(a[t]==a[q2]*2) q2++;
29             if(a[t]==a[q3]*3) q3++;
30             if(a[t]==a[q5]*5) q5++;
31         }
32         printf("%lld
",a[n]);
33     }
34     return 0;
35 }
View Code

D - Rails

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

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

E - Black Box

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

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

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<cmath>
 5 #include<vector>
 6 #include<queue>
 7 #include<stack>
 8 #include<algorithm>
 9 #define N 100000
10 #define ll long long
11 using namespace std;
12 priority_queue<int,vector<int>,less<int> >q1;
13 priority_queue<int,vector<int>,greater<int> >q2;
14 int a[30005],b[30005];
15 void sock(int d)
16 {
17     while(q1.size()>d) {
18         q2.push(q1.top());q1.pop();
19     }
20     while(q1.size()<d)
21     {
22         q1.push(q2.top());q2.pop();
23     }
24 }
25 void add1(int x)
26 {
27     if(q1.empty()){  q1.push(x);return;
28     } 
29     if(x>q1.top()) q2.push(x);
30     else q1.push(x);
31 }
32 int main()
33 {
34     int n,m;
35     cin>>n>>m;
36     for(int i=1;i<=n;++i) scanf("%d",&a[i]);
37     for(int i=1;i<=m;++i) scanf("%d",&b[i]);
38     int k=0,get=1;
39     for(int i=1;i<=n;++i)
40     {
41         add1(a[i]);
42         while(b[get]==i&&get<=m)
43         {
44             get++;k++;
45             sock(k);
46             cout<<q1.top()<<endl;
47         }
48     }
49 }
View Code

F - Fence Repair

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

G - Running Median

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

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<cmath>
 5 #include<vector>
 6 #include<queue>
 7 #include<stack>
 8 #include<algorithm>
 9 #define N 100000
10 #define ll long long
11 using namespace std;
12 priority_queue<int,vector<int>,greater<int> > qa;//小根堆存比中位数大的数 
13 priority_queue<int,vector<int>,less<int> > qb;//大根堆存小于中位数的数 
14 void add(int x)
15 {
16     if(qa.empty())
17     {
18         qa.push(x);return;
19      } 
20     if(x>qa.top()) qa.push(x);
21     else qb.push(x);
22     while(qa.size()<qb.size()) 
23     {
24         qa.push(qb.top());qb.pop();
25     }
26     while(qa.size()>qb.size()+1)
27     {
28         qb.push(qa.top());qa.pop();
29     }
30 }
31 int a[10000];
32 int main()
33 {
34     int T,index,n,x;
35     cin>>T;
36     while(T--)
37     {
38         while(!qa.empty()) qa.pop();
39         while(!qb.empty()) qb.pop();
40         int t=0;
41         scanf("%d %d",&index,&n);
42         for(int i=1;i<=n;++i) 
43         {
44             scanf("%d",&x);
45             add(x);
46             if(i%2) a[++t]=qa.top();
47         }
48         printf("%d %d
",index,(n+1)/2);
49         for(int i=1;i<=t;++i)
50         {
51            printf("%d",a[i]);
52            if(i>0&&i%10==0) printf("
");
53            if(i%10) printf(" ");
54         }
55         if(t%10) printf("
");
56     }
57 }
View Code

J - Train Problem I

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

K - 看病要排队

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

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<cmath>
 5 #include<vector>
 6 #include<queue>
 7 #include<stack>
 8 #include<algorithm>
 9 #define N 100000
10 #define ll long long
11 using namespace std;
12 struct patient{
13     int cla,id;
14     bool operator < (const patient& p1) const
15     {
16         if(cla!=p1.cla) return cla<p1.cla;
17         else return id>p1.id;
18     }
19 };
20 int main()
21 {
22     int n;string comm;
23     while(~scanf("%d",&n))
24     {
25         priority_queue<patient>doctor[4];
26         int id=1;
27         for(int i=1;i<=n;++i)
28 {
29         cin>>comm;
30         if(comm=="IN")
31         {
32             int x,y;
33             scanf("%d%d",&x,&y);
34             patient p1;
35             p1.cla=y;p1.id=id;
36             id++;
37             doctor[x].push(p1);
38         }
39         else
40         {
41             int x;
42             scanf("%d",&x);
43             if(doctor[x].empty()) printf("EMPTY
");
44             else 
45             {
46                 printf("%d
",doctor[x].top().id);
47                 doctor[x].pop();
48             }
49         }
50     }
51 }
52 }
View Code

L - Team Queue

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

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<cmath>
 5 #include<vector>
 6 #include<queue>
 7 #include<stack>
 8 #include<algorithm>
 9 #include<map>
10 #define N 100000
11 #define ll long long
12 using namespace std;
13 int main()
14 {
15     int t,cas=1;
16     while(cin>>t&&t)
17     {
18         int n,x;
19         queue<int> q1,q2[1010];
20         while(!q1.empty()) q1.pop();
21         for(int i=0;i<1010;++i) 
22         {
23             while(!q2[i].empty()) q2[i].pop();
24         }
25         cout<<"Scenario #"<<cas++<<endl;
26         map<int,int> team;
27         for(int i=0;i<t;++i)
28         {
29             cin>>n;
30             while(n--)
31             {
32                 cin>>x;team[x]=i;
33             }
34         }
35         string s;
36         for(;;)
37         {
38             cin>>s;
39             if(s[0]=='S') break;
40             else if(s[0]=='D')
41             {
42                 int hh=q1.front();
43                 cout<<q2[hh].front()<<endl;
44                 q2[hh].pop();
45                 if(q2[hh].empty()) q1.pop();
46             }
47             else if(s[0]=='E')
48             {
49                 cin>>x;
50                 int hh=team[x];
51                 if(q2[hh].empty()) q1.push(hh);
52                 q2[hh].push(x);
53             }
54         }
55         cout<<endl;
56     }
57 }
View Code

M - Windows Message Queue

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

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<cmath>
 5 #include<vector>
 6 #include<queue>
 7 #include<stack>
 8 #include<algorithm>
 9 #define N 100000
10 #define ll long long
11 using namespace std;
12 struct Msg{
13     string name;
14     int pa,pr,id;
15     friend bool operator < (Msg p1,Msg p2)
16     {
17         if(p1.pr!=p2.pr) return p1.pr>p2.pr;
18         else return p1.id>p2.id;
19     }
20 };
21 priority_queue<Msg> msg;
22 int main()
23 {
24     string s;
25     int id=1;
26     while(cin>>s)
27     {
28         if(s=="GET")
29         {
30             if(msg.empty()) printf("EMPTY QUEUE!
");
31             else 
32             {
33                 cout<<msg.top().name<<" "<<msg.top().pa<<endl;
34                 msg.pop();
35             }
36         }
37         else
38         {
39             Msg p;
40             cin>>p.name>>p.pa>>p.pr;
41             p.id=id++;
42             msg.push(p);
43         }
44     }
45 }
View Code
原文地址:https://www.cnblogs.com/TYH-TYH/p/8322404.html