CDOJ 1269 ZhangYu Speech 数组处理

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 ifB1 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 Input Sample 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趣味程序设计竞赛第四场(正式赛)

错因:看懂题意后就直接根据题意暴力求解了,结果O(n^2)的复杂度果断超时

总结:看到数组题后分析下时间复杂度再做,暴力一般都会超时,需要进行下转化

比如 求和,打表 之类

解答分析:水题

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int n[100005],s[100005];
char c[100005];
int main()
{
     int p,m,x;
     s[0]=0;
     while(~scanf("%d %d",&p,&m))
     {
         scanf("%s",c);
         for(int i=1;i<=p;i++)
            n[i]=c[i-1]-'0';
         for(int j=1;j<=p;j++)
            s[j]=s[j-1]+n[j];
         while(m--)
         {
         scanf("%d",&x);
         int temp=(x-1)*n[x]-s[x-1];
         if(temp>0)
            printf("Keep some distance from me
");
         else if(temp<0)
            printf("I agree
");
         else
            printf("Next time
");
         }
     }
     return 0;
}
原文地址:https://www.cnblogs.com/smilesundream/p/6642538.html