Skip to content

Commit

Permalink
some easy algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
RryLee committed Mar 6, 2016
1 parent 2e39dd2 commit ca46262
Show file tree
Hide file tree
Showing 9 changed files with 238 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Search/1-sherlock-and-array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <iostream>
#include <cstdio>

using namespace std;

int main(int argc, char const *argv[])
{
int T;
cin >> T;
while (T --) {
int N;
cin >> N;
int A[N];
int sum = 0;
for (int i = 0; i < N; i ++) {
scanf("%d", &A[i]);
sum += A[i];
}

int right = sum;
bool exists = false;

for (int i = 0; i < N; i ++) {
int left = sum - right;
right -= A[i];

if (left == right) {
exists = true;
break;
}
}

if (exists) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}

return 0;
}
31 changes: 31 additions & 0 deletions Search/1-sherlock-and-array.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, "%d", $t);

while ($t --) {
fscanf($handle, "%d", $n);
$arr = array_map('intval', explode(' ', trim(fgets($handle))));

if ($n == 1) {
echo "YES\n";
continue;
}

$exists = false;

$sum = array_sum($arr);
$right = $sum;

for ($i=0; $i < $n; $i++) {
$left = $sum - $right;
$right = $right - $arr[$i];

if ($left == $right) {
$exists = true;
break;
}
}

echo $exists ? "YES\n" : "NO\n";
}
22 changes: 22 additions & 0 deletions Search/2-icecream-parlor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, "%d", $T);

while ($T --) {
fscanf($handle, "%d", $M);
fscanf($handle, "%d", $N);
$prices = array_map('intval', explode(' ', trim(fgets($handle))));

$searched = [];
for ($i=0; $i < $N; $i++) {
$diff = $M - $prices[$i];

if (array_key_exists($diff, $searched)) {
printf("%d %d\n", $searched[$diff] + 1, $i + 1);
break;
}

$searched[$prices[$i]] = $i;
}
}
51 changes: 51 additions & 0 deletions Search/3-maximise-sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <cstdio>
#include <vector>
#include <set>

using namespace std;

typedef unsigned long long ull;

ull find_result(const vector<ull> &v, ull m) {
vector<ull> prefix;
set<ull> s;

// get prefix array
size_t v_size = v.size();
ull tmp = 0;
for (size_t i=0; i<v_size; ++i) {
tmp += v[i];
tmp %= m;
prefix.push_back(tmp);
}

ull res = 0;
for (size_t i=0; i<v_size; ++i) {
auto it = s.upper_bound(prefix[i]);
if (it != s.end()) {
tmp = (prefix[i] - *it + m) % m;
res = max(res, tmp);
}
s.insert(prefix[i]);
res = max(res, prefix[i]);
}
return res;
}

int main()
{
int cnt;
scanf("%d", &cnt);
vector<ull> v;
ull n, m, tmp;
for (int i=0; i<cnt; ++i) {
scanf("%llu %llu", &n, &m);
v.clear();
for (int j=0; j<n; ++j) {
scanf("%llu", &tmp);
v.push_back(tmp);
}
printf("%llu\n", find_result(v, m));
}
return 0;
}
42 changes: 42 additions & 0 deletions Search/3-maximise-sum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

$T = trim(fgets(STDIN));

while ($T --) {
fscanf(STDIN, "%d %d", $n, $m);
$nums = array_map('intval', explode(' ', trim(fgets(STDIN))));

$psum = array(-1 => 0);
$msum = array(-1 => 0);
$midx = array();
$midx[0][] = -1;
$max = 0;
for ($i=0; $i < $n; $i++) {
$psum[$i] = $psum[$i - 1] + $nums[$i];
$msum[$i] = $psum[$i] % $m;
$midx[$msum[$i]][] = $i;
if ($msum[$i] > $max)
$max = $msum[$i];
}
// print_r($psum);
// print_r($msum);

sort($msum);
for ($i=1; $i <= $n; $i++) {
$sum1 = $msum[$i - 1];
$sum2 = $msum[$i];
// echo "sum1:$sum1, sum2:$sum2\n";
foreach ($midx[$sum1] as $p1) {
foreach ($midx[$sum2] as $p2) {
if ($p1 > $p2) {
$d = $sum1 - $sum2;
if ($d < 0 && $m + $d > $max) {
$max = $m + $d;
}
}
}
}
}

echo $max . "\n";
}
33 changes: 33 additions & 0 deletions Search/4-missing-numbers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <iostream>
#include <cstdio>

typedef unsigned long ul;

using namespace std;

int main(int argc, char const *argv[])
{
ul n, m;
cin >> n;
int times[10005] = {0};
for (ul i = 0; i < n; i ++) {
int temp;
scanf("%d", &temp);
times[temp] --;
}

cin >> m;
for (ul i = 0; i < m; i ++) {
int temp;
scanf("%d", &temp);
times[temp] ++;
}

for (int i = 0; i < 10005; i ++) {
if (times[i] >= 1) {
cout << i << " ";
}
}

return 0;
}
6 changes: 6 additions & 0 deletions Search/4-missing-numbers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, "%d", $n);

var_dump($list1);
12 changes: 12 additions & 0 deletions Search/5-connected-cell-in-a-grid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

// not solve

$handle = fopen("php://stdin", "r");
fscanf($handle, "%d", $m);
fscanf($handle, "%d", $n);

while ($m --) {
$matrix[] = array_map('intval', explode(' ', trim(fgets($handle))));
}
// var_dump($matrix);
Binary file added Search/a.out
Binary file not shown.

0 comments on commit ca46262

Please sign in to comment.