惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
S
Schneier on Security
The Hacker News
The Hacker News
P
Proofpoint News Feed
Spread Privacy
Spread Privacy
L
LINUX DO - 热门话题
L
Lohrmann on Cybersecurity
I
Intezer
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Schneier on Security
Schneier on Security
Security Latest
Security Latest
AWS News Blog
AWS News Blog
B
Blog RSS Feed
Microsoft Security Blog
Microsoft Security Blog
有赞技术团队
有赞技术团队
博客园 - 叶小钗
The Last Watchdog
The Last Watchdog
O
OpenAI News
月光博客
月光博客
Hacker News: Ask HN
Hacker News: Ask HN
阮一峰的网络日志
阮一峰的网络日志
S
Security @ Cisco Blogs
Google Online Security Blog
Google Online Security Blog
云风的 BLOG
云风的 BLOG
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Latest news
Latest news
P
Palo Alto Networks Blog
Last Week in AI
Last Week in AI
M
MIT News - Artificial intelligence
Google DeepMind News
Google DeepMind News
P
Proofpoint News Feed
C
CERT Recently Published Vulnerability Notes
Apple Machine Learning Research
Apple Machine Learning Research
U
Unit 42
PCI Perspectives
PCI Perspectives
博客园 - 聂微东
SecWiki News
SecWiki News
宝玉的分享
宝玉的分享
Forbes - Security
Forbes - Security
H
Heimdal Security Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Troy Hunt's Blog
博客园 - 三生石上(FineUI控件)
Application and Cybersecurity Blog
Application and Cybersecurity Blog
罗磊的独立博客
WordPress大学
WordPress大学
D
Darknet – Hacking Tools, Hacker News & Cyber Security

hayato

Chromium's DOM Deep Dive: Understanding Shadow DOM and Tree Structures - hayato Introducing the Task Scheduler in Chromium's Network Stack: A 0.3% Improvement in Loading Speed - hayato 出版のお知らせ 「普通の人が資産運用で99点をとる方法とその考え方」 - hayato Resource loading with Content Addressable Bundles - hayato 普通の人が資産運用で99点をとる方法とその考え方 - hayato Imperative Shadow DOM Distribution API - hayato Google にソフトウェアエンジニアとして入社して 10 年と 10 日がたちました - hayato Web のつくりかた: ソフトウェアエンジニアリングと雰囲気で理解する Web 標準とブラウザのつくりかた - hayato ブラウザのしくみ: データ構造とアルゴリズムと雰囲気で理解する DOM と Shadow DOM - hayato Rust が最強のプログラミング言語である証明 - hayato 六本木ではたらくソフトウェアエンジニアへのよくある質問とその答え (FAQ) (2015 - 2017) - hayato What's New in Shadow DOM v1 (by examples) - hayato Simple timer application for the pomodoro technique - hayato Google の面接試験で出る (はずがない)「海賊と金貨」問題を空気を読まないでプログラムで解いてみる - hayato hayato.io (4topcoder) - hayato SRM432 - hayato SRM405 - hayato SRM386 - hayato SRM385 - hayato SRM384 - ゲーム - hayato SRM369 - hayato SRM368 - hayato SRM366 - typo 2 連発 - hayato Python - generator で素数生成 - hayato New Launguage - 新言語追加の予定? - hayato lxml で HTML スクレーピング - hayato SRM358 - Today is a Bad Day (-50). - hayato SRM349 FizzBuzz 問題が簡単すぎるそんなあなたへ。「赤いマーブル問題」。 - hayato Suffix Array - hayato google-code-prettify を導入してみる - hayato Subset の生成 - hayato 2006 年のまとめ - SRM332 - hayato Google Code Jam 2006 - Championship Round - hayato Blogger Beta に移行 - hayato Google Code Jam 2006 - Round1 に挑戦 - Generics と AutoBoxing - hayato Google Code Jam 2006 - Qualification Round 通過 - hayato Google Code Jam 2006 - Qualification Round に挑戦 - hayato Google Code Jam 2006 - Practice1 - hayato Google Code Jam 2006 - 開催決定 - hayato Stay tuned... Global Google Code Jam 2006 - 秋に開催? - hayato SRM308 - Hard DP - hayato SRM304 - 条件付確率 - hayato JavaWorld に寄稿しました - hayato 2006 TopCoder Open Final Round - hayato IBM Rational Software Development Forum Tokyo で講演します - hayato SRM298 - ひとふで書き - hayato SRM293 - ビンゴ - hayato 2006 TopCoder Open - Round1 撃墜 - hayato 2006 TopCoder Open - Odds 発表 - hayato 2006 TopCoder Open Qualification Round - hayato SRM291 - OutOfMemory - hayato デブサミ 2006 で講演します - hayato SRM287 - 気分は中学生?連立2元方程式 - hayato SRM281 - 2006 年はポイントゼロスタート - hayato SRM280 - Challenge 祭り - hayato SRM277 - 初賞金げっと - hayato マラソン・マッチ - hayato reStructuredText で blogger - hayato 2006 TopCoder Open - NSA がスポンサーに - hayato SRM276 - お買い物は楽しく DP で - hayato SRM273 - bit 演算は int で行われるのね - hayato SRM272 - Division1 - 4 度目の正直 - hayato JavaOne Tokyo で講演します - hayato SRM268 - Division2 脱出 - hayato SRM266 - NASA, we have a problem... - hayato SRM265 - Division2 落ち... - hayato SRM262 - 初 Division1 は Typo に終わる - hayato SRM260 - TopCoder への挑戦のはじまり - hayato
SRM270 - Integer == Integer - hayato
2005-11-03 · via hayato

SRM270 に挑戦.Division1 に挑戦するのはこれが 3 回目ですが,前の 2 回は,

  • Level1 問題 - どちらも Submit するも,単純なミスで System Test に落ちる.
  • Level2 問題 - どちらも時間切れ.
  • Level3 問題 - Open したことさえありません..

とまだ 1 問も System Test に通ったことがありません.今度こそは 1 問は通るように..

300 点問題. Code は提出.今度こそ大丈夫と思っていたもの,また System Test で落ちてしまいました.どこがおかしいんだろと Code を見ていて,5 分ほど.ようやく間違いを発見しました.ここです

if (l.get(i) == maxscore.get(key))

Integer と Integer を == で比較しています...あーやってしまった. J2SE5 から autoboxing/unboxing が使えるようになりましたが,すっかりそれに頼りきっていました.このように左辺 と 右辺 ともに Integer の場合は,だめですね..equals(..) じゃないと. どちらかが int ならよかったんだけど.また単純ミスだ..

600 点問題. 巡回セールスマン問題..ではありません.基本的には最短経路を求める問題に落ち着くのですが,やっかいな点としてネガティブ・ループの考慮をする必要があります. SRM 中は,時間切れで Submit できませんでした.もう少しだったんですけど.

SRM 後に,完成させたコードです.タイムオーバーになるかと思ったのですが,今回の条件ではこれでも十分 2 秒以内でした.

import java.util.Scanner;
public class SalesmansDilemma {

int[][] p;
int[] visited;
int destination;

final int NA = Integer.MIN_VALUE;
final int ENDLESS = Integer.MAX_VALUE;
final int IMPOSSIBLE = Integer.MIN_VALUE;

int goal = IMPOSSIBLE;

public String bestRoute(int towns, int origin, int destination,
        String[] travelCosts, int[] profits) {
    this.destination = destination;
    p = new int[towns][towns];
    for (int i = 0; i < towns; i++) Arrays.fill(p[i], NA);
    for (int i = 0; i < travelCosts.length; i++) {
        Scanner s = new Scanner(travelCosts[i]);
        int from = s.nextInt();
        int to = s.nextInt();
        int cost = s.nextInt();
        p[from][to] = Math.max(p[from][to], profits[to] - cost);
    }

    visited = new int[towns];
    Arrays.fill(visited, IMPOSSIBLE);
    visited[origin] = 0;
    go(origin);

    if (goal == IMPOSSIBLE) return "IMPOSSIBLE";
    if (goal == ENDLESS) return "ENDLESS PROFIT";
    return "BEST PROFIT: " + (goal + profits[origin]);
}

void go(int town) {
    if (town == destination) goal = Math.max(goal, visited[town]);

    for (int i = 0; i < p[town].length; i++) {
        if (p[town][i] == NA) continue;

        int old = visited[i];
        int update = (visited[town] == ENDLESS) ?
                     ENDLESS : visited[town] + p[town][i];

        if (old == IMPOSSIBLE) {
            visited[i] = update;
            go(i);
            visited[i] = old;
        } else if (update > old) {
            visited[i] = ENDLESS;
            go(i);
            visited[i] = old;
        }
    }
}

この手の問題では,Bellman-Ford アルゴリズムを用いるのが定石のようです.ある地点からある地点までの,最短経路,およびネガティブ・ループを発見するアルゴリズムです.練習がてら,Java で実装してみました.

import java.util.Scanner;
public class SalesmansDilemma {

class Edge {
    int source;
    int destination;
    int weight;
}

public String bestRoute(int towns, int origin, int destination,
        String[] travelCosts, int[] profits) {
    Edge[] edges  = new Edge[travelCosts.length];
    for (int i = 0; i < travelCosts.length; i++) {
        Scanner scan  = new Scanner(travelCosts[i]);
        edges[i] = new Edge();
        edges[i].source = scan.nextInt();
        edges[i].destination = scan.nextInt();
        edges[i].weight = scan.nextInt()
                         - profits[edges[i].destination];
    }
    long[] verticles = new long[towns];
    long INFINITY = Long.MAX_VALUE;
    for (int i = 0; i < verticles.length; i++) {
        verticles[i] = INFINITY;
    }
    verticles[origin] = 0;

    boolean[] negative = new boolean[towns];

    // relax and find negative cycle
    for (int i = 0; i < verticles.length + 1; i++) {
        for (Edge e : edges) {
            int u = e.source;
            int v = e.destination;
            if (verticles[u] == INFINITY) continue;
            if (verticles[v] > verticles[u] + e.weight) {
                verticles[v] = verticles[u] + e.weight;
                if (i == verticles.length) {
                    negative[v] = true;
                }
            }
        }
    }

    if (verticles[destination] == INFINITY) return "IMPOSSIBLE";

    boolean[] mark = new boolean[towns];
    for (int i = 0; i < towns; i++) {
        if (negative[i] && !mark[i]) {
            go(i, mark, edges);
        }
    }
    if (mark[destination]) return "ENDLESS PROFIT";

    return "BEST PROFIT: " +
           -(verticles[destination] - profits[origin]);
}

void go(int v, boolean[] mark, Edge[] edges) {
    mark[v] = true;
    for (Edge e : edges) {
        if (v == e.source && !mark[e.destination]) {
            go(e.destination, mark, edges);
        }
    }
}
}

900 点問題. SRM 中は Open しませんでした.ある長方形のなかに,別の長方形をはみださないようにいれることができるか判定する問題です.たとえば,幅 100 x 高さ 100 の長方形には,幅 140 x 高さ 1 の長方形は(傾ければ)いれることができます. 幅 140 x 50 の長方形はどんなにがんばってもはいりません.

結果

System Test の結果です. ( Room Statistics )

Room Statistics

レーティングは 1303 -> 1214 と低下.かろうじて Division1 には残っています.

今日の教訓

  • Primitive とラッパー..Java を使い続ける限りつきまとう,もっとも頭のいたい問題です. autoboxing/unboxing には惑わされないように.