結果
| 問題 | No.1251 絶対に間違ってはいけない最小化問題 |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-10-09 21:26:33 |
| 言語 | C++14 (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 95 ms / 2,000 ms |
| コード長 | 525 bytes |
| 記録 | |
| コンパイル時間 | 479 ms |
| コンパイル使用メモリ | 97,008 KB |
| 実行使用メモリ | 127,952 KB |
| 最終ジャッジ日時 | 2026-04-02 22:14:20 |
| 合計ジャッジ時間 | 8,835 ms |
|
ジャッジサーバーID (参考情報) |
judge1_1 / judge2_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 42 |
コンパイルメッセージ
main.cpp:8:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
8 | main()
| ^~~~
main.cpp: In function 'int main()':
main.cpp:31:48: warning: 'x' may be used uninitialized [-Wmaybe-uninitialized]
31 | for(int i=0;i<N;i++)ans+=(long)B[i]*abs(x-A[i]);
| ~~~^~~~~~~~
main.cpp:20:20: note: 'x' was declared here
20 | long sum=0,x;
| ^
ソースコード
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int N;
long all;
int A[2<<17],B[2<<17];
main()
{
cin>>N;
vector<pair<int,int> >X(N);
for(int i=0;i<N;i++)cin>>A[i];
for(int i=0;i<N;i++)cin>>B[i];
for(int i=0;i<N;i++)
{
X[i]=make_pair(A[i],B[i]);
all+=B[i];
}
sort(X.begin(),X.end());
long sum=0,x;
for(pair<int,int>p:X)
{
sum+=p.second;
if(sum*2>=all)
{
x=p.first;
break;
}
}
long ans=0;
for(int i=0;i<N;i++)ans+=(long)B[i]*abs(x-A[i]);
cout<<x<<" "<<ans<<endl;
}