Poj 4090: Super Memorandum

Source: Internet
Author: User
Poj 4090: Super Memorandum
Question:
Description

Your friend Jackson is invited to a TV program called "Super memorandum. In this program, participants need to play a memory game. At the beginning, the host will tell all participants a series of {A1, A2,..., }. Next, the host will perform the following operations on the series:

  1. Add x y D: Add D to the subsequence {ax,..., ay. For example, if you operate "add 2 4 1" on {1, 2, 3, 4}, you will get {1, 3, 4, 5, 5 }.

  2. Reverse x y: deploy the subsequence {ax,..., ay} in reverse order. For example, if you operate "reverse 2 4" on {1, 2, 3, 4, 5}, you will get {1, 4, 3, 2, 5 }.

  3. Revolve x y t: rotate the subsequence {ax,..., ay} t times. For example, if you operate "revolve 2 4 2" on {1, 2, 3, 4}, you will get {1, 3, 4, 2, 5 }.

  4. Insert x P: insert P after ax. For example, if you operate "insert 2 4" on {1, 2, 3, 4, 5}, you will get {1, 2, 4, 3, 4, 5 }.

  5. Delete X: delete ax. Insert P after ax. For example, if you operate "Delete 2" on {1, 2, 3, 4, 5}, you will get {1, 3, 4, 5 }.

  6. Min x y: queries the minimum value in the subsequence {ax,..., ay. For example, the correct answer for executing "Min 2 4" on {1, 2, 3, 4, 5} is 2.

In order to make the program look better, every contestant has the opportunity to call the outside audience for help in case of any difficulty. Your task is to watch this TV program, and then write a program to calculate the results for every query, so that Jackson can give the correct answer whenever he calls for help.

Input
The first row contains a number n (n ≤ 100000 ).
The next n rows show the number in the sequence.
The next line contains a number of M (m ≤ 100000), describing the number of operations and queries.
All the operations and inquiries are given in the next m row.
The most in-depth question in the solution is rotation. So how can we analyze the rotation of an array only? Is the violence law acceptable? Yes, but it is very slow. What should we do? If there are many experiments, we will find a rule. We set the length to the length of the array, and the number of times to rotate is n (0 <n <length)
Can we divide M (= n <(length-N) by length )? N: length-N) is divided into two types of design algorithms, but in each case, the time complexity is O (length), so this is faster. Let's first look at the situation that can be divided.
Length = 6; M = 1; length % m = 0; A total of m large loops
Length = 6; M = 2; length % m = 0; A total of m large loops



In the second case, length = 7; n = 4; M = 3; length % m = 1; in this case, you can use only one large loop.

Code
#include <iostream>using namespace std;class intList{int * data;int length;public:intList(){data = NULL;length = 0;}void add( int x,int y,int D ){for( int i=x;i<=y;i++ ){data[i-1] += D;}}void reverse( int x,int y ){for(int i = 0; i < (y-x+1)/2 ; i++){int d = data[x+i-1];data[x+i-1] = data[y-i-1];data[y-i-1] = d;}}void revolve(int x,int y,int T){int len = y - x + 1;int m = T<(len-T)? T:(len - T);T = T % len;if( T != 0 )if( len % m == 0 ){for(int i=0;i<m;i++){int flag = i;int flag_data = data[i+x-1];int j = i ;while ( (j+T)%len != flag ){int d = data[(j+T)%len + x-1];data[(j+T)%len + x-1] = flag_data ;flag_data = d;j = (j+T)%len ;}data[flag+x-1] = flag_data;}}else{int flag = 0;int flag_data = data[flag+x-1];int j = 0;while( (j+T)%len != flag ){int d = data[(j+T)%len + x-1] ;data[(j+T)%len + x-1] = flag_data ;flag_data = d ;j = (j+T)%len ;}data[flag] = flag_data ;}}void insert( int x,int P ){length++ ;int * big_data = new int[ length ];for(int i=length-1;i>=x;i--)big_data[i] = data[i-1];big_data[x-1] = P;for(int i=x-2;i>=0;i--)big_data[i] = data[i];delete [] data;data = big_data;}void delete_x(int x){for( int i = x-1;i < length-1 ; i++ ){data[i] = data[i+1];}length -- ;}int min(int x,int y){int min = INT_MAX;for(int i=x; i<=y; i++){if(data[i-1] < min )min = data[i-1];}return min;}void print_list(){for( int i=0; i<length; i++ ){cout<<data[i]<<" ";}cout<<endl;}};int main(){intList mylist;for(int i=1;i<10;i++)mylist.insert(i,i);mylist.print_list();mylist.insert( 1,4 );mylist.add( 1,9,1 );mylist.print_list( );mylist.delete_x( 3 );mylist.print_list( );cout<<mylist.min(5,9)<<endl;mylist.revolve(1,6,4);mylist.print_list( );mylist.reverse(1,4);mylist.print_list();system("pause");return 0;}


Poj 4090: Super Memorandum

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.