您的位置:首页 > 产品设计 > UI/UE

UESTC 1269 ZhangYu Speech 预处理、前缀和

2015-12-20 18:12 357 查看


ZhangYu Speech


Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)


Submit
Status

as we all know, ZhangYu(Octopus vulgaris) brother has a very famous speech - "Keep some distance from me". ZhangYu brother is so rich that
everyone want to contact he, and scfkcf is one of them. One day , ZhangYu brother agreed with scfkcf to contact him if scfkcf could beat him.
There are n digits(lets
give them indices from 1 to n and
name them a1,a2...aN
) and some queries.
for each query:

ZhangYu brother choose an index x from 1 to n.
For all indices y ( y < x)
calculate the difference by=ax−ay.
Then ZhangYu brother calculate B1 ,the
sum of all by which are greater than 0 ,
and scfkcf calculate B2 ,
the sum of all by which are
less than 0.

if B1>|B2| ,
ZhangYu brother won and did not agree with scfkcf to contact him; else if B1 is
equals to |B2| ,
ZhangYu brother would ignore
the result; else if B1< |B2| ,
ZhangYu brother lost and agreed with scfkcf to contact him.


Input

The first line contains two integers n, m (1≤n,m≤100000) denoting
the number of digits and number of queries. The second line
contains n digits
(without spaces) a1,a2,...,an.(0≤ai≤9)
Each of next m lines
contains single integer x (1≤x≤n) denoting
the
index for current query.


Output

For each of m queries
print "Keep some distance from me" if ZhangYu won, else print "Next time" if ZhangYu brother ignored the result, else
print "I agree" if ZhangYu brother lost in a line - answer of the query.


Sample input and output

Sample InputSample Output
10 3
0324152397
1
4
7

Next time
Keep some distance from me
I agree


Hint

It's better to use "scanf" instead of "cin" in your code.


Source

第七届ACM趣味程序设计竞赛第四场(正式赛)B


My Solution

打表搞出前n项和;//像这样输入一次数据,然后n(n<1e6)次询问的,一般要预处理一下,来减少复杂度;

主要是找出x前的所有数的和与line[x-1]*x比较,如果前面的和大一点,说明负数和大;如果小,说明正数和大;相等,则相等;

这样省去了每次做差的麻烦;

#include <iostream>
#include <cstdio>
//#define LOCAL
const int maxn=100008;
char ch[maxn];
int line[maxn],sum[maxn];
using namespace std;

int main()
{
    #ifdef LOCAL
    freopen("a.txt","r",stdin);
    #endif // LOCAL
    int n,m,x,t;
    scanf("%d%d",&n,&m);
    scanf("%s",ch);
    //直接先转化过来,这样扫一遍就好了。不然要好多边临时的转化
    for(int i=0;i<n;i++){
        line[i]=ch[i]-'0';
    }
    //直接预处理出前i项和  前缀和
    t=0;
    for(int i=0;i<n;i++){
    	t+=line[i];
    	sum[i]=t;
    }
    //每次询问直接用sum[x-1];
    while(m--){
        scanf("%d",&x);

        //x1=ch[x-1]-'0';//cout<<x1<<" ";
        if(sum[x-1]-line[x-1]*(x)<0) printf("Keep some distance from me");
        else if(sum[x-1]-line[x-1]*(x)>0) printf("I agree");
        else if(sum[x-1]-line[x-1]*(x)==0)printf("Next time");
        if(m) printf("\n");
    }

    return 0;
}


谢谢
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: