Codeforces Round #331 (Div. 2) 596ABC题解

46 篇文章 0 订阅
17 篇文章 0 订阅

题目链接: 点击打开链接


A: 给出n个点的坐标, 问你这n个点能不能确定一个矩形, 如果可以求出面积, 不可以则输出-1.

脑洞题, n为1输出-1, 其他情况则求面积,   若面积为0则不能确定, 注意数据初始化, 为此wa了一发.

AC代码:

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
const int MAXN = 10;
struct node
{
    /* data */
    int x, y;
}a[MAXN];
int n, max_x = -1005, max_y = -1005, min_x = 1005, min_y = 1005;
int main(int argc, char const *argv[])
{
    scanf("%d", &n);
    for(int i = 0; i < n; ++i) {
        scanf("%d%d", &a[i].x, &a[i].y);
        max_x = max(max_x, a[i].x);
        max_y = max(max_y, a[i].y);
        min_x = min(min_x, a[i].x);
        min_y = min(min_y, a[i].y);
    }
    int ans = (max_x - min_x) * (max_y - min_y);
    if(ans == 0) printf("-1\n");
    else printf("%d\n", ans);
    return 0;
}


B: 一个开始为0的序列, 选定一个数, 每次可以让后面的数加1或者减1, 问你最少多少步可以变成给出的序列.

脑洞题, 由于每次只能对后面的数操作, 所以要从左边遍历, 答案即为两相邻数字差绝对值之和, 注意使用long long.

AC代码:

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 5;
int n;
ll a[MAXN], ans;
int main(int argc, char const *argv[])
{
    scanf("%d", &n);
    for(int i = 0; i < n; ++i)
        scanf("%lld", &a[i]);
    if(a[0] < 0) ans = -a[0];
    else ans = a[0];
    for(int i = 1; i < n; ++i)
        if(a[i] > a[i - 1]) ans += a[i] - a[i - 1];
        else ans += a[i - 1] - a[i];
    printf("%lld\n", ans);
    return 0;
}


C: 对一个点(x, y), 定义w[i] = y - x, 给出n个点的坐标和n个w[i], 问是否可以将w[i]与坐标匹配.

STL的使用,  读入w[i]时若不存在则不可匹配, 而后根据w[i]与坐标的关系判断是否可以匹配, w[i]大则x' >= x && y' >= y.

AC代码:

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "utility"
#include "map"
#include "set"
#include "vector"
using namespace std;
const int MAXN = 1e5 + 5;
int n;
map<int, set<pair<int, int> > > mp;
vector<int> row(MAXN, -1);
vector<int> col(MAXN, -1);
int main(int argc, char const *argv[])
{
    scanf("%d", &n);
    vector<pair<int, int> > ans(n);
    for(int i = 0; i < n; ++i) {
        int x, y;
        scanf("%d%d", &x, &y);
        mp[y - x].insert(make_pair(x, y));
    }
    for(int i = 0; i < n; ++i) {
        int diff;
        scanf("%d", &diff);
        if(mp[diff].empty()) {
            printf("NO\n");
            return 0;
        }
        ans[i] = *(mp[diff].begin());
        mp[diff].erase(mp[diff].begin());
    }
    for(int i = 0; i < n; ++i) {
        int x = ans[i].first, y = ans[i].second;
        if(row[x] < y - 1 || col[y] < x - 1) {
            printf("NO\n");
            return 0;
        }
        row[x]++, col[y]++;
    }
    printf("YES\n");
    for(int i = 0; i < n; ++i) 
        printf("%d %d\n", ans[i].first, ans[i].second);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值