Skip to content

Instantly share code, notes, and snippets.

@gusanthiago
Created May 27, 2017 14:22
Show Gist options
  • Save gusanthiago/dc16dfc76d8468d4d02f87ad2590e1e7 to your computer and use it in GitHub Desktop.
Save gusanthiago/dc16dfc76d8468d4d02f87ad2590e1e7 to your computer and use it in GitHub Desktop.
Questoes parcialmente aceitas
#include <bits/stdc++.h>
using namespace std;
#define maxn 100010
const int inf = 0x3f3f3f3f;
int main () {
int n;
int nova;
vector<int> numeros;
cin >> n;
for(int i=0;i<n;i++) {
cin >> nova;
numeros.push_back(nova);
}
vector<int> direita(n);
vector<int> esquerda(n);
vector<int> novoDir;
vector<int> novoEsq;
int maxEsq = inf * -1,
sumEsq = 0,
maxDir = inf * -1,
sumDir = 0;
for (int i=0;i<n;i++) {
sumEsq += numeros[i];
maxEsq = max(maxEsq, sumEsq);
if (maxEsq == sumEsq)
novoEsq.push_back(numeros[i]);
esquerda[i] = maxEsq;
sumEsq = max(sumEsq, 0);
}
for (int i= n-1;i>=0;i--) {
sumDir += numeros[i];
maxDir = max(maxDir, sumDir);
direita[i] = maxDir;
sumDir = max(sumDir, 0);
}
int totMax = inf * -1;
for (int i=0,totN = n-1; i<totN; ++i) {
// cout << direita[i+1] << endl;
// cout << esquerda[i] << endl;
totMax = max(totMax, esquerda[i] * direita[i+1]);
}
cout << totMax << endl;
return 0;
}
#include <iostream>
using namespace std;
int knapsack(int W, int wt[], int n)
{
int V[n + 1][W + 1];
for(int w = 0; w <= W; w++)
V[0][w] = 0;
for(int i = 1; i <= n; i++)
V[i][0] = 0;
for(int i = 1; i <= n; i++)
{
for(int w = 1; w <= W; w++)
{
if(wt[i - 1] <= w)
{
if((1 + V[i - 1][w - wt[i - 1]]) > V[i - 1][w])
V[i][w] = 1 + V[i - 1][w - wt[i - 1]];
else
V[i][w] = V[i - 1][w];
}
else
V[i][w] = V[i - 1][w];
}
}
return V[n][W];
}
int main(int argc, char *argv[])
{
int W = 3,n; // TODO LER NÙMERO DE VISTAS
cin >> n >> W;
int wt[n];
for (int i=0;i<n;i++)
cin >> wt[i];
int max_valor = knapsack(n, wt, W);
cout << max_valor << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment