acm sdut refresh的停车场
2017-02-17 09:40
176 查看
Time Limit: 1000MS Memory Limit: 65536KB
refresh最近发了一笔横财,开了一家停车场。由于土地有限,停车场内停车数量有限,但是要求进停车场的车辆过多。当停车场满时,要进入的车辆会进入便道等待,最先进入便道的车辆会优先
进入停车场,而且停车场的结构要求只出去的车辆必须是停车场中最后进去的车辆。现告诉你停车场容量N以及命令数M,以及一些命令(Add num 表示车牌号为num的车辆要进入停车场或便道,
Del 表示停车场中出去了一辆车,Out 表示便道最前面的车辆不再等待,放弃进入停车场)。假设便道内的车辆不超过1000000.
输入为多组数据,每组数据首先输入N和M(0< n,m <200000),接下来输入M条命令。
输入结束后,如果出现停车场内无车辆而出现Del或者便道内无车辆而出现Out,则输出Error,否则输出停车场内的车辆,最后进入的最先输出,无车辆不输出。
code:
#include<stdio.h>
#include<string.h>
char a[1000500][20];
int main()
{
char s[10];
int n, m, top, i;
while(~scanf("%d%d", &n, &m))
{
top = 0;
int flag = 1;
while(m--)
{
scanf("%s", s);
if(strcmp(s, "Add")==0)
{
scanf("%s", a[++top]);
}
else if(strcmp(s, "Del")==0)
{
if(top>0)
{
if(top>n)
{
for(i = n+1; i<=top; i++)
{
strcpy(a[i-1],a[i]);
}
}
else top--;
}
else flag = 0;
}
else if(strcmp(s, "Out")==0)
{
if(top<=n) flag = 0;
else
{
for(i = n+2; i<=top; i++)
{
strcpy(a[i-1],a[i]);
}
}
}
}
if(flag == 0) printf("Error\n");
else
{
if(top>n)
{
for(i = n;i>=1;i--)
{
printf("%s\n", a[i]);
}
}
else
{
for(i = top;i>=1;i--)
{
printf("%s\n", a[i]);
}
}
}
}
}
还可采用另一种方法,停车场内看作是栈,等待区看作是队列:
code:
#include <iostream>
#include <queue>
#include <stack>
#include <cstring>
using namespace std;
int main()
{
int n, m, i;
string s1, s2;
while(cin>>n>>m)
{
stack<string> s;
queue<string> q;
int flag = 1;
for(i = 0;i<m;i++)
{
cin>>s1;
if(s1 == "Add")
{
cin>>s2;
if(s.size()<n)
{
s.push(s2);
}
else
{
q.push(s2);
}
}
else if(s1 == "Del")
{
af52
if(s.empty()) flag = 0;
else
{
s.pop();
if(!q.empty())
{
s.push(q.front());
q.pop();
}
}
}
else if(s1 == "Out")
{
if(q.empty()) flag = 0;
else q.pop();
}
}
if(!flag) cout<<"Error"<<endl;
else
{
while(!s.empty())
{
cout<<s.top()<<endl;
s.pop();
}
}
}
}
Problem Description
refresh最近发了一笔横财,开了一家停车场。由于土地有限,停车场内停车数量有限,但是要求进停车场的车辆过多。当停车场满时,要进入的车辆会进入便道等待,最先进入便道的车辆会优先进入停车场,而且停车场的结构要求只出去的车辆必须是停车场中最后进去的车辆。现告诉你停车场容量N以及命令数M,以及一些命令(Add num 表示车牌号为num的车辆要进入停车场或便道,
Del 表示停车场中出去了一辆车,Out 表示便道最前面的车辆不再等待,放弃进入停车场)。假设便道内的车辆不超过1000000.
Input
输入为多组数据,每组数据首先输入N和M(0< n,m <200000),接下来输入M条命令。
Output
输入结束后,如果出现停车场内无车辆而出现Del或者便道内无车辆而出现Out,则输出Error,否则输出停车场内的车辆,最后进入的最先输出,无车辆不输出。
Example Input
2 6 Add 18353364208 Add 18353365550 Add 18353365558 Add 18353365559 Del Out
Example Output
18353365558 18353364208
此题为动态规划问题, 可设置一个top设置上限,1~n的在停车场内, n~top在便道内
code:#include<stdio.h>
#include<string.h>
char a[1000500][20];
int main()
{
char s[10];
int n, m, top, i;
while(~scanf("%d%d", &n, &m))
{
top = 0;
int flag = 1;
while(m--)
{
scanf("%s", s);
if(strcmp(s, "Add")==0)
{
scanf("%s", a[++top]);
}
else if(strcmp(s, "Del")==0)
{
if(top>0)
{
if(top>n)
{
for(i = n+1; i<=top; i++)
{
strcpy(a[i-1],a[i]);
}
}
else top--;
}
else flag = 0;
}
else if(strcmp(s, "Out")==0)
{
if(top<=n) flag = 0;
else
{
for(i = n+2; i<=top; i++)
{
strcpy(a[i-1],a[i]);
}
}
}
}
if(flag == 0) printf("Error\n");
else
{
if(top>n)
{
for(i = n;i>=1;i--)
{
printf("%s\n", a[i]);
}
}
else
{
for(i = top;i>=1;i--)
{
printf("%s\n", a[i]);
}
}
}
}
}
还可采用另一种方法,停车场内看作是栈,等待区看作是队列:
code:
#include <iostream>
#include <queue>
#include <stack>
#include <cstring>
using namespace std;
int main()
{
int n, m, i;
string s1, s2;
while(cin>>n>>m)
{
stack<string> s;
queue<string> q;
int flag = 1;
for(i = 0;i<m;i++)
{
cin>>s1;
if(s1 == "Add")
{
cin>>s2;
if(s.size()<n)
{
s.push(s2);
}
else
{
q.push(s2);
}
}
else if(s1 == "Del")
{
af52
if(s.empty()) flag = 0;
else
{
s.pop();
if(!q.empty())
{
s.push(q.front());
q.pop();
}
}
}
else if(s1 == "Out")
{
if(q.empty()) flag = 0;
else q.pop();
}
}
if(!flag) cout<<"Error"<<endl;
else
{
while(!s.empty())
{
cout<<s.top()<<endl;
s.pop();
}
}
}
}
相关文章推荐
- refresh的停车场 SDUT
- SDUT 2088 refresh的停车场
- SDUT OJ refresh的停车场
- [SDUT](2088)refresh的停车场 ---栈和队列
- refresh的停车场 SDUT 2088
- SDUT -refresh的停车场(栈和队列)
- SDUT 2088 refresh的停车场
- SDUT 2088 refresh的停车场
- sdut 2088 数据结构实验之栈与队列十一:refresh的停车场
- [ACM] SDUT 2607 Mountain Subsequences
- Sdut 2164 Binomial Coeffcients (组合数学) (山东省ACM第二届省赛 D 题)
- SDUT第一届ACM知识挑战赛Fire Emblem [You Will Win - Special Version]
- SDUT 3258 Square Number(2015年山东省第六届ACM大学生程序设计竞赛)
- “师创杯”SDUT-ACM校赛正式赛-C~K玩游戏
- refresh的停车场 分类: 栈和队列 2015-06-18 17:13 26人阅读 评论(0) 收藏
- refresh的停车场
- refresh的停车场
- [ACM] SDUT 2886 Weighted Median (省赛最悲剧的一道题)
- Sdut 2164 Binomial Coeffcients (组合数学) (山东省ACM第二届省赛 D 题)
- sdutacm-迷瘴