Created
January 8, 2016 01:12
-
-
Save MagallanesFito/07c0d763d36982f05ce0 to your computer and use it in GitHub Desktop.
Solucion gasto mensual. Búsqueda Binaria
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #define op_io ios_base::sync_with_stdio(0);cin.tie(0); | |
| using namespace std; | |
| bool posible(int A[],int hi,int dias,int n){ | |
| int suma = 0; | |
| int curr_dias = 1; | |
| for(int i=0;i<n;i++){ | |
| if(A[i] > hi) return false; | |
| if(suma + A[i] <= hi){ | |
| suma+=A[i]; | |
| } | |
| else{ | |
| curr_dias++; | |
| suma = A[i]; | |
| } | |
| } | |
| if(curr_dias<=dias) return true; | |
| else return false; | |
| } | |
| long int getMinimo(int A[],int lo,int hi,int n,int dias){ | |
| if(lo == hi){ | |
| return lo; | |
| } | |
| else{ | |
| int m = (lo+hi)/2; | |
| if(posible(A,m,dias,n)) return getMinimo(A,lo,m,n,dias); | |
| else return getMinimo(A,m+1,hi,n,dias); | |
| } | |
| } | |
| int main(){ | |
| op_io | |
| long int n,dias; | |
| cin>>n>>dias; | |
| int dinero[n]; | |
| for(int i=0;i<n;i++)cin>>dinero[i]; | |
| int maximo = 0; | |
| for(int i=0;i<n;i++) maximo+=dinero[i]; | |
| cout << getMinimo(dinero,dinero[0],maximo,n,dias); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment