1487: 红桃X
Time Limit: 1 Sec Memory Limit: 128 MB
[Submit][Status][Web Board]
Description
从西安到杭州的火车实在是太漫长了,为了打发时间,H买了n张扑克牌 (一张一张卖的你们没见过吧¯▽¯) 牌面大小为 3....,9,10,J,Q,K,A,2,现在告诉你n张扑克牌的牌面(已按照从小到大的顺序理好),你能告诉我第k张红桃X在哪个位置吗?题目保证H买的牌全都是红桃的
Input
输入多组数据
第一行输入一个n (1<=n<=100000)表示H买了n张牌
第二行输入n个字符 a1,a2,a3,....,an
第三行输入一个数m(1<=m<=100000) 表示有m次询问
接下来m行每行输入一个整数k(1<=k<=n)和字符X
Output
输出第k张红桃X所在的位置,如果牌不存在则输出-1,每个输出占一行
Sample Input
5
3 9 9 J 2
5
1 3
2 3
2 9
1 2
1 J
Sample Output
1
-1
3
5
4
【分析】
TLE了很多次....需要scanf读取不能用cin
另外有一句话一开始没注意直接导致做法有问题...已经按照从小到大的顺序理好
有了这句话题目就简单....就只是给几个字母编个号然后算一下前缀和就好了....
一开始没看到那句顺序已经排好的话,用vector做的..代码蛮短的不过超时了....那就附上两个代码了
【代码】
//vector超时
#include <iostream>
#include <cstdio>
#include <vector>
#include <map>
using namespace std;
vector<int>f[200];
int main()
{
int n;
while (~scanf("%d",&n))
{
for (int i='2';i<='9';i++) f[i].clear();
f['I'].clear();f['J'].clear();f['K'].clear();f['Q'].clear();
for (int i=1;i<=n;i++)
{
string x;cin>>x;
if (x.length()>1) x="I";
char xx=x[0];
f[xx].push_back(i);
// cout<<f[xx][f[xx].size()-1]<<endl;
}
int m;scanf("%d",&m);
for (int i=0;i<m;i++)
{
int t;string x;cin>>t>>x;
if (x.length()>1) x="I";
char xx=x[0];
if (f[xx].size()<t) cout<<-1<<endl;
else
cout<<f[xx][t-1]<<endl;
}
}
}
//AC代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
map<string,int>f;
void init()
{
f["3"]=1;f["4"]=2;f["5"]=3;f["6"]=4;f["7"]=5;f["8"]=6;f["9"]=7;
f["10"]=8;f["J"]=9;f["Q"]=10;f["K"]=11;f["A"]=12;f["2"]=13;
}
int main()
{
init();
int n;
int a[100];
int b[100];
while(~scanf("%d",&n))
{
memset(a,0,sizeof(a));
for(int i=0;i<n;i++)
{
char x[5];
scanf("%s",x);
a[f[x]]++;
}
b[1]=a[1];
b[0]=0;
for(int i=2;i<14;i++) b[i]=b[i-1]+a[i];
int m;scanf("%d",&m);
for (int i=0;i<m;i++)
{
int t;char x[5];
scanf("%d%s",&t,x);
if(a[f[x]]<t) puts("-1");
else
printf("%d\n",b[f[x]-1]+t);
}
}
return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)