The topic is simple, understanding is no problem, violence alone can score a lot. So the method is many, the most easy to think is in the violence optimization, in the way of two points can also, but the method is still a little trouble, so to use the monotonous queue, monotonous stack. I'm using a monotone queue here.
According to the topic, there are only two operations, one increment and one output maximum. So first you have an array to record the current queue, and then you need a monotone queue record maximum to make it easy to find the maximum value. For what is a monotonic stack, here is a simple example: if there is a column number 1,5,6,2,4,3,1. It is arranged in a monotonically descending order: A number of--1, two number when--5, three number when--6, four number when--6,2, five number when--6,4, six number when--6,4,3, seven number when--6,4,3,1. Some of the details in the title can be a, please.
A lot of methods, depending on the situation, if you do not understand the monotonous stack, monotonous queue of the search Baidu. (Address: Http://baike.baidu.com/link?url=u4hRLKsFmBCcIldHxh6cqy8qGsGJhs68nI9k_d3nxL3lEBTX_LWyosNHbcjO-JGJ_ L4s6ew3wn6qvet54deehq,http://baike.baidu.com/link?url=6gqfwc4radbhkacshiztbewz7rtjxqs__ PNQ23M835QV2EKGI1Y1--LX1OUD2T6NZVNOYKINYHCVSUWOJAXNJQ)
#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<queue>#include<string>using namespacestd;intI_caozuo,i_mo;//the operand, modulo value. inti_shuzu[200001],i_temp,i_max[200001];//number in array, number of deposits in, monotone queue (decrement)inti_last=0, I_shuru;//The result of the last calculation, the value entered. Chari_panduan[1];//the input judgment character. intMain () {scanf ("%d%d", &i_caozuo, &i_mo);//enter the operand and modulo. while(i_caozuo--)//use the While loop for each operation. {scanf ("%s%d", Q,&i_shuru);//array operation judgment and numerical value. if(i_panduan[0]=='A')//if it is judged as ' A ', the array is incremented. {i_shuzu[++i_temp]= (I_last+i_shuru)%i_mo;//the newly added value. for(inti=i_temp;i;i--) { if(I_max[i]<i_shuzu[i_temp]) i_max[i]=I_shuzu[i_temp]; //The monotonic queue is judged so that the max array is always monotonically reduced. Else Break; } } Elseprintf"%d\n", i_last=i_max[i_temp-i_shuru+1]); //If the judgment is ' Q ', then the output operation is performed. The output value is recorded at the same time. } return 0;}
Bzoj 1012 Maximum number MaxNumber