<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>_RedForest</title>
<link>https://blog.redforest.org.cn/</link>
<atom:link href="https://blog.redforest.org.cn/index.xml" rel="self" type="application/rss+xml"/>
<description>Notes, solutions and essays — an archive kept since 2018.</description>
<generator>quarto-1.9.38</generator>
<lastBuildDate>Wed, 16 Oct 2024 16:00:00 GMT</lastBuildDate>
<item>
  <title>m次k小值复杂度nlogm–上海科技大学2024年考研数据结构的某个题的第二问</title>
  <link>https://blog.redforest.org.cn/posts/0001.html</link>
  <description><![CDATA[ 




<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Content note — Needs verification · 待核验
</div>
</div>
<div class="callout-body-container callout-body">
<p>多次第 k 小值的 nlogm 复杂度只给了均匀/平均情形直觉，缺少严格最坏情形证明。</p>
<ul>
<li>文中承认分布不均匀时子问题规模缩减变慢，因此标题中的复杂度不应被当成无条件结论。</li>
<li>没有测试代码或边界处理，建议补证明后再作为正式算法笔记引用。</li>
</ul>
</div>
</div>
<section id="m次k小值复杂度nlogm上海科技大学2024年考研数据结构的某个题的第二问" class="level1">
<h1>m次k小值复杂度nlogm–上海科技大学2024年考研数据结构的某个题的第二问</h1>
<section id="前言" class="level2">
<h2 class="anchored" data-anchor-id="前言">前言：</h2>
<p>当时场上没想出来，我感觉我很容易算错时间复杂度，22年的icpc济南，那道矩阵取模的题也是因为当时时间复杂度算错了结果没写。主要原因应该还是太依赖感觉了。</p>
</section>
<section id="题目描述" class="level2">
<h2 class="anchored" data-anchor-id="题目描述">题目描述：</h2>
<p>这个题的第一问是无序序列求k小值。快速选择 时间空间都是o(n)，显然没有亚线性的算法。</p>
<p>第二问：</p>
<p>有m次询问，每次询问<img src="https://latex.codecogs.com/png.latex?k_i">小值要求空间复杂度不变（还是o(n)）要求时间复杂度是o(nlogm)注意m是询问次数。</p>
</section>
<section id="思路" class="level2">
<h2 class="anchored" data-anchor-id="思路">思路：</h2>
<p>很显然要考虑离线</p>
<p>首先发现m次询问可以先排个序，感觉很有用但好像也没什么用（当时场上想到这里就开始想用平衡树，treap之类的了）</p>
<p>考虑一下快速选择的平均复杂度为什么是o(n)很显然平均情况下它每层递归的复杂度应该是<img src="https://latex.codecogs.com/png.latex?n/(2%5Ei)">，等比级数求和即可。 <img src="https://latex.codecogs.com/png.latex?%0AS(n)%20=%20%5Csum_%7Bi=0%7D%5E%7Binf%7D%20%5Cfrac%7Bn%7D%7B2%5Ei%7D=2n%0A"> 延续一下这个思想，我们发现在快速选择的时候，每当选定一个轴，做完一轮分组以后，轴的位置可能产生移动。在快速选择中，我们会抛弃此时的轴的左边全部或者右边全部。</p>
<p>考虑同时处理m次询问。我们首先对m次询问进行排序，然后处理中间的那个询问，假设是第p个询问。发现当我们通过快速选择法找到中间那个询问的答案（假设是<img src="https://latex.codecogs.com/png.latex?k_i">，下标为h）以后此时的数组里，第1~p-1个询问的答案都会在<img src="https://latex.codecogs.com/png.latex?k_i">的左边，第p+1~m个询问的答案都在<img src="https://latex.codecogs.com/png.latex?k_i">的右边。</p>
<p>那么我们就得到了一个子问题，假设数组是a，下标从1开始。也就是在a[1]~a[h-1]中处理 询问1~p-1和在a[h+1]~a[n]中处理 询问p+1~m。这个子问题可以通过遍历1/2个序列得到答案并获得另外两个子问题。同理，每层子问题都可以通过遍历<img src="https://latex.codecogs.com/png.latex?1/%7B2%5E%7B%E5%B1%82%E6%95%B0-1%7D%7D">个序列获得答案并得到两个子问题。</p>
<p>递归求解即可。</p>
</section>
<section id="复杂度分析" class="level2">
<h2 class="anchored" data-anchor-id="复杂度分析">复杂度分析：</h2>
<p>考虑m个询问是均匀分布的情况，分布均匀是指k小值的k在1~n中的分布。（注意，当分布不均匀的时候我们没办法像快速选择法一样抛弃部分序列，且分布不均匀的情况下，子问题规模缩减的速度会变慢。可以类比快速选择的最坏情况考虑。）</p>
<p>至于为什么时间复杂度是nlogm，或许可以算个级数。但是还有一种比较简单的方法：考虑一个元素最多被访问多少次，显然每次递归都会导致某个子序列被切成两半，分别给不同的两个询问。那么一个元素最多被logm个询问访问到。所以时间复杂度nlogm。</p>
<p>没有测试数据，代码就不写了，注意一下边界情况即可。</p>


</section>
</section>

 ]]></description>
  <category>算法</category>
  <guid>https://blog.redforest.org.cn/posts/0001.html</guid>
  <pubDate>Wed, 16 Oct 2024 16:00:00 GMT</pubDate>
</item>
<item>
  <title>kde-plasma文件丢失无法登陆和sddm设置无效的解决方法</title>
  <link>https://blog.redforest.org.cn/posts/001b.html</link>
  <description><![CDATA[ 




<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Content note — Outdated environment · 环境过时
</div>
</div>
<div class="callout-body-container callout-body">
<p>KDE/SDDM 修复方式强依赖当时发行版状态，不能作为通用 Linux 修复方案。</p>
<ul>
<li>直接 remove/install 桌面环境可能破坏当前系统依赖，应先确认发行版、日志和包管理状态。</li>
</ul>
</div>
</div>
<section id="kde-plasma文件丢失无法登陆和sddm设置无效的解决方法" class="level1">
<h1>kde-plasma文件丢失无法登陆和sddm设置无效的解决方法</h1>
<section id="kde-plasma文件丢失无法登陆" class="level2">
<h2 class="anchored" data-anchor-id="kde-plasma文件丢失无法登陆">1.kde-plasma文件丢失无法登陆</h2>
<p>症状主要表现为登陆界面忽然变得十分复古，并且伴随输入密码之后enter没反应，有的时候会报告文件缺失。</p>
<p><strong>解决方法在于使用救援模式重新安装kde-plasma-desktop和sddm</strong></p>
<p>关于sddm：这不是必须的，但是就我目前使用情况，伴随kde-plasma安装的sddm是损坏，或者至少功能不完整的。</p>
<section id="使用系统救援模式" class="level3">
<h3 class="anchored" data-anchor-id="使用系统救援模式">1.1 使用系统救援模式</h3>
<p>如果尝试使用系统自带的救援模式重新安装kde-plasma，那么会遇到一个重要的问题：需要手动开启wifi然后联网。 要做到这一点需要手动加载驱动，比较麻烦。</p>
</section>
<section id="使用安装介质救援" class="level3">
<h3 class="anchored" data-anchor-id="使用安装介质救援">1.2 使用安装介质救援</h3>
<p>使用安装介质带有的救援功能，这允许用户使用chroot将被救援的系统挂载到根目录并使用，比较好的一点是这样不需要手动加载驱动。 使用</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">apt</span> remove kde-plasma-desktop</span>
<span id="cb1-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">apt</span> install kde-plasma-desktop</span></code></pre></div></div>
<p>重启即可</p>
</section>
</section>
<section id="sddm设置无效" class="level2">
<h2 class="anchored" data-anchor-id="sddm设置无效">2.sddm设置无效</h2>
<p>正如我前面所说，伴随kde-plasma安装的sddm是损坏，或者至少功能不完整的</p>
<p>这个时候只需要</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb2-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">apt</span> remove sddm</span>
<span id="cb2-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">apt</span> install sddm</span></code></pre></div></div>
<p>重启即可</p>


</section>
</section>

 ]]></description>
  <category>web</category>
  <guid>https://blog.redforest.org.cn/posts/001b.html</guid>
  <pubDate>Mon, 09 Oct 2023 16:00:00 GMT</pubDate>
</item>
<item>
  <title>AOE/AOV网下的关键路径</title>
  <link>https://blog.redforest.org.cn/posts/0031.html</link>
  <description><![CDATA[ 




<div class="callout callout-style-default callout-caution callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Caution</span>Content note — Known errors · 明显错误
</div>
</div>
<div class="callout-body-container callout-body">
<p>AOE/AOV 定义和关键路径结论有明显错误，代码也存在索引 bug。</p>
<ul>
<li>AOV 通常是顶点表示活动、边表示优先关系；AOE 是边表示活动、顶点表示事件，文中写成了同一种。</li>
<li>“所有关键点一定组成一棵树”不成立，关键活动可形成子图。</li>
<li>代码里 <code>iscritical [j] = true</code> 应重新核对，j 是邻接表下标而不是点编号。</li>
</ul>
</div>
</div>
<section id="aoeaov网下的关键路径" class="level1">
<h1>AOE/AOV网下的关键路径</h1>
<p>在做那本王道考研数据结构书的时候发现这个东西，以前没听说过（太菜了</p>
<section id="定义" class="level2">
<h2 class="anchored" data-anchor-id="定义">1.定义</h2>
<p>一般而言这个模型可以用来表示一个项目的子项之间的依赖关系，用来管理项目进度。</p>
<section id="aov网" class="level3">
<h3 class="anchored" data-anchor-id="aov网">1.1 AOV网</h3>
<p>AOV网指的是Activity-On-Vertex，用点表示事件，用边表示事件之间的关系（活动），边权是活动时间。这是一个DAG（有向无环图）</p>
</section>
<section id="aoe网" class="level3">
<h3 class="anchored" data-anchor-id="aoe网">1.2 AOE网</h3>
<p>AOE网指的是Activity-On-Edge，用点表示事件，用边表示事件之间的关系（活动），边权是活动时间。这也是一个DAG（有向无环图）</p>
</section>
<section id="关键路径" class="level3">
<h3 class="anchored" data-anchor-id="关键路径">1.3 关键路径</h3>
<p>如果改变一条路径上的任意一条边的边权，都可以加速项目的最终进度，那么这条路径被称为关键路径。</p>
</section>
</section>
<section id="计算方法" class="level2">
<h2 class="anchored" data-anchor-id="计算方法">2.计算方法</h2>
<p>计算关键路径事实上只是在DAG上的简单dp，首先计算一个事件的最早开始时间，然后计算一个事件的不会影响到最终项目进度的最晚开始时间，当最早开始时间等于最晚开始时间，那么这个点是一个关键的点。所有的关键点一定会组成一颗树。树上的所有路径都是关键路径。</p>
<p>首先计算拓扑序，然后在拓扑序上正向递推一个事件的最早开始时间，最后在逆拓扑序上递推最迟开始时间</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Afor%5C%20each%5C%20edge%5C%20%3Cu%20%5Crightarrow%20v%3E%20%5C%20%5C:%0A"> <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0AminStartTime%5Bv%5D%20&amp;=%20Max(minStartTime%5Bv%5D,minStartTime%5Bu%5D+w%5Bu%5D%5Bv%5D)%5C%5C%0AmaxStartTime%5Bu%5D%20&amp;=%20Min(maxStartTime%5Bu%5D,maxStartTime%5Bv%5D-w%5Bu%5D%5Bv%5D)%0A%5Cend%7Baligned%7D%0A"></p>
<p>第一个点的 minStartTime = 0，最后一个点的 maxStartTime = minStartTime</p>
</section>
<section id="代码" class="level2">
<h2 class="anchored" data-anchor-id="代码">3.代码</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode cpp code-with-copy"><code class="sourceCode cpp"><span id="cb1-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;iostream&gt;</span></span>
<span id="cb1-2"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;cstdio&gt;</span></span>
<span id="cb1-3"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;vector&gt;</span></span>
<span id="cb1-4"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;queue&gt;</span></span>
<span id="cb1-5"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;cstring&gt;</span></span>
<span id="cb1-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">using</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">namespace</span> std<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> MAXN <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e6</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> INF <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x3f3f3f3f</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">template</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">typename</span> _TP<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb1-9"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">inline</span> _TP qread<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>_TP <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-10">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-11">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(!</span>isdigit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)){</span>w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'-'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>getchar<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();}</span></span>
<span id="cb1-12">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>isdigit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)){</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=(</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)+(</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)+(</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'0'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>getchar<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();}</span></span>
<span id="cb1-13">    X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?-</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-14">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-15"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-16">queue <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> q<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-17">vector <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span>d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span>pre<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span>predis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-18"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> ind<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-19"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">inline</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> adde<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> z<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span> </span>
<span id="cb1-20">    v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>push_back<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-21">    d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>push_back<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>z<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-22">    pre<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>push_back<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-23">    predis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>push_back<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>z<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-24">    ind<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]++;</span></span>
<span id="cb1-25"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-26"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> topoOrder<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span>vis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span>cntTopo<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-27"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> topoSort<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-28">    vis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-29">    q<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>push<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-30">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> nxt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-31">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(!</span>q<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>empty<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()){</span></span>
<span id="cb1-32">        s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> q<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>front<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb1-33">        vis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> </span>
<span id="cb1-34">        q<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>pop<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb1-35">        topoOrder<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[++</span>cntTopo<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=</span>s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-36">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-37">            nxt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">][</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-38">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(!</span>vis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>nxt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]){</span> </span>
<span id="cb1-39">                <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(!(--</span>ind<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>nxt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">])){</span></span>
<span id="cb1-40">                    q<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>push<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>nxt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-41">                <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-42">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-43">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-44">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-45"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-46"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> minStart<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span>maxStart<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>m<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-47"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">bool</span> iscritical <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-48"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">inline</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> Max<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-49">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-50"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-51"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">inline</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> Min<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-52">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-53"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-54"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">inline</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> PRINT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>tmp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-55">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-56">        cout<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span>tmp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]&lt;&lt;</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">' '</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-57">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-58">    cout<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-59"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-60"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> main<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(){</span></span>
<span id="cb1-61">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>z<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-62">    qread<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span>qread<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>m<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-63">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>m<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-64">        qread<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span>qread<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span>qread<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>z<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-65">        adde<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>z<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-66">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-67">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//start node</span></span>
<span id="cb1-68">    topoSort<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-69">    cout<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"topoOrder: "</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-70">    PRINT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>topoOrder<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span> </span>
<span id="cb1-71">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> now<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>nxt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-72">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-73">        now <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> topoOrder<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-74">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>now<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-75">            nxt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>now<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">][</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-76">            minStart<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>nxt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Max<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>minStart<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>nxt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span>minStart<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>now<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]+</span>d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>now<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">][</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]);</span></span>
<span id="cb1-77">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-78">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-79">    cout<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"minStart: "</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-80">    PRINT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>minStart<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span> </span>
<span id="cb1-81">    memset<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>maxStart<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x3f</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">sizeof</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>maxStart<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">));</span></span>
<span id="cb1-82">    maxStart<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>topoOrder<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> minStart<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>topoOrder<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]];</span></span>
<span id="cb1-83">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">--){</span></span>
<span id="cb1-84">        now <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> topoOrder<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-85">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>pre<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>now<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-86">            nxt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pre<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>now<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">][</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-87">            maxStart<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>nxt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Min<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>maxStart<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>nxt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span>maxStart<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>now<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]-</span>predis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>now<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">][</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]);</span></span>
<span id="cb1-88">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-89">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-90">    cout<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"maxStart: "</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-91">    PRINT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>maxStart<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span> </span>
<span id="cb1-92">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span>      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//go through all edges</span></span>
<span id="cb1-93">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-94">            nxt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">][</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-95">            <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> minHappen <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> minStart<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-96">            <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> maxHappen <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> maxStart<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>nxt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]-</span>d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">][</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-97">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>maxHappen <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> minHappen <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-98">                iscritical<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> iscritical <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-99">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-100">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-101">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-102">    cout<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"critical point: "</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-103">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-104">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>iscritical<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">])</span>cout<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">' '</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-105">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-106">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-107"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</section>
<section id="样例" class="level2">
<h2 class="anchored" data-anchor-id="样例">4.样例</h2>
<pre><code>10 14
1 2 5
1 3 6
2 5 3
3 5 6
3 4 3
4 5 3
4 7 1
4 8 4
5 6 4
5 7 2
6 10 4
7 9 5
8 9 2
9 10 2</code></pre>
<p>第一行n,m是点的数量和边的数量</p>
<p>样例来自王道数据结构考研复习指导的习题13</p>


</section>
</section>

 ]]></description>
  <category>算法</category>
  <guid>https://blog.redforest.org.cn/posts/0031.html</guid>
  <pubDate>Sun, 01 Oct 2023 16:00:00 GMT</pubDate>
</item>
<item>
  <title>题解:齿轮</title>
  <link>https://blog.redforest.org.cn/posts/0016.html</link>
  <description><![CDATA[ 




<section id="题解齿轮" class="level1">
<h1>题解:齿轮</h1>
<section id="题意" class="level2">
<h2 class="anchored" data-anchor-id="题意">1.题意</h2>
<p>地址:https://www.luogu.com.cn/problem/P8799</p>
<section id="题目描述" class="level3">
<h3 class="anchored" data-anchor-id="题目描述">1.1 题目描述</h3>
<p>这天，小明在组装齿轮。</p>
<p>他一共有 <img src="https://latex.codecogs.com/png.latex?n"> 个齿轮，第 <img src="https://latex.codecogs.com/png.latex?i"> 个齿轮的半径为 <img src="https://latex.codecogs.com/png.latex?r_%7Bi%7D">, 他需要把这 <img src="https://latex.codecogs.com/png.latex?n"> 个齿轮按一定顺序从左到右组装起来，这样最左边的齿轮转起来之后，可以传递到最右边的齿轮，并且这些齿轮能够起到提升或者降低转速（角速度）的作用。</p>
<p><img src="https://luogu.oss-cn-hangzhou.aliyuncs.com/upload/vjudge_pic/lanqiao/2022_09_29_8ee8d95d6d0319bca20dg-17.jpg" class="img-fluid"></p>
<p>小明看着这些齿轮，突然有 <img src="https://latex.codecogs.com/png.latex?Q"> 个疑问: 能否按一定顺序组装这些齿轮使得最右边的齿轮的转速是最左边的齿轮的 <img src="https://latex.codecogs.com/png.latex?q_%7Bi%7D"> 倍?</p>
</section>
<section id="输入格式" class="level3">
<h3 class="anchored" data-anchor-id="输入格式">1.2 输入格式</h3>
<p>输入共 <img src="https://latex.codecogs.com/png.latex?Q+2"> 行，第一行为两个正整数 <img src="https://latex.codecogs.com/png.latex?n,%20Q">, 表示齿轮数量和询问数量。</p>
<p>第二行为 <img src="https://latex.codecogs.com/png.latex?n"> 个正整数 <img src="https://latex.codecogs.com/png.latex?r_%7B1%7D,%20r_%7B2%7D,%20%5Cldots,%20r_%7Bn%7D">，表示每个齿轮的半径。</p>
<p>后面 <img src="https://latex.codecogs.com/png.latex?Q"> 行，每行一个正整数 <img src="https://latex.codecogs.com/png.latex?q_%7Bi%7D"> 表示询问。</p>
</section>
<section id="输出格式" class="level3">
<h3 class="anchored" data-anchor-id="输出格式">1.3输出格式</h3>
<p><img src="https://latex.codecogs.com/png.latex?Q"> 行，对于每个询问，如果存在至少一种组装方案满足条件，输出 <code>YES</code>, 否则输出 <code>NO</code>。</p>
</section>
<section id="样例输入" class="level3">
<h3 class="anchored" data-anchor-id="样例输入">样例输入</h3>
<pre><code>5 3
4 2 3 3 1
2
4
6</code></pre>
</section>
<section id="样例输出" class="level3">
<h3 class="anchored" data-anchor-id="样例输出">样例输出</h3>
<pre><code>YES
YES
NO</code></pre>
</section>
<section id="提示" class="level3">
<h3 class="anchored" data-anchor-id="提示">提示</h3>
<p><strong>【样例说明】</strong></p>
<p>询问 <img src="https://latex.codecogs.com/png.latex?1"> 方案之一：<code>23341</code>。</p>
<p>询问 <img src="https://latex.codecogs.com/png.latex?2"> 方案之一：<code>42331</code>。</p>
<p>询问 <img src="https://latex.codecogs.com/png.latex?3"> 没有方案。</p>
<p><strong>【评测用例规模与约定】</strong></p>
<p>对于 <img src="https://latex.codecogs.com/png.latex?15%20%5C%25"> 的数据，保证 <img src="https://latex.codecogs.com/png.latex?n,%20Q%20%5Cleq%20100">;</p>
<p>对于 <img src="https://latex.codecogs.com/png.latex?30%20%5C%25"> 的数据，保证 <img src="https://latex.codecogs.com/png.latex?n,%20Q%20%5Cleq%202000">;</p>
<p>对于 <img src="https://latex.codecogs.com/png.latex?100%20%5C%25"> 的数据，保证 <img src="https://latex.codecogs.com/png.latex?n%5Cge%202,n,%20Q%20%5Cleq%202%20%5Ctimes%2010%5E%7B5%7D%20;%20a_%7Bi%7D,%20q_%7Bi%7D%20%5Cleq%202%20%5Ctimes%2010%5E%7B5%7D">。</p>
</section>
</section>
<section id="思路" class="level2">
<h2 class="anchored" data-anchor-id="思路">2.思路</h2>
<p>很显然，放大（缩小）的倍率只和最开始和最后的两个齿轮有关系。与中间的齿轮无关。所以，对于每个询问<img src="https://latex.codecogs.com/png.latex?q_i">我们只需要找到是否存在两个齿轮使得它们的比值为<img src="https://latex.codecogs.com/png.latex?q_i">即可。</p>
<p>但是考虑到数据规模为2e5，且<img src="https://latex.codecogs.com/png.latex?q_i">范围也只有2e5，考虑预处理答案。注意一定要提前把齿轮离散化一下，也就是去重。</p>
<p>枚举两个齿轮的时间复杂度是<img src="https://latex.codecogs.com/png.latex?n%5E2">，显然不行。</p>
<p>考虑枚举一个齿轮，然后枚举它的不超过2e5倍数。这种做法的时间复杂度不好估计。在场上可以写个Python程序算一下：最坏情况下齿轮大小为1到2e5。显然可以像下面这样计算时间复杂度。要注意的是，齿轮要去重。否则最坏情况是全是大小为1的齿轮。</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1">ans<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb3-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100000</span>):</span>
<span id="cb3-3">    ans <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ans <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100000</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>i)</span>
<span id="cb3-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(ans)</span></code></pre></div></div>
<p>结果为<code>1166749</code></p>
<p>另外需要注意放大倍率为1的时候需要特判一下。</p>
</section>
<section id="代码" class="level2">
<h2 class="anchored" data-anchor-id="代码">3.代码</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode cpp code-with-copy"><code class="sourceCode cpp"><span id="cb4-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;iostream&gt;</span></span>
<span id="cb4-2"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;cstdio&gt;</span></span>
<span id="cb4-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">using</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">namespace</span> std<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb4-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> MAXN <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2e5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb4-5"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span>vis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span>m<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb4-6"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">bool</span> ans<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb4-7"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> main<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(){</span></span>
<span id="cb4-8">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>q<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>cnt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb4-9">    scanf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%d%d</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,&amp;</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,&amp;</span>q<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb4-10">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb4-11">        scanf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%d</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,&amp;</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]);</span></span>
<span id="cb4-12">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>vis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]]){</span></span>
<span id="cb4-13">            ans<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb4-14">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">continue</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>   </span>
<span id="cb4-15">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb4-16">        vis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]]=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb4-17">        m<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[++</span>cnt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb4-18">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb4-19">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>cnt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb4-20">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=(</span>m<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]&lt;&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200000</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span>m<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]){</span></span>
<span id="cb4-21">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>vis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">])</span>ans<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>m<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]]=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb4-22">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb4-23">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb4-24">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb4-25">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>q<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb4-26">        scanf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%d</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,&amp;</span>t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb4-27">        cout<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;(</span>ans<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]==</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"YES</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"NO</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span> </span>
<span id="cb4-28">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb4-29">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb4-30"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>


</section>
</section>

 ]]></description>
  <category>算法</category>
  <guid>https://blog.redforest.org.cn/posts/0016.html</guid>
  <pubDate>Mon, 22 May 2023 16:00:00 GMT</pubDate>
</item>
<item>
  <title>题解:Spreading the Wealth</title>
  <link>https://blog.redforest.org.cn/posts/0007.html</link>
  <description><![CDATA[ 




<section id="题解-spreading-the-wealth" class="level1">
<h1>题解 : Spreading the Wealth</h1>
<p>地址：https://vjudge.net/problem/UVA-11300</p>
<section id="题意" class="level2">
<h2 class="anchored" data-anchor-id="题意">1：题意</h2>
<p>A Communist regime is trying to redistribute wealth in a village. They have have decided to sit everyone around a circular table. First, everyone has converted all of their properties to coins of equal value, such that the total number of coins is divisible by the number of people in the village. Finally, each person gives a number of coins to the person on his right and a number coins to the person on his left, such that in the end, everyone has the same number of coins. Given the number of coins of each person, compute the minimum number of coins that must be transferred using this method so that everyone has the same number of coins.</p>
<p>大致意思是：初始n个人围坐成环形，每个人都有<img src="https://latex.codecogs.com/png.latex?a_i">金币。每个人可以给他的左或右边的人一些金币。要求所有人有相同数量的金币，求最小的转手金币的数量，保证金币的总数可以整除人数。</p>
</section>
<section id="思路" class="level2">
<h2 class="anchored" data-anchor-id="思路">2：思路：</h2>
<p>设<img src="https://latex.codecogs.com/png.latex?x_i">为第i个人向右边的人传递的金币数量，若<img src="https://latex.codecogs.com/png.latex?x_%7Bi-1%7D">为负则表示第i个人向左传递金币。最终结果即为<img src="https://latex.codecogs.com/png.latex?%5Csum%20%7Cx_i%7C">。很显然，我们知道每个人最终的金币数量为金币的平均数设为m。对于任意一个人而言，他最终的金币数<img src="https://latex.codecogs.com/png.latex?m=a_i+x_%7Bi-1%7D-x_i">即<img src="https://latex.codecogs.com/png.latex?x_i=a_i+x_%7Bi-1%7D-m">每个<img src="https://latex.codecogs.com/png.latex?x_i">均可以由<img src="https://latex.codecogs.com/png.latex?x_1">递推求得。结果可表示为<img src="https://latex.codecogs.com/png.latex?x_i=x_1-c_i"> 所求答案即为<img src="https://latex.codecogs.com/png.latex?%5Csum%20%7Cx_1-c_i%7C"> 。这个式子的几何意义为在一维坐标轴上点<img src="https://latex.codecogs.com/png.latex?x_1">到点<img src="https://latex.codecogs.com/png.latex?c_i">的距离和。我们要最小化这个距离和。那么<img src="https://latex.codecogs.com/png.latex?x_1">应该取<img src="https://latex.codecogs.com/png.latex?c_i">的中位数。递推求解<img src="https://latex.codecogs.com/png.latex?x_i">即可得到答案。</p>
</section>
<section id="代码" class="level2">
<h2 class="anchored" data-anchor-id="代码">3：代码</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode cpp code-with-copy"><code class="sourceCode cpp"><span id="cb1-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;iostream&gt;</span></span>
<span id="cb1-2"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;cstdio&gt;</span></span>
<span id="cb1-3"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;algorithm&gt;</span></span>
<span id="cb1-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">using</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">namespace</span> std<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> MAXN <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e6</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-6"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span>c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">inline</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> Abs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-8">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?-</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> </span>
<span id="cb1-9"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-10"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> main<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(){</span></span>
<span id="cb1-11">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-12">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>scanf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%d</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,&amp;</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)==</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-13">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> sum<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>ans<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-14">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-15">            scanf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%lld</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,&amp;</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]);</span> </span>
<span id="cb1-16">            sum<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-17">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-18">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> m<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>sum<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-19">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-20">            c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=</span>c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]+</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]-</span>m<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-21">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-22">        sort<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-23">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> x1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-24">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-25">            ans<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span>Abs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]-</span>x1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-26">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-27">        cout<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span>ans<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-28">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-29">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-30"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span> </span></code></pre></div></div>


</section>
</section>

 ]]></description>
  <category>算法</category>
  <guid>https://blog.redforest.org.cn/posts/0007.html</guid>
  <pubDate>Sat, 20 May 2023 16:00:00 GMT</pubDate>
</item>
<item>
  <title>FWT学习笔记</title>
  <link>https://blog.redforest.org.cn/posts/001a.html</link>
  <description><![CDATA[ 




<div class="callout callout-style-default callout-caution callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Caution</span>Content note — Known errors · 明显错误
</div>
</div>
<div class="callout-body-container callout-body">
<p>FWT 笔记中 AND/OR 卷积的公式和章节标题混用，容易误导。</p>
<ul>
<li>“AND”小节写的是 <code>i = j | k</code>，这是 OR 卷积形式。</li>
<li>建议重新拆分 OR、AND、XOR 的定义、变换和逆变换，并重新验证代码下标约定。</li>
</ul>
</div>
</div>
<section id="fwt学习笔记" class="level1">
<h1>FWT学习笔记</h1>
<section id="参考文献" class="level2">
<h2 class="anchored" data-anchor-id="参考文献">0.参考文献</h2>
<p><a href="https://www.luogu.com.cn/blog/xht37/solution-p4717">参考文献</a></p>
</section>
<section id="fwt的定义与证明" class="level2">
<h2 class="anchored" data-anchor-id="fwt的定义与证明">1.FWT的定义与证明</h2>
<p>首先fwt是解决一类二进制下与或非运算的卷积问题的</p>
<p>也就是形如 <img src="https://latex.codecogs.com/png.latex?%0AC_i=%5Csum_%7Bj%5Coplus%20k%20=%20i%7DA_j%20%5Ctimes%20B_k%0A"> 使用fwt可以nlogn的进行计算</p>
<p>但是实际上似乎遇到一些任意进制下且不进位的卷积操作都可以考虑使用这种思想进行处理</p>
<section id="and" class="level3">
<h3 class="anchored" data-anchor-id="and">1.1 AND</h3>
<p><img src="https://latex.codecogs.com/png.latex?%0Ac_i%20=%20%5Csum_%7Bi=j%7Ck%7Da_jb_k%0A"></p>
<p>有 <img src="https://latex.codecogs.com/png.latex?%0Aj%7Ci=i,k%7Ci=i,(j%7Ck)%7Ci=i%0A"></p>
<p>因为 <img src="https://latex.codecogs.com/png.latex?%0A(j%7Ck)%7Cj%20=%20(j%7Ck),(j%7Ck)%7Ci%20=%20(j%7Ck)%0A"></p>
<p>构造 <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0Afwt_%7Bai%7D%20%20&amp;=%20%5Csum_%7Bj%7Ci=i%7Da_j%5C%5C%0Afwt_%7Bbi%7D%20%20&amp;=%20%5Csum_%7Bk%7Ci=i%7Db_k%5C%5C%0Afwt_%7Bci%7D%20%20&amp;=%20%5Csum_%7B(j%7Ck)%7Ci=i%7Da_j%20b_k%0A%5Cend%7Baligned%7D%0A"> 有 <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0Afwt%5Ba%5D%5Ctimes%20fwt%5Bb%5D%0A&amp;=(%5Csum_%7Bj%7Ci=i%7Da%5Bj%5D))%5Ctimes(%5Csum_%7Bk%7Ci=i%7Db%5Bk%5D))%20%5C%5C%0A&amp;=%5Csum_%7Bj%7Ci=i%7D%5Csum_%7Bk%7Ci=i%7Da_j%20b_k%5C%5C%0A&amp;=%5Csum_%7B(j%7Ck)%7Ci=i%7Da_j%20b_k%5C%5C%0A&amp;=fwt%5Bc%5D%0A%5Cend%7Baligned%7D%0A"> 如何以nlogn的复杂度计算fwt &amp; ifwt 令<img src="https://latex.codecogs.com/png.latex?a_0">为a中下标最高位为0的部分，<img src="https://latex.codecogs.com/png.latex?a_1">为a中下标最高位为1的部分</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Afwt%5Ba%5D=merge(fwt%5Ba_0%5D,fwt%5Ba_0%5D+fwt%5Ba_1%5D)%0A"></p>
</section>
<section id="or-证明与and相同" class="level3">
<h3 class="anchored" data-anchor-id="or-证明与and相同">1.2 OR &gt;&gt; 证明与AND相同</h3>
</section>
<section id="xor" class="level3">
<h3 class="anchored" data-anchor-id="xor">1.3 XOR</h3>
<p>这里构造了不同于OR/AND的变换</p>
<p>定义<img src="https://latex.codecogs.com/png.latex?x%5Cotimes%20y%20=%20popcount(x%5C&amp;y)%252"></p>
<p>其中<img src="https://latex.codecogs.com/png.latex?popcount(x)">为x在二进制下1的个数</p>
<p>显然该运算满足</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A(i%5Cotimes%20j)%5C%20xor%5C%20(i%5Cotimes%20k)=i%5Cotimes(j%5C%20xor%20%5C%20k)%20%20%0A"></p>
<p>构造</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Afwt_%7Bai%7D%20=%20%5Csum_%7Bi%5Cotimes%20j%20=0%7Da_jb_k%0A"></p>
<p>有 <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0Afwt%5Ba%5D%5Ctimes%20fwt%5Bb%5D&amp;=(%5Csum_%7Bi%5Cotimes%20j%20=0%7Da_j-%5Csum_%7Bi%5Cotimes%20j%20=1%7Da_j)%5Ctimes(%5Csum_%7Bi%5Cotimes%20j%20=0%7Db_k-%5Csum_%7Bi%5Cotimes%20j%20=1%7Db_k)%5C%5C%0A&amp;=(%5Csum_%7Bi%5Cotimes%20j=0%7Da_j)(%5Csum_%7Bi%5Cotimes%20k=0%7Db_k)-(%5Csum_%7Bi%5Cotimes%20j=0%7Da_j)(%5Csum_%7Bi%5Cotimes%20k=1%7Db_k)-(%5Csum_%7Bi%5Cotimes%20j=1%7Da_j)(%5Csum_%7Bi%5Cotimes%20k=0%7Db_k)+(%5Csum_%7Bi%5Cotimes%20j=1%7Da_j)(%5Csum_%7Bi%5Cotimes%20k=1%7Db_k)%5C%5C%0A&amp;=(%5Csum_%7Bi%5Cotimes(j%5C%20xor%5C%20k)=0%7Da_jb_k-%5Csum_%7Bi%5Cotimes(j%5C%20xor%5C%20k)=1%7Da_jb_k)%5C%5C%0A&amp;=fwt%5Bc%5D%0A%5Cend%7Baligned%7D%0A"> 那么</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0Afwt%5Ba%5D&amp;=merge(fwt%5Ba_0%5D+fwt%5Ba_1%5D,fwt%5Ba_0%5D-fwt%5Ba_1%5D)%5C%5C%0Aa&amp;=merge(%5Cfrac%7Ba_0+a_1%7D%7B2%7D,%5Cfrac%7Ba_0-a_1%7D%7B2%7D)%0A%5Cend%7Baligned%7D%0A"></p>
</section>
</section>
<section id="代码" class="level2">
<h2 class="anchored" data-anchor-id="代码">2.代码</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode cpp code-with-copy"><code class="sourceCode cpp"><span id="cb1-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;iostream&gt;</span></span>
<span id="cb1-2"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;cstdio&gt;</span></span>
<span id="cb1-3"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;cstring&gt;</span></span>
<span id="cb1-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">using</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">namespace</span> std<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-5"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#define int </span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;"> </span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span></span>
<span id="cb1-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> INF <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x3f3f3f3f</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>MAXN <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">17</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>P <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">998244353</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-7"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>inv2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=(</span>P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)&gt;&gt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">template</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">typename</span> _TP<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb1-9"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">inline</span> _TP qread<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>_TP <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-10">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-11">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(!</span>isdigit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)){</span>w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'-'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>getchar<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();}</span></span>
<span id="cb1-12">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>isdigit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)){</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=(</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)+(</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)+(</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">48</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>getchar<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();}</span></span>
<span id="cb1-13">    X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?-</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-14">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> </span>
<span id="cb1-15"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-16"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> A<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span>B<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-17"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">inline</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> cpy<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(){</span></span>
<span id="cb1-18">    memcpy<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>A<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">sizeof</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>A<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">));</span></span>
<span id="cb1-19">    memcpy<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>B<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">sizeof</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>B<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">));</span></span>
<span id="cb1-20"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-21"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">inline</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> mul<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(){</span></span>
<span id="cb1-22">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-23">        a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=(</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]*</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">])%</span>P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-24">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-25"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-26"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">inline</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> PRINT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(){</span></span>
<span id="cb1-27">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-28">        cout<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]&lt;&lt;</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" "</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-29">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-30">    cout<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-31"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-32"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">inline</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> fwtOr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> opt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-33">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-34">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span>p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-35">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-36">                a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=(</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]+</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]*</span>opt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)%</span>P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-37">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-38">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-39">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-40"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-41"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">inline</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> fwtAnd<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> opt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-42">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-43">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span>p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-44">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-45">                a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=(</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]+</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]*</span>opt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)%</span>P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-46">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span> </span>
<span id="cb1-47">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span> </span>
<span id="cb1-48">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span> </span>
<span id="cb1-49"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-50"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">inline</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> fwtXor<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> opt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-51">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-52">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span>p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-53">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-54">                <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span>Y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-55">                a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=(</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>Y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)%</span>P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-56">                a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=(</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>Y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)%</span>P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-57">                a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=(</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]*</span>opt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)%</span>P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-58">                a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=(</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]*</span>opt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)%</span>P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-59">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-60">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-61">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-62"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-63"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">signed</span> main<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(){</span></span>
<span id="cb1-64">    qread<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-65">    n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-66">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-67">        qread<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>A<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]);</span></span>
<span id="cb1-68">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-69">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-70">        qread<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>B<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]);</span></span>
<span id="cb1-71">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-72">    cpy<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span>fwtOr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">),</span>fwtOr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">),</span>mul<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(),</span>fwtOr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">),</span>PRINT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb1-73">    cpy<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span>fwtAnd<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">),</span>fwtAnd<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">),</span>mul<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(),</span>fwtAnd<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">),</span>PRINT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb1-74">    cpy<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span>fwtXor<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">),</span>fwtXor<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">),</span>mul<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(),</span>fwtXor<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>inv2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">),</span>PRINT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb1-75">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-76"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>


</section>
</section>

 ]]></description>
  <category>算法</category>
  <guid>https://blog.redforest.org.cn/posts/001a.html</guid>
  <pubDate>Sun, 16 Oct 2022 16:00:00 GMT</pubDate>
</item>
<item>
  <title>题解:Two Frogs</title>
  <link>https://blog.redforest.org.cn/posts/002f.html</link>
  <description><![CDATA[ 




<section id="题解-two-frogs" class="level1">
<h1>题解 : Two Frogs</h1>
<section id="题意" class="level2">
<h2 class="anchored" data-anchor-id="题意">1. 题意</h2>
<p>地址：https://ac.nowcoder.com/acm/contest/33194/B</p>
<section id="题目描述" class="level3">
<h3 class="anchored" data-anchor-id="题目描述">1.1 题目描述</h3>
<p>In the Lonely Mountain, there are a lot of treasure well protected by dwarfs. Later, one day, the last dragon Smaug came and sensed the treasure being. As known to all, dragons are always much too greedy for treasure. So definitely, the war between dwarfs and Smaug begins.</p>
<p>During the war, two goblins Alice and Bob are turned into frogs by Gandalf, The Grey. In front of them, there are nnn lotus leaves in a line. In order to break the spell, they must jump from the 1-st lotus leaf to the nnn-th lotus leaf. If the frog is now on the i-th lotus leaf instead of the n-th lotus leaf, it can jump to a lotus leaf in range <img src="https://latex.codecogs.com/png.latex?(i,i+a_i%5D">.</p>
<p>Goblins are lack of intelligence and it’s also true even after turned into frogs. So Alice and Bob will jump randomly, which means, they will separately pick an available lotus leaf in every jump uniformly at random.</p>
<p>Since Alice and Bob have already being playing games for decades, so they want to know the probability that they jump to the nnn-th lotus leaf with the same count of jumps.</p>
</section>
<section id="输入描述" class="level3">
<h3 class="anchored" data-anchor-id="输入描述">1.2 输入描述:</h3>
<p>The first line contains an integer n (2≤n≤8 000), denoting the number of lotus leaf.</p>
<p>The second line contains n−1 integers <img src="https://latex.codecogs.com/png.latex?a_1,a_2">,…,<img src="https://latex.codecogs.com/png.latex?a_%7Bn%E2%88%921%7D">, where the i-th integer (1≤<img src="https://latex.codecogs.com/png.latex?a_i">≤n−i) indicates the range of lotus leaves that can be reached from the i-th lotus leaf.</p>
</section>
<section id="输出描述" class="level3">
<h3 class="anchored" data-anchor-id="输出描述">1.3输出描述:</h3>
<p>Output a line containing a single integer, indicating the probability that Alive and Bob jump to nnn-th lotus leaf with the same count of jumps, taken modulo 998,244,353.</p>
<p>Formally speaking, let the result, which is a rational number, be <img src="https://latex.codecogs.com/png.latex?%5Cfrac%7Bx%7D%7By%7D"> as an irreducible fraction, you need to output x⋅y−1 mod 998 244 353, where y−1 is a number such that <img src="https://latex.codecogs.com/png.latex?y%20%5Ccdot%20y%5E%7B-1%7D%20%5Cequiv%201%20%5Cpmod%7B998%5C,244%5C,353%7D">. You may safely assume that such <img src="https://latex.codecogs.com/png.latex?y%5E%7B-1%7D"> always exists.</p>
</section>
</section>
<section id="思路" class="level2">
<h2 class="anchored" data-anchor-id="思路">2.思路</h2>
<section id="状态设计与转移方程" class="level3">
<h3 class="anchored" data-anchor-id="状态设计与转移方程">2.1 状态设计与转移方程</h3>
<p>显而易见的我们可以得到一个 o(<img src="https://latex.codecogs.com/png.latex?n%5E3">) 的状态设计和转移方程。令<img src="https://latex.codecogs.com/png.latex?f%5Bi%5D%5Bj%5D">表示跳了i次恰好跳到j这个位置的概率。所求答案是 <img src="https://latex.codecogs.com/png.latex?%0A%5Csum_%7Bi=1%7D%5E%7Bn%7Df%5Bi%5D%5Bn%5D%5E2%0A"> 显而易见的转移方程是： <img src="https://latex.codecogs.com/png.latex?%0Af%5Bi+1%5D%5Bj+k%5D=f%5Bi%5D%5Bj%5D%5Ctimes%5Cfrac%7B1%7D%7Ba%5Bj%5D%7D,k%5Cin%20%5B1,a%5Bj%5D%5D%0A"> 在这个过程中注意取模，<img src="https://latex.codecogs.com/png.latex?%5Cfrac%7B1%7D%7Ba%5Bj%5D%7D"> 要变成 inv(a[j])。这一部分可以线性的预处理出来</p>
</section>
<section id="优化" class="level3">
<h3 class="anchored" data-anchor-id="优化">2.2 优化</h3>
<p>我们不能接受o(<img src="https://latex.codecogs.com/png.latex?n%5E3">) 的时间复杂度与o(<img src="https://latex.codecogs.com/png.latex?n%5E2">)的空间复杂度。在方程中枚举k是为了给后面的区间全部加上一个值。我们可以容易的想到使用前缀和进行优化。</p>
<p><del>当然还可以更轻易地想到使用滚动数组优化以后再使用树状数组或线段树一类的数据结构维护区间信息，当然这样会t，别问怎么知道的</del></p>
<p>只需要给需要区间加的区间的左端点加上要加的值，在右端点减去这个值。求前缀和的时候就完成了区间加的操作。</p>
<p>另外需要使用滚动数组优化空间复杂度。</p>
</section>
</section>
<section id="代码" class="level2">
<h2 class="anchored" data-anchor-id="代码">3.代码</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode cpp code-with-copy"><code class="sourceCode cpp"><span id="cb1-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;iostream&gt;</span></span>
<span id="cb1-2"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;cstdio&gt;</span></span>
<span id="cb1-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">using</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">namespace</span> std<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">8e3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">998244353</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-5"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span>sum<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span>inv<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">template</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">typename</span> _TP<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb1-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">inline</span> _TP qread<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>_TP <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-8">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-9">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(!</span>isdigit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)){</span>w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'-'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>getchar<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();}</span></span>
<span id="cb1-10">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>isdigit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)){</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=(</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)+(</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)+(</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">48</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>getchar<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();}</span></span>
<span id="cb1-11">    X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?-</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-12">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-13"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-14"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> main<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(){</span></span>
<span id="cb1-15">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-16">    qread<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-17">    inv<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-18">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-19">        inv<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=((</span>P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)*</span>inv<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">])%</span>P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-20">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-21">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-22">        qread<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]);</span></span>
<span id="cb1-23">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-24">    f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-25">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> ans<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-26">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-27">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-28">            sum<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]+=</span>sum<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-29">            sum<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]%=</span>P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-30">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-31">        f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=(</span>f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]+</span>sum<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">])%</span>P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-32">        ans<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=(</span>ans<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+(</span>f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]*</span>f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">])%</span>P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)%</span>P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-33">        f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>sum<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-34">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">--){</span></span>
<span id="cb1-35">            f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=(</span>f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]+</span>sum<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">])%</span>P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-36">            sum<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=(</span>sum<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]+(</span>f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]*</span>inv<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]])%</span>P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)%</span>P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-37">            sum<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=(</span>sum<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]-(</span>f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]*</span>inv<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]])%</span>P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)%</span>P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-38">            f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>sum<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-39">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-40">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-41">    cout<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span>ans<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-42">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-43"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>


</section>
</section>

 ]]></description>
  <category>算法</category>
  <guid>https://blog.redforest.org.cn/posts/002f.html</guid>
  <pubDate>Tue, 16 Aug 2022 16:00:00 GMT</pubDate>
</item>
<item>
  <title>题解:Fabled Rooks</title>
  <link>https://blog.redforest.org.cn/posts/002c.html</link>
  <description><![CDATA[ 




<section id="题解-fabled-rooks" class="level1">
<h1>题解 : Fabled Rooks</h1>
<section id="题意" class="level2">
<h2 class="anchored" data-anchor-id="题意">1.题意</h2>
<p>地址：https://vjudge.net/problem/UVA-11134</p>
<p>We would like to place n rooks, 1 ≤ n ≤ 5000, on a n × n board subject to the following restrictions • The i-th rook can only be placed within the rectangle given by its left-upper corner (xl , yl) and its rightlower corner (xr , yr), where 1 ≤ i ≤ n, 1 ≤ xl ≤ xr ≤ n , 1 ≤ yl ≤ yr ≤ n.&nbsp;• No two rooks can attack each other, that is no two rooks can occupy the same column or the same row.</p>
<p>大概意思是，把n个棋子放在一个n*n的棋盘里，求任意一种方案。每个棋子有一个题目给出的位置区间xl,yl,xr,yr</p>
</section>
<section id="思路" class="level2">
<h2 class="anchored" data-anchor-id="思路">2.思路</h2>
<p>很显然，对于每一个棋子行和列的决策互不干扰，对于行或列，只要没有任意两个或以上棋子在同一行/列，方案合法，所以我们可以对于每一个棋子分别计算行和列的位置。</p>
<p>目前目前看到了三种方法：</p>
<p>假设我们现在在处理每个棋子的行位置</p>
<ul>
<li>1.以每个棋子的限制区间的右端点从小到大排序，显然右端点越小越应该先给它分位置。然后放在这个棋子限制区间地最靠左的空位上。时间复杂度是o(n^2)，索性这个题n最大只有5000，我开始时想到使用类似 lowbit 函数的方法把每个位置的使用情况放在bitset里面 这样查询某个棋子限制区间地最靠左的空位的时候就会变成o(1)，但是仔细想想这个过程仍然是o(n)的。</li>
<li>2.考虑把每个位置分配给每个区间，先把所有的限制区间按照右端点从小到大排序，然后从左到右遍历每个位置，将区间按顺序放进堆里，堆按照每个区间的左端点排序，一旦有区间的左端点&lt;=当前位置，就把当前位置分配给该区间。对于这种方法的正确性其实和第一种方法差不多。在一定意义上可以算是对第一种方法的优化。时间复杂度o(nlogn)，十分优秀</li>
<li>3.网络流</li>
</ul>
<p>当然还是有一些细节需要处理的，比如说把算的时候要记录每个点的序号，不然排序以后就乱了。最后输出答案以前记得按序号排回去</p>
</section>
<section id="代码" class="level2">
<h2 class="anchored" data-anchor-id="代码">3.代码：</h2>
<p>提供第一种方法的代码，因为懒</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode cpp code-with-copy"><code class="sourceCode cpp"><span id="cb1-1"> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;iostream&gt;</span></span>
<span id="cb1-2"> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;cstdio&gt;</span></span>
<span id="cb1-3"> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;algorithm&gt;</span></span>
<span id="cb1-4"> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;cstring&gt;</span></span>
<span id="cb1-5"> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">using</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">namespace</span> std<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-6"> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> MAXN <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-7"> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">template</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">typename</span> _TP<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb1-8"> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">inline</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> qread<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>_TP <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-9">     <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-10">     <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(!</span>isdigit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)){</span>w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'-'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>getchar<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();}</span></span>
<span id="cb1-11">     <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>isdigit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)){</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=(</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)+(</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)+(</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">48</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>getchar<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();}</span></span>
<span id="cb1-12">     X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?-</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> </span>
<span id="cb1-13"> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-14"> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> vis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-15"> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">struct</span> node<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-16">     <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> num<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>l<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-17"> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span>ax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span>ay<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-18"> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">struct</span> ansNode<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-19">     <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> num<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>val<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-20"> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span>ansx<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span>ansy<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-21"> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">inline</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">bool</span> cmpAns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> ansNode <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> ansNode <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-22">     <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>num<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>num<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-23"> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-24"> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">inline</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">bool</span> cmp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> node <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> node <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-25">     <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-26"> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span> </span>
<span id="cb1-27"> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">inline</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> INIT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(){</span></span>
<span id="cb1-28">     memset<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>vis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">sizeof</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>vis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">));</span></span>
<span id="cb1-29"> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-30"> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> solve<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-31">     INIT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb1-32">     <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> xl<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>yl<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>xr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>yr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-33">     <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-34">         qread<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>xl<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span>qread<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>yl<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span>qread<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>xr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span>qread<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>yr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-35">         ax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=(</span>node<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>xl<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>xr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">};</span></span>
<span id="cb1-36">         ay<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=(</span>node<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>yl<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>yr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">};</span></span>
<span id="cb1-37">     <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-38">     sort<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>ax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>cmp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-39">     <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-40">         ansx<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>num<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>num<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-41">         <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> flag<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-42">         <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>l<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>ax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-43">             <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(!</span>vis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]){</span></span>
<span id="cb1-44">                 vis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-45">                 ansx<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>val<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-46">                 flag<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-47">                 <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">break</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-48">             <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-49">         <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-50">         <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(!</span>flag<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-51">             cout<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"IMPOSSIBLE</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-52">             <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-53">         <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-54">     <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-55">     sort<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ay<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>ay<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>cmp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-56">     memset<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>vis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">sizeof</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>vis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">));</span></span>
<span id="cb1-57">     <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-58">         ansy<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>num<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ay<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>num<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-59">         <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> flag<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-60">         <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ay<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>l<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>ay<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-61">             <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(!</span>vis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]){</span></span>
<span id="cb1-62">                 vis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-63">                 ansy<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>val<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-64">                 flag<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-65">                 <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">break</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-66">             <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-67">         <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-68">         <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(!</span>flag<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-69">             cout<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"IMPOSSIBLE</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-70">             <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-71">         <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-72">     <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-73">     sort<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ansx<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>ansx<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>cmpAns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-74">     sort<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ansy<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>ansy<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>cmpAns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-75">     <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-76">         cout<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span>ansx<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>val<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">' '</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span>ansy<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>val<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-77">     <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-78"> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-79"> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> main<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(){</span></span>
<span id="cb1-80">     <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-81">     <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-82">         qread<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-83">         <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(!</span>t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">break</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-84">         solve<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-85">     <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-86">     <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-87"> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-88"> </span></code></pre></div></div>


</section>
</section>

 ]]></description>
  <category>算法</category>
  <guid>https://blog.redforest.org.cn/posts/002c.html</guid>
  <pubDate>Mon, 11 Apr 2022 16:00:00 GMT</pubDate>
</item>
<item>
  <title>termux+xfce4+vnc下导致性能骤降的一个小坑</title>
  <link>https://blog.redforest.org.cn/posts/002d.html</link>
  <description><![CDATA[ 




<section id="termuxxfce4vnc下导致性能骤降的一个小坑" class="level1">
<h1>termux+xfce4+vnc下导致性能骤降的一个小坑</h1>
<p>遇到的问题是： 当时用上述环境时，第一次进入系统性能还看得过去。 当经过多次的start &amp; kill 以后，性能下降严重。 原因在于：<img src="https://img-blog.csdnimg.cn/a786929be18549d49757063090647c25.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAX19SZWRGb3Jlc3Q=,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center.png" class="img-fluid" alt="在这里插入图片描述"> 解决方法在于： 取消”save session for future logins”</p>


</section>

 ]]></description>
  <category>环境配置</category>
  <guid>https://blog.redforest.org.cn/posts/002d.html</guid>
  <pubDate>Thu, 07 Apr 2022 16:00:00 GMT</pubDate>
</item>
<item>
  <title>题解:So you want to be a 2n-aire</title>
  <link>https://blog.redforest.org.cn/posts/0025.html</link>
  <description><![CDATA[ 




<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Content note — Needs verification · 待核验
</div>
</div>
<div class="callout-body-container callout-body">
<p>期望 DP 的最终代码可能可用，但推导文字里对分段积分和断点的说明不够严谨。</p>
<ul>
<li>断点应来自 <code>2^i = p * d[i+1]</code>，不是两段积分相等。</li>
<li>建议补上除以区间长度 <code>1 - t</code> 的期望归一化推导。</li>
</ul>
</div>
</div>
<section id="题解so-you-want-to-be-a-2n-aire" class="level1">
<h1>题解:So you want to be a 2n-aire</h1>
<section id="题意" class="level2">
<h2 class="anchored" data-anchor-id="题意">1.题意</h2>
<p>地址:https://vjudge.net/problem/UVA-10900</p>
<p>The player starts with a prize of $1, and is asked a sequence of n questions. For each question, he may • quit and keep his prize. • answer the question. If wrong, he quits with nothing. If correct, the prize is doubled, and he continues with the next question. After the last question, he quits with his prize. The player wants to maximize his expected prize. Once each question is asked, the player is able to assess the probability p that he will be able to answer it. For each question, we assume that p is a random variableniformly distributed over the range t..1.</p>
<p>大致意思是：参加有奖竞赛，初始有1$，之后每答对一题奖金翻倍，对于每个题答对的概率为[t,1]，如果答对，奖金翻倍，答错当场退赛且没有奖金，放弃答题则拿走已经得到的奖金退赛。求玩家在最优策略下奖金期望的最大值</p>
</section>
<section id="解法" class="level2">
<h2 class="anchored" data-anchor-id="解法">2.解法</h2>
<p>这个题最大的问题就在于答对的概率p不是一个定值。</p>
<p>我们不妨先看看p一定的情况。</p>
<p>设 d[i] 为已经答对了i道题的情况下，这场比赛在最优策略下的期望奖金，很显然答案为 d[0]</p>
<p>那么转移方程为 <img src="https://latex.codecogs.com/png.latex?d%5Bi%5D=max(2%5Ei%20,%20p%5Ctimes%20d%5Bi+1%5D)"> 也就是放弃答题和继续答题两种策略取最大值</p>
<p>那么如果概率不是一个定值应该怎么办</p>
<p>根据连续概率</p>
<p><img src="https://latex.codecogs.com/png.latex?d_i%20=%20%5Cint_%7Bt%7D%5E%7B1%7Dmax(2%5Ei%20,%20p%20%5Ctimes%20d%5Bi+1%5D)%20%5C,dp"></p>
<p>很显然，这是个分段函数，所以积分的结果等于两段函数分别的积分的和</p>
<p><img src="https://latex.codecogs.com/png.latex?F(p)%20=%20max(2%5Ei%20,%20p%20%5Ctimes%20d_%7Bi+1%7D)%20%5CLeftrightarrow%20%5Cbegin%7Bcases%7D%7B%5Cint_t%5E%7Bp_0%7D2%5Ei%20%5C,%20dp%7D%20&amp;%20p%5Cleqslant%20p%5E0%5C%20%20%7B%5Cint_%7Bp_0%7D%5E1d%5Bi+1%5D%5C,dp%7D%20&amp;p%5E0%3Cp%3C1%5Cend%7Bcases%7D"></p>
<p>当 <img src="https://latex.codecogs.com/png.latex?p=p_0"> 时，<img src="https://latex.codecogs.com/png.latex?%7B%5Cint_%7Bp_0%7D%5E1d%5Bi+1%5D%5C,dp%7D%20=%20%7B%5Cint_t%5E%7Bp_0%7D2%5Ei%20%5C,%20dp%7D"></p>
<p>求解之后</p>
<p><img src="https://latex.codecogs.com/png.latex?d%5Bi%5D%20=%202%5Ei%20%5Ctimes%20%5Cfrac%7Bp_0-t%7D%7B1-t%7D%20+%20%5Cfrac%7B1+p_0%7D%7B2%7D%20%5Ctimes%20d%5Bi+1%5D%20%5Ctimes%20(1-%5Cfrac%7Bp_0-t%7D%7B1-t%7D)"></p>
<p><strong>我认为这个题的难点在于dp方程中状态的设计</strong></p>
</section>
<section id="代码" class="level2">
<h2 class="anchored" data-anchor-id="代码">3.代码</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode cpp code-with-copy"><code class="sourceCode cpp"><span id="cb1-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;iostream&gt;</span></span>
<span id="cb1-2"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;cstdio&gt;</span></span>
<span id="cb1-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">using</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">namespace</span> std<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> MAXN <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5e1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-5"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">double</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">template</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">typename</span> _TP<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb1-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">inline</span> _TP qread<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>_TP <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-8">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-9">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(!</span>isdigit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)){</span>w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'-'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>getchar<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();}</span></span>
<span id="cb1-10">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>isdigit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)){</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=(</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)+(</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)+(</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">48</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>getchar<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();}</span></span>
<span id="cb1-11">    X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?-</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-12">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-13"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-14"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">inline</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">double</span> Max<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">double</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">double</span> b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-15">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> </span>
<span id="cb1-16"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span> </span>
<span id="cb1-17"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> solve<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">double</span> t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-18">    d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-19">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">--){</span></span>
<span id="cb1-20">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">double</span> p0<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>Max<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">double</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)/</span>d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]);</span></span>
<span id="cb1-21">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">double</span> p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=(</span>p0<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)/(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-22">        d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)*</span>p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>p0<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]*(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span>    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//积分 </span></span>
<span id="cb1-23">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-24">    printf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.3lf\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]);</span></span>
<span id="cb1-25"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-26"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> main<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(){</span></span>
<span id="cb1-27">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-28">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">double</span> t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-29">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-30">        scanf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%d%lf</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,&amp;</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,&amp;</span>t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span> </span>
<span id="cb1-31">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">break</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-32">        solve<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span> </span>
<span id="cb1-33">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-34">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-35"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span> </span></code></pre></div></div>


</section>
</section>

 ]]></description>
  <category>算法</category>
  <guid>https://blog.redforest.org.cn/posts/0025.html</guid>
  <pubDate>Thu, 07 Apr 2022 16:00:00 GMT</pubDate>
</item>
<item>
  <title>题解:Xor Sum 2</title>
  <link>https://blog.redforest.org.cn/posts/0011.html</link>
  <description><![CDATA[ 




<div class="callout callout-style-default callout-caution callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Caution</span>Content note — Known errors · 明显错误
</div>
</div>
<div class="callout-body-container callout-body">
<p>Xor Sum 2 的核心性质描述有误，代码思路和文字说法不一致。</p>
<ul>
<li>多个数“全部按位与为 0”不足以推出异或和等于普通和，例如 1、1、2 的整体 AND 为 0 但条件不成立。</li>
<li>正确条件应是当前集合各二进制位至多出现一次；代码用 <code>res &amp; a[q]</code> 检查冲突更接近正确做法。</li>
</ul>
</div>
</div>
<section id="题解-xor-sum-2" class="level1">
<h1>题解 : Xor Sum 2</h1>
<section id="题意" class="level2">
<h2 class="anchored" data-anchor-id="题意">1.题意</h2>
<p>地址：https://vjudge.net/problem/AtCoder-arc098_b</p>
<section id="problem-statement" class="level3">
<h3 class="anchored" data-anchor-id="problem-statement">1.1 Problem Statement</h3>
<p>There is an integer sequence A of length N.</p>
<p>Find the number of the pairs of integers <em>l</em> and r (<img src="https://latex.codecogs.com/png.latex?1%20%5Cleq%20l%20%5Cleq%20r%20%5Cleq%20N">) that satisfy the following condition:</p>
<ul>
<li><img src="https://latex.codecogs.com/png.latex?A_l%20xor%20A_%7Bl+1%7D%20xor%20...%20xor%20A_r=A_l%20+%20A_%7Bl+1%7D%20+%20...%20+%20A_r"></li>
</ul>
<p>Here, xor denotes the bitwise exclusive OR.</p>
</section>
<section id="constraints" class="level3">
<h3 class="anchored" data-anchor-id="constraints">1.2 Constraints</h3>
<ul>
<li><img src="https://latex.codecogs.com/png.latex?1%20%5Cleq%20N%20%5Cleq%202%20%5Ctimes%2010%5E5"></li>
<li><img src="https://latex.codecogs.com/png.latex?0%20%5Cleq%20A_i%20%3C%202%5E%7B20%7D"></li>
<li>All values in input are integers.</li>
</ul>
<p>大致意思就是一个序列问它有多少个连续的子序列满足这个子序列的异或和等于这个子序列的和</p>
</section>
</section>
<section id="思路" class="level2">
<h2 class="anchored" data-anchor-id="思路">2.思路</h2>
<p>异或和这种东西，往往出了就要找一点小性质，但一般有不会很难。</p>
<p><strong>性质</strong></p>
<p>这个题首先异或和等于和，考虑二进制下的加法，如果异或和等于和的话，那么这些数的任意一个相同的位都不会同事出现1个以上个1。</p>
<p>比如说8 4 1这三个数</p>
<p>二进制下：</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th>1</th>
<th>0</th>
<th>0</th>
<th>0</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td></td>
<td>1</td>
<td>0</td>
<td>0</td>
</tr>
<tr class="even">
<td></td>
<td></td>
<td></td>
<td>1</td>
</tr>
</tbody>
</table>
<p>这三个数的任意相同位上都没有超过一个1。他们的和等于异或和。</p>
<p>判断方法很简单，只要把它们全都 &amp; 一下如果结果不是0就一定有某些位出现了一个以上的1</p>
<p>所以对于一个连续子串，如果把他们全都&amp;起来答案为0那么他们的异或和等于和</p>
<p><strong>优化</strong></p>
<p>很显然<img src="https://latex.codecogs.com/png.latex?n%5E2">过不了1e5所以要优化。</p>
<p>可以尺取</p>
<p>因为很显然以a[i]为开头的连续子串的结尾位置j一定小于等于以a[i+1]为开头的子串的结尾位置。因为少&amp;一个数总不可能加剧&amp;出非0数</p>
</section>
<section id="代码" class="level2">
<h2 class="anchored" data-anchor-id="代码">3：代码</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode cpp code-with-copy"><code class="sourceCode cpp"><span id="cb1-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;iostream&gt;</span></span>
<span id="cb1-2"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;cstdio&gt;</span></span>
<span id="cb1-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">using</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">namespace</span> std<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> MAXN <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2e5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-5"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">template</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">typename</span> _TP<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb1-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">inline</span> _TP qread<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>_TP <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-8">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-9">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(!</span>isdigit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)){</span>w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'-'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>getchar<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();}</span></span>
<span id="cb1-10">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>isdigit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)){</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=(</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)+(</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)+(</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">48</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>getchar<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();}</span></span>
<span id="cb1-11">    X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?-</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-12">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-13"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-14"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">inline</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> Max<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-15">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-16"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-17"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> main<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(){</span></span>
<span id="cb1-18">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-19">    qread<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-20">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-21">        qread<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]);</span></span>
<span id="cb1-22">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-23">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> res <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>p <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>q <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-24">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> ans <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-25">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-26">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>q<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>q<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-27">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(!(</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>q<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]&amp;</span>res<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> q<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-28">            res<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^=</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>q<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++];</span></span>
<span id="cb1-29">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-30">        ans<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span>q<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-31">        res<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^=</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-32">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(++</span>p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">break</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-33">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-34">    cout<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span>ans<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-35">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-36"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>


</section>
</section>

 ]]></description>
  <category>算法</category>
  <guid>https://blog.redforest.org.cn/posts/0011.html</guid>
  <pubDate>Mon, 07 Feb 2022 16:00:00 GMT</pubDate>
</item>
<item>
  <title>题解:消失之物</title>
  <link>https://blog.redforest.org.cn/posts/0020.html</link>
  <description><![CDATA[ 




<div class="callout callout-style-default callout-caution callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Caution</span>Content note — Known errors · 明显错误
</div>
</div>
<div class="callout-body-container callout-body">
<p>消失之物的文字转移方程边界写错，和后面的代码不一致。</p>
<ul>
<li>文中写 <code>j-v[i] &gt; 0</code>，代码和背包语义应处理 <code>j &gt;= v[i]</code>。</li>
<li>公式里的 <code>g[i-1][j-v[i]]</code> 与滚动数组推导不一致，建议重新推导。</li>
</ul>
</div>
</div>
<section id="题解-消失之物" class="level1">
<h1>题解 : 消失之物</h1>
<section id="题意" class="level2">
<h2 class="anchored" data-anchor-id="题意">1.题意:</h2>
<p>地址：https://www.luogu.com.cn/problem/P4141</p>
<p>ftiasch 有 n 个物品, 体积分别是 <img src="https://latex.codecogs.com/png.latex?w_1,w_2,%5Cdots,w_n">。由于她的疏忽，第 i个物品丢失了。</p>
<p>“要使用剩下的 n-1 物品装满容积为 x 的背包，有几种方法呢？”——这是经典的问题了。</p>
<p>她把答案记为 <img src="https://latex.codecogs.com/png.latex?%5Ctext%7Bcnt%7D(i,x)"> ，想要得到所有<img src="https://latex.codecogs.com/png.latex?i%20%5Cin%20%5B1,n%5D,%20x%20%5Cin%20%5B1,m%5D"> 的 <img src="https://latex.codecogs.com/png.latex?%5Ctext%7Bcnt%7D(i,x)%5C%2510"> 表格。</p>
</section>
<section id="思路" class="level2">
<h2 class="anchored" data-anchor-id="思路">2.思路：</h2>
<p>很显然，可以轻松地求出使用所有物品装满容积 j 的方案数</p>
<p><img src="https://latex.codecogs.com/png.latex?f%5Bi%5D%5Bj%5D">表示使用前 i 个物品的容积为 j 的背包方案数</p>
<p>令<img src="https://latex.codecogs.com/png.latex?g%5Bi%5D%5Bj%5D">表示不使用第 i 个物品的容积为 j 的背包方案数</p>
<p><strong>·</strong> 1.当<img src="https://latex.codecogs.com/png.latex?j-v%5Bi%5D%20%3E%200"> 的时候，<img src="https://latex.codecogs.com/png.latex?f%5Bi%5D%5Bj%5D"> 必然会包含选取了第 i 个物品所构成的一些方案，且在这些情况中，第 i 个物品必然只存在一个。那么，在$f[i][j] <img src="https://latex.codecogs.com/png.latex?%E4%B8%AD%E7%9A%84%E8%BF%99%E4%BA%9B%E7%8A%B6%E6%80%81%E5%BF%85%E7%84%B6%E6%98%AF%E4%BB%8E">g[i][j-v[i]]$(不选i装满容积j-v[i])的状态下再拿了一个 i 构成的。</p>
<p><strong>·</strong> 2.当<img src="https://latex.codecogs.com/png.latex?j-v%5Bi%5D%5Cleq0">的时候，<img src="https://latex.codecogs.com/png.latex?f%5Bi%5D%5Bj%5D"> 中本来就不包含选取 i 这个物品的情况所以直接就是<img src="https://latex.codecogs.com/png.latex?f%5Bi%5D%5Bj%5D"> 。</p>
<p>转移方程为： <img src="https://latex.codecogs.com/png.latex?%0AH(f)=%5Cleft%5C%7B%5Cbegin%7Bmatrix%7D%20g%5Bi%5D%5Bj%5D=f%5Bn%5D%5Bj%5D-g%5Bi-1%5D%5Bj-v%5Bi%5D%5D%EF%BC%8Cj-v%5Bi%5D%3E0%5C%5C%0Af%5Bi%5D%5Bj%5D%EF%BC%8Cj-v%5Bi%5D%5Cleq0%20%5Cend%7Bmatrix%7D%5Cright.%0A"> 最后加上滚动数组优化就好了</p>
</section>
<section id="代码" class="level2">
<h2 class="anchored" data-anchor-id="代码">3.代码：</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode cpp code-with-copy"><code class="sourceCode cpp"><span id="cb1-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;iostream&gt;</span></span>
<span id="cb1-2"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;cstdio&gt;</span></span>
<span id="cb1-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">using</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">namespace</span> std<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3e3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-5"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span>g<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span>v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">template</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">typename</span> _TP<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb1-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">inline</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> qread<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>_TP <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-8">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-9">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(!</span>isdigit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)){</span>w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'-'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>getchar<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();}</span></span>
<span id="cb1-10">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>isdigit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)){</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=(</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)+(</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)+(</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">48</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>getchar<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();}</span></span>
<span id="cb1-11">    X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?-</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-12"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-13"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> main<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(){</span></span>
<span id="cb1-14">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>m<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-15">    qread<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span>qread<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>m<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-16">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-17">        qread<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]);</span></span>
<span id="cb1-18">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-19">    f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>g<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-20">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-21">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>m<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span>v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">--){</span></span>
<span id="cb1-22">            f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=(</span>f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]+</span>f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]])%</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-23">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-24">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-25">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-26">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>m<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-27">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span>v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">])</span>g<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=(</span>f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]-</span>g<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]]+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)%</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-28">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> g<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=</span>f<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-29">            cout<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span>g<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-30">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-31">        cout<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-32">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-33">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-34"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span> </span></code></pre></div></div>


</section>
</section>

 ]]></description>
  <category>算法</category>
  <guid>https://blog.redforest.org.cn/posts/0020.html</guid>
  <pubDate>Fri, 21 Jan 2022 16:00:00 GMT</pubDate>
</item>
<item>
  <title>题解:Incremental Induction</title>
  <link>https://blog.redforest.org.cn/posts/0022.html</link>
  <description><![CDATA[ 




<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Content note — Needs verification · 待核验
</div>
</div>
<div class="callout-body-container callout-body">
<p>Incremental Induction 中“更强者先领奖”的直觉和代码排序方向不一致。</p>
<ul>
<li>代码按出度升序排序，但文字写“实力更强的人先去领奖”。</li>
<li>建议明确图边方向、出度含义，以及公式中 masters/noobs 的边计数方向。</li>
</ul>
</div>
</div>
<section id="题解incremental-induction" class="level1">
<h1>题解:Incremental Induction</h1>
<p><strong>这题的难点在于</strong> + 1.阅读理解 + 2.统计答案时的思路</p>
<section id="题意" class="level2">
<h2 class="anchored" data-anchor-id="题意">1.题意</h2>
<p>地址:https://vjudge.net/problem/Kattis-incrementalinduction The Nordic Collegiate Pong Championship (NCPC) is an insanely competive tournament where every contestant plays exactly one game of Pong against every other contestant. The last game of the tournament just finished, so only one item now remains on the programme: the traditional diploma ceremony, where all this year’s participants get inducted into the NCPC Hall of Fame. According to the ancient customs, contestants who have not been inducted into the Hall of Fame yet (the pathetic nobodies) must stay on the left side of the stage, whereas contestants who have been inducted (the awesome legends) must be on the right side of the stage. Then, when a contestant is receiving their diploma, they will symbolically walk from the left to the right side of the stage and thus become an awesome legend. Only one contestant is inducted into the Hall of Fame at a time, and every contestant starts on the left side initially.</p>
<p>The NCPC Head of Jury believes it reflects badly on her if too many of the awesome legends on the right have lost matches against pathetic nobodies on the left, but she quickly realizes that it might be impossible to avoid this at every point in time during the diploma ceremony. However, she certainly wants to keep such atrocities at a minimum. Specifically, she wants to find the smallest number k for which there exists an order of handing out diplomas to the contestants, such that at no point there were more than k games played where an awesome legend lost against a pathetic nobody.</p>
<p>大致意思是：有n（n&lt;=5000）个人需要去领奖，已经领奖的人就会变成大师，没有领奖的人就是菜鸡。每两人之间都会有对应的强弱关系。每当有一个人要去领奖时，对于每一对菜鸡和大师（场上所有人组成的关系而不是当前领奖者和所有大师组成的关系），k为当前情况下菜鸡战胜大师的对数。要求你求出一个合适的领奖顺序，使得k最小。输出最小的k值。</p>
<p>输入输出阅读原题</p>
</section>
<section id="解法" class="level2">
<h2 class="anchored" data-anchor-id="解法">2.解法</h2>
<p>很显然，我们应该让实力更强的人先去领奖。</p>
<p>在这个原则下，很显然的我们需要<img src="https://latex.codecogs.com/png.latex?n%5E2">的时间统计当前所有的菜鸡打败大师的数量，总的时间复杂度就是<img src="https://latex.codecogs.com/png.latex?n%5E3">,显然会TLE。对于每一对强弱关系，比如i强于j，则i向j连一条边。对于每个点的出度排序（入度也可，不过后面就要反着了）。</p>
<p>对于一个状态，它的k值就是noobs集合到masters集合的边的总数 <img src="https://latex.codecogs.com/png.latex?%5Csum_j%5E%7Bj%5Cin%20masters%7D%7Bd%5Bj%5D%7D-%5Cfrac%20%7Bi%20%5Ctimes%20(i-1)%7D2"></p>
<p>因为每两个人之间都会有强弱关系，所以一这张图是一张完全图。所有所以两个集合之间的边数等于masters集合的出度数-master集合内部的所有边的数量，即为上式。 o(n)求解即可</p>
</section>
<section id="代码" class="level2">
<h2 class="anchored" data-anchor-id="代码">3.代码</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode cpp code-with-copy"><code class="sourceCode cpp"><span id="cb1-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;iostream&gt;</span></span>
<span id="cb1-2"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;cstdio&gt;</span></span>
<span id="cb1-3"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;algorithm&gt;</span></span>
<span id="cb1-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">using</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">namespace</span> std<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> MAXN <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5e3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-6"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">][</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span>vis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">struct</span> node<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-8">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>num<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-9"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span>d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-10"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">inline</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">bool</span> cmp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> node a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> node b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-11">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-12"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-13"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">inline</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> Max<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-14">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-15"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-16"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> main<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(){</span></span>
<span id="cb1-17">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-18">    scanf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%d</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,&amp;</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span>getchar<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb1-19">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-20">        d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>num<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-21">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-22">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-23">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-24">            a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">][</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=</span>getchar<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">()-</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'0'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-25">            a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">][</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">][</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-26">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">][</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">])</span>d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++;</span></span>
<span id="cb1-27">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++;</span></span>
<span id="cb1-28">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-29">        getchar<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb1-30">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-31">    sort<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>cmp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-32">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> ans<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>res<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-33">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-34">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//vis[d[i].num]=1;</span></span>
<span id="cb1-35">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// for(int j=1;j&lt;=n;j++){</span></span>
<span id="cb1-36">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//     if(i == j) continue;</span></span>
<span id="cb1-37">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//     if(a[d[i].num][d[j].num] &amp;&amp; !vis[d[j].num])res++;</span></span>
<span id="cb1-38">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//     else if(a[d[j].num][d[i].num] &amp;&amp; vis[d[j].num])res--;   </span></span>
<span id="cb1-39">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// }</span></span>
<span id="cb1-40">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//另外n^2的方法是反着统计的，图上的更好理解</span></span>
<span id="cb1-41">        res<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span>d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-42">        ans<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>Max<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ans<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>res<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-(</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*(</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">))/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//有向完全图，所以右边内部的边数=(i*(i-1))/2</span></span>
<span id="cb1-43">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-44">    cout<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span>ans<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-45">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-46"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>


</section>
</section>

 ]]></description>
  <category>算法</category>
  <guid>https://blog.redforest.org.cn/posts/0022.html</guid>
  <pubDate>Mon, 20 Dec 2021 16:00:00 GMT</pubDate>
</item>
<item>
  <title>线性筛(欧拉筛)</title>
  <link>https://blog.redforest.org.cn/posts/0030.html</link>
  <description><![CDATA[ 




<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Content note — Needs verification · 待核验
</div>
</div>
<div class="callout-body-container callout-body">
<p>线性筛证明中的“最小因数/最小质因数”表述和部分变量关系写错。</p>
<ul>
<li>“i 一定是 p_j 的因数”应重新核对，语义上应是 <code>p_j</code> 整除 <code>i</code>。</li>
<li>代码使用 <code>MAXN = 1e8+7</code> 会带来非常高的静态内存占用，需结合题目内存限制验证。</li>
</ul>
</div>
</div>
<section id="线性筛欧拉筛" class="level1">
<h1>线性筛(欧拉筛)</h1>
<section id="关于欧拉筛" class="level2">
<h2 class="anchored" data-anchor-id="关于欧拉筛">1.关于欧拉筛</h2>
<p>用途：线性的求区间[1,n]的所有质数</p>
<p>事实上，优化以后的埃氏筛已经很优秀了，一般情况下，很难通过时间复杂度区分欧拉筛和埃氏筛。</p>
<p>所以我们为什么需要欧拉筛</p>
<ul>
<li>欧拉筛可以完成很多计数，比如欧拉函数</li>
<li>欧拉筛比较高级且常数略小一些</li>
</ul>
</section>
<section id="原理和过程" class="level2">
<h2 class="anchored" data-anchor-id="原理和过程">2.原理和过程</h2>
<p><strong>过程：</strong></p>
<p>欧拉筛的主要思想是，用已知的质数筛掉这个质数的倍数。且需要保证<code>对于任意一个合数，它会且只会它的最小因数筛掉</code></p>
<p>所以我们只需要枚举i = 2~n（主意1不是质数)在这个过程中维护已经发现的所有质数的集合。然后筛掉这些质数的i倍。一旦发现某个质数<img src="https://latex.codecogs.com/png.latex?p_j">是i的因数，则不去筛所有比 <img src="https://latex.codecogs.com/png.latex?p_j">大的质数的i倍 即：</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode cpp code-with-copy"><code class="sourceCode cpp"><span id="cb1-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(!(</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>prime<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]))</span><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">break</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span></code></pre></div></div>
<p><strong>正确性证明：</strong></p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cforall%20p_j%20%E5%A6%82%E6%9E%9C%20i">%<img src="https://latex.codecogs.com/png.latex?p_j%20=%200"> , 那么:</p>
<p>1.<img src="https://latex.codecogs.com/png.latex?p_j">一定是i的最小质因数。因为<img src="https://latex.codecogs.com/png.latex?p_j">是质数且，如果i还有比<img src="https://latex.codecogs.com/png.latex?p_i">更小的因子，由于p数组是有序的，所以会在更靠前的位置被枚举到。为了满足<code>对于任意一个合数，它会且只会它的最小因数筛掉</code>，所以小于等于<img src="https://latex.codecogs.com/png.latex?p_i">的质数都可以被使用。</p>
<p>2. i 一定是<img src="https://latex.codecogs.com/png.latex?p_j">的因数。对于所有大于<img src="https://latex.codecogs.com/png.latex?p_j">的质数，很显然<img src="https://latex.codecogs.com/png.latex?p_j">不是<img src="https://latex.codecogs.com/png.latex?p_j%5Ctimes%20i">的最小的因子，因为i里面一定有比<img src="https://latex.codecogs.com/png.latex?p_i">更小的因子。为了满足<code>对于任意一个合数，它会且只会它的最小因数筛掉</code>，所以不使用所有大于等于<img src="https://latex.codecogs.com/png.latex?p_j">的质数。</p>
</section>
<section id="代码" class="level2">
<h2 class="anchored" data-anchor-id="代码">3.代码</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode cpp code-with-copy"><code class="sourceCode cpp"><span id="cb2-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;iostream&gt;</span></span>
<span id="cb2-2"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;cstdio&gt;</span></span>
<span id="cb2-3"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;cstring&gt;</span></span>
<span id="cb2-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">using</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">namespace</span> std<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> MAXN <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e8</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-6"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">bool</span> isprime<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb2-7"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> prime<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb2-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">template</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">typename</span> _TP<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb2-9"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">inline</span> _TP read<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>_TP <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb2-10">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-11">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(!</span>isdigit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)){</span>w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'-'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>getchar<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();}</span></span>
<span id="cb2-12">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>isdigit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)){</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=(</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)+(</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)+(</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">48</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>getchar<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();}</span></span>
<span id="cb2-13">    X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?-</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-14">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-15"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-16"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> main<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(){</span></span>
<span id="cb2-17">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-18">    read<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span>read<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb2-19">    memset<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>isprime<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">sizeof</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>isprime<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">));</span></span>
<span id="cb2-20">    isprime<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-21">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> cnt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-22">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb2-23">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>isprime<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">])</span>prime<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[++</span>cnt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-24">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>cnt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>prime<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb2-25">            isprime<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>prime<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]]=</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">false</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-26">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(!(</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>prime<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]))</span><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">break</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-27">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-28">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-29">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb2-30">        read<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb2-31">        cout<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span>prime<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]&lt;&lt;</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-32">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-33">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-34"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>


</section>
</section>

 ]]></description>
  <category>算法</category>
  <guid>https://blog.redforest.org.cn/posts/0030.html</guid>
  <pubDate>Wed, 01 Dec 2021 16:00:00 GMT</pubDate>
</item>
<item>
  <title>题解:Space Rescuers&gt;&gt;模拟退火/二分</title>
  <link>https://blog.redforest.org.cn/posts/0029.html</link>
  <description><![CDATA[ 




<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Content note — Needs verification · 待核验
</div>
</div>
<div class="callout-body-container callout-body">
<p>Space Rescuers 的模拟退火说明更像启发式梯度移动，缺少收敛与正确性证明。</p>
<ul>
<li>文中已经标注二分方法正确性存疑，相关段落应继续保持警示。</li>
<li>如果要作为正式题解，建议补最小包围球的标准算法或可验证证明。</li>
</ul>
</div>
</div>
<section id="题解space-rescuers模拟退火二分" class="level1">
<h1>题解:Space Rescuers&gt;&gt;模拟退火/二分</h1>
<section id="题意" class="level2">
<h2 class="anchored" data-anchor-id="题意">1.题意：</h2>
<p>地址:https://vjudge.net/problem/CodeForces-106E The Galaxy contains n planets, there are many different living creatures inhabiting each planet. And each creature can get into troubles! Space rescuers know it perfectly well and they are always ready to help anyone who really needs help. All you need to do is call for them.<br>
Now the space rescuers plan to build the largest in the history of the Galaxy rescue station; however, the rescue station’s location is yet to be determined. As some cases are real emergencies, the rescuers want to find such a point in the Galaxy from which it would be possible to get to the remotest planet in the minimum possible time. In other words, the rescuers need such point in the space that the distance between it and the planet remotest from it was minimal (if we compare this point with all other possible points in the space). Unfortunately, the rescuers can’t sole this problem.<br>
As the planets are quite remote from each other, they can be considered as points in Euclidean three-dimensional space. The distance between points (<img src="https://latex.codecogs.com/png.latex?x_i,%E2%80%89y_i,%E2%80%89z_i">) and (<img src="https://latex.codecogs.com/png.latex?x_j,%E2%80%89y_j,%E2%80%89z_j">) can be calculated by the formula <img src="https://vj.ppsucxtt.cn/1d45477e0887afb7a2e114ec15669538?v=1630136117.png" class="img-fluid" alt="img">. The rescue station can be positioned in any point in the space. It can also coincide with some planet.<br>
Galaxy is in danger! Save the space rescuers and find the required point for them.</p>
<p><strong>Input</strong></p>
<p>The first line of the input file contains integer <em>n</em> — the number of planets (1 ≤ <em>N</em> ≤ 100). Each of the following <em>n</em> lines contains information about the planets. The i-th line contains three integers <img src="https://latex.codecogs.com/png.latex?x_i,%E2%80%89y_i,%E2%80%89z_i"> — the coordinates of the i-th planet ($ -10<sup>4 ≤ x_i, y_i, z_i ≤ 10</sup>4, 1 ≤ i ≤ n$). No two planets coincide.</p>
<p><strong>Output</strong></p>
<p>Print on the first line of the output file three space-separated real numbers <img src="https://latex.codecogs.com/png.latex?x_0,y_0,z_0"> — the coordinates for the future base. If there are several solutions, you are allowed to print any of them. The answer will be accepted if the distance from this point to the remotest planet will differ from the juries’ variant in no more than <img src="https://latex.codecogs.com/png.latex?10%E2%80%89%5E%7B-6%7D"> in absolute or relative value.</p>
</section>
<section id="思路" class="level2">
<h2 class="anchored" data-anchor-id="思路">2.思路：</h2>
<p><strong>本文提供两种做法，但只有一种做法写了程序并AC</strong></p>
<section id="方法一模拟退火" class="level3">
<h3 class="anchored" data-anchor-id="方法一模拟退火">2.1 方法一&gt;&gt;模拟退火</h3>
<p>这个题是非常简单的模拟退火，很适合入门学习。</p>
<p>首先，设<img src="https://latex.codecogs.com/png.latex?g(x,y,z)">为距离点 P<img src="https://latex.codecogs.com/png.latex?(x,y,z)">距离最远的点的距离，很容易证明<img src="https://latex.codecogs.com/png.latex?g(x,y,z)">在定义域内连续：考虑边界情况，当距离P最远的点发生改变时，一定会有两个点以上的点距离P的距离相等，一些是即将成为离P最远的点，另一些是之前距离P最远的点。那么在任何情况下，<img src="https://latex.codecogs.com/png.latex?g(x,y,z)">不会发生突变。</p>
<p>模拟退火的过程如下：我们不妨设置初始温度为T，T随着时间推移以<img src="https://latex.codecogs.com/png.latex?f(t%EF%BC%8Ctime)">减小，即：<img src="https://latex.codecogs.com/png.latex?T_i=f(time)"> 且 <img src="https://latex.codecogs.com/png.latex?f(0)=T_0">,注意，<img src="https://latex.codecogs.com/png.latex?f(time)">一般情况下为单调不增函数。</p>
<p>我们以T标定我们对于不一定是最优解的方案的选择概率。标定方法多种多样，对于本题，我们首先随意取一个点作为当前的答案点，对于当前，我们遍历所有的n个点，找出离当前点最远的一个点Q，将当前点向Q移动<img src="https://latex.codecogs.com/png.latex?T_i%20%5Ctimes%20dis(P,Q)">的距离。当T逐步减小的时候，答案逐步向正确答案靠近。</p>
<p>一种更容易的理解是：考虑一个粒子，在一个三维曲面上做热运动，由于一开始环境温度非常高，粒子每次沿着曲面梯度下降最快的方向运动，但由于粒子具有非常大的能量，所以它十分容易冲过头导致冲出当前的凹陷。这就是以一定的概率接受可能不是最优的解。当体系温度逐步降低的时候，粒子保持在能量较低处的概率是比较大的。所以一般模拟退火都会进行多次，以防止退出来一个较优解而不是最优解。但由于本题过于特殊（简单），所以我把初始位置设在重心，只退一次就能得到答案。同理，当初始位置选的足够好，三分法也可以解决本题（即直接对于初始位置梯度下降）。</p>
</section>
<section id="代码" class="level3">
<h3 class="anchored" data-anchor-id="代码">代码：</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode cpp code-with-copy"><code class="sourceCode cpp"><span id="cb1-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;iostream&gt;</span></span>
<span id="cb1-2"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;cstdio&gt;</span></span>
<span id="cb1-3"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;vector&gt;</span></span>
<span id="cb1-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">using</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">namespace</span> std<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> MAXN <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">double</span> EPS <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-8</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">template</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">typename</span> _TP<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb1-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">inline</span> _TP read<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>_TP <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-9">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-10">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(!</span>isdigit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)){</span>w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'-'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>getchar<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();}</span></span>
<span id="cb1-11">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>isdigit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)){</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=(</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)+(</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)+(</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">48</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>getchar<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();}</span></span>
<span id="cb1-12">    X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?-</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-13">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-14"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-15"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">struct</span> Point<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-16">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>z<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-17"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span>v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-18"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> main<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(){</span></span>
<span id="cb1-19">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">double</span> x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>z<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-20">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>minPos<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-21">    read<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-22">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-23">        Point tmp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-24">        read<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>tmp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span>read<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>tmp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span>read<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>tmp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>z<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-25">        v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=</span>tmp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-26">        x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span>tmp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span>tmp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>z<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span>tmp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>z<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-27">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-28">    x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>z<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//初始位置在重心 较优</span></span>
<span id="cb1-29">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> EPS<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-30">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">double</span> maxx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-31">        minPos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-32">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-33">            <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">double</span> t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)*(</span>x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)+(</span>y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)*(</span>y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)+(</span>z<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>z<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)*(</span>z<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>z<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-34">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> maxx<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-35">                maxx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-36">                minPos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-37">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-38">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-39">        T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.999</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-40">        x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span>T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*(</span>v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>minPos<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-41">        y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span>T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*(</span>v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>minPos<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-42">        z<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span>T<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*(</span>v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>minPos<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>z<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>z<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-43">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-44">    printf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.10f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.10f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%.10f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>z<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-45">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-46"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
</section>
<section id="方法2正确性存疑" class="level3">
<h3 class="anchored" data-anchor-id="方法2正确性存疑">2.2方法2(正确性存疑)</h3>
<p> 本方法是我在场上写的方法，即：二分答案点到离它最远的点的距离，注意在judge的时候进行判断，如果在此距离下发现了两个以上的点，那这种方案也不合法。由此得到了答案距离。</p>
<p> 接下来就是寻找答案点的位置。答案点一定由至少三个点确定，所以我们可以枚举三个点，计算答案点的位置。</p>


</section>
</section>
</section>

 ]]></description>
  <category>算法</category>
  <guid>https://blog.redforest.org.cn/posts/0029.html</guid>
  <pubDate>Mon, 30 Aug 2021 16:00:00 GMT</pubDate>
</item>
<item>
  <title>题解:Parabolic sorting</title>
  <link>https://blog.redforest.org.cn/posts/0010.html</link>
  <description><![CDATA[ 




<div class="callout callout-style-default callout-caution callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Caution</span>Content note — Known errors · 明显错误
</div>
</div>
<div class="callout-body-container callout-body">
<p>题解代码存在无法编译的变量名错误，文字证明也较依赖直觉。</p>
<ul>
<li>代码中使用了未定义的 <code>cPre</code>，应重新检查并给出可编译版本。</li>
<li>“每个数独立取正序对/逆序对最小值”需要更严谨的交换过程证明。</li>
</ul>
</div>
</div>
<section id="题解parabolic-sorting" class="level2">
<h2 class="anchored" data-anchor-id="题解parabolic-sorting">题解:Parabolic sorting</h2>
<p>地址:https://vjudge.net/problem/Gym-102780K</p>
</section>
<section id="题意" class="level2">
<h2 class="anchored" data-anchor-id="题意">1.题意：</h2>
<p>Professor R. is the greatest specialist in the field of discrete mathematics. He has already got bored with all the standard sorting algorithms, and has come up with a new algorithm — the parabolic array sorting algorithm.</p>
<p>Let there be an array of N different integer numbers ai. We want to sort it in such a way that the first part of the array strictly decreases, and the second part of the array strictly increases. In this case, we can only interchange the neighboring elements. Both the first and second parts of the array can be of zero length. The professor is interested in the following question: what is the minimum number of actions we need?</p>
<p><strong>Input</strong><br>
In the input N+1 there is a number, the first of which is N (1≤N≤105). Then follow the numbers ai, whose module does not exceed 109.</p>
<p><strong>Output</strong><br>
Print a single number — the answer to the problem.</p>
</section>
<section id="思路" class="level2">
<h2 class="anchored" data-anchor-id="思路">2.思路：</h2>
<p>目前网上找到两份题解，内容基本一致，但都讲的含糊不清。</p>
<p>考虑一个序列，我们先找到它最大的元素，它可以被放在两个位置，前半部分降序序列的尾部，或后半部分升序序列的前部。</p>
<p>我们所熟知的定理：只交换相邻的两数让序列变成升序，交换次数为逆序对数。</p>
<p>证明如下：考虑一个排列，先将其最大的数移至序列尾，次数为最大数与它后面的数组成的逆序对数。此时最后一位已定，再考虑前n-1个数的排列。</p>
<p>类比，如果要放在升序序列的尾部，那么它交换的最少次数为此序列中在a[i]前面且比a[i]小的数的数量。我们不妨称之为“正序对”</p>
<p>如果要放在降序序列的首部，那么它交换的最少次数为此序列中在a[i]后面且比a[i]小的数的数量。即为以a[i]为开头的逆序对数。</p>
<p>对于任意一个数，我们选择它的“正序对”与逆序对数最小的一个作为将它归位的步数即可。</p>
</section>
<section id="代码" class="level2">
<h2 class="anchored" data-anchor-id="代码">3.代码:</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode cpp code-with-copy"><code class="sourceCode cpp"><span id="cb1-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;iostream&gt;</span></span>
<span id="cb1-2"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;cstdio&gt;</span></span>
<span id="cb1-3"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;algorithm&gt;</span></span>
<span id="cb1-4"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;cmath&gt;</span></span>
<span id="cb1-5"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;cstring&gt;</span></span>
<span id="cb1-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">using</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">namespace</span> std<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> MAXN <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e6</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-8"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#define lowbit</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;"> </span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)&amp;(-</span>x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb1-9"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span>l<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span>r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-10"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">struct</span> node<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-11">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>num<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-12"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span>t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-13"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">inline</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">bool</span> cmp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> node a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> node b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-14">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-15"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-16"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">inline</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">bool</span> cmpNum<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> node x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> node y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-17">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>num<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>num<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-18"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-19"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> Insert<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> pos<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-20">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>pos<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-21">        c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>pos<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]+=</span>x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-22">        pos<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span>lowbit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>pos<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-23">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-24"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-25"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> sum<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> pos<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-26">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> ans<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-27">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>pos<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-28">        ans<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span>c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>pos<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-29">        pos<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-=</span>lowbit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>pos<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-30">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-31">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> ans<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-32"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-33"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">inline</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> Min<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-34">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-35"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-36"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> main<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(){</span></span>
<span id="cb1-37">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> ans<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-38">    scanf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%d</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,&amp;</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-39">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-40">        scanf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%d</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,&amp;</span>t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-41">        t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>num<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-42">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-43">    t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>num<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-44">    sort<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>cmp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-45">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> posMin <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>num<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-46">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-47">        t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-48">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-49">    sort<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>cmpNum<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-50">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-51">        l<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=</span>sum<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-52">        Insert<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-53">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-54">    memset<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>cPre<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">sizeof</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>cPre<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">));</span></span>
<span id="cb1-55">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">--){</span></span>
<span id="cb1-56">        r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=</span>sum<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-57">        Insert<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">].</span>v<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-58">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-59">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-60">        ans<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span>Min<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>l<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span>r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]);</span></span>
<span id="cb1-61">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-62">    cout<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span>ans<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-63">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-64"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>


</section>

 ]]></description>
  <category>算法</category>
  <guid>https://blog.redforest.org.cn/posts/0010.html</guid>
  <pubDate>Mon, 16 Aug 2021 16:00:00 GMT</pubDate>
</item>
<item>
  <title>题解:火柴排序</title>
  <link>https://blog.redforest.org.cn/posts/000d.html</link>
  <description><![CDATA[ 




<section id="题解-火柴排序" class="level1">
<h1>题解 : 火柴排序</h1>
<section id="题意" class="level2">
<h2 class="anchored" data-anchor-id="题意">1.题意</h2>
<p>地址：https://www.luogu.com.cn/problem/P1966</p>
<p>有两个序列a,b，每个序列的元素不同。任意一个序列的任意两个相邻的元素可以互换，要求对两个序列进行操作，使得<img src="https://latex.codecogs.com/png.latex?%5Csum(a_i-b_i)%5E2">最小，求最小操作次数。</p>
</section>
<section id="思路" class="level2">
<h2 class="anchored" data-anchor-id="思路">2.思路：</h2>
<p>对于这两个序列而言，要求我们最小化的是 <img src="https://latex.codecogs.com/png.latex?%0A%5Csum(a_i-b_i)%5E2%0A%E5%B1%95%E5%BC%80%E8%BF%99%E4%B8%AA%E5%BC%8F%E5%AD%90%EF%BC%8C%E5%BE%97%E5%88%B0%0A"></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Csum(a_i%5E2-2a_ib_i+b_i%5E2)%0A"></p>
<p>也就是 <img src="https://latex.codecogs.com/png.latex?%0A%5Csum(a_i%5E2+b_i%5E2)-%5Csum(2a_ib_i)%0A"> 因为<img src="https://latex.codecogs.com/png.latex?a_i,b_i">都是定值，所以我们只需要 最大化 <img src="https://latex.codecogs.com/png.latex?%0A%5Csum(2a_ib_i)%0A"> 即可。</p>
<p>对于两个序列而言，全部升序/降序时总比乱序时的<img src="https://latex.codecogs.com/png.latex?%5Csum(2a_ib_i)">大。这一点很容易证明可以考虑转化为面积或者通过数学归纳法进行证明。</p>
<p>另外很显然的一点就是，对于序列a,b，对他们的对应元素进行同样的操作，也就是“一起进行某种操作”对答案没有影响。</p>
<p>那么，问题就转化为，对于序列a,b，求对b最少的操作次数使得对b操作后产生的b’序列和a“一起”进行某些操作后使得a变为升序后，a,b’的元素对应的大小位置相等（大小位置就是对于某个序列排完序以后某个元素的位置）。</p>
<p>那么，我们先对a，b序列进行一步奇妙的离散化。把序列中的元素映射为对应元素的下标，设a映射为c，b映射为d。然后，对吧c以a为基准排序，再把d以b为基准排序。得到新的序列c’,d’。此时这两个序列保存的是把a和b分别变成升序时，原来元素的新位置。那么，根据前面所说的，a和b“一起”进行某些操作时，对答案没有影响，我们已经成功的把问题转化为：只能交换序列中的相邻元素，使得d’=c’的最小交换次数。但是我们仍然不能很容易的进行求解。</p>
<p>那么，我们把c排序，再把<img src="https://latex.codecogs.com/png.latex?d'">以<img src="https://latex.codecogs.com/png.latex?c'">为基准排序。这时候得到的新序列<img src="https://latex.codecogs.com/png.latex?c''">,<img src="https://latex.codecogs.com/png.latex?d''">相当于把<img src="https://latex.codecogs.com/png.latex?c'">，<img src="https://latex.codecogs.com/png.latex?d'"> “一起”进行了某些操作使得c’升序时的d’。到了这一步我们就会求只能交换序列中的相邻元素，使得d’=c’的最小交换次数了。</p>
<p>根据线性代数老师所言： + 只交换相邻的两数让他们变成升序：交换次数为逆序对数 + 证明：考虑一个排列，先将其最大的数移至序列尾，次数为最大数与它后面的数组成的逆序对数。此时最后一位已定，再考虑前n-1个数的排列。最后整个序列的操作次数即为逆序对数。</p>
</section>
<section id="代码" class="level2">
<h2 class="anchored" data-anchor-id="代码">3.代码：</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode cpp code-with-copy"><code class="sourceCode cpp"><span id="cb1-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;iostream&gt;</span></span>
<span id="cb1-2"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;cstdio&gt;</span></span>
<span id="cb1-3"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;algorithm&gt;</span></span>
<span id="cb1-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">using</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">namespace</span> std<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> MAXN <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e8</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">template</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">typename</span> _TP<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb1-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">inline</span> _TP read<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>_TP <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-8">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-9">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(!</span>isdigit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)){</span>w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'-'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>getchar<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();}</span></span>
<span id="cb1-10">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>isdigit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)){</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=(</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)+(</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)+(</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">48</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>getchar<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();}</span></span>
<span id="cb1-11">    X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?-</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-12">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-13"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-14"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span>t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span>pos<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span>w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-15"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">inline</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">bool</span> cmpA<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-16">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]&lt;</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-17"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-18"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">inline</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">bool</span> cmpB<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-19">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]&lt;</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-20"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-21"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> cnt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-22"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> merge_sort<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> l<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-23">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>l<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-24">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> m<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>l<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+(</span>r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>l<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-25">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>l<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>q<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>m<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>l<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-26">        merge_sort<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>l<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>m<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-27">        merge_sort<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>m<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-28">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> q<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-29">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>q<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span>r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]&lt;=</span>w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>q<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">])){</span></span>
<span id="cb1-30">                t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++]=</span>w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++];</span></span>
<span id="cb1-31">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-32">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-33">                t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++]=</span>w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>q<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++];</span></span>
<span id="cb1-34">                cnt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span>m<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-35">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-36">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-37">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>l<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-38">            w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=</span>t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-39">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-40">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-41"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-42"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span>d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-43"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> main<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(){</span></span>
<span id="cb1-44">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-45">    read<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-46">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-47">        read<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]);</span></span>
<span id="cb1-48">        c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-49">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-50">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-51">        read<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]);</span></span>
<span id="cb1-52">        d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-53">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-54">    sort<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>cmpA<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-55">    sort<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>cmpB<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-56">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-57">        w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>c<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]]=</span>d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-58">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-59">    merge_sort<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-60">    cout<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span>cnt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%(</span>P<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-61">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-62"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-63"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/*</span></span>
<span id="cb1-64"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">关于序列操作：</span></span>
<span id="cb1-65"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    1.只交换相邻的两数让他们变成升序：</span></span>
<span id="cb1-66"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    交换次数为逆序对数</span></span>
<span id="cb1-67"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    原理需要咨询线性代数老师:</span></span>
<span id="cb1-68"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    考虑一个排列，先将其最大的数移至序列尾，次数为最大数与它后面的数组成的逆序对数</span></span>
<span id="cb1-69"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    此时最后一位已定，再考虑前n-1个数的排列</span></span>
<span id="cb1-70"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    2.交换任意的两数是使得序列升序</span></span>
<span id="cb1-71"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    交换次数为n-循环个数</span></span>
<span id="cb1-72"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    定义一个循环为3在5的位置，5在8的位置，8在3的位置</span></span>
<span id="cb1-73"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*/</span></span></code></pre></div></div>


</section>
</section>

 ]]></description>
  <category>算法</category>
  <guid>https://blog.redforest.org.cn/posts/000d.html</guid>
  <pubDate>Sat, 14 Aug 2021 16:00:00 GMT</pubDate>
</item>
<item>
  <title>vscode导出markdown时出现的代码块分、换行错误、不渲染公式的解决方法</title>
  <link>https://blog.redforest.org.cn/posts/0021.html</link>
  <description><![CDATA[ 




<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Content note — Outdated environment · 环境过时
</div>
</div>
<div class="callout-body-container callout-body">
<p>VS Code Markdown PDF 的 MathJax 配置引用了旧版 <code>cdn.mathjax.org</code>，该服务早已不适合作为现代配置。</p>
<ul>
<li>新配置应使用当前插件能力或 MathJax 3 的可用 CDN/本地资源。</li>
<li>插件版本 <code>yzane.markdown-pdf-1.4.1</code> 是旧环境路径，不应照抄。</li>
</ul>
</div>
</div>
<section id="vscode导出markdown时出现的代码块分换行错误不渲染公式的解决方法公式" class="level1">
<h1>vscode导出markdown时出现的代码块分、换行错误、不渲染公式的解决方法公式</h1>
<section id="本文一切叙述前提为使用-markdown-pdf-插件导出pdf" class="level2">
<h2 class="anchored" data-anchor-id="本文一切叙述前提为使用-markdown-pdf-插件导出pdf">0.本文一切叙述前提为使用 <code>Markdown PDF</code> 插件导出PDF</h2>
</section>
<section id="导出时代码块自动换页与换行错误" class="level2">
<h2 class="anchored" data-anchor-id="导出时代码块自动换页与换行错误">1.导出时代码块自动换页与换行错误</h2>
<p>如果使用 <code>Markdown Preview Enhanced</code> 插件进行预览，并从预览页面右键导出PDF的话，就有可能遇到代码块换页的问题和换行错误问题。</p>
<p>解决方法为：卸载 <code>Markdown Preview Enhanced</code> ，安装 <code>Markdown All in One</code> 然后在markdown源码页面右键导出PDF。并且，在 <code>Markdown Preview Enhanced</code> 提供的预览中，渲染出的换行和导出后的换行一致。</p>
</section>
<section id="使用上述方法导出的pdf不渲染数学公式" class="level2">
<h2 class="anchored" data-anchor-id="使用上述方法导出的pdf不渲染数学公式">2.使用上述方法导出的PDF不渲染数学公式</h2>
<p>找到如下路径(username为你的username)</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode html code-with-copy"><code class="sourceCode html"><span id="cb1-1">C://Users/<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">username</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span>/.vscode/extensions/yzane.markdown-pdf-1.4.1/template/template.html</span></code></pre></div></div>
<p>在文件末尾添加(注意要写在body里面)</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode html code-with-copy"><code class="sourceCode html"><span id="cb2-1"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">script</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> type=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text/javascript"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> src</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">script</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb2-2"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">script</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> type=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text/x-mathjax-config"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span> MathJax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Hub</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Config</span>({ <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">tex2jax</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">inlineMath</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> [[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'$'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'$'</span>]]}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">messageStyle</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span> })<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">script</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span></code></pre></div></div>
<p>效果如下：</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode html code-with-copy"><code class="sourceCode html"><span id="cb3-1"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;!DOCTYPE</span> html<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb3-2"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">html</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb3-3"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">head</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb3-4"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">title</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span>{{{title}}}<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">title</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb3-5"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">meta</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> http-equiv</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Content-type"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> content</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text/html;charset=UTF-8"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb3-6">{{{style}}}</span>
<span id="cb3-7">{{{mermaid}}}</span>
<span id="cb3-8"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">head</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb3-9"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">body</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb3-10">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">script</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb3-11">    mermaid<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">initialize</span>({</span>
<span id="cb3-12">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">startOnLoad</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb3-13">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">theme</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">document</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">body</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">classList</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contains</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'vscode-dark'</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">document</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">body</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">classList</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contains</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'vscode-high-contrast'</span>)</span>
<span id="cb3-14">          <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'dark'</span></span>
<span id="cb3-15">          <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'default'</span></span>
<span id="cb3-16">    })<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb3-17">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">script</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb3-18">{{{content}}}</span>
<span id="cb3-19"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">body</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb3-20"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">script</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> type=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text/javascript"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> src</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">script</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb3-21"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">script</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> type=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text/x-mathjax-config"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span> MathJax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Hub</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Config</span>({ <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">tex2jax</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">inlineMath</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> [[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'$'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'$'</span>]]}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">messageStyle</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span> })<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">script</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb3-22"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">html</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span></code></pre></div></div>


</section>
</section>

 ]]></description>
  <category>环境配置</category>
  <guid>https://blog.redforest.org.cn/posts/0021.html</guid>
  <pubDate>Sat, 14 Aug 2021 16:00:00 GMT</pubDate>
</item>
<item>
  <title>题解:Product 1 Modulo N</title>
  <link>https://blog.redforest.org.cn/posts/001c.html</link>
  <description><![CDATA[ 




<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Content note — Needs verification · 待核验
</div>
</div>
<div class="callout-body-container callout-body">
<p>Product 1 Modulo N 的结论基本方向可用，但证明部分有变量和取模表达错误。</p>
<ul>
<li>文中多处写成 <code>n % m</code>，语义上应检查是否为 <code>m % n</code>。</li>
<li>“m % p != 0”处的 p 未清晰定义，建议重写证明。</li>
</ul>
</div>
</div>
<section id="题解-product-1-modulo-n" class="level1">
<h1>题解 : Product 1 Modulo N</h1>
<section id="前言" class="level2">
<h2 class="anchored" data-anchor-id="前言">1.前言：</h2>
<p>场上这题证了三个小时没有证出来。所以以后有些题不证出来也要先找找规律出了。否则过于影响节奏。</p>
</section>
<section id="题意" class="level2">
<h2 class="anchored" data-anchor-id="题意">2.题意：</h2>
<p>地址：https://vjudge.net/problem/CodeForces-1514C</p>
<p>Now you get Baby Ehab’s first words: “Given an integer n, find the longest subsequence of 1,2,…,n−1 whose product is 11 modulo n.” Please solve the problem.</p>
<p>A sequence bb is a subsequence of an array aa if bb can be obtained from aa by deleting some (possibly all) elements. The product of an empty subsequence is equal to 11.</p>
<p><strong>Input</strong></p>
<p>The only line contains the integer <img src="https://latex.codecogs.com/png.latex?n,(2%E2%89%A4n%E2%89%A410%5E5)">.</p>
<p><strong>Output</strong></p>
<p>The first line should contain a single integer, the length of the longest subsequence.</p>
<p>The second line should contain the elements of the subsequence, in increasing order.</p>
<p>If there are multiple solutions, you can print any.</p>
</section>
<section id="解法" class="level2">
<h2 class="anchored" data-anchor-id="解法">2.解法：</h2>
<p>首先，我们对于给定的这 n-1 个数，只选取与 n 互质的数作为答案中出现的数。</p>
<p>然后，我们将这n-1个数中与n互质的数乘起来（对n取模）最后会得到一个数m，如果m=1，那么答案就是所有与n互质的数，如果m!=1，那么m一定是出现在 1到n-1的所有与n互质的数中。那么我们只需要在答案中删掉m这个数即可。</p>
</section>
<section id="证明" class="level2">
<h2 class="anchored" data-anchor-id="证明">3.证明：</h2>
<p>很显然，若i和n不互质，那么i的某个因数p（p!=1）一定是n的因数，那么如果i在答案中，那么所求答案序列的乘积中一定有p这个因数。根据 <img src="https://latex.codecogs.com/png.latex?gcd(a,b)%20=%20gcd(b,a">%<img src="https://latex.codecogs.com/png.latex?b)"> 得到，<img src="https://latex.codecogs.com/png.latex?gcd(n,m)%20=%20p%20!=%201"> 所以答案中必然不会出现与n不互质的数。</p>
<p>那么，问题的关键就在于，如何确定当<img src="https://latex.codecogs.com/png.latex?m%5C%25p%20!=%200">的时候，m%n必然出现在1到n-1中与n互质的数中。很显然，因为m是所有小于n的与n互质的数的乘积，那么m中必然不含有n的因数（除了1）,那么<img src="https://latex.codecogs.com/png.latex?n%5C%25m">一定与n互质。且<img src="https://latex.codecogs.com/png.latex?n%5C%25m">一定小于n（因为模过了）。因为小于n的所有与n互质的数现在都在答案序列里，且n%m也是一个比n小的与n互质的数，那么n%m一定属于答案序列。</p>
</section>
<section id="代码" class="level2">
<h2 class="anchored" data-anchor-id="代码">4.代码：</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode cpp code-with-copy"><code class="sourceCode cpp"><span id="cb1-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;iostream&gt;</span></span>
<span id="cb1-2"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;cstdio&gt;</span></span>
<span id="cb1-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">using</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">namespace</span> std<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">const</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> MAXN <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-5"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> ans<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MAXN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">template</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">typename</span> _TP<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb1-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">inline</span> _TP read<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>_TP <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-8">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-9">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(!</span>isdigit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)){</span>w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'-'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>getchar<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();}</span></span>
<span id="cb1-10">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>isdigit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)){</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=(</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)+(</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)+(</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">48</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>getchar<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();}</span></span>
<span id="cb1-11">    X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?-</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-12">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-13"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-14"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> gcd<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-15">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%=</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^=</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^=</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^=</span>b<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;}</span></span>
<span id="cb1-16">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-17"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-18"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> main<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(){</span></span>
<span id="cb1-19">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>res<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>cnt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-20">    read<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-21">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-22">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>gcd<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-23">            ans<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[++</span>cnt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-24">            res<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*=</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-25">            res<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-26">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-27">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-28">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>res<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-29">        cout<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span>cnt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-30">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-31">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb1-32">        cout<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span>cnt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-33">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-34">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>cnt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-35">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ans<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]!=</span>res <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> res<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-36">            cout<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span>ans<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]&lt;&lt;</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">' '</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-37">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-38">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-39">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-40"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>


</section>
</section>

 ]]></description>
  <category>算法</category>
  <guid>https://blog.redforest.org.cn/posts/001c.html</guid>
  <pubDate>Sat, 31 Jul 2021 16:00:00 GMT</pubDate>
</item>
<item>
  <title>题解:射击&gt;&gt;枚举–不只是暴力！</title>
  <link>https://blog.redforest.org.cn/posts/0028.html</link>
  <description><![CDATA[ 




<div class="callout callout-style-default callout-caution callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Caution</span>Content note — Known errors · 明显错误
</div>
</div>
<div class="callout-body-container callout-body">
<p>射击题解代码片段存在明显笔误和不可编译风险。</p>
<ul>
<li>双重枚举中 <code>for(int j=1;j&lt;=1;j++)</code> 明显应重新核对。</li>
<li><code>i=1,j=n+k</code> 未声明变量；数组上界 <code>1e6+7</code> 作为浮点字面量也不适合直接做数组大小。</li>
<li>题面是反向回忆生成，不能当作可靠原题描述。</li>
</ul>
</div>
</div>
<section id="题解-射击枚举不只是暴力" class="level1">
<h1>题解 : 射击&gt;&gt;枚举–不只是暴力！</h1>
<section id="前言" class="level2">
<h2 class="anchored" data-anchor-id="前言">1.前言：</h2>
<p>CSP2019的前一天，早起到了学校。今天是高三二诊的第一天，恰好CSP和二诊冲掉了，不得已请了假。本来高三是不打算停课的，借此机会重温了一下子机房停课的快乐生活。停一天也是停嘛。</p>
<p>到了学校发现教练还没到，可能是我太久没有机房停课的生活，忘记了机房的生物钟了。一群人在实验楼连廊里等教练。等到大约八点半教练终于来上班了。（中间还被级部主任误以为是逃考试的学生……</p>
<p>看完了教练发的一套题，感觉挺简单的，做完了还没到中午放学，扭头看旁边同学在干什么，发现我的两个同学(均为高三)在研究一道题。<del>(主角出场</del></p>
</section>
<section id="题目大意" class="level2">
<h2 class="anchored" data-anchor-id="题目大意">2.题目大意</h2>
<section id="attation" class="level3">
<h3 class="anchored" data-anchor-id="attation">2.1 ATTATION:</h3>
<p>题目过于久远，已经不记得具体是什么了。题目在NOIOJ上面，现在这个网站挂掉了所以题面是我根据之前的代码以及依稀的印象反向编的。</p>
<p><del>（似乎什么时候听说NOIP会从题库里出原题来着？那我就盲猜一把射击这个题 2020.1.31</del> …好了，事实证明NOIP取消了，CSP：我和NOIP没有（you）关系</p>
</section>
<section id="题面" class="level3">
<h3 class="anchored" data-anchor-id="题面">2.2 题面：</h3>
<p>一共有N种物品（N&lt;=5e3），你有放回的取4次，每一次可以不取，也可以取 i ，每个物品有权值ai，求四次取到的和比m小的最大值是多少 (0&lt;m&lt;Max_long long,0&lt;ai&lt;MAX_int)</p>
</section>
<section id="注" class="level3">
<h3 class="anchored" data-anchor-id="注">2.3注：</h3>
<p>因为m的值非常大，所以没办法背包。</p>
</section>
</section>
<section id="题解" class="level2">
<h2 class="anchored" data-anchor-id="题解">3.题解</h2>
<p>首先，这道题是绝世好题。</p>
<p>我记得之前做题的时候见到一道题 <a href="https://www.luogu.com.cn/problem/P3396">P3396 哈希冲突</a> 题解里说这道题是一篇省选论文里的，论文说：根号算法，不只是分块。(这也是一道绝世好题)</p>
<p>那么我也要说，今天的这道题 <strong>射击</strong> 是：枚举算法，不只是暴力。 这道题第一眼看来没什么思路，首先写搜索，大力剪枝之后最多只能拿到40分 搜索的复杂度是O(n^4)，妥妥的TLE那怎么办呢？ 先上代码：</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode cpp code-with-copy"><code class="sourceCode cpp"><span id="cb1-1">  <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;iostream&gt;</span></span>
<span id="cb1-2">  <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;cstdio&gt;</span></span>
<span id="cb1-3">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">using</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">namespace</span> std<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-4">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">long</span> a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e6</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">],</span>ans<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-5">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">template</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">typename</span> _TP<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb1-6">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">inline</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">void</span> read<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>_TP <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-7">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-8">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(!</span>isdigit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)){</span>w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">'-'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>getchar<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();}</span></span>
<span id="cb1-9">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>isdigit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)){</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=(</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)+(</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)+(</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">48</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span>ch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>getchar<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();}</span></span>
<span id="cb1-10">      X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?-</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>X<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-11">  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-12">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> main<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(){</span></span>
<span id="cb1-13">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>m<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-14">      read<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span>read<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>m<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-15">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-16">          read<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]);</span></span>
<span id="cb1-17">      <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-18">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-19">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-20">          <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb1-21">              <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++</span>k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-22">              a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]+</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-23">          <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-24">      <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-25">      sort<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb1-26">      i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-27">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-28">          <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]+</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]&lt;=</span>m<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb1-29">              <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]+</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]&gt;</span>ans<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>ans<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]+</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb1-30">              <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-31">          <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-32">          <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">--</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-33">      <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb1-34">      cout<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;&lt;</span>ans<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-35">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-36">  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>这就是完整的代码了，什么意思呢？<br>
考虑这样一个问题，我们可以把过程看成两个取两次而不是一个取四次<br>
这里实际上使用了分别枚举的技巧，也就是说，先对前两次进行O(n^2)的枚举 ，记录状态，然后再对后两次取进行O(n^2)的枚举，记录状态。但实际上这两次枚举的结果是一模一样的，所以枚举一次就好了。<br>
最后再对于前两次的结果进行一次O(n^2)的枚举。答案就有了。<br>
这样看起来整体的复杂度是O(n<sup>2)的，但是，最后一次枚举的时候实际上状态数量是O(n</sup>2)的.其实最后一次的枚举复杂度是O（n^4）的。所以说其实复杂度并没有优化。<br>
<strong>枚举的代码是这样的：</strong></p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode cpp code-with-copy"><code class="sourceCode cpp"><span id="cb2-1">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb2-2">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++){</span></span>
<span id="cb2-3">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++</span>k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-4">            a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]=</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]+</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb2-5">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-6">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p><strong>但是</strong><br>
进行了前两次枚举就可以用一种类似贪心的方法进行处理了，也就是</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode cpp code-with-copy"><code class="sourceCode cpp"><span id="cb3-1">    sort<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb3-2">        i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb3-3">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb3-4">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]+</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]&lt;=</span>m<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">){</span></span>
<span id="cb3-5">                <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]+</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]&gt;</span>ans<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>ans<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]+</span>a<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb3-6">                <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++</span>i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb3-7">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb3-8">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">--</span>j<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb3-9">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>这样，最后一次的贪心是O(n^2)的。</p>


</section>
</section>

 ]]></description>
  <category>算法</category>
  <guid>https://blog.redforest.org.cn/posts/0028.html</guid>
  <pubDate>Thu, 30 Jan 2020 16:00:00 GMT</pubDate>
</item>
</channel>
</rss>
