Exemplo n.º 1
0
    }
}
/** Aqui fica a base do Strategy
 * Ordem um conjunto de dados de acordo com seu tamanho. Caso tenha mais de 100 dados, irá realizar
 * quicksort, caso contrário executará o algoritmo de inserção.
 * @param array $dados Dados que serão ordenados
 * @return array Dados ordenados
 */
function ordena(array $dados)
{
    if (count($dados) > 100) {
        $obj = new QuickSort($dados);
    } else {
        $obj = new InsertSort($dados);
    }
    return $obj->sort();
}
/**
 * Exemplo de chamada sem o padrão:
 * sempre quicksort, independente do tamanho ou da sequencia dos dados
 * $dados = array(1,2,3,1,2,3,5,6,6,2,1);
 * $quick = new QuickSort($dados);
 * $dados = $quick->sort();
 * 
 */
$dados = array('banana', 'abacate', 'uva', 'chinelo', 'cafe');
$dados = ordena($dados);
print_r($dados);
/**
 * Resultado: trará sempre os dados ordenados com a melhor estratégia para o desempenho
 */
Exemplo n.º 2
0
include_once "../../../clases/bd.php";
switch ($_POST["metodo"]) {
    case "buscar":
        busca();
        break;
    case "filtrarCat":
        filtraCat();
        break;
    case "filtrarEst":
        filtraEst();
        break;
    case "filtrarCon":
        filtraCon();
        break;
    case "ordenar":
        ordena();
        break;
}
function filtraCat()
{
    $bd = new bd();
    $clasificado = new clasificados($_POST["id"]);
    $palabra = $_POST["palabra"] != "" ? " and titulo like '%{$_POST["palabra"]}%'" : "";
    if ($_POST["estado"] != "") {
        $strEstado = " and usuarios_id in (select id from usuarios where estados_id={$_POST["estado"]})";
    } else {
        $strEstado = "";
    }
    if ($_POST["condicion"] != "") {
        $strCondicion = " and condiciones_publicaciones_id={$_POST["condicion"]}";
    } else {
Exemplo n.º 3
0
function DBScoreSite($contest, $site, $verifylastmile, $hor = -1, $data = null)
{
    if (($blocal = DBSiteInfo($contest, $_SESSION["usertable"]["usersitenumber"])) == null) {
        exit;
    }
    if (($b = DBSiteInfo($contest, $site, null, false)) == null) {
        $b = $blocal;
    }
    if (($ct = DBContestInfo($contest)) == null) {
        exit;
    }
    $t = time();
    $ta = $blocal["currenttime"];
    if ($hor >= 0) {
        $ta = $hor;
    }
    if ($verifylastmile) {
        $tf = $b["sitelastmilescore"];
    } else {
        $tf = $b["siteduration"];
    }
    if ($data != null && is_numeric($data)) {
        if ($data < $ta) {
            $ta = $data;
        }
        $data = null;
    }
    $data0 = array();
    if ($data == null) {
        $c = DBConnect();
        $resp = array();
        $r = DBExec($c, "select * from usertable where contestnumber={$contest} and usersitenumber={$site} and " . "usertype='team' and userlastlogin is not null and userenabled='t'", "DBScoreSite(get users)");
        $n = DBnlines($r);
        for ($i = 0; $i < $n; $i++) {
            $a = DBRow($r, $i);
            $resp[$a["usernumber"]]["user"] = $a["usernumber"];
            $resp[$a["usernumber"]]["site"] = $a["usersitenumber"];
            $resp[$a["usernumber"]]["username"] = $a["username"];
            $resp[$a["usernumber"]]["usertype"] = $a["usertype"];
            $resp[$a["usernumber"]]["userfullname"] = $a["userfullname"];
            $resp[$a["usernumber"]]["totaltime"] = 0;
            $resp[$a["usernumber"]]["totalcount"] = 0;
            $resp[$a["usernumber"]]["problem"] = array();
        }
        $r = DBExec($c, "select r.usernumber as user, p.problemname as problemname, r.runproblem as problem, " . "p.problemcolor as color, p.problemcolorname as colorname, " . "r.rundatediff as time, r.rundatediffans as anstime, a.yes as yes, r.runanswer as answer from " . "runtable as r, answertable as a, problemtable as p where r.runanswer=a.answernumber and " . "a.contestnumber={$contest} and p.problemnumber=r.runproblem and p.contestnumber={$contest} and " . "r.contestnumber={$contest} and r.runsitenumber={$site} and (r.runstatus ~ 'judged' or r.runstatus ~ 'judged+') and " . "r.rundatediff>=0 and r.rundatediff<={$tf} and r.rundatediffans<={$ta} " . "order by r.usernumber, r.runproblem, r.rundatediff", "DBScoreSite(get runs)");
        $n = DBnlines($r);
        $a = array();
        for ($i = 0; $i < $n; $i++) {
            $a[$i] = DBRow($r, $i);
        }
        $data0['n'] = $n;
        $data0['resp'] = $resp;
        $data0['a'] = $a;
        $data0['site'] = $site;
    } else {
        $resp = $data['resp'];
        $n = $data['n'];
        $a = $data['a'];
    }
    $i = 0;
    while ($i < $n) {
        if ($a[$i]["anstime"] > $ta) {
            $i++;
            continue;
        }
        $user = $a[$i]["user"];
        $problem = $a[$i]["problem"];
        $time = 0;
        $k = 0;
        if (!isset($resp[$user])) {
            $i++;
            continue;
        }
        $resp[$user]["user"] = $user;
        $resp[$user]["site"] = $site;
        $resp[$user]["problem"][$problem]["name"] = $a[$i]["problemname"];
        $resp[$user]["problem"][$problem]["color"] = $a[$i]["color"];
        $resp[$user]["problem"][$problem]["colorname"] = $a[$i]["colorname"];
        $resp[$user]["problem"][$problem]["solved"] = false;
        $resp[$user]["problem"][$problem]["judging"] = false;
        $resp[$user]["problem"][$problem]["time"] = 0;
        $resp[$user]["problem"][$problem]["penalty"] = 0;
        $resp[$user]["problem"][$problem]["count"] = 0;
        while ($i < $n && $a[$i]["anstime"] <= $ta && $a[$i]["user"] == $user && $a[$i]["problem"] == $problem && $a[$i]["yes"] != 't') {
            $time += (int) ($ct["contestpenalty"] / 60);
            $k++;
            $i++;
        }
        $resp[$user]["problem"][$problem]["count"] = $k;
        if ($i >= $n) {
            break;
        }
        if ($a[$i]["anstime"] <= $ta && $a[$i]["user"] == $user && $a[$i]["problem"] == $problem && $a[$i]["yes"] == 't') {
            $timet = (int) ($a[$i]["time"] / 60);
            if (!isset($resp[$user]["first"]) || $timet < $resp[$user]["first"]) {
                $resp[$user]["first"] = $timet;
            }
            $time += $timet;
            $resp[$user]["problem"][$problem]["time"] = $timet;
            $resp[$user]["problem"][$problem]["penalty"] = $time;
            $resp[$user]["problem"][$problem]["solved"] = true;
            $resp[$user]["problem"][$problem]["count"]++;
            $resp[$user]["totaltime"] += $time;
            $resp[$user]["totalcount"]++;
        }
        while ($i < $n && $a[$i]["user"] == $user && $a[$i]["problem"] == $problem) {
            $i++;
        }
    }
    if ($data == null) {
        $aa = DBRecentNews($contest, $site, $verifylastmile, $ta);
        $data0['aa'] = $aa;
    } else {
        $aa = $data['aa'];
    }
    for ($i = 0; $i < count($aa); $i++) {
        if ($aa[$i]["fut"] == 't') {
            $resp[$aa[$i]["usernumber"]]["problem"][$aa[$i]["problemnumber"]]["judging"] = true;
        }
    }
    if (($result = ordena($resp)) === false) {
        LOGError("Error while sorting scores (contest={$contest}, site={$site}).");
        MSGError("Error while sorting scores. Contact an admin now!");
    }
    return array($result, $data0);
}