?? 1234.cpp
字號:
#include <algorithm>
#include <memory>
#include <iostream>
using namespace std;
int table[5005][1005];
const int INFI = 9999999;
int main()
{
int t, k , n, chopsticks[5001];
cin >> t;
while(t--)
{
cin >> k >> n;
memset(table, INFI, sizeof(table));
for(int i = 1; i <= n; i++)
cin >> chopsticks[i];
for(int i = 1; i <= n; i++)
table[i][0] = 0;
for(int i = 1; i <= k + 8; i++)
for(int j = n - 2; j >= 1; j--) // 從倒數第j個筷子開始取i * 3只筷子供i個人使用
if(n - j + 1 < i * 3) // 取不了
continue;
else
table[j][i] = min(table[j + 1][i],
(chopsticks[j+1]-chopsticks[j])*(chopsticks[j+1]-chopsticks[j]) + table[j + 2][i - 1]);
cout << table[1][k + 8] << endl;
}
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -