This commit is contained in:
Anthony Wang 2020-05-24 20:59:14 -05:00
parent ca235104c7
commit 7697dbf553
9 changed files with 369 additions and 369 deletions

2
.gitignore vendored
View file

@ -1,2 +1,2 @@
out
out
.vscode

View file

@ -1,2 +1,2 @@
language = "cpp"
language = "cpp"
run = "g++ src/*.cpp -o out -std=c++11; ./out; rm out"

View file

@ -1,62 +1,62 @@
Farmer John is worried for the health of his cows (conveniently numbered
$1 ... N$ as always) after an outbreak of the highly contagious bovine
disease COWVID-19.
Recently, Farmer John tested all of his cows and found some of them to be
positive for the disease. Using video footage from inside his barn, he is able
to review recent interactions between pairs of cows --- it turns out that when
cows greet each-other, they shake hooves, a gesture that can unfortunately
spread the infection from one cow to another. Farmer John assembles a
time-stamped list of interacting pairs of cows, with entries of the form
$(t, x, y)$, meaning that at time $t$, cow $x$ shook hooves with cow $y$.
Farmer John also knows the following:
(i) Exactly one cow on his farm could have started out carrying the disease
(we'll call this cow "patient zero").
(ii) Once a cow is infected, she passes the infection along with her next $K$
hoof shakes (possibly including the same partner cow several times). After
shaking hooves $K$ times, she no longer passes the infection along with
subsequent hoof shakes (since at this point she realizes she is spreading the
infection and washes her hooves carefully).
(iii) Once a cow is infected, she stays infected.
Unfortunately, Farmer John doesn't know which of his $N$ cows is patient zero,
nor does he know the value of $K$! Please help him narrow down the
possibilities for these unknowns based on his data. It is guaranteed that at
least one possibility is valid.
INPUT FORMAT
The first line of the input file contains $N$ ($2 <= N <= 100$) and $T$
($1 <= T <= 250$). The next line contains a string of length $N$ whose
entries are 0s and 1s, describing the current state of Farmer John's $N$ cows
--- 0 represents a healthy cow and 1 represents a cow presently with the
disease. Each of the next $T$ lines describes a record in Farmer John's list of
interactions and consists of three integers $t$, $x$, and $y$, where $t$ is a
positive integer time of the interaction ($t <= 250$) and $x$ and $y$ are
distinct integers in the range $1 \ldots N$, indicating which cows shook hands
at time $t$. At most one interaction happens at each point in time.
OUTPUT FORMAT
Print a single line with three integers $x$, $y$, and $z$, where $x$ is the
number of possible cows who could have been patient zero, $y$ is the smallest
possible value of $K$ consistent with the data, and $z$ is the largest possible
value of $K$ consistent with the data (if there is no upper bound on $K$ that
can be deduced from the data, print "Infinity" for $z$). Note that it might be
possible to have $K=0$.
SAMPLE INPUT
4 3
1100
7 1 2
5 2 3
6 2 4
SAMPLE OUTPUT
1 1 Infinity
The only candidate for patient zero is cow 1. For all $K>0$, cow 1 infects cow 2
at time 7, while cows 3 and 4 remain uninfected.
Farmer John is worried for the health of his cows (conveniently numbered
$1 ... N$ as always) after an outbreak of the highly contagious bovine
disease COWVID-19.
Recently, Farmer John tested all of his cows and found some of them to be
positive for the disease. Using video footage from inside his barn, he is able
to review recent interactions between pairs of cows --- it turns out that when
cows greet each-other, they shake hooves, a gesture that can unfortunately
spread the infection from one cow to another. Farmer John assembles a
time-stamped list of interacting pairs of cows, with entries of the form
$(t, x, y)$, meaning that at time $t$, cow $x$ shook hooves with cow $y$.
Farmer John also knows the following:
(i) Exactly one cow on his farm could have started out carrying the disease
(we'll call this cow "patient zero").
(ii) Once a cow is infected, she passes the infection along with her next $K$
hoof shakes (possibly including the same partner cow several times). After
shaking hooves $K$ times, she no longer passes the infection along with
subsequent hoof shakes (since at this point she realizes she is spreading the
infection and washes her hooves carefully).
(iii) Once a cow is infected, she stays infected.
Unfortunately, Farmer John doesn't know which of his $N$ cows is patient zero,
nor does he know the value of $K$! Please help him narrow down the
possibilities for these unknowns based on his data. It is guaranteed that at
least one possibility is valid.
INPUT FORMAT
The first line of the input file contains $N$ ($2 <= N <= 100$) and $T$
($1 <= T <= 250$). The next line contains a string of length $N$ whose
entries are 0s and 1s, describing the current state of Farmer John's $N$ cows
--- 0 represents a healthy cow and 1 represents a cow presently with the
disease. Each of the next $T$ lines describes a record in Farmer John's list of
interactions and consists of three integers $t$, $x$, and $y$, where $t$ is a
positive integer time of the interaction ($t <= 250$) and $x$ and $y$ are
distinct integers in the range $1 \ldots N$, indicating which cows shook hands
at time $t$. At most one interaction happens at each point in time.
OUTPUT FORMAT
Print a single line with three integers $x$, $y$, and $z$, where $x$ is the
number of possible cows who could have been patient zero, $y$ is the smallest
possible value of $K$ consistent with the data, and $z$ is the largest possible
value of $K$ consistent with the data (if there is no upper bound on $K$ that
can be deduced from the data, print "Infinity" for $z$). Note that it might be
possible to have $K=0$.
SAMPLE INPUT
4 3
1100
7 1 2
5 2 3
6 2 4
SAMPLE OUTPUT
1 1 Infinity
The only candidate for patient zero is cow 1. For all $K>0$, cow 1 infects cow 2
at time 7, while cows 3 and 4 remain uninfected.

View file

@ -1,38 +1,38 @@
A terrible new disease, COWVID-19, has begun to spread among cows worldwide.
Farmer John is trying to take as many precautions as possible to protect his
herd from infection.
Farmer John's barn is a long narrow building containing $N$ stalls in a row
($2 <= N <= 10^5$). Some of these stalls are currently occupied by cows,
and some are vacant. Having read about the importance of "social distancing",
Farmer John wants to maximize $D$, where $D$ is the distance between the closest
two occupied stalls. For example, if stalls 3 and 8 are the closest that are
occupied, then $D = 5$.
Two new cows recently joined Farmer John's herd and he needs to decide to which
formerly-unoccupied stalls they should be assigned. Please determine how he can
place his two new cows so that the resulting value of $D$ is still as large as
possible. Farmer John cannot move any of his existing cows; he only wants to
assign stalls to the new cows.
INPUT FORMAT
The first line of input contains $N$. The next line contains a string of length
$N$ of 0s and 1s describing the sequence of stalls in the barn. 0s indicate
empty stalls and 1s indicate occupied stalls. The string has at least two 0s,
so there is at least enough room for two new cows.
OUTPUT FORMAT
Please print the largest value of $D$ (the closest distance between two occupied
stalls) that Farmer John can achieve after adding his two new cows in an optimal
fashion.
SAMPLE INPUT
14
10001001000010
SAMPLE OUTPUT
2
In this example, Farmer John could add cows to make the occupancy string look
like 10x010010x0010, where x's indicate the new cows. In this case $D = 2$. It
A terrible new disease, COWVID-19, has begun to spread among cows worldwide.
Farmer John is trying to take as many precautions as possible to protect his
herd from infection.
Farmer John's barn is a long narrow building containing $N$ stalls in a row
($2 <= N <= 10^5$). Some of these stalls are currently occupied by cows,
and some are vacant. Having read about the importance of "social distancing",
Farmer John wants to maximize $D$, where $D$ is the distance between the closest
two occupied stalls. For example, if stalls 3 and 8 are the closest that are
occupied, then $D = 5$.
Two new cows recently joined Farmer John's herd and he needs to decide to which
formerly-unoccupied stalls they should be assigned. Please determine how he can
place his two new cows so that the resulting value of $D$ is still as large as
possible. Farmer John cannot move any of his existing cows; he only wants to
assign stalls to the new cows.
INPUT FORMAT
The first line of input contains $N$. The next line contains a string of length
$N$ of 0s and 1s describing the sequence of stalls in the barn. 0s indicate
empty stalls and 1s indicate occupied stalls. The string has at least two 0s,
so there is at least enough room for two new cows.
OUTPUT FORMAT
Please print the largest value of $D$ (the closest distance between two occupied
stalls) that Farmer John can achieve after adding his two new cows in an optimal
fashion.
SAMPLE INPUT
14
10001001000010
SAMPLE OUTPUT
2
In this example, Farmer John could add cows to make the occupancy string look
like 10x010010x0010, where x's indicate the new cows. In this case $D = 2$. It
is impossible to add the new cows to achieve any higher value of $D$.

View file

@ -1,43 +1,43 @@
Farmer John is worried for the health of his cows after an outbreak of the
highly contagious bovine disease COWVID-19.
Despite his best attempt at making his $N$ cows ($1 <= N <= 1000$) practice
"social distancing", many of them still unfortunately contracted the disease.
The cows, conveniently numbered $1 \ldots N$, are each standing at distinct
points along a long path (essentially a one-dimensional number line), with cow
$i$ standing at position $x_i$. Farmer John knows that there is a radius $R$
such that any cow standing up to and including $R$ units away from an infected
cow will also become infected (and will then pass the infection along to
additional cows within $R$ units away, and so on).
Unfortunately, Farmer John doesn't know $R$ exactly. He does however know which
of his cows are infected. Given this data, please determine the minimum
possible number of cows that were initially infected with the disease.
INPUT FORMAT
The first line of input contains $N$. The next $N$ lines each describe one cow
in terms of two integers, $x$ and $s$, where $x$ is the position
($0 <= x <= 10^6$), and $s$ is 0 for a healthy cow or 1 for a sick cow. At
least one cow is sick, and all cows that could possibly have become sick from
spread of the disease have now become sick.
OUTPUT FORMAT
Please output the minimum number of cows that could have initially been sick,
prior to any spread of the disease.
SAMPLE INPUT
6
7 1
1 1
15 1
3 1
10 0
6 1
SAMPLE OUTPUT
3
In this example, we know that $R < 3$ since otherwise the cow at position 7
would have infected the cow at position 10. Therefore, at least 3 cows must
have started out infected -- one of the two cows at positions 1 and 3, one of
Farmer John is worried for the health of his cows after an outbreak of the
highly contagious bovine disease COWVID-19.
Despite his best attempt at making his $N$ cows ($1 <= N <= 1000$) practice
"social distancing", many of them still unfortunately contracted the disease.
The cows, conveniently numbered $1 \ldots N$, are each standing at distinct
points along a long path (essentially a one-dimensional number line), with cow
$i$ standing at position $x_i$. Farmer John knows that there is a radius $R$
such that any cow standing up to and including $R$ units away from an infected
cow will also become infected (and will then pass the infection along to
additional cows within $R$ units away, and so on).
Unfortunately, Farmer John doesn't know $R$ exactly. He does however know which
of his cows are infected. Given this data, please determine the minimum
possible number of cows that were initially infected with the disease.
INPUT FORMAT
The first line of input contains $N$. The next $N$ lines each describe one cow
in terms of two integers, $x$ and $s$, where $x$ is the position
($0 <= x <= 10^6$), and $s$ is 0 for a healthy cow or 1 for a sick cow. At
least one cow is sick, and all cows that could possibly have become sick from
spread of the disease have now become sick.
OUTPUT FORMAT
Please output the minimum number of cows that could have initially been sick,
prior to any spread of the disease.
SAMPLE INPUT
6
7 1
1 1
15 1
3 1
10 0
6 1
SAMPLE OUTPUT
3
In this example, we know that $R < 3$ since otherwise the cow at position 7
would have infected the cow at position 10. Therefore, at least 3 cows must
have started out infected -- one of the two cows at positions 1 and 3, one of
the two cows at positions 6 and 7, and the cow at position 15.

View file

@ -1,5 +1,5 @@
// include this file at the top of every source file
#pragma once
#include <bits/stdc++.h>
// include this file at the top of every source file
#pragma once
#include <bits/stdc++.h>
using namespace std;

View file

@ -1,84 +1,84 @@
#include "huffman.h"
#include "common.h"
namespace huffman {
struct node {
char c; int f; node * l, * r;
node(char _c, int _f) { c = _c, f = _f, l = r = NULL; }
};
struct comp { bool operator()(node * a, node * b) { return a->f > b->f; } };
node * root;
vector<bool> path, code[128];
void traverse(node * n) {
if (n->l) path.push_back(0), traverse(n->l), path.pop_back();
if (n->r) path.push_back(1), traverse(n->r), path.pop_back();
if (n->c) code[n->c] = path;
}
void encode_tree(node * n, vector<bool> & v) {
if (n->c) {
v.push_back(1);
for (int i = 0; i < 8; ++i) v.push_back(1 & (n->c >> i));
}
else {
v.push_back(0);
if (n->l) v.push_back(1), encode_tree(n->l, v);
else v.push_back(0);
if (n->r) v.push_back(1), encode_tree(n->r, v);
else v.push_back(0);
}
}
int idx = 0;
void decode_tree(node * n, vector<bool> & v) {
if (v[idx++] == 1) {
for (int i = 0; i < 8; ++i) if (v[idx++]) n->c |= (1 << i);
}
else {
if (v[idx++] == 1) n->l = new node(0, 0), decode_tree(n->l, v);
if (v[idx++] == 1) n->r = new node(0, 0), decode_tree(n->r, v);
}
}
void generate(vector<int> f) {
priority_queue<node *, vector<node *>, comp> pq;
for (int c = 1; c < 128; ++c) {
node * n = new node(c, f[c]);
pq.push(n);
}
while (pq.size() > 1) {
node * l = pq.top(); pq.pop(); node * r = pq.top(); pq.pop();
node * n = new node(0, l->f + r->f);
n->l = l, n->r = r;
pq.push(n);
}
root = pq.top();
}
void solve(node * n, vector<bool> & v, string & s) {
if (idx > v.size()) return;
if (n->c) s += n->c, solve(root, v, s);
else v[idx++] == 0 ? solve(n->l, v, s) : solve(n->r, v, s);
}
vector<bool> encode(string s) {
vector<int> f(128, 0);
for (auto& c : s) ++f[c];
generate(f);
traverse(root);
vector<bool> ret;
encode_tree(root, ret);
for (auto& c : s) {
for (auto b : code[c]) ret.push_back(b);
}
return ret;
}
string decode(vector<bool> v) {
root = new node(0, 0);
decode_tree(root, v);
string ret;
solve(root, v, ret);
return ret;
}
#include "huffman.h"
#include "common.h"
namespace huffman {
struct node {
char c; int f; node * l, * r;
node(char _c, int _f) { c = _c, f = _f, l = r = NULL; }
};
struct comp { bool operator()(node * a, node * b) { return a->f > b->f; } };
node * root;
vector<bool> path, code[128];
void traverse(node * n) {
if (n->l) path.push_back(0), traverse(n->l), path.pop_back();
if (n->r) path.push_back(1), traverse(n->r), path.pop_back();
if (n->c) code[n->c] = path;
}
void encode_tree(node * n, vector<bool> & v) {
if (n->c) {
v.push_back(1);
for (int i = 0; i < 8; ++i) v.push_back(1 & (n->c >> i));
}
else {
v.push_back(0);
if (n->l) v.push_back(1), encode_tree(n->l, v);
else v.push_back(0);
if (n->r) v.push_back(1), encode_tree(n->r, v);
else v.push_back(0);
}
}
int idx = 0;
void decode_tree(node * n, vector<bool> & v) {
if (v[idx++] == 1) {
for (int i = 0; i < 8; ++i) if (v[idx++]) n->c |= (1 << i);
}
else {
if (v[idx++] == 1) n->l = new node(0, 0), decode_tree(n->l, v);
if (v[idx++] == 1) n->r = new node(0, 0), decode_tree(n->r, v);
}
}
void generate(vector<int> f) {
priority_queue<node *, vector<node *>, comp> pq;
for (int c = 1; c < 128; ++c) {
node * n = new node(c, f[c]);
pq.push(n);
}
while (pq.size() > 1) {
node * l = pq.top(); pq.pop(); node * r = pq.top(); pq.pop();
node * n = new node(0, l->f + r->f);
n->l = l, n->r = r;
pq.push(n);
}
root = pq.top();
}
void solve(node * n, vector<bool> & v, string & s) {
if (idx > v.size()) return;
if (n->c) s += n->c, solve(root, v, s);
else v[idx++] == 0 ? solve(n->l, v, s) : solve(n->r, v, s);
}
vector<bool> encode(string s) {
vector<int> f(128, 0);
for (auto& c : s) ++f[c];
generate(f);
traverse(root);
vector<bool> ret;
encode_tree(root, ret);
for (auto& c : s) {
for (auto b : code[c]) ret.push_back(b);
}
return ret;
}
string decode(vector<bool> v) {
root = new node(0, 0);
decode_tree(root, v);
string ret;
solve(root, v, ret);
return ret;
}
}

View file

@ -1,6 +1,6 @@
#include "common.h"
namespace huffman {
vector<bool> encode(string s);
string decode(vector<bool> v);
#include "common.h"
namespace huffman {
vector<bool> encode(string s);
string decode(vector<bool> v);
}

View file

@ -1,135 +1,135 @@
#include "common.h"
#include "huffman.h"
#define pb push_back
int getweight(string s)
{
if(s[0]=='$')
return 1000;
else if(s=="farmer"||s.length()==1||s=="example")return 1;
else return 10;
}
struct clause
{
vector<string> words;
vector<int> weights;
string s;
int weight;
void parse()
{
string word="";
bool iseq=false;
for(int j=0;j<s.length();j++)
{
if(s.at(j)=='$') iseq=!iseq;
if(('a'<=s.at(j)&&s.at(j)<='z')||('A'<=s.at(j)&&s.at(j)<='Z')||('0'<=s.at(j)&&s.at(j)<='9')||(iseq&&s.at(j)!=' '))
word+=('A'<=s.at(j)&&s.at(j)<='Z')?(s.at(j)-'A'+'a'):(s.at(j));
else
{
if(!iseq)
{
if(word!="")words.pb(word);
word="";
}
}
}
if(word!="") words.pb(word);
}
void calcweight()
{
for(auto& w: words)weights.pb(getweight(w));
weight=-100000;
for(auto& w : weights)if(w>weight)weight=w;
}
};
struct sentence
{
vector<clause> clauses;
string s;
int weight;
void parse()
{
string cl="";
bool iseq=false;
for(int j=0;j<s.length();j++)
{
if(s.at(j)=='$') iseq=!iseq;
if(s.at(j)!=','|| iseq) cl+=s.at(j);
else
{
clause tmp;
tmp.s=cl;clauses.pb(tmp);cl="";
}
}
clause tmp;
tmp.s=cl;clauses.pb(tmp);
for(auto& c:clauses) c.parse();
int numclause=0;
for(int i=0;i<clauses.size();i++)
if(clauses[i].words.size()!=0)
{
auto tmp=clauses[i];
clauses[numclause]=tmp;
numclause++;
}
clauses.resize(numclause);
}
void calcweight()
{
weight=-100000;
for(auto& c : clauses)
{
c.calcweight();
if(c.weight>weight)weight=c.weight;
}
}
};
vector<sentence> text;
vector<int> value;
string line;
int b,c;
void init()
{
cin>>c;
ifstream stin("soc1.txt");
string sentencefile="";
bool iseq=false;
while(getline(stin,line))
{
for(int i=0;i<line.length();i++)
{
b++;
if(line.at(i)=='$') iseq=!iseq;
if((line.at(i)!='.'&&line.at(i)!='?'&&line.at(i)!='!')|| iseq)sentencefile+=line.at(i);
else
{
sentence tmp;tmp.s=sentencefile;text.pb(tmp);
sentencefile="";
}
}
b++;
sentencefile+=' ';
}
for(auto& i:text)i.parse();
}
int main() {
/*init();
for(sentence& i:text)
{
for(clause& j:i.clauses)
{
for(auto k:j.words)
{
cout<<k<<endl;
}
}
}*/
vector<bool> test = huffman::encode("asdfjkl;1234");
for (auto b : test) cout << b;
cout << '\n';
string s = huffman::decode(test);
cout << s << '\n';
#include "common.h"
#include "huffman.h"
#define pb push_back
int getweight(string s)
{
if(s[0]=='$')
return 1000;
else if(s=="farmer"||s.length()==1||s=="example")return 1;
else return 10;
}
struct clause
{
vector<string> words;
vector<int> weights;
string s;
int weight;
void parse()
{
string word="";
bool iseq=false;
for(int j=0;j<s.length();j++)
{
if(s.at(j)=='$') iseq=!iseq;
if(('a'<=s.at(j)&&s.at(j)<='z')||('A'<=s.at(j)&&s.at(j)<='Z')||('0'<=s.at(j)&&s.at(j)<='9')||(iseq&&s.at(j)!=' '))
word+=('A'<=s.at(j)&&s.at(j)<='Z')?(s.at(j)-'A'+'a'):(s.at(j));
else
{
if(!iseq)
{
if(word!="")words.pb(word);
word="";
}
}
}
if(word!="") words.pb(word);
}
void calcweight()
{
for(auto& w: words)weights.pb(getweight(w));
weight=-100000;
for(auto& w : weights)if(w>weight)weight=w;
}
};
struct sentence
{
vector<clause> clauses;
string s;
int weight;
void parse()
{
string cl="";
bool iseq=false;
for(int j=0;j<s.length();j++)
{
if(s.at(j)=='$') iseq=!iseq;
if(s.at(j)!=','|| iseq) cl+=s.at(j);
else
{
clause tmp;
tmp.s=cl;clauses.pb(tmp);cl="";
}
}
clause tmp;
tmp.s=cl;clauses.pb(tmp);
for(auto& c:clauses) c.parse();
int numclause=0;
for(int i=0;i<clauses.size();i++)
if(clauses[i].words.size()!=0)
{
auto tmp=clauses[i];
clauses[numclause]=tmp;
numclause++;
}
clauses.resize(numclause);
}
void calcweight()
{
weight=-100000;
for(auto& c : clauses)
{
c.calcweight();
if(c.weight>weight)weight=c.weight;
}
}
};
vector<sentence> text;
vector<int> value;
string line;
int b,c;
void init()
{
cin>>c;
ifstream stin("soc1.txt");
string sentencefile="";
bool iseq=false;
while(getline(stin,line))
{
for(int i=0;i<line.length();i++)
{
b++;
if(line.at(i)=='$') iseq=!iseq;
if((line.at(i)!='.'&&line.at(i)!='?'&&line.at(i)!='!')|| iseq)sentencefile+=line.at(i);
else
{
sentence tmp;tmp.s=sentencefile;text.pb(tmp);
sentencefile="";
}
}
b++;
sentencefile+=' ';
}
for(auto& i:text)i.parse();
}
int main() {
/*init();
for(sentence& i:text)
{
for(clause& j:i.clauses)
{
for(auto k:j.words)
{
cout<<k<<endl;
}
}
}*/
vector<bool> test = huffman::encode("asdfjkl;1234");
for (auto b : test) cout << b;
cout << '\n';
string s = huffman::decode(test);
cout << s << '\n';
}