Skip to content

Commit 038d342

Browse files
committed
Bitmask DP 2 problems added
1 parent 1cc8648 commit 038d342

File tree

2 files changed

+247
-0
lines changed

2 files changed

+247
-0
lines changed
+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
2+
/**
3+
4+
Problem : LightOJ 1011
5+
6+
**/
7+
8+
/**Which of the favors of your Lord will you deny ?**/
9+
10+
#include<bits/stdc++.h>
11+
using namespace std;
12+
13+
#define LL long long
14+
#define PII pair<int,int>
15+
#define PLL pair<LL,LL>
16+
#define MP make_pair
17+
#define F first
18+
#define S second
19+
#define INF INT_MAX
20+
21+
#define ALL(x) (x).begin(), (x).end()
22+
#define DBG(x) cerr << __LINE__ << " says: " << #x << " = " << (x) << endl
23+
24+
#include <ext/pb_ds/assoc_container.hpp>
25+
#include <ext/pb_ds/tree_policy.hpp>
26+
using namespace __gnu_pbds;
27+
28+
template<class TIn>
29+
using indexed_set = tree<
30+
TIn, null_type, less<TIn>,
31+
rb_tree_tag, tree_order_statistics_node_update>;
32+
33+
/*
34+
PBDS
35+
-------------------------------------------------
36+
1) insert(value)
37+
2) erase(value)
38+
3) order_of_key(value) // 0 based indexing
39+
4) *find_by_order(position) // 0 based indexing
40+
41+
*/
42+
43+
inline void optimizeIO()
44+
{
45+
ios_base::sync_with_stdio(false);
46+
cin.tie(NULL);
47+
}
48+
49+
const int nmax = 2e5+7;
50+
const LL LINF = 1e17;
51+
52+
string to_str(LL x)
53+
{
54+
stringstream ss;
55+
ss<<x;
56+
return ss.str();
57+
}
58+
59+
//bool cmp(const PII &A,const PII &B)
60+
//{
61+
//
62+
//}
63+
64+
65+
int n;
66+
LL ara[16][16];
67+
LL dp[16][1<<16];
68+
69+
LL solve(int r,int mask)
70+
{
71+
if(r==n) return 0;
72+
73+
LL &ret = dp[r][mask];
74+
75+
if(ret!=-1) return ret;
76+
77+
for(int c = 0;c<n;c++)
78+
if(!(mask&(1<<c)))
79+
ret = max(ret, ara[r][c] + solve(r+1,mask | (1<<c)));
80+
81+
return ret;
82+
83+
}
84+
85+
int main()
86+
{
87+
//freopen("out.txt","w",stdout);
88+
89+
optimizeIO();
90+
91+
int tc;
92+
cin>>tc;
93+
94+
for(int qq=1; qq<=tc; qq++)
95+
{
96+
cin>>n;
97+
98+
for(int i=0;i<n;i++)
99+
for(int j=0;j<n;j++)
100+
cin>>ara[i][j];
101+
102+
memset(dp,-1,sizeof dp);
103+
LL ans = solve(0,0);
104+
105+
cout<<"Case "<<qq<<": "<<ans<<endl;
106+
}
107+
108+
return 0;
109+
}
110+
111+

Dynamic Programming/28 Brush IV.cpp

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
2+
/**
3+
4+
Problem : LightOJ 1018
5+
6+
**/
7+
8+
/**Which of the favors of your Lord will you deny ?**/
9+
10+
#include<bits/stdc++.h>
11+
using namespace std;
12+
13+
#define LL long long
14+
#define PII pair<int,int>
15+
#define PLL pair<LL,LL>
16+
#define MP make_pair
17+
#define F first
18+
#define S second
19+
#define INF INT_MAX
20+
21+
#define ALL(x) (x).begin(), (x).end()
22+
#define DBG(x) cerr << __LINE__ << " says: " << #x << " = " << (x) << endl
23+
24+
#include <ext/pb_ds/assoc_container.hpp>
25+
#include <ext/pb_ds/tree_policy.hpp>
26+
using namespace __gnu_pbds;
27+
28+
template<class TIn>
29+
using indexed_set = tree<
30+
TIn, null_type, less<TIn>,
31+
rb_tree_tag, tree_order_statistics_node_update>;
32+
33+
/*
34+
PBDS
35+
-------------------------------------------------
36+
1) insert(value)
37+
2) erase(value)
38+
3) order_of_key(value) // 0 based indexing
39+
4) *find_by_order(position) // 0 based indexing
40+
41+
*/
42+
43+
inline void optimizeIO()
44+
{
45+
ios_base::sync_with_stdio(false);
46+
cin.tie(NULL);
47+
}
48+
49+
const int nmax = 2e5+7;
50+
const LL LINF = 1e17;
51+
52+
string to_str(LL x)
53+
{
54+
stringstream ss;
55+
ss<<x;
56+
return ss.str();
57+
}
58+
59+
//bool cmp(const PII &A,const PII &B)
60+
//{
61+
//
62+
//}
63+
64+
65+
int n;
66+
int X[16];
67+
int Y[16];
68+
int col[16][16];
69+
int dp[1<<16];
70+
71+
bool collinear(int i, int j, int k) {
72+
return ((X[i] - X[j]) * (Y[k] - Y[j]) - (Y[i] - Y[j]) * (X[k] - X[j])) == 0;
73+
}
74+
75+
void precalc()
76+
{
77+
memset(col,0,sizeof col);
78+
79+
for(int i=0;i<n;i++)
80+
for(int j=0;j<n;j++)
81+
for(int k=0;k<n;k++)
82+
if(i!=j && collinear(i,j,k))
83+
col[i][j] |= (1<<k); /** Number of 1's in col[i][j] = How many Points are collinear with ith and jth point **/
84+
85+
}
86+
87+
int solve(int mask)
88+
{
89+
if(mask==((1<<n)-1)) return 0;
90+
91+
if (__builtin_popcount(mask) == (n - 1)) /** n-1 other points are already collinear with this one **/
92+
return 1;
93+
94+
int &ret = dp[mask];
95+
96+
if(ret!=-1) return ret;
97+
98+
int i;
99+
for(i=0;mask&(1<<i);i++); /** Go to the lsb which is not collinear **/
100+
/**Suppose you have satisfied bitmask M of the points. Let the lowest index dust point which hasn't been cleaned yet be x. Note that you are going to have to clean x eventually. Therefore you lose nothing by assuming you definitely clean x now. Then at each state you narrow down to N edges because you have one point which must be on the line **/
101+
102+
ret = INT_MAX;
103+
for(int j=0;j<n;j++)
104+
if(i!=j && !(mask&(1<<j)))
105+
ret = min(ret,1 + solve(mask|col[i][j]));
106+
107+
return ret;
108+
}
109+
110+
int main()
111+
{
112+
//freopen("out.txt","w",stdout);
113+
114+
optimizeIO();
115+
116+
int tc;
117+
cin>>tc;
118+
119+
for(int qq=1; qq<=tc; qq++)
120+
{
121+
cin>>n;
122+
123+
for(int i=0;i<n;i++)
124+
cin>>X[i]>>Y[i];
125+
126+
precalc();
127+
memset(dp,-1,sizeof dp);
128+
int ans = solve(0);
129+
130+
cout<<"Case "<<qq<<": "<<ans<<endl;
131+
}
132+
133+
return 0;
134+
}
135+
136+

0 commit comments

Comments
 (0)