結果

問題 No.1251 絶対に間違ってはいけない最小化問題
ユーザー kotatsugamekotatsugame
提出日時 2020-10-09 21:26:33
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 150 ms / 2,000 ms
コード長 525 bytes
コンパイル時間 795 ms
コンパイル使用メモリ 77,208 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-20 08:23:28
合計ジャッジ時間 14,707 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 42
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:8:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
    8 | main()
      | ^~~~
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/iostream:39,
                 from main.cpp:1:
In member function 'std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char; _Traits = std::char_traits<char>]',
    inlined from 'int main()' at main.cpp:32:8:
/home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/ostream:167:25: warning: 'x' may be used uninitialized [-Wmaybe-uninitialized]
  167 |       { return _M_insert(__n); }
      |                ~~~~~~~~~^~~~~
main.cpp: In function 'int main()':
main.cpp:20:20: note: 'x' was declared here
   20 |         long sum=0,x;
      |                    ^

ソースコード

diff #

#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;
}
0