結果
問題 | No.1251 絶対に間違ってはいけない最小化問題 |
ユーザー | beet |
提出日時 | 2020-10-09 21:25:19 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 73 ms / 2,000 ms |
コード長 | 1,399 bytes |
コンパイル時間 | 2,094 ms |
コンパイル使用メモリ | 207,112 KB |
最終ジャッジ日時 | 2025-01-15 03:52:37 |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 42 |
ソースコード
#include <bits/stdc++.h> using namespace std; using Int = long long; const char newl = '\n'; template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;} template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;} template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);} template<typename T=Int> vector<T> read(size_t n){ vector<T> ts(n); for(size_t i=0;i<n;i++) cin>>ts[i]; return ts; } template<typename ...Ts> decltype(auto) zip(vector<Ts>... args){ vector<decltype(make_tuple(args[0]...))> res; Int n=min({args.size()...}); res.reserve(n); for(Int i=0;i<n;i++) res.emplace_back(args[i]...); return res; } //INSERT ABOVE HERE signed main(){ cin.tie(0); ios::sync_with_stdio(0); Int n; cin>>n; auto as=read(n); auto bs=read(n); auto zs=zip(as,bs); sort(zs.begin(),zs.end()); for(Int i=0;i<n;i++) tie(as[i],bs[i])=zs[i]; Int sum=0; for(Int b:bs) sum+=b; Int res=0,t=-1; for(Int i=0;i<n;i++){ if((res+bs[i])*2<=sum) res+=bs[i]; else{ t=i; break; } } auto calc=[&](Int i){ Int x=as[i]; Int res=0; for(Int i=0;i<n;i++) res+=bs[i]*abs(x-as[i]); return res; }; Int ans=t; for(Int i=0;i<10;i++){ if(t+i< n and calc(ans)>calc(t+i)) ans=t+i; if(t-i>=0 and calc(ans)>calc(t-i)) ans=t-i; } cout<<as[ans]<<' '<<calc(ans)<<newl; return 0; }