Skip to content

Commit 682812e

Browse files
committed
DP/
1 parent 48caf8e commit 682812e

File tree

2 files changed

+165
-0
lines changed

2 files changed

+165
-0
lines changed

UVA/10131-is-bigger-smarter.cpp

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// problem:
2+
// https://s3-us-west-2.amazonaws.com/prod-runcodes-files/exercisefiles/14552/p10131.pdf?X-Amz-Content-Sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAI3OUHGVPPSQMGO5Q%2F20200517%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20200517T182414Z&X-Amz-SignedHeaders=Host&X-Amz-Expires=120&X-Amz-Signature=ec4ec7d41dde38e46f3cb6a81112e5a8fca7a722dbe9ba4602681a0bb5712fb8
3+
#include <iostream>
4+
#include <vector>
5+
#include <map>
6+
#include <queue>
7+
#include <string>
8+
#include <string.h> // memset
9+
#include <algorithm> // lower_bound
10+
#include <bitset>
11+
12+
using namespace std;
13+
14+
#define fs first
15+
#define sd second
16+
#define pb push_back
17+
// #define mp make_pair
18+
#define MAXN 100001
19+
20+
const int INF = 0x3f3f3f3f;
21+
//priority_queue<pair<int, vector<pair<int, int>>>> elem;
22+
#define M 10001
23+
24+
struct Elephant{
25+
int id,
26+
w,
27+
s;
28+
};
29+
30+
bool mySort(const Elephant& a, const Elephant& b){
31+
return (a.w != b.w)? (a.w < b.w) : (a.s > b.s);
32+
}
33+
34+
bitset<MAXN> dp;
35+
Elephant elem[MAXN];
36+
37+
38+
int lis(int n) {
39+
vector<pair<int, int>> v;
40+
vector<int> topo, // QI
41+
ans; // subsequence of elephantes
42+
vector<int>::iterator it;
43+
int ultimoTopo;
44+
45+
/*************************/
46+
// primeira iteração, inicializa topo
47+
48+
//cout << v[0].first << ' ' << v[0].second << endl;
49+
50+
// armazeno as tres informações ordenadas pelo maior peso
51+
topo.pb(elem[0].s);
52+
ultimoTopo = topo.back();
53+
/*************************/
54+
55+
for(int i = n - 1; i >= 0; --i) {
56+
// procuro a maior subsequencia crescente em QI
57+
it = lower_bound(topo.begin(), topo.end(), elem[i].s);
58+
// se o peso ainda nao foi escolhido
59+
if(!dp[elem[i].w]) {
60+
// crio uma nova pilha se o elemnto eh maior que o topo das anteriores
61+
if(it == topo.end()) {
62+
dp[elem[i].w] = 1; // marco o peso como escolhido
63+
ans.pb(topo.back()); // o topo da ultima pilha faz parte da subsequencia
64+
topo.pb(elem[i].s); // topo da nova pilha
65+
ultimoTopo = elem[i].s; // o ultimo elemento da sequencia
66+
} else {
67+
*it = elem[i].s; // atualizo topo
68+
}
69+
}
70+
71+
}
72+
73+
// ultima iteração, atualiza resposta
74+
ans.pb(ultimoTopo);
75+
76+
//reverse(ans.begin(), ans.end());
77+
78+
// o tamanho da sequencia é
79+
cout << (int)topo.size() << endl;
80+
81+
// a sequencia é
82+
int i = 0;
83+
while(ans.size()) {
84+
int qi = ans.back();
85+
while(i < n) {
86+
if(elem[i].s == qi) {
87+
cout << elem[i].id << endl;
88+
break;
89+
}
90+
++i;
91+
}
92+
ans.pop_back();
93+
}
94+
95+
return 0;
96+
}
97+
98+
int main()
99+
{
100+
ios_base::sync_with_stdio(false);
101+
cin.tie(NULL);
102+
103+
int n = 0, a, b, i = 0;
104+
while(cin >> elem[n].w >> elem[n].s) {
105+
elem[n].id = n + 1;
106+
++n;
107+
}
108+
109+
lis(n);
110+
111+
return 0;
112+
}

UVA/357-let-count-the-ways.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// problem:
2+
// https://s3-us-west-2.amazonaws.com/prod-runcodes-files/exercisefiles/14551/p357.pdf?X-Amz-Content-Sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAI3OUHGVPPSQMGO5Q%2F20200517%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20200517T161409Z&X-Amz-SignedHeaders=Host&X-Amz-Expires=120&X-Amz-Signature=34906f6b50e7356f85fcc8f321941df9d8077f1b828c1327f2d0cc19c24d9dbb
3+
#include <iostream>
4+
#include <vector>
5+
#include <map>
6+
#include <queue>
7+
#include <string>
8+
#include <string.h> // memset
9+
#include <algorithm> // lower_bound
10+
11+
using namespace std;
12+
13+
#define fs first
14+
#define sd second
15+
#define pb push_back
16+
#define MAXN 30001
17+
18+
const long long int INF = 0x3f3f3f3f;
19+
20+
long long int coins[] = {1, 5, 10, 25, 50},
21+
dp[MAXN][5];
22+
23+
24+
long long int change_2(long long int value, long long int i) {
25+
if(value < 0 || i == 5)
26+
return 0;
27+
28+
if(value == 0)
29+
return 1;
30+
31+
if(dp[value][i] != -1)
32+
return dp[value][i];
33+
34+
return dp[value][i] = change_2(value - coins[i], i) + change_2(value, i + 1);
35+
}
36+
37+
int main()
38+
{
39+
ios_base::sync_with_stdio(false);
40+
cin.tie(NULL);
41+
42+
memset(dp, -1, sizeof dp);
43+
long long int n;
44+
while(cin >> n) {
45+
long long int ans = change_2(n, 0);
46+
if(ans != 1) {
47+
cout << "There are " << ans << " ways to produce " << n << " cents change.\n";
48+
} else {
49+
cout << "There is only " << 1 << " way to produce " << n << " cents change.\n";
50+
}
51+
}
52+
return 0;
53+
}

0 commit comments

Comments
 (0)