結果

問題 No.648  お や す み 
ユーザー Yuki_UtaaiYuki_Utaai
提出日時 2018-05-27 23:50:23
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 2,985 bytes
コンパイル時間 507 ms
コンパイル使用メモリ 11,004 KB
実行使用メモリ 9,472 KB
最終ジャッジ日時 2023-09-12 19:56:05
合計ジャッジ時間 90,738 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
9,312 KB
testcase_01 WA -
testcase_02 AC 21 ms
9,268 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 21 ms
9,352 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 22 ms
9,144 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
testcase_61 WA -
testcase_62 WA -
testcase_63 WA -
testcase_64 WA -
testcase_65 WA -
testcase_66 WA -
testcase_67 WA -
testcase_68 WA -
testcase_69 WA -
testcase_70 WA -
testcase_71 WA -
testcase_72 WA -
testcase_73 WA -
testcase_74 WA -
testcase_75 WA -
testcase_76 WA -
testcase_77 WA -
testcase_78 WA -
testcase_79 WA -
testcase_80 WA -
testcase_81 WA -
testcase_82 WA -
testcase_83 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import random

#--------粒 子 群 最 適 化------------------------------

#評価関数: 
def criterion(x, y):
    z=-abs(k-(x*(x+1))/2)
    if x<0:
      z=-1000000

    return z

#粒子の位置の更新を行う関数
def update_position(x, y, vx, vy):
    new_x = x + vx
    new_y = y + vy
    return new_x, new_y

#粒子の速度の更新を行う関数
def update_velocity(x, y, vx, vy, p, g, w=0.5, ro_max=1,v_max=10):
    #パラメーターroはランダムに与える
    ro1 = random.uniform(0, ro_max)
    ro2 = random.uniform(0, ro_max)
    #粒子速度の更新を行う
    new_vx = int(w * vx + ro1 * (p[0] - x) + ro2 * (g[0] - x))
    if abs(new_vx)>v_max:
        new_vx=int(v_max*new_vx/abs(new_vx))

    new_vy = w * vy + ro1 * (p[1] - y) + ro2 * (g[1] - y)
    return new_vx, new_vy

def main():
    N = 600  #粒子の数
    x_min, x_max = 0, k
    y_min, y_max = 0, 0
    #粒子位置, 速度, パーソナルベスト, グローバルベストの初期化を行う
    ps = [[random.randint(x_min, x_max), random.uniform(y_min, y_max)] for i in range(N)]
    vs = [[0.0, 0.0] for i in range(N)]

    personal_best_positions = list(ps)
    personal_best_scores = [criterion(p[0], p[1]) for p in ps]
    best_particle = personal_best_scores.index(max(personal_best_scores))
    global_best_position = personal_best_positions[best_particle]



    T = 300  #世代数(ループの回数)
    for t in range(T):
        for n in range(N):
            x, y = ps[n][0], ps[n][1]
            vx, vy = vs[n][0], vs[n][1]
            p = personal_best_positions[n]

            #粒子の位置の更新を行う
            new_x, new_y = update_position(x, y, vx, vy)
            ps[n] = [new_x, new_y]

            #粒子の速度の更新を行う
            new_vx, new_vy = update_velocity(new_x, new_y, vx, vy, p, global_best_position)
            vs[n] = [new_vx, new_vy]

            #評価値を求め, パーソナルベストの更新を行う
            score = criterion(new_x, new_y)
            if score > personal_best_scores[n]:
                personal_best_scores[n] = score
                personal_best_positions[n] = [new_x, new_y]

        #グローバルベストの更新を行う
        best_particle = personal_best_scores.index(max(personal_best_scores))
        global_best_position = personal_best_positions[best_particle]

        if k==(global_best_position[0]*(global_best_position[0]+1))//2:
          print("YES")
          print(global_best_position[0])
          exit()
 


    #最適解

    print(max(personal_best_scores), global_best_position)
    return max(personal_best_scores), global_best_position[0], global_best_position[1]
#--------------------------------------------------------------

k=int(input())
best=100000000000000
xx=0
yy=0
for i in range(3):
    kouho_best, kouho_x, kouho_y=main()
    if best>kouho_best:
        best=kouho_best
        xx=kouho_x
        yy=kouho_y
 
print("NO")
0