Submission #1168367


Source Code Expand

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Globalization;
using System.Diagnostics;
using static System.Console;
using Pair = System.Collections.Generic.KeyValuePair<int, int>;
 
class Program
{
    static void Main()
    {
        SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
        new Program().Solve();
        Out.Flush();
    }
    
    Scanner cin = new Scanner();
    Random rnd = new Random();
    Stopwatch sw = new Stopwatch();
    readonly int[] dd = { 0, 1, 0, -1, 0 };
    readonly int mod = 1000000007;
    readonly string alfa = "abcdefghijklmnopqrstuvwxyz";
 
    int N;
    void Solve()
    {
       N = cin.Nextint;
       //各長方形にくっつけてみる
       var L = new int[N];
       var R = new int[N];
       for (int i = 0; i < N; i++)
       {
           L[i] = cin.Nextint;
           R[i] = cin.Nextint;
       }
       long ans = long.MaxValue;
       
       for (int i = 0; i < N; i++)
       {
           long x = 0;
           int l = L[i], r = R[i];
           for (int j = i - 1; j >= 0; j--)
           {
               if(R[j] < l || r < L[j])
               {
                   if (R[j] < l)
                   {
                       x+=l-R[j];
                       r = l;
                       l = r - (R[j] - L[j]); 
                   }
                   else 
                   {
                       x+=L[j] - r;
                       l= r;
                       r = l + (R[j] - L[j]);
                   }
               }
               else 
               {
                   r = R[j];
                   l = L[j];
               }
                //WriteLine($"j={j} r={r} l={l}");
           }
           l = L[i]; r = R[i];
           for (int j = i + 1; j < N; j++)
           {
               if(R[j] < l || r < L[j])
               {
                   if (R[j] < l)
                   {
                       x+=l-R[j];
                       r = l;
                       l = r - (R[j] - L[j]); 
                   }
                   else 
                   {
                       x+=L[j] - r;
                       l=r;
                       r = l + (R[j] - L[j]);
                   }
               }
               else 
               {
                   r = R[j];
                   l = L[j];
               }
               //WriteLine($"j={j} r={r} l={l}");
           }

           ans = Math.Min(ans, x);
           //WriteLine($"x={x} i={i}");
       }
       WriteLine(ans);
    }
 
}
 
class Scanner
{
    string[] s; int i;
    char[] cs = new char[] { ' ' };
    public Scanner() { s = new string[0]; i = 0; }
    public string[] Scan { get { return ReadLine().Split(); } }
    public int[] Scanint { get { return Array.ConvertAll(Scan, int.Parse); } }
    public long[] Scanlong { get { return Array.ConvertAll(Scan, long.Parse); } }
    public double[] Scandouble { get { return Array.ConvertAll(Scan, double.Parse); } }
    public string Next
    {
        get
        {
            if (i < s.Length) return s[i++];
            string st = ReadLine();
            while (st == "") st = ReadLine();
            s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
            i = 0;
            return Next;
        }
    }
    public int Nextint { get { return int.Parse(Next); } }
    public long Nextlong { get { return long.Parse(Next); } }
    public double Nextdouble { get { return double.Parse(Next); } }
}

Submission Info

Submission Time
Task E - NarrowRectangles
User claw88
Language C# (Mono 4.6.2.0)
Score 0
Code Size 3626 Byte
Status WA
Exec Time 2108 ms
Memory 18012 KB

Judge Result

Set Name Sample Subtask All
Score / Max Score 0 / 0 0 / 300 0 / 700
Status
AC × 5
AC × 2
WA × 11
AC × 7
WA × 11
TLE × 19
Set Name Test Cases
Sample 0_000.txt, 0_001.txt, 0_002.txt, 0_003.txt, 0_004.txt
Subtask 0_000, 0_001, 0_004, 1_005.txt, 1_006.txt, 1_007.txt, 1_008.txt, 1_009.txt, 1_010.txt, 1_011.txt, 1_012.txt, 1_013.txt, 1_014.txt, 1_015.txt, 1_016.txt, 1_017.txt
All 0_000.txt, 0_001.txt, 0_002.txt, 0_003.txt, 0_004.txt, 1_005.txt, 1_006.txt, 1_007.txt, 1_008.txt, 1_009.txt, 1_010.txt, 1_011.txt, 1_012.txt, 1_013.txt, 1_014.txt, 1_015.txt, 1_016.txt, 1_017.txt, 2_018.txt, 2_019.txt, 2_020.txt, 2_021.txt, 2_022.txt, 2_023.txt, 2_024.txt, 2_025.txt, 2_026.txt, 2_027.txt, 2_028.txt, 2_029.txt, 2_030.txt, 2_031.txt, 2_032.txt, 2_033.txt, 2_034.txt, 2_035.txt, 2_036.txt
Case Name Status Exec Time Memory
0_000.txt AC 22 ms 11216 KB
0_001.txt AC 22 ms 11216 KB
0_002.txt AC 22 ms 11216 KB
0_003.txt AC 21 ms 11216 KB
0_004.txt AC 22 ms 9168 KB
1_005.txt AC 22 ms 9168 KB
1_006.txt WA 22 ms 9168 KB
1_007.txt AC 22 ms 9168 KB
1_008.txt WA 22 ms 9168 KB
1_009.txt WA 22 ms 11216 KB
1_010.txt WA 23 ms 11216 KB
1_011.txt WA 23 ms 11216 KB
1_012.txt WA 23 ms 11216 KB
1_013.txt WA 23 ms 11216 KB
1_014.txt WA 22 ms 11216 KB
1_015.txt WA 23 ms 11216 KB
1_016.txt WA 23 ms 11216 KB
1_017.txt WA 23 ms 13264 KB
2_018.txt TLE 2108 ms 13908 KB
2_019.txt TLE 2108 ms 15964 KB
2_020.txt TLE 2107 ms 15964 KB
2_021.txt TLE 2108 ms 15960 KB
2_022.txt TLE 2108 ms 15960 KB
2_023.txt TLE 2108 ms 18012 KB
2_024.txt TLE 2107 ms 15964 KB
2_025.txt TLE 2108 ms 15964 KB
2_026.txt TLE 2108 ms 13916 KB
2_027.txt TLE 2108 ms 15964 KB
2_028.txt TLE 2108 ms 18008 KB
2_029.txt TLE 2108 ms 13916 KB
2_030.txt TLE 2108 ms 13916 KB
2_031.txt TLE 2108 ms 15964 KB
2_032.txt TLE 2108 ms 15964 KB
2_033.txt TLE 2108 ms 15960 KB
2_034.txt TLE 2108 ms 15964 KB
2_035.txt TLE 2108 ms 13916 KB
2_036.txt TLE 2108 ms 13916 KB