1083 - XJT Love Digits
Time Limit:1s Memory Limit:64MByte
Submissions:344Solved:107
DESCRIPTION
Define a function f(x) = the sum of digits of x, such as f(123) = 6. You will be given one sequence a1,a2,a3,...,ana1,a2,a3,...,an, and q queries. Each query contains a number x, you need to output max number p among a1,...,ax,ax−1a1,...,ax,ax−1,which satisfies f(p) = f(axax).
INPUT
The first line of input contains an integer
T(T≤5)T(T≤5) indicating the number of test cases.The first line of each case contains two interger
n(1≤n≤100000)n(1≤n≤100000) and
q(1≤q≤100000)q(1≤q≤100000).The second line contains n numbers:
a1,a2,...an(1≤ai≤109)a1,a2,...an(1≤ai≤109).Following q lines, each line contains a integer
x(1≤x≤n)x(1≤x≤n).
OUTPUT
For each case, output "Case #X:" in a line where X is the case number, staring from 1. Then for each query, output one line. Each line contains one integer p. If there is no such p among
a1,...,axa1,...,ax which satisfies f(p) = f(
axax), then just output -1.
SAMPLE INPUT
16 61 2 20 11 4 1654321
SAMPLE OUTPUT
Case #1:1-1202-1-1
SOLUTION
题目链接 http://www.ifrog.cc/acm/problem/1083
大意就是有一个函数, 这个函数的结果是一个数的所有位数的和。
然后 输入个x 查找1到x 之中 除了x 的最大值
f(p) = f(
axax
).
并且输出这个数
查询的东西排序,按照顺序记录最大值。
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <map>
#include <queue>
#include <iomanip>
#include <cstdio>
using namespace std;
map<int ,int >d;
map<int ,int >tag;
int a[100005];
int p[100005];
struct yu
{
int x,y,z;
} se[100005];
bool cmp(yu x,yu y)
{
return x.x<y.x;
}
bool cmp1(yu x,yu y)
{
return x.y<y.y;
}
int main()
{
int t;
scanf("%d",&t);
int ee=1;
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
// cin>>n>>m;
int x,sum;
for(int i=1;i<=n;i++)
{
//cin>>a[i];
scanf("%d",&a[i]);
}
int y,j=1;
for(int i=1;i<=m;i++)
{
//cin>>se[i].x;
scanf("%d",&se[i].x);
se[i].y=i;
} //cout<<endl;
sort(se+1,se+m+1,cmp);
for(int i=1;i<=m;i++)
{
y=a[se[i].x];
sum=0;
while(y>0)
{
sum+=y%10;
y/=10;
}
int s=sum;
if(tag[sum]==0) tag[sum]=se[i].x;
// cout<<sum<<' '<<tag[sum]<<endl;
for(;j<se[i].x;j++)
{
y=a[j];
sum=0;
while(y>0)
{
sum+=y%10;
y=y/10;
}
if(a[j]>=d[sum])
{
d[sum]=a[j];
tag[sum]=j;
}
//cout<<sum<<' '<<a[j]<<' '<<j<<' '<<d[sum]<<endl;
}
if(tag[s]!=se[i].x)
{
// cout<<tag[s]<<' '<<se[i].x<<" "<<d[s]<<' '<<s<<endl;
se[i].z=d[s];
}
else
{
//cout<<tag[s]<<' '<<se[i].x<<" "<<"-1"<<' '<<s<<endl;
se[i].z=-1;
}
}
sort(se+1,se+m+1,cmp1);//复位排序
cout<<"Case #"<<ee<<":"<<endl;
for(int i=1;i<=m;i++)
{
printf("%d\n",se[i].z);
//cout<<se[i].z<<endl;
}
d.clear();
tag.clear();
memset(a,0,sizeof(a));
memset(p,0,sizeof(p));
memset(se,0,sizeof(se));
ee++;
}
}