Skip to content

Commit

Permalink
Added more problems
Browse files Browse the repository at this point in the history
  • Loading branch information
JahodaPaul committed Nov 12, 2018
1 parent 27356e0 commit 289f52f
Show file tree
Hide file tree
Showing 8 changed files with 436 additions and 0 deletions.
64 changes: 64 additions & 0 deletions ACM/Data Structures/SquareRootBlocks_DominoPrinciple.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <bits/stdc++.h>

using namespace std;

struct SingleDominoPiece{
int index;
int height;
int position;
};
vector<SingleDominoPiece> v1;
int indexes[100005];
int block[1000];
int toWhatIndexDoesItFall[100005];

int blockSize;
int n;

int main() {
ios::sync_with_stdio(0);
cin>>n;
int blockSize = (int)sqrt(n);
v1.resize((unsigned long)n,SingleDominoPiece());
int pos,length;
for(int i=0;i<n;i++){
cin>>pos>>length;
v1[i].index=i;
v1[i].height = length;
v1[i].position = pos;
}
sort(v1.begin(),v1.end(),[](const SingleDominoPiece & a, const SingleDominoPiece & b){return a.position < b.position;});
for(int i=0;i<v1.size();i++){
indexes[v1[i].index] = i;
}

SingleDominoPiece tmp;
for(int i=n-1;i>=0;i--){
tmp.position = v1[i].position+v1[i].height;
auto it = lower_bound(v1.begin(),v1.end(),tmp,[](const SingleDominoPiece & a, const SingleDominoPiece & b){return a.position < b.position;});
int index = it-v1.begin();
int maxFound = -1;
index--;
for(int j=i+1;j<=index;j++){
while(j%(blockSize) == 0 && j+blockSize < index){
if(block[j/blockSize] > maxFound){
maxFound = block[j/blockSize];
}
j+=blockSize;
}
if(toWhatIndexDoesItFall[j] > maxFound){
maxFound = toWhatIndexDoesItFall[j];
}
}
if(maxFound == -1){maxFound=index;}
if(block[i/blockSize] < maxFound){
block[i/blockSize] = maxFound;
}
toWhatIndexDoesItFall[i] = maxFound;
}
for(int i=0;i<n;i++){
cout << toWhatIndexDoesItFall[indexes[i]]-indexes[i]+1 << " ";
}
cout << endl;
return 0;
}
34 changes: 34 additions & 0 deletions ACM/Data Structures/SquareRootBlocks_DominoPrinciple.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
https://codeforces.com/problemset/problem/56/E

Codeforces - 56 E


Vasya is interested in arranging dominoes. He is fed up with common dominoes and he uses the dominoes of different heights. He put n dominoes on the table along one axis, going from left to right. Every domino stands perpendicular to that axis so that the axis passes through the center of its base. The i-th domino has the coordinate xi and the height hi. Now Vasya wants to learn for every domino, how many dominoes will fall if he pushes it to the right. Help him do that.

Consider that a domino falls if it is touched strictly above the base. In other words, the fall of the domino with the initial coordinate x and height h leads to the fall of all dominoes on the segment [x + 1, x + h - 1].


Input
The first line contains integer n (1 ≤ n ≤ 105) which is the number of dominoes. Then follow n lines containing two integers xi and hi ( - 108 ≤ xi ≤ 108, 2 ≤ hi ≤ 108) each, which are the coordinate and height of every domino. No two dominoes stand on one point.

Output
Print n space-separated numbers zi — the number of dominoes that will fall if Vasya pushes the i-th domino to the right (including the domino itself).

Examples
input
4
16 5
20 5
10 10
18 2
output
3 1 4 1

input
4
0 10
1 5
9 10
15 10
output
4 1 2 1
87 changes: 87 additions & 0 deletions ACM/Graphs and Trees/BellmanFord_wormholes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include <bits/stdc++.h>

//Input
//The input file starts with a line containing the number of cases c to be analysed. Each case starts with
//a line with two numbers n and m. These indicate the number of star systems (1 ≤ n ≤ 1000) and
//the number of wormholes (0 ≤ m ≤ 2000). The star systems are numbered from 0 (our solar system)
//through n − 1 . For each wormhole a line containing three integer numbers x, y and t is given. These
//numbers indicate that this wormhole allows someone to travel from the star system numbered x to the
//star system numbered y, thereby ending up t (−1000 ≤ t ≤ 1000) years in the future.
//Output
//The output consists of c lines, one line for each case, containing the word ‘possible’ if it is indeed
//possible to go back in time indefinitely, or ‘not possible’ if this is not possible with the given set of
//star systems and wormholes

using namespace std;
//kam, cena
vector<vector<pair<int,int>>> graph;
int T,nOfEdges,nOfVertices;
int arr[1002];
int arr2[1002];

int main() {
int a,b,cost;
ios::sync_with_stdio(0);
cin >> T;
while(T--){
memset(arr,63,sizeof(arr));
memset(arr2,63,sizeof(arr2));
cin>>nOfVertices>>nOfEdges;
int kMinusOne = nOfVertices-1,k = nOfVertices;
for(int i =0;i<=nOfVertices;i++){
vector<pair<int,int>> vector1;
graph.push_back(vector1);
}

for(int i=0;i<nOfEdges;i++){
cin>>a>>b>>cost;
graph[a].push_back(make_pair(b,cost));
}
//cena, kam, round
priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>,greater<pair<int,pair<int,int>>>> q;
q.push(make_pair(0,make_pair(0,0)));
arr[0] = 0;
while(!q.empty()){
pair<int,pair<int,int>> q2 = q.top();
q.pop();
for(int i=0;i<graph[q2.second.first].size();i++){
int length = arr[q2.second.first] + graph[q2.second.first][i].second;
if(length < arr[graph[q2.second.first][i].first] && q2.second.second < kMinusOne)
{
arr[graph[q2.second.first][i].first] = length;
q.push(make_pair(length,make_pair(graph[q2.second.first][i].first,q2.second.second+1)));
}
}

}
while(!q.empty()){q.pop();}
q.push(make_pair(0,make_pair(0,0)));
arr2[0] = 0;
while(!q.empty()){
pair<int,pair<int,int>> q2 = q.top();
q.pop();
for(int i=0;i<graph[q2.second.first].size();i++){
int length = arr2[q2.second.first] + graph[q2.second.first][i].second;
if(length < arr2[graph[q2.second.first][i].first] && q2.second.second < k)
{
arr2[graph[q2.second.first][i].first] = length;
q.push(make_pair(length,make_pair(graph[q2.second.first][i].first,q2.second.second+1)));
}
}
}
bool b = false;
for(int i=0;i<1001;i++){
if(arr[i] != arr2[i]){
cout << "possible" << endl;
b = true;
break;
}
}
if(!b){
cout << "not possible" << endl;
}

graph.clear();
}
return 0;
}
52 changes: 52 additions & 0 deletions ACM/Graphs and Trees/BellmanFord_wormholes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=499

uva - 558 - Wormholes


In the year 2163, wormholes were discovered. A wormhole is a subspace tunnel through space and time
connecting two star systems. Wormholes have a few peculiar properties:
• Wormholes are one-way only.
• The time it takes to travel through a wormhole is negligible.
• A wormhole has two end points, each situated in a star system.
• A star system may have more than one wormhole end point within its boundaries.
• For some unknown reason, starting from our solar system, it is always possible to end up in any
star system by following a sequence of wormholes (maybe Earth is the centre of the universe).
• Between any pair of star systems, there is at most one wormhole in either direction.
• There are no wormholes with both end points in the same star system.
All wormholes have a constant time difference between their end points. For example, a specific
wormhole may cause the person travelling through it to end up 15 years in the future. Another wormhole
may cause the person to end up 42 years in the past.
A brilliant physicist, living on earth, wants to use wormholes to study the Big Bang. Since warp
drive has not been invented yet, it is not possible for her to travel from one star system to another one
directly. This can be done using wormholes, of course.
The scientist wants to reach a cycle of wormholes somewhere in the universe that causes her to end
up in the past. By travelling along this cycle a lot of times, the scientist is able to go back as far in
time as necessary to reach the beginning of the universe and see the Big Bang with her own eyes. Write
a program to find out whether such a cycle exists.

Input
The input file starts with a line containing the number of cases c to be analysed. Each case starts with
a line with two numbers n and m. These indicate the number of star systems (1 ≤ n ≤ 1000) and
the number of wormholes (0 ≤ m ≤ 2000). The star systems are numbered from 0 (our solar system)
through n − 1 . For each wormhole a line containing three integer numbers x, y and t is given. These
numbers indicate that this wormhole allows someone to travel from the star system numbered x to the
star system numbered y, thereby ending up t (−1000 ≤ t ≤ 1000) years in the future.
Output
The output consists of c lines, one line for each case, containing the word ‘possible’ if it is indeed
possible to go back in time indefinitely, or ‘not possible’ if this is not possible with the given set of
star systems and wormholes.

Sample Input
2
3 3
0 1 1000
1 2 15
2 1 -42
4 4
0 1 10
1 2 20
2 3 30
3 0 -60
Sample Output
possible
not possible
112 changes: 112 additions & 0 deletions ACM/Graphs and Trees/LCA_LCASQ.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#include <bits/stdc++.h>

using namespace std;

struct node{
int parent;
int index;
int depth;
int nOfDescendants;
vector<int> children;
node(){
depth = 0;
nOfDescendants = 0;
}
};



vector<int> paths; // heads
vector<node> nodes;

int nOfNodes,nOfChildren,nOfqueries,u,v,tmp_node;

int preorder(int index, int depth){
nodes[index].depth = depth;

for(int i=0;i<nodes[index].children.size();i++){
int nOfDescendants = preorder(nodes[nodes[index].children[i]].index,depth+1);
nodes[index].nOfDescendants += nOfDescendants;
}

return nodes[index].nOfDescendants + 1;
}

int main() {
cin>>nOfNodes;

for(int i=0;i<nOfNodes;i++) {
nodes.push_back(node());
paths.push_back(-1);
}

for(int i=0;i<nOfNodes;i++){
nodes[i].index = i;
cin>>nOfChildren;
for(int j=0;j<nOfChildren;j++) {
cin>>tmp_node;
nodes[i].children.push_back(tmp_node);
nodes[tmp_node].parent = i;
}
}

// root of the tree is always 0, therefore we don't need to find it
// create paths
// BFS

preorder(0,0);

// pick the fattest one - the one with most descendants

deque<int> q;
q.push_back(0);
paths[0] = 0;

while(!q.empty()){
int now = q.front();
q.pop_front();
int maximum = -1;
int index = -1;
for(int i=0;i<nodes[now].children.size();i++){
q.push_back(nodes[now].children[i]);
if (nodes[nodes[now].children[i]].nOfDescendants > maximum){
maximum = nodes[nodes[now].children[i]].nOfDescendants;
index = i;
}
}

for(int i=0;i<nodes[now].children.size();i++){
if(index == i){
paths[nodes[now].children[i]] = paths[now];
}
else{
paths[nodes[now].children[i]] = nodes[now].children[i];
}
}
}


//queries
cin>>nOfqueries;
for(int i=0;i<nOfqueries;i++){
cin>>u>>v;
while(paths[u] != paths[v]){
int depthOfHeadU = nodes[paths[u]].depth;
int depthOfHeadV = nodes[paths[v]].depth;
if(depthOfHeadU > depthOfHeadV){
u = nodes[paths[u]].parent;
}
else{
v = nodes[paths[v]].parent;
}
}
if(nodes[u].depth < nodes[v].depth){
cout << u << endl;
}
else{
cout << v << endl;
}
}

return 0;
}
29 changes: 29 additions & 0 deletions ACM/Graphs and Trees/LCA_LCASQ.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
https://www.spoj.com/problems/LCASQ/

You are given a rooted tree and an ordered list of queries. Each query is specified by two nodes u and v and the answer to a query is the lowest common ancestor of u and v.

Recall that the Lowest Common Ancestor of two nodes is the node that is furthest from the root and also an ancestor of the two nodes. In this problem we use the convention that a node is in fact an ancestor of itself.

Input
The first line contains an integer N, the number of nodes in the tree (N <= 10000). Each of the next N lines will start with a number M the number of child nodes of the Nth node, (0 <= M <= 999) followed by M numbers the child nodes of the Nth node. Nodes will be labeled from 0 to N-1. Following will be a number Q, the number of queries that will be asked (Q <= 10000). Each of the next Q lines will have two numbers u and v (0 <= u, v < N) which specify the parameters of that specific query.

The root of the tree will always be node 0.

Output

For each query output the answer on its own line. Answers should follow the same order as given in the input.

Example
Input:
3
2 1 2
0
0
3
1 2
1 1
2 2
Output:
0
1
2
Loading

0 comments on commit 289f52f

Please sign in to comment.