件名の説明:
マキシムと配列
テストあたりの時間制限
2秒
テストごとのメモリ制限
256メガバイト
入力
標準入力
出力
標準出力
最近、マキシムは、配列の発見したn個の誰もが必要とする整数を、。彼はすぐにそれを変えるというアイデアを思い付く:彼は正の整数考案のxを追加または任意の配列要素からそれを引くことにしました。正式には、単一の操作マキシムは整数で選択適用することによって、I(1≤ iは ≤ nで)と置き換えIアレイの番目の要素を* **私はどちらか** I *と* + I **はX *又は*と - バツ*。操作は同じ位置に複数回適用される場合がありますのでご了承ください。
マキシムは、このように、彼はすべての配列要素(すなわちの製品は、最小値が何であるかを知りたい、好奇心minimalisあるマキシムは以下で適用されないならば、)に達することができるのkそれに操作を。その中で彼を助けてください。
入力
入力の最初の行は三つの整数が含まれているN、 K及びX(≤1 N、 K ≤200 000、≤1 X ≤109) -マキシムによって考案アレイ、操作の最大数と数の要素数、それぞれ。
2行目は含まれて、n個の整数1、 2、...、* ** N×(マキシムによって見出さ配列の要素を- )。
出力
プリントnは整数B 1、 B 2、...、B * **のみラインにおけるn * -よりも多くを適用しない後の配列要素k個のアレイに動作。具体的には、すべての1≤ため忠実べき iが ≤ Nが、すべての配列要素の積が可能最小であるべきです。
複数回答がある場合は、それらのいずれかを印刷します。
例
入力
コピー
5 3 1
5 4 3 5 2
出力
コピー
5 4 3 5 -1
入力
コピー
5 3 1
5 4 3 5 5
出力
コピー
5 4 0 5 5
入力
コピー
5 3 1
5 4 4 5 5
出力
コピー
5 1 4 5 5
入力
コピー
3 2 7
5 4 2
出力
コピー
5 11 -5
アイデア:
対象は、直列に与えるプラスまたはマイナスXの操作の数をK、列の最小数の積となるように頼まれます。正および負の数ので、議論を分割します。
負の数は現在でもある場合、正の数の積は、最小の変化の絶対値をさせ、逆相アプローチにそれを作るべきである。なぜなら、最も近いゼロからの距離。あなたが否定的である変更したい場合は、それが正であるデクリメントxを変更したい場合は、製品番号は、製品をより小さくすることができることができますされていない場合、その後、Xを追加します。
現在の負の数が奇数の場合、製品が負の数、我々は製品をより小さくしたい製品の絶対値です。最小絶対値となり、正のxの数を増加させることで、負デクリメントxは、である。なぜならとき数に近い山の大きさ、大きいこのパイルの製品の数。
すぐにプライオリティキュー構造を維持するために、最小の要素の絶対値を得るために、数は変更が否定されるたびに更新されることに注意してください、私たちは、構造体の定義の中に<演算子をオーバーライドする必要があります。
最初は私は2つの砂の彫刻キュー、店の整数、負の預金、だけでなく、各選挙の比較的小さな絶対値、使用\(IF-ELSEの\)に多く、最終的に間違って書き込みを。背後-_- ||コードを参照してください。
コード:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#define max_n 200005
using namespace std;
int n,k,x;
long long a[max_n];
struct node
{
int id;
long long val;
bool operator<(const node& a) const
{
return abs(val-0)>abs(a.val-0);
}
};
int cnt = 0;
priority_queue<node> que;
int main()
{
cin >> n >> k >> x;
for(int i = 0;i<n;i++)
{
cin >> a[i];
node nw;
nw.val = a[i];
nw.id = i;
if(a[i]<0)
{
cnt++;
}
que.push(nw);
}
while(k)
{
node nw = que.top();
int id = nw.id;
if(cnt%2==0)
{
if(a[id]<0)
{
a[id] += x;
if(a[id]>=0)
{
cnt--;
}
}
else
{
a[id] -= x;
if(a[id]<0)
{
cnt++;
}
}
}
else
{
if(a[id]<0)
{
a[id] -= x;
}
else
{
a[id] += x;
}
}
nw.val = a[id];
que.pop();
que.push(nw);
k--;
}
for(int i = 0;i<n;i++)
{
cout << a[i] << " ";
}
cout << endl;
}
どこで間違ったコードは不明です。
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#define max_n 200005
using namespace std;
long long n,k,x;
long long a[max_n];
struct node
{
int id;
long long val;
bool operator<(const node& a) const
{
return abs(val-0)>abs(a.val-0);
}
};
priority_queue<node> fque;
priority_queue<node> zque;
int main()
{
cin >> n >> k >> x;
for(int i = 0;i<n;i++)
{
cin >> a[i];
node nw;
nw.val = a[i];
nw.id = i;
if(a[i]<0)
{
fque.push(nw);
}
else
{
zque.push(nw);
}
}
//cout << "input " << endl;
while(k)
{
/*for(int i = 0;i<n;i++)
{
cout << a[i] << " ";
}
cout << endl;*/
if(fque.size()%2==0)
{
if(fque.size()==0)
{
int id = zque.top().id;
a[id] -= x;
node nw;
nw.id = id;
nw.val = a[id];
if(a[id]<0)
{
zque.pop();
fque.push(nw);
}
else
{
zque.pop();
zque.push(nw);
}
}
else if(zque.size()==0)
{
int id = fque.top().id;
a[id] += x;
node nw;
nw.id = id;
nw.val = a[id];
if(a[id]>=0)
{
fque.pop();
zque.push(nw);
}
else
{
fque.pop();
fque.push(nw);
}
}
else
{
int gapz = abs(zque.top().val-0);
int idz = zque.top().id;
int gapf = abs(fque.top().val-0);
int idf = fque.top().id;
if(gapz<gapf)
{
a[idz] -= x;
node nw;
nw.id = idz;
nw.val = a[idz];
if(a[idz]<0)
{
zque.pop();
fque.push(nw);
}
else
{
zque.pop();
zque.push(nw);
}
}
else
{
a[idf] += x;
node nw;
nw.id = idf;
nw.val = a[idf];
if(a[idf]>=0)
{
fque.pop();
zque.push(nw);
}
else
{
fque.pop();
fque.push(nw);
}
}
}
}
else
{
if(zque.size()==0)
{
int id = fque.top().id;
a[id] -= x;
node nw;
nw.id = id;
nw.val = a[id];
fque.pop();
fque.push(nw);
}
else
{
int gapz = abs(zque.top().val-0);
int idz = zque.top().id;
int gapf = abs(fque.top().val-0);
int idf = fque.top().id;
if(gapz<gapf)
{
a[idz] += x;
zque.pop();
node nw;
nw.id = idz;
nw.val = a[idz];
zque.push(nw);
}
else
{
a[idf] -= x;
fque.pop();
node nw;
nw.id = idf;
nw.val = a[idf];
fque.push(nw);
}
}
}
k--;
}
for(int i = 0;i<n;i++)
{
cout << a[i] << " ";
}
cout << endl;
return 0;
}
参考記事:
負担、D.マキシムと配列の木材フロー獣、https://www.cnblogs.com/thunder-110/p/9340279.html