首页 > 其他分享 >[AGC066A] Adjacent Difference

[AGC066A] Adjacent Difference

时间:2024-06-19 15:59:02浏览次数:10  
标签:int 2d pmod Adjacent AGC066A Difference

[AGC066A] Adjacent Difference

  • 考虑我们生成的矩阵中的数都是 \(d\) 的倍数
  • 我们显然只需要保证 \(a'_{i,j}=xd\) 中的 \(x\) 互不相同即可
  • 我们钦定根据 \(i+j\) 的奇偶性来设置 \(x\) 为 \(0\) 或 \(1\),\(a_{i,j}\equiv xd\pmod{2d}\)
  • 我们尝试只对 \(x=0\) 时分析它此时的代价,\(x=1\) 只需在模意义下相对平移 \(d\) 即可
    • 若 \(a_{i,j}\bmod 2d\le d\),\(a_{i,j}\)
    • 若 \(d<a_{i,j}\bmod 2d\),\(2d-a_{i,j}\)
  • 此时的最坏代价可以达到 \(n^2d\)
  • 但我们发现有机可乘:考虑构造
    • 若 \(a_{i,j}\bmod 2d\le d\),\(d-a_{i,j}\)
    • 若 \(d<a_{i,j}\bmod 2d\),\(a_{i,j}-d\)
  • 若这种存在这样的贡献则与之前的贡献和为 \(n^2d\),那么两个中至少有一个符合答案
  • 而这恰好是对 \(x=1\) 分析的情况
  • 这就说明第一种 \(x=(i+j)\pmod 2\)
  • 第二种 \(x=(i+j+1)\pmod 2\)
#include <bits/stdc++.h>
using namespace std;
const int N=510;
int a[N][N],b[N][N];
int main()
{
    int n,d,dd,cost=0; cin>>n>>d; dd=d<<1;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
    cin>>a[i][j],b[i][j]=a[i][j];
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
    {
        int x=(a[i][j]%dd+dd)%dd;
        if((i+j)&1)
        {
            int t=x-d;a[i][j]-=t;cost+=abs(t);
            b[i][j]-=(x<=d)?x:x-2*d;
        }
        else
        {
            int t=x-d;b[i][j]-=t;
            a[i][j]-=(x<=d)?x:x-2*d;
            cost+=min(abs(x),abs(x-2*d));
        }
    }
    if(2*cost<=n*n*d)
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            cout<<a[i][j]<<" ";
            cout<<endl;
        }
    }
    else
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            cout<<b[i][j]<<" ";
            cout<<endl;
        }
    }
    return 0;
}

标签:int,2d,pmod,Adjacent,AGC066A,Difference
From: https://www.cnblogs.com/LUHCUH/p/18256439

相关文章

  • World Tour Finals 2022 Day2 E: Adjacent Xor Game
    考虑从高到低位做,不断贪心的一个过程。即假设把当前所有数\(a_i\)看成\(\lfloor\frac{a_i}{2^d}\rfloor\),有当前最优答案\(ans_d\);现在把所有数看成\(\lfloor\frac{a_i}{2^{d-1}}\rfloor\),推出下一步的答案\(ans_{d-1}\)。假设\(/2^d\)时,每一步xor完的序列是\(a_1......
  • LeetCode 2340. Minimum Adjacent Swaps to Make a Valid Array
    原题链接在这里:https://leetcode.com/problems/minimum-adjacent-swaps-to-make-a-valid-array/description/题目:Youaregivena 0-indexed integerarray nums.Swaps of adjacent elementsareabletobeperformedon nums.A valid arraymeetsthefollowingco......
  • [LeetCode] 2903. Find Indices With Index and Value Difference I
    Youaregivena0-indexedintegerarraynumshavinglengthn,anintegerindexDifference,andanintegervalueDifference.Yourtaskistofindtwoindicesiandj,bothintherange[0,n-1],thatsatisfythefollowingconditions:abs(i-j)>=index......
  • 题解【[ABC147F] Sum Difference】
    题目链接下为口胡题解:入手方向推导:直接考虑题目所给式子显然困难:\[w(S)=\sum_{i\inS}A_i-\sum_{i\notinS}A_i\]因为两个式子虽然相关但是都在变化,不妨转化为:\[w(S)=2\times\sum_{i\inS}A_i-\sum_{i=1}^nA_i\]这样只用求出有多少个不同的\(\sum_{i\inS}A_i\)。由于......
  • POI2011ROZ-Difference
    POI#Year2011#枚举#贪心枚举最后差最大的两个字符\(a,b\),将原串中\(a\rightarrow1,b\rightarrow-1\),其他标\(0\)原来的问题转化为强制包含\(1,-1\)的最大字段和问题,维护每个位置前最近的\(-1\),贪心取最大的//Author:xiaruizeconstintMOD=1000000007;const......
  • 52 Things: Number 40: What is normally considered the difference between SPA and
    52Things:Number40:WhatisnormallyconsideredthedifferencebetweenSPAandDPA?52件事:第40件:通常认为SPA和DPA之间的区别是什么? Thisisthelatestinaseriesofblogpoststoaddressthelistof '52ThingsEveryPhDStudentShouldKnowToDoCryptogr......
  • 52 Things: Number 38: What is the difference between a covert channel and a side
    52Things:Number38:Whatisthedifferencebetweenacovertchannelandaside-channel?52件事:第38件:隐蔽通道和侧通道之间的区别是什么? Thisisthelatestinaseriesofblogpoststoaddressthelistof'52ThingsEveryPhDStudentShouldKnowToDoCrypt......
  • 52 Things: Number 39: What is the difference between a side-channel attack and a
    52Things:Number39:Whatisthedifferencebetweenaside-channelattackandafaultattack?52件事:第39件:侧通道攻击和故障攻击之间的区别是什么? Thisisthelatestinaseriesofblogpoststoaddressthelistof '52ThingsEveryPhDStudentShouldKnowT......
  • 52 Things: Number 32: difference between game-based and simulation-based securit
    52Things:Number32:differencebetweengame-basedandsimulation-basedsecuritydefinitions52件事:数字32:基于游戏和基于模拟的安全定义之间的区别 Thisisthelatestinaseriesofblogpoststoaddressthelistof'52ThingsEveryPhDStudentShouldKnowt......
  • 52 Things: Number 2: What is the difference between a multi-core processor and a
    52Things:Number2:Whatisthedifferencebetweenamulti-coreprocessorandavectorprocessor?52件事:数字2:多核处理器和矢量处理器有什么区别?Onthefaceofit,youmaybeconfusedastowhatthedifferenceisbetweenthesetwoprocessors.Afterall,yo......