您的位置:首页 > 其它

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

2015-11-17 08:37 169 查看
题目链接: 点击打开链接

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: