結果

問題 No.913 木の燃やし方
ユーザー ei1333333ei1333333
提出日時 2019-10-18 13:28:30
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 1,303 ms / 3,000 ms
コード長 4,505 bytes
コンパイル時間 3,608 ms
コンパイル使用メモリ 107,772 KB
実行使用メモリ 57,664 KB
最終ジャッジ日時 2023-09-07 20:29:37
合計ジャッジ時間 44,489 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 61 ms
22,796 KB
testcase_01 AC 60 ms
20,704 KB
testcase_02 AC 59 ms
18,740 KB
testcase_03 AC 84 ms
21,040 KB
testcase_04 AC 83 ms
19,080 KB
testcase_05 AC 85 ms
21,064 KB
testcase_06 AC 79 ms
21,088 KB
testcase_07 AC 73 ms
21,032 KB
testcase_08 AC 1,238 ms
43,668 KB
testcase_09 AC 1,208 ms
44,248 KB
testcase_10 AC 1,194 ms
44,316 KB
testcase_11 AC 1,223 ms
42,744 KB
testcase_12 AC 1,293 ms
42,392 KB
testcase_13 AC 1,220 ms
44,640 KB
testcase_14 AC 1,171 ms
43,900 KB
testcase_15 AC 1,179 ms
43,956 KB
testcase_16 AC 1,231 ms
44,080 KB
testcase_17 AC 1,175 ms
41,192 KB
testcase_18 AC 1,177 ms
41,292 KB
testcase_19 AC 1,274 ms
57,664 KB
testcase_20 AC 1,303 ms
44,892 KB
testcase_21 AC 1,231 ms
39,724 KB
testcase_22 AC 1,249 ms
38,804 KB
testcase_23 AC 1,239 ms
48,224 KB
testcase_24 AC 1,225 ms
52,896 KB
testcase_25 AC 1,206 ms
52,788 KB
testcase_26 AC 1,252 ms
45,696 KB
testcase_27 AC 1,303 ms
56,028 KB
testcase_28 AC 1,284 ms
36,328 KB
testcase_29 AC 1,271 ms
35,600 KB
testcase_30 AC 1,268 ms
35,620 KB
testcase_31 AC 1,228 ms
52,060 KB
testcase_32 AC 1,264 ms
51,608 KB
testcase_33 AC 1,237 ms
53,624 KB
testcase_34 AC 1,273 ms
37,648 KB
testcase_35 AC 1,279 ms
36,832 KB
testcase_36 AC 1,235 ms
35,188 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Numerics;

class Program
{
  Scanner cin;

  const long INF = 1000000000000000000L;

  int N;
  long[] S;
  long[] ans;

  void rec(int l, int r)
  {
    if(l >= r)
    {
      return;
    }

    int md = (l + r) / 2;
    rec(l, md);
    rec(md + 1, r);


    if (l < md)
    {
      var cht = new Optimization.SimpleCHT();
      for (long i = md + 1; i <= r; i++)
      {
        cht.AddLine(-2 * i, i * i + S[i]);
      }
      long ret = INF;
      for (long i = l; i < md; i++)
      {
        ret = Math.Min(ret, cht.Get((int)i) + i * i - S[i]);
        ans[i] = Math.Min(ans[i], ret);
      }
    }

    if (md < r)
    {
      var cht = new Optimization.SimpleCHT();
      for (long i = md; i >= l; i--)
      {
        cht.AddLine(2 * i, i * i - S[i]);
      }
      long ret = INF;
      for (long i = r; i > md; i--)
      {
        ret = Math.Min(ret, cht.Get((int)-i) + i * i + S[i]);
        ans[i - 1] = Math.Min(ans[i - 1], ret);
      }
    }
  }

  void myon()
  {
    cin = new Scanner();

    N = cin.nextInt();

    S = new long[N + 1];
    for(int i = 0; i < N; i++)
    {
      S[i + 1] = S[i] + cin.nextLong();
    }

    ans = new long[N];
    for(int i = 0; i < N; i++)
    {
      ans[i] = INF;
    }
    rec(0, N);

    for(int i = 0; i < N; i++)
    {
      Console.WriteLine(ans[i]);
    }
  }

  static void Main(string[] args)
  {
    new Program().myon();
  }
}

class Scanner
{
  string[] s;
  int i;
  readonly char[] cs = new char[] { ' ' };

  public Scanner()
  {
    s = new string[0];
    i = 0;
  }

  public string next()
  {
    if (i < s.Length) return s[i++];
    string st = Console.ReadLine();
    while (st == "") st = Console.ReadLine();
    s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
    if (s.Length == 0) return next();
    i = 0;
    return s[i++];
  }

  public string nextLine()
  {
    return Console.ReadLine();
  }

  public int nextInt()
  {
    return int.Parse(next());
  }

  public long nextLong()
  {
    return long.Parse(next());
  }

  public double nextDouble()
  {
    return double.Parse(next());
  }
}


// https://yukicoder.me/submissions/109325

#region CHT
namespace Optimization
{
  using N = System.Int64;
  struct L
  {
    internal N a, b;
    /// <summary>
    /// u*x+v
    /// </summary>
    public L(N u, N v) : this() { a = u; b = v; }

    /// <summary>
    /// F(x)=a*x+b
    /// </summary>
    public N F(int x) { return a * x + b; }
  }
  /// <summary>
  /// 追加される直線の傾きが単調減少
  /// </summary>
  public class SimpleCHT
  {
    int s, t;
    List<L> deq = new List<L>();
    /// <summary>
    /// ax+b を追加
    /// aは単調減少でないとダメ,ならしO(1)
    /// </summary>
    public void AddLine(N a, N b)
    {
      var l = new L(a, b);
      while (s + 1 < t && check_fast(deq[t - 2], deq[t - 1], l)) { t--; deq.RemoveAt(t); }
      deq.Add(l);
      t++;
    }
    /// <summary>
    /// xが単調増加のときのみ使ってよい,ならしO(1)
    /// </summary>
    public N Get(int x)
    {
      while (s + 1 < t && deq[s].F(x) >= deq[s + 1].F(x)) s++;
      return deq[s].F(x);
    }

    /// <summary>
    /// xが単調増加でないときでも使える,O(logN)
    /// </summary>
    /// <param name="x"></param>
    /// <returns></returns>
    public N Query(int x)
    {
      var l = s - 1; var h = t - 1;
      while (l + 1 < h)
      {
        var m = (l + h) / 2;
        if (isR_fast(deq[m], deq[m + 1], x)) l = m;
        else h = m;
      }
      return deq[h].F(x);

    }

    /// <summary>
    /// ax,bが小さい
    /// </summary>
    private bool isR_fast(L l1, L l2, int x)
    {
      return (l1.b - l2.b) >= x * (l2.a - l1.a);
    }
    /// <summary>
    /// ax,bが大きい
    /// </summary>
    private bool isR_slow(L l1, L l2, int x)
    {
      BigInteger b1 = l1.b, b2 = l2.b;
      BigInteger a1 = l1.a, a2 = l2.a;
      return (b1 - b2) >= x * (a2 - a1);
    }

    /// <summary>
    /// a*bが小さい
    /// </summary>
    private bool check_fast(L l1, L l2, L l3)
    {
      return (l2.a - l1.a) * (l3.b - l2.b) >= (l2.b - l1.b) * (l3.a - l2.a);
    }
    /// <summary>
    /// a*bが大きい
    /// </summary>
    private bool check_slow(L l1, L l2, L l3)
    {
      BigInteger a1 = l1.a, a2 = l2.a, a3 = l3.a;
      BigInteger b1 = l1.b, b2 = l2.b, b3 = l3.b;

      return (a2 - a1) * (b3 - b2) >= (b2 - b1) * (a3 - a2);
    }
  }
}
#endregion
0