UESTC1269 (数组优化)

Description

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  digits(lets give them indices from  to  and name them ) and some queries.

for each query:

  1. ZhangYu brother choose an index  from  to .
  2. For all indices  (  < ) calculate the difference .
  3. Then ZhangYu brother calculate  ,the sum of all by which are greater than  , and scfkcf calculate  , the sum of all by which are less than .

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

Input

The first line contains two integers   denoting the number of digits and number of queries. The second line contains  digits (without spaces) . 
Each of next  lines contains single integer   denoting the index for current query.

Output

For each of  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

10 3 
0324152397 


7

Sample Output

Next time 
Keep some distance from me 
I agree

Hint

It‘s better to use “scanf” instead of “cin” in your code.

分析题吧, 多用一个数组, 少一层循环

#include <cstdio>
const int N = 100001;
int a[N], b[N];
char c[N];
int main()
{
    int n, m;
    while(scanf("%d%d", &n, &m) != EOF)
    {
        b[0]=0;
        scanf("%s", c);
        for(int i = 1; i <= n; i++)
            a[i]=c[i-1]-‘0‘;
        for(int i = 1; i <= n; i++)
            b[i]=b[i-1]+c[i-1]-‘0‘;
        for(int i = 0; i < m; i++)
        {
            int d;
            scanf("%d", &d);
            int tem=(d-1)*a[d]-b[d-1];
            if(tem>0) printf("Keep some distance from me\n");
            else if(tem==0) printf("Next time\n");
            else printf("I agree\n");
        }
    }
    return 0;
}
时间: 2024-11-10 17:50:50

UESTC1269 (数组优化)的相关文章

HDU - 1024 Max Sum Plus Plus 滚动数组优化

给定n个数字,求其中m段的最大值(段与段之间不用连续,但是一段中要连续) 例如:2 5 1 -2 2 3 -1五个数字中选2个,选择1和2 3这两段. dp[i][j]从前j个数字中选择i段,然后根据第j个数字是否独立成一段,可以写出 状态转移方程:dp[i][j]=max(dp[i][j-1]+num[j],max(dp[i-1][k])+num[j]) 这里的max(dp[i-1][k])代表的拥有i-1段时的最大值,然后再加上num[j]独立成的一段. 但是题目中没有给出m的取值范围,有可

HDU - 1024 Max Sum Plus Plus(dp+滚动数组优化)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1024 题意:给定n个数字,求其中m段的最大值(段与段之间不用连续,但是一段中要连续) 例如:2 5 1 -2 2 3 -1五个数字中选2个,选择1和2 3这两段. 题解:dp[i][j]从前j个数字中选择i段,然后根据第j个数字是否独立成一段,可以写出 状态转移方程:dp[i][j]=max(dp[i][j-1]+num[j],max(dp[i-1][k])+num[j]) 这里的max(dp[i-

Codeforces 909 C. Python Indentation (DP+树状数组优化)

题目链接:Python Indentation 题意: Python是没有大括号来标明语句块的,而是用严格的缩进来体现.现在有一种简化版的Python,只有两种语句: (1)'s'语句:Simple statements. 相当于一般语句.(2)'f'语句:For statements. 相当于for循环,并且规定它的循环体不能为空. 然后给你一段没有缩进的Python程序,共n行(n <= 5000).问你添加缩进后,有多少种合法且不同的Python程序. 题解:题目解析 DP过去,如果第i个

HDU 6240 Server(2017 CCPC哈尔滨站 K题,01分数规划 + 树状数组优化DP)

题目链接  2017 CCPC Harbin Problem K 题意  给定若干物品,每个物品可以覆盖一个区间.现在要覆盖区间$[1, t]$. 求选出来的物品的$\frac{∑a_{i}}{∑b_{i}}$的最小值. 首先二分答案,那么每个物品的权值就变成了$x * b_{i} - a_{i}$ 在判断的时候先把那些权值为正的物品全部选出来, 然后记录一下从$1$开始可以覆盖到的最右端点的位置. 接下来开始DP,按照区间的端点升序排序(左端点第一关键字,右端点第二关键字) 问题转化为能否用剩

HDU 6447 - YJJ&#39;s Salesman - [树状数组优化DP][2018CCPC网络选拔赛第10题]

Problem DescriptionYJJ is a salesman who has traveled through western country. YJJ is always on journey. Either is he at the destination, or on the way to destination.One day, he is going to travel from city A to southeastern city B. Let us assume th

LUOGU P2344 奶牛抗议 (树状数组优化dp)

传送门 解题思路 树状数组优化dp,f[i]表示前i个奶牛的分组的个数,那么很容易得出$f[i]=\sum\limits_{1\leq j\leq i}f[j-1]*(sum[i]\ge sum[j-1])$,但是这样的时间复杂度是$O(n^2)?$,所以考虑优化,发现必须满足$sum[i]\ge sum[j-1]?$才能进行转移,那么直接离散化后用树状数组维护一个前缀和即可. #include<iostream> #include<cstdio> #include<cstr

HDU - 5542 The Battle of Chibi(LIS+树状数组优化)

The Battle of Chibi Cao Cao made up a big army and was going to invade the whole South China. Yu Zhou was worried about it. He thought the only way to beat Cao Cao is to have a spy in Cao Cao's army. But all generals and soldiers of Cao Cao were loya

POJ 1159 回文LCS滚动数组优化

详细解题报告可以看这个PPT 这题如果是直接开int 5000 * 5000  的空间肯定会MLE,优化方法是采用滚动数组. 原LCS转移方程 : dp[i][j] = dp[i - 1][j] + dp[i][j -1] 因为 dp[i][j] 只依赖于 dp[i - 1][j] 和 dp[i][j - 1] 所以可以采用滚动数组如下: dp[i % 2][j] = dp[(i - 1) % 2][j] + dp[i % 2][j - 1] 可以实现节省空间的方法 答案存储在 dp[n % 2

【转】关于LIS和一类可以用树状数组优化的DP 预备知识

原文链接 http://www.cnblogs.com/liu-runda/p/6193690.html 预备知识 DP(Dynamic Programming):一种以无后效性的状态转移为基础的算法,我们可以将其不严谨地先理解为递推.例如斐波那契数列的递推求法可以不严谨地认为是DP.当然DP的状态也可以是二维/三维的,某一维的含义也不仅仅是指某个数列的第几项. 树状数组(BIT or fenwick tree):一种高效地动态维护一个序列并动态求取前缀和的数据结构.修改某个元素/求一次前缀和的