LoginSignup
1
4

More than 5 years have passed since last update.

python3 競プロ用memo

Last updated at Posted at 2017-04-08

Description

競技プログラミングはじめてみました。基本的な事柄についてのメモです。

IO

input

use sys

import sys
inputs = sys.stdin.readline().split()

use input()

#for multiple inputs
n, m = list(map(int, input().split()))

#when list is needed
x = list(map(int, input().split()))

#define as a function
def read(): return list(map(int, input().split()))

Ideas

複数区間の距離の和

複数の点の座標が与えられたとき、任意の2点間を結ぶ線分の長さの総和

#given list of dots, **SORTED**
x = list(map(int, input().split()))
nx = len(x)
sumx = sum( x[i]*(2*i - nx + 1) for i in range(nx) )

2*i -nx + 1 の部分は、総和の式の中でその点の座標の加算される回数(その点の左にある点の個数と等しい)と、その点の座標が減算される回数(その点の右にある点の個数と等しい)との、の和を求めています。

Update Log

  1. 170409 initial upload
  2. 170410 コメントでいただいた内容を反映
1
4
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4