Codeforces Round #374 (Div. 2) D. Maxim and Array
2016-11-04 19:19
330 查看
D. Maxim and Array
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Recently Maxim has found an array of n integers, needed by no one. He immediately come up with idea of changing it: he invented positive
integer x and decided to add or subtract it from arbitrary array elements. Formally, by applying single operation Maxim chooses integer i (1 ≤ i ≤ n)
and replaces the i-th element of array ai either
with ai + x or
with ai - x.
Please note that the operation may be applied more than once to the same position.
Maxim is a curious minimalis, thus he wants to know what is the minimum value that the product of all array elements (i.e.
)
can reach, if Maxim would apply no more than k operations to it. Please help him in that.
Input
The first line of the input contains three integers n, k and x (1 ≤ n, k ≤ 200 000, 1 ≤ x ≤ 109) —
the number of elements in the array, the maximum number of operations and the number invented by Maxim, respectively.
The second line contains n integers a1, a2, ..., an (
) —
the elements of the array found by Maxim.
Output
Print n integers b1, b2, ..., bn in
the only line — the array elements after applying no more than k operations to the array. In particular,
should
stay true for every 1 ≤ i ≤ n, but the product of all array elements should be minimum
possible.
If there are multiple answers, print any of them.
Examples
input
output
input
output
input
output
input
output
题目大意:
给出 n 个数,限制 m 次操作,每次操作可以将 数组中的某个数加上或者减去 k
然后让 得到的 n 个数乘积最小。
思路:
当 n 个数 都为正数的时候,他们绝对值越接近,那么他们的乘积越大。
那么如果他们之间有负数的话,并且负数的个数为奇数个,那么我们就可以让他们的绝对值越接近。
所以用多重map 对他们进行操作。
AC代码:
#include<bits/stdc++.h>
using namespace std;
multimap<long long int,long long int> mm;
long long int a[200005];
int main()
{
long long int n,m,k;
long long int num=0;
scanf("%I64d%I64d%I64d",&n,&m,&k);
for(int i=0;i<n;i++)
{
scanf("%I64d",&a[i]);
if(a[i]<0)
num++;
mm.insert(pair<long long int ,long long int>(fabs(a[i]),i));
}
int c;
for(int i=0;i<m;i++)
{
c=mm.begin()->first;
int j;
j=mm.begin()->second;
if(num&1)
{
if(a[j]>=0)
{
a[j]+=k;
}
else
{
a[j]-=k;
}
}
else
{
if(a[j]>=0)
{
a[j]=a[j]-k;
if(a[j]<0)
num++;
}
else
{
a[j]+=k;
if(a[j]>=0)
num--;
}
}
mm.erase(mm.begin());
mm.insert(pair<long long int,long long int>(fabs(a[j]),j));
}
for(int i=0;i<n;i++)
{
if(i==n-1)
cout<<a[i]<<endl;
else
cout<<a[i]<<" ";
}
}
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Recently Maxim has found an array of n integers, needed by no one. He immediately come up with idea of changing it: he invented positive
integer x and decided to add or subtract it from arbitrary array elements. Formally, by applying single operation Maxim chooses integer i (1 ≤ i ≤ n)
and replaces the i-th element of array ai either
with ai + x or
with ai - x.
Please note that the operation may be applied more than once to the same position.
Maxim is a curious minimalis, thus he wants to know what is the minimum value that the product of all array elements (i.e.
)
can reach, if Maxim would apply no more than k operations to it. Please help him in that.
Input
The first line of the input contains three integers n, k and x (1 ≤ n, k ≤ 200 000, 1 ≤ x ≤ 109) —
the number of elements in the array, the maximum number of operations and the number invented by Maxim, respectively.
The second line contains n integers a1, a2, ..., an (
) —
the elements of the array found by Maxim.
Output
Print n integers b1, b2, ..., bn in
the only line — the array elements after applying no more than k operations to the array. In particular,
should
stay true for every 1 ≤ i ≤ n, but the product of all array elements should be minimum
possible.
If there are multiple answers, print any of them.
Examples
input
5 3 1 5 4 3 5 2
output
5 4 3 5 -1
input
5 3 1 5 4 3 5 5
output
5 4 0 5 5
input
5 3 1 5 4 4 5 5
output
5 1 4 5 5
input
3 2 7 5 4 2
output
5 11 -5
题目大意:
给出 n 个数,限制 m 次操作,每次操作可以将 数组中的某个数加上或者减去 k
然后让 得到的 n 个数乘积最小。
思路:
当 n 个数 都为正数的时候,他们绝对值越接近,那么他们的乘积越大。
那么如果他们之间有负数的话,并且负数的个数为奇数个,那么我们就可以让他们的绝对值越接近。
所以用多重map 对他们进行操作。
AC代码:
#include<bits/stdc++.h>
using namespace std;
multimap<long long int,long long int> mm;
long long int a[200005];
int main()
{
long long int n,m,k;
long long int num=0;
scanf("%I64d%I64d%I64d",&n,&m,&k);
for(int i=0;i<n;i++)
{
scanf("%I64d",&a[i]);
if(a[i]<0)
num++;
mm.insert(pair<long long int ,long long int>(fabs(a[i]),i));
}
int c;
for(int i=0;i<m;i++)
{
c=mm.begin()->first;
int j;
j=mm.begin()->second;
if(num&1)
{
if(a[j]>=0)
{
a[j]+=k;
}
else
{
a[j]-=k;
}
}
else
{
if(a[j]>=0)
{
a[j]=a[j]-k;
if(a[j]<0)
num++;
}
else
{
a[j]+=k;
if(a[j]>=0)
num--;
}
}
mm.erase(mm.begin());
mm.insert(pair<long long int,long long int>(fabs(a[j]),j));
}
for(int i=0;i<n;i++)
{
if(i==n-1)
cout<<a[i]<<endl;
else
cout<<a[i]<<" ";
}
}
相关文章推荐
- Codeforces Round #374 (Div. 2)D. Maxim and Array(贪心)
- Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心
- Codeforces Round #374 (Div. 2) D. Maxim and Array
- Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心
- Codeforces Round #374 (Div. 2) D. Maxim and Array 线段树+贪心
- Codeforces Round #374 (Div. 2) D. Maxim and Array —— 贪心
- 【Codeforces Round 374 (Div 2)D】【贪心】 Maxim and Array n个数做K次±X使得乘积尽可能小
- Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心
- Codeforces Round 374 (Div 2)D Maxim and Array 【贪心】
- Codeforces Round #374 (Div. 2) D. Maxim and Array
- Codeforces Round #374 (Div. 2) D - Maxim and Array
- 【24.17%】【codeforces 721D】Maxim and Array
- Codeforces #253 (Div. 1)C. Artem and Array ( 贪心
- CodeForces 721D. Maxim and Array(贪心,思维题)
- Codeforces Round #312 (Div. 2) B. Amr and The Large Array
- Codeforces Round #136 (Div. 1) B. Little Elephant and Array
- Codeforces #179(Div 2)C Greg and Array
- Codeforces Round #331 (Div. 2) B. Wilbur and Array (贪心)
- Codeforces Round #179 (Div. 1)-A. Greg and Array
- 水题(Maxim and Array,cf 721D)