SPOJ-CMG - Collecting Mango
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<cmath>
#include<algorithm>
#define psb push_back
#define ppb pop_back
#define all(x) x.begin(),x.end()
#define N 1000100
#define sf scanf
#define pf printf
using namespace std;
typedef long long LL;
typedef vector<LL>vll;
LL ar[N] , num[N];
int main()
{
int t , tc=0;
//cin >> t;
sf("%d",&t);
while(t--)
{
LL n, x, arlen=-1, nmlen=-1 , i , maxi=-1 , zar;
char c;
//cin >> n;
sf("%lld",&n);
//cout << "Case " << ++tc << ":\n";
pf("Case %d:\n",++tc);
getchar();
while(n--)
{
//cin >> c;
sf("%c",&c);
if(c == 'A')
{
//cin >> x;
sf("%lld",&x);
nmlen++; //cout << nmlen << " ";
num[nmlen] = x;
if(x >= maxi)
{
maxi = x;
arlen++;
ar[arlen] = maxi;
}
}
else if(c == 'R')
{
zar = num[nmlen];
if(zar == maxi)
{
if(nmlen >= 0)
{
nmlen--;
arlen--;
maxi = ar[arlen];
}
}
else
{
if(nmlen >= 0)
{
nmlen--;
}
}
}
else if(c == 'Q')
{
if(nmlen < 0)
{
//cout << "Empty\n";
pf("Empty\n");
}
else
{
//cout << maxi << "\n";
pf("%lld\n",maxi);
}
}
getchar();
}
}
return 0;
}
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<cmath>
#include<algorithm>
#define psb push_back
#define ppb pop_back
#define all(x) x.begin(),x.end()
#define N 1000100
#define sf scanf
#define pf printf
using namespace std;
typedef long long LL;
typedef vector<LL>vll;
LL ar[N] , num[N];
int main()
{
int t , tc=0;
//cin >> t;
sf("%d",&t);
while(t--)
{
LL n, x, arlen=-1, nmlen=-1 , i , maxi=-1 , zar;
char c;
//cin >> n;
sf("%lld",&n);
//cout << "Case " << ++tc << ":\n";
pf("Case %d:\n",++tc);
getchar();
while(n--)
{
//cin >> c;
sf("%c",&c);
if(c == 'A')
{
//cin >> x;
sf("%lld",&x);
nmlen++; //cout << nmlen << " ";
num[nmlen] = x;
if(x >= maxi)
{
maxi = x;
arlen++;
ar[arlen] = maxi;
}
}
else if(c == 'R')
{
zar = num[nmlen];
if(zar == maxi)
{
if(nmlen >= 0)
{
nmlen--;
arlen--;
maxi = ar[arlen];
}
}
else
{
if(nmlen >= 0)
{
nmlen--;
}
}
}
else if(c == 'Q')
{
if(nmlen < 0)
{
//cout << "Empty\n";
pf("Empty\n");
}
else
{
//cout << maxi << "\n";
pf("%lld\n",maxi);
}
}
getchar();
}
}
return 0;
}
Okay whatever, your solution is wrong if you don't notice that
ReplyDeleteUse this test case for example
1
10
A 5
A 4
A 3
A 2
A 1
Q
R
Q
R
Q
Answer should be:
5
4
3
In your code it appears as:
5
5
5
During picking up mangoes, Mina can have 3 types of question/instruction for you.
Delete1. Type 1: put an ‘x’ size mango in the basket, which is picked up form garden.
2. Type 2: Throw out last picked up mango. (There can be consecutive throw out operation. )
3. Type 3: Ask for the biggest mango size in the basket at that moment.
Please notice the point 3.
in your test case you have entered 5, 4 , 3 , 2, 1
then in query section you have pressed Q so the answer will be 5, because it is the current biggest,
then pressed R that means removed 1. then Q means answer will be 5 cause it is the current biggest which is existed in the jar.
then pressed R that means removed 2. after that, you have pressed Q that means the answer will be 5 cause it is the current biggest which is existed in the jar. :)
by the way, did you get AC by submit your code ?
Delete