I am getting time limit exceeded in hotels from spoj .
Here 's my code:
int main()
{
long long n,m;
scanf("%lld",&n);
scanf("%lld",&m);
long long a[300005],dp[300005];
for(int i=0;i<n;i++) scanf("%lld",&a[i])
for(int i=1;i<n;i++) a[i]+=a[i-1];
for(int i=n-1;i>=0;i--)
{
if(a[i]<m){
dp[0] = a[i];
break;
}
}
for(int i=1;i<n;i++)
{
long long x = a[i-1];
for(int j=i;j<n;j++)
{
a[j]-=x;
}
for(int j=n-1;j>=i;j--)
{
if(a[j]<=m){
dp[i] = a[j];
break;
}
}
}
long long max_=0;
for(int i=0;i<n;i++)
{
max_ = max(max_,dp[i]);
}
printf("%lld\n",max_);
return 0;
}
Explaination:
First of all, i calculated all the values in the array "a" by summing with previous values.Then, calulated the maximum value less than equal to "m" in dp array.Then, i started subtracting from the previous minus the current value from each element of array and stored value in dp array.So,max of dp array would give me the answer.
for e.g. :
n=5,m=12;
2 1 3 4 5
My array 'a':
2 3 6 10 15
then after 2nd pass :
2 1 4 8 13
then after 3rd pass :
2 1 3 7 12
Here's the code in action:
Ideone
This problem has complexity O(n).
#include <iostream>
using namespace std;
int a[300013];
int main(){
int n,m;
scanf("%d%d",&n,&m);
for(int i=0; i<n; i++) scanf("%d",&a[i]);
long long sumNow = 0;
long long maxAns = 0;
int left=0,right=-1;
while(right!= n-1){
right++;
sumNow+=a[right];
while(sumNow>m){
sumNow-=a[left];
left++;
}
maxAns = max(maxAns,sumNow);
}
printf("%lld", maxAns);
return 0;
}
Related
I have a task to find the biggest number in 2D array, except the a[2][1] element.
The input is:
4
4 2 3 4
8 5 9 6
3 9 8 4
6 4 2 3
The output should be:
9
Im getting the output
8
Since there are two 9's in the array, I dont know how to fix it.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int n;
int a[10][10];
cin>>n;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>a[i][j];
}
}
int max=1;
for(int i=0;i<n;++i){
for(int j=0;j<n;++j){
if(a[i][j]==a[2][1]){
continue;
}
if(a[i][j]>max){
max=a[i][j];
}
}
}
cout<<max<<endl;
return 0;
}
Since there are two 9's in the array, I dont know how to skip over the a[2][1] element.
You are ignoring the value of a[2][1] you should ignore the index pair (2,1) instead:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int n;
int a[10][10];
cin>>n;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>a[i][j];
}
}
int max=1;
for(int i=0;i<n;++i){
for(int j=0;j<n;++j){
if(i == 2 && j == 1){
continue;
}
if(a[i][j]>max){
max=a[i][j];
}
}
}
cout<<max<<endl;
return 0;
}
Take as input N, the size of array. Take N more inputs - digits between 0 and 9 - and store that in an array. Take as input M, the size of second array and take M more input digits and store that in second array. Write a function that returns the sum of the numbers represented by the two arrays. Print the value returned.
Input:
4
1 0 2 9
5
3 4 5 6 7
Output:
3, 5, 5, 9, 6, END
Below is the code I have written for it, but it's not giving any result rather it crashes.
using namespace std;
int main()
{
int tsum,n,m,s,carry=0;
cin>>n;
int a[n],sum[]={0};
for(int i=0; i<n; i++)
{
cin>>a[i];
}
cin>>m;
int b[m];
for(int i=0; i<m; i++)
{
cin>>b[i];
}
s=max(n,m);
sum[s] = {0};
while(n>0 and m>0)
{
tsum=a[n]+b[m]+carry;
sum[s] = tsum%10;
carry = tsum/10;
n--;
m--;
s--;
}
if(n>m)
{
while(n>0)
{
tsum=a[n]+carry;
sum[s] = tsum%10;
carry = tsum/10;
n--;
s--;
}
}
if(m>n)
{
while(m>0)
{
tsum=b[m]+carry;
sum[s] = tsum%10;
carry = tsum/10;
m--;
s--;
}
}
for (int i=1; i<s; i++)
{
cout<<sum[i]<<", ";
}
cout<<"END";
return 0;
}```
I was trying to make a program that swaps all the biggest and smallest numbers in an array or a vector. I came up with a program but for some reason I'm not able to debug it to get the problem. Its not printing the vector, neither do I know what the issue is. Can anyone please help me.
Desired Input and Output
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
int n;
cin>>n; //size input
vector<int> arr;
for(int i=0;i<n;i++) //filling up the vector
{
int input;
cin>>input;
arr.push_back(input);
}
vector<int> arr1=arr; //copying the vector
sort(arr1.begin(), arr1.end()); //sorting the new vector
int i=0,j=n-1,i1=0,j1=n-1; //i and j are for the vector arr & i1 and j1 are for vector arr1
while(i1<j1)
{
if(arr1[i1]==arr[i] && arr1[j1]==arr[j]) //if the first and last number of the sorted vector is found in arr the swap
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
i1++;
j1--;
i=0; // i and j are set to initial value so that it is checked from the start
j=n-1;;
}
else if(arr1[i1]<arr[i] && arr1[j1]==arr[j]) //if only the biggest place element is found the increase i
{
i++;
}
else if(arr1[i1]==arr[i] && arr1[j1]>arr[j]) //if only the smallest place element is found the decrease j
{
j--;
}
else if(arr1[i1]!=arr[i] && arr1[j1]!=arr[j]) //if none of them are found then increase i and decrease j
{
i++;
j--;
}
}
for(int f=0;f<n;f++) //print the vector
cout<<arr[f]<<" ";
return 0;
}
/*
Sample input 1
6
12 34 87 56 38 98
Sample output 1
98 87 34 38 56 12
Sample input 2
6
8 7 9 2 4 6
Sample output 2
4 6 2 9 8 7
*/
Some help would be really appreciated.
A simpler method would be to make a vector of indexes rather than sorting the vector and trying to find the values in the original vector. For example:
#include <iostream>
#include <algorithm>
#include <vector>
int main()
{
int n;
std::cin >> n;
std::vector<int> arr;
std::vector<int> indexes;
for (int i = 0; i < n; i++)
{
int input;
std::cin >> input;
arr.push_back(input);
indexes.push_back(i);
}
std::sort(indexes.begin(), indexes.end(), [&](int a, int b) {return arr[a] < arr[b]; });
for (int i = 0, j = n - 1; i < n / 2; i++, j--)
{
std::swap(arr[indexes[i]], arr[indexes[j]]);
}
for (int f = 0; f < n; f++)
{
std::cout << arr[f] << " ";
}
return 0;
}
to resolve a codeforces problem
I have written this c++ code and i have a big problem while displaying the result: in fact,if i add this code :
cout<<"t2simplifier"<<endl;
aff(t2simplifier);
cout<<endl;
aff(t2primsimplifier);
cout<<endl;
the result will be correct
Otherwise it will be wrong
The code:
#include <bits/stdc++.h>
using namespace std;
void aff (vector<int> v)
{
for (int i=0;i<v.size();i++)
cout<<v[i]<<"|";
}
int main()
{
int n;
cin>>n;
int t1[2][n];
vector <int> t2 ;
vector <int> t2prim ;//flous
vector <int> t2simplifier ;
vector <int> t2primsimplifier ;//flous
vector <int> t3prim ;//flous
for(int i=0;i<n;i++)
cin>>t1[0][i];
for(int i=0;i<n;i++)
cin>>t1[1][i];
for (int i = 0;i<n;i++)
for (int j = i+1 ;j<n ;j++)
{
if(t1[0][i]<t1[0][j])
{
t2.push_back(j);
t2prim.push_back(t1[1][i]+t1[1][j]);
}
}
// cout<<"t2"<<endl;
// aff(t2);
// cout<<endl;
// aff(t2prim);
// cout<<endl;
//pour simplifier t2 et t2prim
int minn;
for (int i = 1;i<n;i++)
{
minn==1000000000;
for (int j = 0 ;j<n ;j++)
{
if((t2[j]==i)&&(t2prim[j]<minn))
{
minn=t2prim[j];
}
}
t2simplifier.push_back(i);
t2primsimplifier.push_back(minn);
}
cout<<"t2simplifier"<<endl;
aff(t2simplifier);
cout<<endl;
aff(t2primsimplifier);
cout<<endl;
for (int i = 0;i<t2simplifier.size();i++)
for (int j = t2simplifier[i] ;j<n ;j++)
{
if(t1[0][t2simplifier[i]]<t1[0][j])
{
t3prim.push_back(t2primsimplifier[i]+t1[1][j]);
}
}
// cout<<"t3prim";
// aff(t3prim);
if (t3prim.size()==0)
cout<<-1;
else
{
//talla3 min
int k = t3prim[0];
for (int i = 1;i<t3prim.size();i++)
{
if(k>t3prim[i])
k=t3prim[i];
}
// k is the result
cout << k ;
}
return 0;
}
input :
5
2 4 5 4 10
40 30 20 10 40
the result with the part of code is :
t2simplifier
1|2|3|4|
70|50|50|50|
90
but when we delete this piece of code(of vector display)
the result will be wrong :
24
Thanks to Mr "François Andrieux"
in the code there is a typing error
The line minn == 1000000000;is performing a comparison and it should be minn = 1000000000;
this error leads to an undefined behavior from reading from an uninitialized variable.
Given N cards where if ith card has number x on its front side then it will have -x on back side and a single operation that can be done only once that is to flip any number of cards in consecutive order only once.
Now we need to flip cards in such a way that sum of number of upper face of cards is maximum.
Example : If N=5 and cards[] be {-2,3,-1,-4,-2} then here answer is 8 as we can flip last 3 cards to get configuration {-2,3,1,4,2} which sum to 8.
My Approach :
Go for each possible way for each ith position as start position and find the maximum.But is their any better solution to this problem?
My Code : Am not able to find problem till yet
#include<bits/stdc++.h>
using namespace std;
int solve(std::vector<int> const & numbers)
{
int min_so_far = numbers[0], min_ending_here = numbers[0];
size_t begin = 0;
size_t begin_temp = 0;
size_t end = 0;
for(size_t i = 1; i < numbers.size(); i++)
{
if(min_ending_here > 0)
{
min_ending_here = numbers[i];
begin_temp = i;
}
else
{
min_ending_here += numbers[i];
}
if(min_ending_here <= min_so_far )
{
min_so_far = min_ending_here;
begin = begin_temp;
end = i;
}
}
int sum=0;
for(int i=0;i<begin;i++){
sum+=numbers[i];
}
for(int i=begin;i<=end;i++){
sum-=numbers[i];
}
for(int i=end+1;i<numbers.size();i++){
sum+=numbers[i];
}
return sum;
}
int main(){
int n;
cin>>n;
vector<int> arr;
for(int i=0;i<n;i++){
int x;
cin>>x;
arr.push_back(x);
}
cout<<solve(arr)<<"\n";
}
The only thing you need to find is the minimum sum that you can form with consecutive numbers, and then flip those. In your example, the last three numbers add up to -7, and there is no other set of consecutive number which have a lower sum, so flipping them does the trick. If the minimum sum is non negative, then you don't need to flip them.
Now, what I described above is a well known algorithm, and it is called Kadane's algorithm, which can be solve in O(n), notice that the Wikipedia link shows how to do it for the maximum, but you can easily modify it to find the minimum.
I used Kadane's algorithm approach in this and the Minimum Subarray Sum function returns the minimum sum in O(n) and since we already had the sum of all elements of the array so we will add (-2) time of min_sum as it was deducted once.
#include<bits/stdc++.h>
using namespace std;
int minsubarraysum(int a[], int n) {
int min_sum = INT_MAX;
int curr_sum = 0;
for (int i = 0 ; i < n; i++) {
curr_sum = curr_sum + a[i];
if (curr_sum < min_sum)
min_sum = curr_sum;
if (curr_sum > 0)
curr_sum = 0;
}
return min_sum;
}
int main() {
int n;
cin >> n;
int a[n];
int sum = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
}
int min_sum = minsubarraysum(a, n);
int ans = sum + (min_sum * (-2));
cout << ans;
}