Array and Queries
Problem Statement :
Given an array, you are asked to perform a number of queries and divide the array into what are called, beautiful subsequences. The array A has length n. A function f(A) is defined to be a minimal possible , such that it's possible to divide array A into x beautiful subsequences. Note that each element of an array should belong to exactly one subsequence, and subsequence does not necessarily need to be consecutive. A subsequence S with length len is called beautiful if and only if: len = 1 or Let S' be a sorted version of S. It must hold that for every You need to find modulo . Input Format The first line contains a single integer , representing the length of array . The next line contains the array given as space-separated integers. The next line contains a single integer , representing the number of queries. Each of the lines contain two integers and , which is described above. Output Format Print the required answer in one line. Sample Input 0 5 2 2 1 1 1 2 3 2 5 5 Sample Output 0 11
Solution :
Solution in C++ :
special credits to :
Tara Mehta
startreckker@gmail.com
#include <bits/stdc++.h>
using namespace std;
long int mod=1000000007 ;
int main(){
int n;
cin>>n;
multiset<int> set1;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
set1.insert(arr[i]);
}
multiset<int> set2=set1;
int count=0;
while(set2.size()){
count++;
int cur=*(set2.begin());
set2.erase(set2.find(cur));
while((set2.find(cur+1))!=set2.end()){
cur++;
set2.erase(set2.find(cur));
}
}
long long int q;
cin>>q;
long long int ans=0;
for(long long int i=0;i<q;i++){
int id,val;
cin>>id>>val;
id--;
int curr=set1.count(arr[id]);
int prev=set1.count(arr[id]-1);
int next=set1.count(arr[id]+1);
//cout<<'\t'<<curr<<'\t'<<prev<<'\t'<<next<<'\n';
if(prev==0 && next==0)
count--;
else if(curr>max(prev,next))
count--;
else if(curr<=min(prev,next))
count++;
set1.erase(set1.find(arr[id]));
arr[id]=val;
curr=set1.count(arr[id]);
prev=set1.count(arr[id]-1);
next=set1.count(arr[id]+1);
//cout<<'\t'<<curr<<'\t'<<prev<<'\t'<<next<<'\n';
if(prev==0 && next==0)
count++;
else if((curr+1)>max(prev,next))
count++;
else if((curr+1)<=min(prev,next))
count--;
set1.insert(val);
ans=(ans+((i+1)*(count))%mod)%mod;
}
cout<<ans;
return 0;
}
View More Similar Problems
Castle on the Grid
You are given a square grid with some cells open (.) and some blocked (X). Your playing piece can move along any row or column until it reaches the edge of the grid or a blocked cell. Given a grid, a start and a goal, determine the minmum number of moves to get to the goal. Function Description Complete the minimumMoves function in the editor. minimumMoves has the following parameter(s):
View Solution →Down to Zero II
You are given Q queries. Each query consists of a single number N. You can perform any of the 2 operations N on in each move: 1: If we take 2 integers a and b where , N = a * b , then we can change N = max( a, b ) 2: Decrease the value of N by 1. Determine the minimum number of moves required to reduce the value of N to 0. Input Format The first line contains the integer Q.
View Solution →Truck Tour
Suppose there is a circle. There are N petrol pumps on that circle. Petrol pumps are numbered 0 to (N-1) (both inclusive). You have two pieces of information corresponding to each of the petrol pump: (1) the amount of petrol that particular petrol pump will give, and (2) the distance from that petrol pump to the next petrol pump. Initially, you have a tank of infinite capacity carrying no petr
View Solution →Queries with Fixed Length
Consider an -integer sequence, . We perform a query on by using an integer, , to calculate the result of the following expression: In other words, if we let , then you need to calculate . Given and queries, return a list of answers to each query. Example The first query uses all of the subarrays of length : . The maxima of the subarrays are . The minimum of these is . The secon
View Solution →QHEAP1
This question is designed to help you get a better understanding of basic heap operations. You will be given queries of types: " 1 v " - Add an element to the heap. " 2 v " - Delete the element from the heap. "3" - Print the minimum of all the elements in the heap. NOTE: It is guaranteed that the element to be deleted will be there in the heap. Also, at any instant, only distinct element
View Solution →Jesse and Cookies
Jesse loves cookies. He wants the sweetness of all his cookies to be greater than value K. To do this, Jesse repeatedly mixes two cookies with the least sweetness. He creates a special combined cookie with: sweetness Least sweet cookie 2nd least sweet cookie). He repeats this procedure until all the cookies in his collection have a sweetness > = K. You are given Jesse's cookies. Print t
View Solution →