UESTC 1269 ZhangYu Speech 预处理、前缀和

来源:互联网 发布:江门美工家具定制 编辑:程序博客网 时间:2024/06/07 09:28

ZhangYu Speech

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

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:

  1. ZhangYu brother choose an index x from 1 to n.
  2. For all indices y ( y < x) calculate the difference by=axay.
  3. 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 
  4. 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 nm (1n,m100000) denoting the number of digits and number of queries. The second line 

contains n digits (without spaces) a1,a2,...,an.(0ai9)   Each of next m lines contains single integer x (1xn) 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 30324152397147
Next timeKeep some distance from meI 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 LOCALconst 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;}

谢谢

0 0