This repository has been archived by the owner on Oct 23, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3519 from ani37/master
Merged by aniket965
- Loading branch information
Showing
5 changed files
with
470 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,241 @@ | ||
|
||
// A divide and conquer program to find convex | ||
// hull of a given set of points. | ||
#include<bits/stdc++.h> | ||
using namespace std; | ||
|
||
// stores the centre of polygon (It is made | ||
// global because it is used in compare function) | ||
pair<int, int> mid; | ||
|
||
// determines the quadrant of a point | ||
// (used in compare()) | ||
int quad(pair<int, int> p) | ||
{ | ||
if (p.first >= 0 && p.second >= 0) | ||
return 1; | ||
if (p.first <= 0 && p.second >= 0) | ||
return 2; | ||
if (p.first <= 0 && p.second <= 0) | ||
return 3; | ||
return 4; | ||
} | ||
|
||
// Checks whether the line is crossing the polygon | ||
int orientation(pair<int, int> a, pair<int, int> b, | ||
pair<int, int> c) | ||
{ | ||
int res = (b.second-a.second)*(c.first-b.first) - | ||
(c.second-b.second)*(b.first-a.first); | ||
|
||
if (res == 0) | ||
return 0; | ||
if (res > 0) | ||
return 1; | ||
return -1; | ||
} | ||
|
||
// compare function for sorting | ||
bool compare(pair<int, int> p1, pair<int, int> q1) | ||
{ | ||
pair<int, int> p = make_pair(p1.first - mid.first, | ||
p1.second - mid.second); | ||
pair<int, int> q = make_pair(q1.first - mid.first, | ||
q1.second - mid.second); | ||
|
||
int one = quad(p); | ||
int two = quad(q); | ||
|
||
if (one != two) | ||
return (one < two); | ||
return (p.second*q.first < q.second*p.first); | ||
} | ||
|
||
// Finds upper tangent of two polygons 'a' and 'b' | ||
// represented as two vectors. | ||
vector<pair<int, int>> merger(vector<pair<int, int> > a, | ||
vector<pair<int, int> > b) | ||
{ | ||
// n1 -> number of points in polygon a | ||
// n2 -> number of points in polygon b | ||
int n1 = a.size(), n2 = b.size(); | ||
|
||
int ia = 0, ib = 0; | ||
for (int i=1; i<n1; i++) | ||
if (a[i].first > a[ia].first) | ||
ia = i; | ||
|
||
// ib -> leftmost point of b | ||
for (int i=1; i<n2; i++) | ||
if (b[i].first < b[ib].first) | ||
ib=i; | ||
|
||
// finding the upper tangent | ||
int inda = ia, indb = ib; | ||
bool done = 0; | ||
while (!done) | ||
{ | ||
done = 1; | ||
while (orientation(b[indb], a[inda], a[(inda+1)%n1]) >=0) | ||
inda = (inda + 1) % n1; | ||
|
||
while (orientation(a[inda], b[indb], b[(n2+indb-1)%n2]) <=0) | ||
{ | ||
indb = (n2+indb-1)%n2; | ||
done = 0; | ||
} | ||
} | ||
|
||
int uppera = inda, upperb = indb; | ||
inda = ia, indb=ib; | ||
done = 0; | ||
int g = 0; | ||
while (!done)//finding the lower tangent | ||
{ | ||
done = 1; | ||
while (orientation(a[inda], b[indb], b[(indb+1)%n2])>=0) | ||
indb=(indb+1)%n2; | ||
|
||
while (orientation(b[indb], a[inda], a[(n1+inda-1)%n1])<=0) | ||
{ | ||
inda=(n1+inda-1)%n1; | ||
done=0; | ||
} | ||
} | ||
|
||
int lowera = inda, lowerb = indb; | ||
vector<pair<int, int>> ret; | ||
|
||
//ret contains the convex hull after merging the two convex hulls | ||
//with the points sorted in anti-clockwise order | ||
int ind = uppera; | ||
ret.push_back(a[uppera]); | ||
while (ind != lowera) | ||
{ | ||
ind = (ind+1)%n1; | ||
ret.push_back(a[ind]); | ||
} | ||
|
||
ind = lowerb; | ||
ret.push_back(b[lowerb]); | ||
while (ind != upperb) | ||
{ | ||
ind = (ind+1)%n2; | ||
ret.push_back(b[ind]); | ||
} | ||
return ret; | ||
|
||
} | ||
|
||
// Brute force algorithm to find convex hull for a set | ||
// of less than 6 points | ||
vector<pair<int, int>> bruteHull(vector<pair<int, int>> a) | ||
{ | ||
// Take any pair of points from the set and check | ||
// whether it is the edge of the convex hull or not. | ||
// if all the remaining points are on the same side | ||
// of the line then the line is the edge of convex | ||
// hull otherwise not | ||
set<pair<int, int> >s; | ||
|
||
for (int i=0; i<a.size(); i++) | ||
{ | ||
for (int j=i+1; j<a.size(); j++) | ||
{ | ||
int x1 = a[i].first, x2 = a[j].first; | ||
int y1 = a[i].second, y2 = a[j].second; | ||
|
||
int a1 = y1-y2; | ||
int b1 = x2-x1; | ||
int c1 = x1*y2-y1*x2; | ||
int pos = 0, neg = 0; | ||
for (int k=0; k<a.size(); k++) | ||
{ | ||
if (a1*a[k].first+b1*a[k].second+c1 <= 0) | ||
neg++; | ||
if (a1*a[k].first+b1*a[k].second+c1 >= 0) | ||
pos++; | ||
} | ||
if (pos == a.size() || neg == a.size()) | ||
{ | ||
s.insert(a[i]); | ||
s.insert(a[j]); | ||
} | ||
} | ||
} | ||
|
||
vector<pair<int, int>>ret; | ||
for (auto e:s) | ||
ret.push_back(e); | ||
|
||
// Sorting the points in the anti-clockwise order | ||
mid = {0, 0}; | ||
int n = ret.size(); | ||
for (int i=0; i<n; i++) | ||
{ | ||
mid.first += ret[i].first; | ||
mid.second += ret[i].second; | ||
ret[i].first *= n; | ||
ret[i].second *= n; | ||
} | ||
sort(ret.begin(), ret.end(), compare); | ||
for (int i=0; i<n; i++) | ||
ret[i] = make_pair(ret[i].first/n, ret[i].second/n); | ||
|
||
return ret; | ||
} | ||
|
||
// Returns the convex hull for the given set of points | ||
vector<pair<int, int>> divide(vector<pair<int, int>> a) | ||
{ | ||
// If the number of points is less than 6 then the | ||
// function uses the brute algorithm to find the | ||
// convex hull | ||
if (a.size() <= 5) | ||
return bruteHull(a); | ||
|
||
// left contains the left half points | ||
// right contains the right half points | ||
vector<pair<int, int>>left, right; | ||
for (int i=0; i<a.size()/2; i++) | ||
left.push_back(a[i]); | ||
for (int i=a.size()/2; i<a.size(); i++) | ||
right.push_back(a[i]); | ||
|
||
// convex hull for the left and right sets | ||
vector<pair<int, int>>left_hull = divide(left); | ||
vector<pair<int, int>>right_hull = divide(right); | ||
|
||
// merging the convex hulls | ||
return merger(left_hull, right_hull); | ||
} | ||
|
||
// Driver code | ||
int main() | ||
{ | ||
vector<pair<int, int> > a; | ||
a.push_back(make_pair(0, 0)); | ||
a.push_back(make_pair(1, -4)); | ||
a.push_back(make_pair(-1, -5)); | ||
a.push_back(make_pair(-5, -3)); | ||
a.push_back(make_pair(-3, -1)); | ||
a.push_back(make_pair(-1, -3)); | ||
a.push_back(make_pair(-2, -2)); | ||
a.push_back(make_pair(-1, -1)); | ||
a.push_back(make_pair(-2, -1)); | ||
a.push_back(make_pair(-1, 1)); | ||
|
||
int n = a.size(); | ||
|
||
// sorting the set of points according | ||
// to the x-coordinate | ||
sort(a.begin(), a.end()); | ||
vector<pair<int, int> >ans = divide(a); | ||
|
||
cout << "convex hull:\n"; | ||
for (auto e:ans) | ||
cout << e.first << " " | ||
<< e.second << endl; | ||
|
||
return 0; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// Given two integers a and b. The task is to print | ||
// sum of all the digits appearing in the | ||
// integers between a and b | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
// Memoization for the state results | ||
long long dp[20][160][3][3][2]; | ||
|
||
// Stores the digits in x in a vector digit | ||
long long getDigits(long long x, vector <int> &digit) | ||
{ | ||
while (x) | ||
{ | ||
digit.push_back(x%10); | ||
x /= 10; | ||
} | ||
} | ||
|
||
// Return sum of digits from 1 to integer in | ||
// digit vector | ||
long long digitSum(int idx, int sum,int even ,int odd , int tight, | ||
vector <int> &digit) | ||
{ | ||
// base case | ||
if (idx == -1) { | ||
if(even <= 2 && odd<= 2) return sum; | ||
return 0; | ||
|
||
} | ||
if(even > 2 || odd > 2) return 0; | ||
|
||
// checking if already calculated this state | ||
|
||
if (dp[idx][sum][even][odd][tight] != -1 and tight != 1) | ||
return dp[idx][sum][even][odd][tight]; | ||
|
||
long long ret = 0; | ||
|
||
// calculating range value | ||
int k = (tight)? digit[idx] : 9; | ||
for (int i = 0; i <= k ; i++) | ||
{ | ||
// caclulating newTight value for next state | ||
int newTight = (digit[idx] == i)? tight : 0; | ||
|
||
if(i==0) | ||
{ | ||
|
||
ret += digitSum(idx-1, sum+i,0,0, newTight, digit); | ||
} | ||
else if(i%2!=0){ | ||
ret += digitSum(idx-1, sum+i,0,odd+1, newTight, digit); | ||
} | ||
else if(i%2==0){ | ||
ret += digitSum(idx-1, sum+i,even+1,0, newTight, digit); | ||
} | ||
// fetching answer from next state | ||
|
||
|
||
} | ||
|
||
if (!tight) | ||
dp[idx][sum][even][odd][tight] = ret; | ||
|
||
return ret; | ||
} | ||
|
||
// Returns sum of digits in numbers from a to b. | ||
long long rangeDigitSum(long long a, long long b) | ||
{ | ||
// initializing dp with -1 | ||
memset(dp, -1, sizeof(dp)); | ||
|
||
// storing digits of a-1 in digit vector | ||
vector<int> digitA; | ||
getDigits(a-1, digitA); | ||
|
||
// Finding sum of digits from 1 to "a-1" which is passed | ||
// as digitA. | ||
long long ans1 = digitSum(digitA.size()-1, 0,0,0, 1, digitA); | ||
|
||
// Storing digits of b in digit vector | ||
vector<int> digitB; | ||
getDigits(b, digitB); | ||
|
||
// Finding sum of digits from 1 to "b" which is passed | ||
// as digitB. | ||
long long ans2 = digitSum(digitB.size()-1, 0,0,0, 1, digitB); | ||
|
||
return (ans2 - ans1); | ||
} | ||
void solve(){ | ||
int t; | ||
cin>>t; | ||
while(t--) | ||
{ | ||
long long l,r,k; | ||
cin>>l>>r; | ||
k= rangeDigitSum(l,r); | ||
cout<< k<<"\n"; | ||
|
||
}} | ||
// driver function to call above function | ||
int main(){ | ||
|
||
ios_base::sync_with_stdio(false); | ||
cin.tie(NULL); | ||
solve(); | ||
return 0; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include <bits/stdc++.h> | ||
#include <cstdio> | ||
#include <cstring> | ||
#include <cmath> | ||
#include <cstring> | ||
#include <chrono> | ||
#include <complex> | ||
#define endl "\n" | ||
#define ll long long int | ||
#define vi vector<int> | ||
#define vll vector<ll> | ||
#define vvi vector < vi > | ||
#define pii pair<int,int> | ||
#define pll pair<long long, long long> | ||
#define mod 1000000007 | ||
#define inf 1000000000000000001; | ||
#define all(c) c.begin(),c.end() | ||
#define mp(x,y) make_pair(x,y) | ||
#define mem(a,val) memset(a,val,sizeof(a)) | ||
#define eb emplace_back | ||
#define f first | ||
#define s second | ||
|
||
using namespace std; | ||
int main() | ||
{ | ||
ifstream fin; | ||
ofstream fout; | ||
fin.open("input"); | ||
fout.open("output"); | ||
|
||
int T; | ||
fin>>T; | ||
for(int t=1;t<=T;t++) | ||
{ | ||
ll ans=0; | ||
|
||
cout<<"Case #"<<t<<": "<<ans<<endl; | ||
} | ||
|
||
return 0; | ||
} |
Oops, something went wrong.