差分 ---- Codeforces Round #672 (Div. 2):C2. Pokémon Army (hard version)[差分的思想]

news/2024/7/7 4:14:11

题目链接


题目大意:就算给你一序列,按照顺序出若干个数组成一个的序列,然后对这个序列定义一个权值就算奇数位置的和减去偶数位置的和,问你能的到的最大的权值是多少?
**a1 - a2 + a3 - a4 + a5 … **


解题思路:1.就是我们观擦一下:就任意多个ai - aj的组合;最后的就是an - 0;
2.我们可以用差分的思想:ai就是差分数组前i位的前缀和,那么ai-aj就是差分数组的区间和,就是贪心挑出任意多个差分大于0的区间求他们的和
3.我们发现如果这个区间内部有出现+配-配+但是总的sum还是+,那么我们最优还可以直接列成两个全是正的小区间,那么最优
4.那么综上所述:我们那么其实就算把差分任意两个相邻的ai-ai+1的大于0的和
5.那么对于交换操作:我们知道改变的只有两个数,那么对差分数组的影响只有4位置,那么就可以O(1)更新答案
注意一点就是:a[n+1] = 0,因为最后一定是+一个数而已相当于-0


代码:


#include <iostream>
#include <cstdio>
#include <stack>
#include <sstream>
#include <limits.h>
#include <vector>
#include <map>
#include <cstring>
#include <deque>
#include <cmath>
#include <iomanip>
#include <unordered_map>
#include <queue>
#include <algorithm>
#include <set>
#define mid ((l + r) >> 1) 
#define Lson rt << 1, l , mid
#define Rson rt << 1|1, mid + 1, r
#define ms(a,al) memset(a,al,sizeof(a))
#define log2(a) log(a)/log(2)
#define _for(i,a,b) for( int i = (a); i < (b); ++i)
#define _rep(i,a,b) for( int i = (a); i <= (b); ++i)
#define for_(i,a,b) for( int i = (a); i >= (b); -- i)
#define rep_(i,a,b) for( int i = (a); i > (b); -- i)
#define lowbit(x) ((-x) & x)
#define IOS std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define INF 0x3f3f3f3f
#define LLF 0x3f3f3f3f3f3f3f3f
#define hash Hash
#define next Next
#define pb push_back
#define f first
#define s second
#define y1 Y
using namespace std;
const int N = 3e5 + 10, MOD = 1e9 + 7;
const int maxn = 2e5;
const long double eps = 1e-5;
const int EPS = 500 * 500;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef pair<double,double> PDD;
template<typename T> void read(T &x)
{x = 0;char ch = getchar();ll f = 1;while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
template<typename T, typename... Args> void read(T &first, Args& ... args) 
{read(first);read(args...);
}
ll a[N], b[N];
int T;
int n, q;
ll ans;inline void work(int pos, ll & ans)
{if(pos - 1 >= 1 && b[pos - 1] > 0) ans -= b[pos - 1];if(pos - 1 >= 1) b[pos - 1] = a[pos - 1] - a[pos];if(pos - 1 >= 1 && b[pos - 1] > 0) ans += b[pos - 1];if(b[pos] > 0) ans -= b[pos];b[pos] = a[pos] - a[pos + 1];if(b[pos] > 0) ans += b[pos];
}int main()
{IOS;cin >> T;while(T --){ans = 0;cin >> n >> q;a[n + 1] = 0;for(int i = 1; i <= n; ++ i)cin >> a[i];for(int i = 1; i <= n; ++ i)b[i] = a[i] - a[i + 1], ans += b[i] * (b[i] > 0);cout << ans << endl;while(q --){int l, r;cin >> l >> r;swap(a[l],a[r]);work(l,ans);work(r,ans);cout << ans << endl;}}return 0;
}

http://lihuaxi.xjx100.cn/news/286923.html

相关文章

oracle rodo 查看大小,Checkpoint not complete故障

Checkpoint not complete故障一、故障现象&#xff1a;Checkpoint not completeCurrent log# 3 seq# 30192 mem# 0: /u01/app/oracle/oradata/yingxiang/redo03.logThread 1 advanced to log sequence 30193 (LGWR switch)Current log# 1 seq# 30193 mem# 0: /u01/app/oracle/o…

原来,1999年已经是20年前了

作者&#xff1a;小武来源&#xff1a;一只武&#xff08;ID&#xff1a;xiaowuwaimai&#xff09;父母的修养&#xff0c;未来不可见&#xff0c;过去真实存在。岁月是神偷&#xff0c;轻易偷走最珍贵的时间。往事不可追&#xff0c;唯有回忆作陪。1999年的你&#xff0c;年龄…

2018热点总结:BERT最热,GANs最活跃,每20分钟就有一篇论文...

作者 | Ross Taylor 译者 | linstancy 整理 | Jane 出品 | AI科技大本营 【导读】本文的作者 Ross Taylor 和 Robert Stojnic 在今年一起启动了一个名为“Paper with Code”的项目&#xff0c;将 AI 领域的一些研究论文和论文开源代码结合展示&#xff0c;方便大家学习与研究…

SQLite的数据类型总结

2019独角兽企业重金招聘Python工程师标准>>> 一般数据采用的固定的静态数据类型&#xff0c;而SQLite采用的是动态数据类型&#xff0c;会根据存入值自动判断。SQLite具有以下五种数据类型&#xff1a; 1.NULL&#xff1a;空值。2.INTEGER&#xff1a;带符号的整型&…

关于NameError: name ‘sns‘ is not defined->ModuleNotFoundError: No module named ‘seaborn‘错误提示

添加&#xff1a;import seaborn as sns 添加这句之后又出现这个错误&#xff1a; 2.ModuleNotFoundError: No module named seaborn 下载seaborn: 方法一&#xff1a;Anaconda Prompt->activate tensorflow (创建tensorflow环境&#xff09;->输入&#xff1a; pip inst…

Codeforces Round #672 (Div. 2)D. Rescue Nibel![扫描线解决区间问题]

题目链接 题目大意&#xff1a;就是给你n个区间&#xff0c;从中选出k个区间&#xff0c;这k个区间共同覆盖了同一个点&#xff0c;问有多少种选法&#xff1f;结果mod 998244353 解题思路&#xff1a;1.首先我们可以这么想&#xff1a;我们把区间左端点赋值为1&#xff0c;右端…

解读算法「时间」与「空间」复杂度——冰与火之歌

作者 | 程序员小吴来源 | 五分钟学算法算法&#xff08;Algorithm&#xff09;是指用来操作数据、解决程序问题的一组方法。对于同一个问题&#xff0c;使用不同的算法&#xff0c;也许最终得到的结果是一样的&#xff0c;比如排序就有前面的十大经典排序和几种奇葩排序&#x…

关于NameError: name ‘train_test_split‘ is not defined错误提示

添加&#xff1a;from sklearn.model_selection import train_test_split