Problem A

贪心~:  尽量把绝对值大的变成正数, 如果操作数剩余,   那么如果剩余次数为奇数, 显然要舍去一个,  舍去绝对值最小的数;

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <cmath>
#include <vector>
#include <set>
#include <stack>
#include <algorithm>
//#include "myAlgorithm.h"

#define MAX 100005
#define INF (1e8 + 5)
#define eps 1e-30
#define Rep(s, e) for( int i = s; i <= e; i++)
using namespace std;
int n, k;
int  a[MAX];
bool cmp(int a, int b){
    return abs(a) < abs(b);
}
int main()
 {
    //freopen("in.txt", "w", stdout);
    while(cin>>n>>k){
        Rep(1, n)cin>>a[i];
        sort(a + 1, a + n + 1, cmp);
        //Rep(1, n)cout<<a[i]<<" ";
        //cout<<endl;
        int ans = 0;
        for(int i = n; i >  0; i--){
            if(a[i] < 0 && k > 0){
                a[i] *= -1;
                k--;
            }ans += a[i];
        }
        if(k%2){
            ans -= 2 * a[1];
        }
        cout<<ans<<endl;
    }
    return 0;
}
/*
3 2 -1 -1 1
3 1 -1 -1 1
*/




 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
题目描述 定义一个Point类,表示平面上的点,具有x和y两个坐标成员变量,以及设置和获取坐标的方法。还要实现计算两点之间距离的方法。 输入描述 无输入。 输出描述 无输出。 样例 无样例。 提示 要求定义Point类,具有如下成员: 成员变量: double x:表示点的横坐标。 double y:表示点的纵坐标。 成员函数: Point():构造函数,将x和y初始值为0.0。 Point(double x, double y):构造函数,将x和y初始值为参数值。 void setX(double x):设置点的横坐标。 double getX():获取点的横坐标。 void setY(double y):设置点的纵坐标。 double getY():获取点的纵坐标。 double distance(Point another):计算当前点与另外一个点之间的距离,返回距离值。 注意:在类的实现中,要包含头文件cmath,使用其中的sqrt函数求平方根。 C++ 代码 ```cpp #include <iostream> #include <cmath> using namespace std; class Point { private: double x, y; public: Point() : x(0.0), y(0.0) {} Point(double x, double y) : x(x), y(y) {} void setX(double x) { this->x = x; } double getX() { return x; } void setY(double y) { this->y = y; } double getY() { return y; } double distance(Point another) { double dx = x - another.x; double dy = y - another.y; return sqrt(dx * dx + dy * dy); } }; int main() { Point p1, p2(3.0, 4.0); p1.setX(1.0); p1.setY(2.0); cout << "p1: (" << p1.getX() << ", " << p1.getY() << ")" << endl; cout << "p2: (" << p2.getX() << ", " << p2.getY() << ")" << endl; cout << "distance between p1 and p2: " << p1.distance(p2) << endl; return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值