結果
| 問題 |
No.463 魔法使いのすごろく🎲
|
| コンテスト | |
| ユーザー |
shimomire
|
| 提出日時 | 2016-12-15 00:54:53 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 5 ms / 2,000 ms |
| コード長 | 8,129 bytes |
| コンパイル時間 | 1,297 ms |
| コンパイル使用メモリ | 131,740 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-11-30 08:10:54 |
| 合計ジャッジ時間 | 2,507 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 36 |
ソースコード
#include <cassert>// c
#include <iostream>// io
#include <iomanip>
#include <fstream>
#include <sstream>
#include <vector>// container
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <stack>
#include <algorithm>// other
#include <complex>
#include <numeric>
#include <functional>
#include <random>
#include <regex>
using namespace std;
typedef long long ll;typedef unsigned long long ull;typedef long double ld;
#define ALL(c) c.begin(),c.end()
#define IN(l,v,r) (l<=v && v < r)
template<class T> void UNIQUE(T& v){v.erase(unique(ALL(v)),v.end());}
//debug
#define DUMP(x) cerr << #x <<" = " << (x)
#define LINE() cerr<< " (L" << __LINE__ << ")"
struct range{
struct Iter{
int v,step;
Iter& operator++(){v+=step;return *this;}
bool operator!=(Iter& itr){return v<itr.v;}
int& operator*(){return v;}
};
Iter i, n;
range(int i, int n,int step):i({i,step}), n({n,step}){}
range(int i, int n):range(i,n,1){}
range(int n):range(0,n){}
Iter& begin(){return i;}
Iter& end(){return n;}
};
struct rrange{
struct Iter{
int v,step;
Iter& operator++(){v-=step;return *this;}
bool operator!=(Iter& itr){return v>itr.v;}
int& operator*(){return v;}
};
Iter i, n;
rrange(int i, int n,int step):i({i-1,step}), n({n-1,step}){}
rrange(int i, int n):rrange(i,n,1){}
rrange(int n) :rrange(0,n){}
Iter& begin(){return n;}
Iter& end(){return i;}
};
//input
template<typename T1,typename T2> istream& operator >> (istream& is,pair<T1,T2>& p){return is>>p.first>>p.second;}
template<typename T1> istream& operator >> (istream& is,tuple<T1>& t){return is >> get<0>(t);}
template<typename T1,typename T2> istream& operator >> (istream& is,tuple<T1,T2>& t){return is >> get<0>(t) >> get<1>(t);}
template<typename T1,typename T2,typename T3> istream& operator >> (istream& is,tuple<T1,T2,T3>& t){return is >>get<0>(t)>>get<1>(t)>>get<2>(t);}
template<typename T1,typename T2,typename T3,typename T4> istream& operator >> (istream& is,tuple<T1,T2,T3,T4>& t){return is >> get<0>(t)>>get<1>(t)>>get<2>(t)>>get<3>(t);}
template<typename T> istream& operator >> (istream& is,vector<T>& as){for(int i:range(as.size()))is >>as[i];return is;}
//output
template<typename T> ostream& operator << (ostream& os, const set<T>& ss){for(auto a:ss){if(a!=ss.begin())os<<" "; os<<a;}return os;}
template<typename T1,typename T2> ostream& operator << (ostream& os, const pair<T1,T2>& p){return os<<p.first<<" "<<p.second;}
template<typename K,typename V> ostream& operator << (ostream& os, const map<K,V>& m){bool isF=true;for(auto& p:m){if(!isF)os<<endl;os<<p;isF=false;}return os;}
template<typename T1> ostream& operator << (ostream& os, const tuple<T1>& t){return os << get<0>(t);}
template<typename T1,typename T2> ostream& operator << (ostream& os, const tuple<T1,T2>& t){return os << get<0>(t)<<" "<<get<1>(t);}
template<typename T1,typename T2,typename T3> ostream& operator << (ostream& os, const tuple<T1,T2,T3>& t){return os << get<0>(t)<<" "<<get<1>(t)<<" "<<get<2>(t);}
template<typename T1,typename T2,typename T3,typename T4> ostream& operator << (ostream& os, const tuple<T1,T2,T3,T4>& t){return os << get<0>(t)<<" "<<get<1>(t)<<" "<<get<2>(t)<<" "<<get<3>(t);}
template<typename T> ostream& operator << (ostream& os, const vector<T>& as){for(int i:range(as.size())){if(i!=0)os<<" "; os<<as[i];}return os;}
template<typename T> ostream& operator << (ostream& os, const vector<vector<T>>& as){for(int i:range(as.size())){if(i!=0)os<<endl; os<<as[i];}return os;}
// values
template<typename T> inline T INF(){assert(false);};
template<> inline int INF<int>(){return 1<<28;};
template<> inline ll INF<ll>(){return 1LL<<60;};
template<> inline double INF<double>(){return 1e16;};
template<> inline long double INF<long double>(){return 1e16;};
template<class T> inline T EPS(){assert(false);};
template<> inline int EPS<int>(){return 1;};
template<> inline ll EPS<ll>(){return 1LL;};
template<> inline double EPS<double>(){return 1e-12;};
template<> inline long double EPS<long double>(){return 1e-12;};
// min{2^r | n < 2^r}
template<typename T> inline T upper_pow2(T n){ T res=1;while(res<n)res<<=1;return res;}
// max{d | 2^d <= n}
template<typename T> inline T msb(T n){ int d=62;while((1LL<<d)>n)d--;return d;}
template<typename T,typename U> T pmod(T v,U M){return (v%M+M)%M;}
namespace Matrix{
class Inconsistent{};//none
class Ambiguous{};//many
class NoSolution{};
template <typename T> class Mat :public vector<vector<T>>{
public:
const int H,W;
Mat(int H,int W):vector<vector<T>>(H,vector<T>(W)),H(H),W(W){
for(int y:range(H))for(int x:range(W)) (*this)[y][x] = ve;
}
const T ve = 0.0;
inline T vc(T a,T b){return a+b;}
inline T vinv(T a){return -a;}
const T vme = 1.0;
inline T vmc(T a,T b){return a*b;}
inline T vminv(T a){return 1/a;}
inline T sig(T a,T b = 0){return a-b>EPS<T>()?+1:a-b<-EPS<T>()?-1:0;}
const inline Mat e(){
return vector<vector<T>>(H,vector<T>(W,ve));
}
inline Mat c(Mat a,Mat b){return a+b;}
inline Mat<T> inv(Mat<T> a){return -a;}
const inline Mat me(){
static Mat A(H,W);
for(int y:range(H))for(int x:range(W))A[y][x]= ve;
for(int i:range(min(H,W)))A[i][i]= vme;
return A;
}
inline Mat<T> mc(Mat<T> a,Mat<T> b){return a*b;}
//O(n^2)
Mat operator+(const Mat& r){
const Mat& l=*this;
Mat res(l.H,r.W);
for(int y:range(l.H))for(int x:range(r.W)) res[y][x]= c(l[y][x],r[y][x]);
return res;
}
//O(n^2)
Mat operator-(){
Mat res=*this;
for(int y:range(res.H))for(int x:range(res.W)) res[y][x] = inv(res[y][x]);
return res;
}
Mat operator-(const Mat& r){ return (*this) + (-r); }
//O(n^3)
Mat operator*(const Mat& r){
const Mat& l=*this;
Mat res(l.H,r.W);
for(int y:range(l.H))for(int x:range(r.W))for(int k:range(r.H))
res[y][x] = c(res[y][x], mc(l[y][k],r[k][x]));
return res;
}
//O(n^2)
vector<T> operator*(const vector<T>& r){
const Mat& l=*this;
vector<T> res(r.size());
for(int y:range(l.H))for(int x:range(l.W))
res[y] = c(res[y],mc(l[y][x],r[x]));
return res;
}
// O(n^3*log(d))
// Mat x must be n*n size
//:TODO abstraction
Mat pow(ll d){
Mat x=*this,res=e();
while(d!=0){
if(d%2)res=res*x;
x=x*x;
d/=2;
}
return res;
}
//O(n^3)
//Ax = b
void gauss(vector<T>& b){
Mat A=*this;
int py =0,px = 0;
while(py < H && px < W){
for(int y=py+1;y<H;y++){ // pivot
if(sig(abs(A[y][px]),abs(A[py][px])) > 0){
swap(A[y],A[py]);swap(b[y],b[py]);
}
}
if(sig(A[py][px]) != 0){
T d = vminv(A[py][px]);
for(int x=0;x<W;x++) A[py][x] = vmc(A[py][x],d);
b[py] = vmc(b[py],d);
for(int y=py+1;y<H;y++){
T k = A[y][px];
for(int x=0;x<W;x++) A[y][x] = vc(A[y][x],vinv(vmc(k,A[py][x])));
b[y] = vc(b[y],vinv(vmc(k,b[py])));
}
py++;
}
px++;
}
for(int y=py;y<H;y++)if(sig(b[y]) != 0)throw NoSolution();
if(py < W || px < W)throw Ambiguous();
for(int x=W-1;x>=0;x--)for(int y=0;y<x;y++){
b[y] = vc(b[y],vinv(vmc(b[x],A[y][x])));
}
}
};
}
using namespace Matrix;
class Main{
public:
void run(){
int n,m;cin >> n >> m;
vector<ld> cs(n);
for(int i:range(1,n-1)){
ld v;cin >> v;cs[i] = v;
}
Mat<ld> mat(n,n);
vector<ld> bs(n);
for(int s:range(n-m-1)) mat[s][s] = 1;
for(int s:range(n-m-1,n)){
bs[s] = m*cs[s];
mat[s][s] = m;
if(s < n-1){
for(int e:range(1,m+1)){
if(s+e < n){
mat[s][(s+e)] -= 1.0;
}else{
mat[s][n-1 - (s+e - (n-1))] -= 1.0;
}
}
}
}
mat.gauss(bs);
vector<vector<ld>> dp(n,vector<ld>(2,INF<ld>()));
for(int i:rrange(n-m-1,n)){
dp[i][0] = bs[i] - cs[i]; dp[i][1] = 0;
}
for(int i:rrange(0,n-m-1)){
for(int x:range(2)){
ld sum = 0;
for(int d:range(1,m+1)) sum += (cs[i+d]+dp[i+d][x])/m;
dp[i][x] = min(dp[i][x],sum);
}
for(int d:range(1,m+1)) dp[i][1] = min(dp[i][1],cs[i+d]+dp[i+d][0]);
}
// cerr << dp << endl;
cout << dp[0][1] << endl;
}
};
int main(){
cout <<fixed<<setprecision(20);
cin.tie(0);
ios::sync_with_stdio(false);
Main().run();
return 0;
}
shimomire