首页 > 其他分享 >AtCoder Beginner Contest 351 E - Jump Distance Sum

AtCoder Beginner Contest 351 E - Jump Distance Sum

时间:2024-05-14 18:32:32浏览次数:30  
标签:pre AtCoder const Beginner Distance int sum len long

题目链接
Hint1: 只能斜着走,容易想到黑白棋盘,\((x+y)\%2==0\)位于一个团,\((x+y)\%2==1\)位于另一个团,分别求和。
Hint2: \(dist=max(|x1-x2|,|y1-y2|)\),这是切比雪夫距离,将坐标系倾斜\(45^{\circ}\),改为曼哈顿距离\(dist=|x1-x2|+|y1-y2|\),即\(X=x+y,Y=x-y\),这样就可以将横纵坐标分开计算,最终结果减半

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
//#define int long long
#define endl "\n"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int base = 131;
const int inf = INT_MAX;
const int mod = 1e9 + 7;
const int N = 2e5 + 5;

void solve() {
    int n;
    cin >> n;
    vector<pair<int, int> > v1, v2;
    for (int i = 0, x, y; i < n; i++) {
        cin >> x >> y;
        if ((x + y) % 2) {
            v1.push_back({x, y});
        }
        else {
            v2.push_back({x, y});
        }
    }
    auto calc = [](vector<pair<int, int> > v) {
        int len = v.size();
        vector<int> a(len), b(len);
        for (int i = 0; i < len; i++) {
            a[i] = v[i].first + v[i].second;
            b[i] = v[i].first - v[i].second;
        }
        sort(a.begin(), a.end());
        sort(b.begin(), b.end());
        ll cnt = 0, sum = 0, pre;
        for (int i = 0; i < len; i++) {
            if (i == 0) {
                pre = a[i];
                continue;
            }
            cnt += sum + i * (a[i] - pre);
            sum += i * (a[i] - pre);
            pre = a[i];
        }
        sum = 0;
        for (int i = 0; i < len; i++) {
            if (i == 0) {
                pre = b[i];
                continue;
            }
            cnt += sum + i * (b[i] - pre);
            sum += i * (b[i] - pre);
            pre = b[i];
        }
        return cnt;
    };
    cout << (calc(v1) + calc(v2)) / 2 << endl;
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T = 1;
    //cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}

标签:pre,AtCoder,const,Beginner,Distance,int,sum,len,long
From: https://www.cnblogs.com/fhyu/p/18191911

相关文章

  • AtCoder Beginner Contest 352 E - Clique Connect
    题目链接不需要将所有边都建立出来,根据\(Kruskal\)最小生成树的贪心策略,相同权值的边不需要形成团,形成一个链就行,对结果没有影响。时间复杂度\(O(mlogm)[m=\sum_{i=1}^{n}k_{i}]\)。#pragmaGCCoptimize(2)#pragmaGCCoptimize(3)#include<bits/stdc++.h>//#defineint......
  • 72_Edit_distance_编辑距离
    题目描述Giventwostringsword1andword2,returntheminimumnumberofoperationsrequiredtoconvertword1toword2.Youhavethefollowingthreeoperationspermittedonaword:InsertacharacterDeleteacharacterReplaceacharacter给定两个字符串wo......
  • AtCoder Beginner Contest 353
    AtCoderBeginnerContest353abc353_c题意:定义\(F(x,y)\)为\((x+y)mod10^8\)的值,求\(\displaystyle\sum_{i=1}^{N-1}\sum_{j=i+1}^Nf(A_i,A_j).\)思路:对于\(\displaystyle\sum_{i=1}^{N-1}\sum_{j=i+1}^N\f(A_i,A_j).\)来说,每个\(A_i\)的次数都是\(n-1\)次,所以如果没有\(m......
  • Atcoder ABC 353 全题解
    最近看的人好少……都快不想写了……你们的支持就是我创作的最大动力!AB%你CDE题意:有一个一个一个函数,把函数两两配对式求值,求这些值最后的总和C考虑将所有的和减去$10^8$出现的次数。将整个数组排序,然后进行二分,求第一个与这个数的和$\ge10^8$的位置,然后与这个数......
  • Atcoder Beginner Contest 353
    AtcoderBeginnerContest353A问题陈述有\(N\)幢楼房排列成一排。左起的\(i\)th楼高\(H_i\)。请判断是否有比左起第一座高的建筑物。如果存在这样的建筑物,请找出从左边起最左边的建筑物的位置。思路签到题。代码#include<bits/stdc++.h>usingnamespacestd;int......
  • AtCoder Beginner Contest 353
    AtCoderBeginnerContest353A-Buildings求第一个\(h_i\)大于\(h_1\)的位置。模拟。点击查看代码#include<bits/stdc++.h>#defineintlonglongusingnamespacestd;intn,h[103];signedmain(){ cin>>n;cin>>h[1]; for(inti=2;i<=n;i++){ cin&......
  • AtCoder Beginner Contest 353
    A-Buildings(abc353A)题目大意给定\(n\)个数字,输出第一个大于第一个数的下标。解题思路依次与第一个数比较,大于则输出。神奇的代码n=input()a=list(map(int,input().split()))b=[i>a[0]foriina]ifTruenotinb:print(-1)else:print(b.ind......
  • AtCoder Beginner Contest 318 Ex Count Strong Test Cases
    洛谷传送门AtCoder传送门首先做一些初步的观察:A和B的解法是对称的,所以A对的方案数等于B对的方案数。同时若A和B同时对则每个置换环环长为\(1\),方案数为\(n!\)。所以,若设A对的方案数为\(x\),那么答案为\(n!^2-(x-n!)-(x-n!)-n!=n!^2+n!-x\)。......
  • ATcoder ABC 352 F - Estimate Order 搜索
    很恶心的一个搜索,当然好像不用搜索也能做。没啥好讲的,一个联通块大小>=2就要搜索找位置,联通块大小等于1的不用搜。能调出来也是真不容易。#include<bits/stdc++.h>#defineintlonglong#defineDBdoubleusingnamespacestd;intn,m,tsiz,yinum;constintN=23;intfa[......
  • AtCoder Beginner Contest 352
    AtCoderBeginnerContest352A-AtCoderLine给\(N,X,Y,Z\)判断是否\(\min(X,Y)\leZ\le\max(X,Y)\)。模拟。点击查看代码#include<bits/stdc++.h>#defineintlonglongusingnamespacestd;intn,x,y,z;signedmain(){ cin>>n>>x>>y>......