Codeforces Round 909 (Div. 3)(A~G)(启发式合并)

1899A - Game with Integers 

        题意:给定一个数 x , 两个人玩游戏,每人能够执行 +1 / -1操作,若操作完x是3的倍数则获胜,问先手的人能否获胜(若无限循环则先手的人输)。

        思路:假如一个数模3余1或者2,那么第一轮操作先手就能获胜,若余0则后手获胜。

        

// Problem: A. Game with Integers
// Contest: Codeforces - Codeforces Round 909 (Div. 3)
// URL: https://codeforces.com/contest/1899/problem/A
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define x first
#define y second 
#define endl '\n'
const LL maxn = 4e05+7;
const LL N=1e05+10;
const LL mod=1e09+7;
typedef pair<int,int>pl;
priority_queue<LL , vector<LL>, greater<LL> >t;
priority_queue<LL> q;
LL gcd(LL a, LL b){return b > 0 ? gcd(b , a % b) : a;
}LL lcm(LL a , LL b){return a / gcd(a , b) * b;
}
int n , m;
int a[N];
void init(int n){for(int i = 0 ; i <= n ; i ++){a[i] = 0;}
}
void solve() 
{cin >> n;if(n % 3 == 0){cout << "Second\n";}	else{cout << "First\n";}	
}            
int main() 
{ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout.precision(10);int t=1;cin>>t;while(t--){solve();}return 0;
}

1899B - 250 Thousand Tons of TNT  

        题意:给定一个整数n , 表示有 n 个集装箱,接下来给定一个数组a , 代表了第i个集装箱有a_{i}吨重。现要将n个集装箱恰好分成连续的k组。要求这当中所有k的取值下集装箱重量的最大值减去最小值的最大值。

        思路:直接暴力做 , 假设每一组有1、2、3、4、...n个,看满足题意的情况下最大值减最小值的值。时间复杂度O(NlogN).

        

// Problem: B. 250 Thousand Tons of TNT
// Contest: Codeforces - Codeforces Round 909 (Div. 3)
// URL: https://codeforces.com/contest/1899/problem/B
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define x first
#define y second 
#define endl '\n'
const LL maxn = 4e05+7;
const LL N=2e05+10;
const LL mod=1e09+7;
typedef pair<int,int>pl;
priority_queue<LL , vector<LL>, greater<LL> >t;
priority_queue<LL> q;
LL gcd(LL a, LL b){return b > 0 ? gcd(b , a % b) : a;
}LL lcm(LL a , LL b){return a / gcd(a , b) * b;
}
int n , m;
LL a[N] , sum[N];
void init(int n){for(int i = 0 ; i <= n ; i ++){a[i] = 0;}
}
void solve() 
{cin >> n;for(int i = 1 ; i <= n ; i ++){cin >> a[i];sum[i] = sum[i - 1] + a[i];	}LL ans = 0;for(int i = 1 ; i <= n ; i ++){LL maxx = 0 , minn = 1e18;if(n % i == 0){for(int j = 0 ; j < n ; j += i){maxx = max(sum[j + i] - sum[j] , maxx);minn = min(minn , sum[j + i] - sum[j]);}ans = max(ans , maxx - minn);}}cout << ans<<endl;
}            
int main() 
{ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout.precision(10);int t=1;cin>>t;while(t--){solve();}return 0;
}

1899C - Yarik and Array  

        题意:给定一组序列,求连续奇数偶数非空子序列的和的最大值(相邻的奇偶性不能相同)。

        思路:同最大连续子序列差不多的做法,只不过要求前一项和当前的奇偶性不同,且至少要选一项。时间O(N)。

        

// Problem: C. Yarik and Array
// Contest: Codeforces - Codeforces Round 909 (Div. 3)
// URL: https://codeforces.com/contest/1899/problem/C
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define x first
#define y second 
#define endl '\n'
const LL maxn = 4e05+7;
const LL N=2e05+10;
const LL mod=1e09+7;
typedef pair<int,int>pl;
priority_queue<LL , vector<LL>, greater<LL> >t;
priority_queue<LL> q;
LL gcd(LL a, LL b){return b > 0 ? gcd(b , a % b) : a;
}LL lcm(LL a , LL b){return a / gcd(a , b) * b;
}
int n , m;
LL a[N];
int dp[N][2];
void init(int n){for(int i = 0 ; i <= n ; i ++){a[i] = 0;}
}
void solve() 
{int n;cin >> n;for(int i = 1 ; i <= n ; i ++)cin >> a[i];LL ans = -1e18;LL now = 0;for(int i = 1 ; i <= n ; i ++){if(now == 0){now += a[i];}else{if(abs(a[i]) % 2 == abs(a[i - 1]) % 2 ){now = a[i];}else{if(now > 0)now += a[i];elsenow = a[i];}}ans = max(ans , now);//	cout << now << endl;if(now < 0)now = 0;}cout << ans << endl;
}            
int main() 
{ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout.precision(10);int t=1;cin>>t;while(t--){solve();}return 0;
}

1899D - Yarik and Musical Notes 

        题意:给定一组数,要求数对(i , j)满足(2^{a_i})^{2^{a_{j}}} = (2^{a_j})^{2^{a_{i}}}(i < j)的数量。

        思路:构造辅助哈希nex(范围过大无法构造数组) , nex[i]标记了当前位置之后有多少个数字i。观察后发现 , 当 i = 1 ,j = 2/i = 2 , j = 1时能够满足,其余均需要i =j才能满足。因此对于1或者2而言,数对的数量为nex[1] +nex[2] ,其余的 i 构成的数对数量都是nex[i]。首先遍历一遍算出nex,然后再遍历一遍求答案,同时不断更新nex即可。

        

// Problem: D. Yarik and Musical Notes
// Contest: Codeforces - Codeforces Round 909 (Div. 3)
// URL: https://codeforces.com/contest/1899/problem/D
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define x first
#define y second 
#define endl '\n'
const LL maxn = 4e05+7;
const LL N=3e05+10;
const LL mod=1e09+7;
typedef pair<int,int>pl;
priority_queue<LL , vector<LL>, greater<LL> >t;
priority_queue<LL> q;
LL gcd(LL a, LL b){return b > 0 ? gcd(b , a % b) : a;
}LL lcm(LL a , LL b){return a / gcd(a , b) * b;
}
int n , m;
int a[N];
void init(int n){for(int i = 0 ; i <= n ; i ++){a[i] = 0;}
}
void solve() 
{cin >> n;unordered_map<LL,LL>mp;LL ans = 0;for(int i = 1 ; i <= n ; i ++){cin >> a[i];mp[a[i]]++;}for(int i = 1 ; i <= n ; i ++){mp[a[i]]--;if(a[i] == 1 || a[i] == 2){ans += mp[1] + mp[2];}else{ans += mp[a[i]];}}cout << ans << endl;
}            
int main() 
{ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout.precision(10);int t=1;cin>>t;while(t--){solve();}return 0;
}

1899E - Queue Sort  

        题意:给定一个数组,每次能够进行如下操作:

1、选择第一个数放入数组最后一个。

2、将最后一个数往前交换,直到到达第一位或者比前一个数大为止。

        问将数组变为递增的最少操作数,若无法则输出-1.

        思路:若最小的数为数组第一个,那么一轮操作之后他还是在第一位,这样便会无限循环。因此只需要满足最小的数之后的数全是递增的即可。

        

// Problem: E. Queue Sort
// Contest: Codeforces - Codeforces Round 909 (Div. 3)
// URL: https://codeforces.com/contest/1899/problem/E
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define x first
#define y second 
#define endl '\n'
const LL maxn = 4e05+7;
const LL N=3e05+10;
const LL mod=1e09+7;
typedef pair<int,int>pl;
priority_queue<LL , vector<LL>, greater<LL> >t;
priority_queue<LL> q;
LL gcd(LL a, LL b){return b > 0 ? gcd(b , a % b) : a;
}LL lcm(LL a , LL b){return a / gcd(a , b) * b;
}
int n , m;
LL a[N];
void init(int n){for(int i = 0 ; i <= n ; i ++){a[i] = 0;}
}
void solve() 
{cin >> n;LL minn = 1e18;for(int i = 1 ;i <= n ; i ++){cin >> a[i];minn = min(a[i] , minn);}
/*	for(int i = 1 ; i < n - 1; i++){if(a[i] < a[i + 1]){break;}if(i == n - 2){cout << 0 << endl;return;}}*/for(int i = 1 ; i <= n ; i ++){if(a[i] == minn){for(int j = i + 1; j <= n ; j ++){if(a[j] < a[j - 1]){cout << -1 << endl;return;}}cout << i - 1 << endl;return;}}
}            
int main() 
{ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout.precision(10);int t=1;cin>>t;while(t--){solve();}return 0;
}

1899F - Alex's whims  

        题意:现有n个顶点,起初你可以任意构造将其形成一棵树。接下来有 d 个数,表示共有d轮。每一轮你可以选择一个已有的边将其删除,然后再连一条边形成一颗新的数。要求每一轮能够满足至少有两个叶子结点的距离恰好为d_{i}

        思路:首先可以将n个顶点连城一条链。然后每一轮当中,我们固定第一个点和最后一个点的距离为我们要的d_{i}。每次我们可以将与点1相连的边删去,然后新增一条边,使得刚好1到n的距离为d_{i}。由于结点2 ~ n都是一条链,所以2 ~ n - 1 任意一个距离都是可以构造出来的。如此便一定能够满足题意。所以我们只需要记录当前1结点和哪个结点相连,然后需要连到哪个结点即可。

        

// Problem: F. Alex's whims
// Contest: Codeforces - Codeforces Round 909 (Div. 3)
// URL: https://codeforces.com/contest/1899/problem/F
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define x first
#define y second 
#define endl '\n'
const LL maxn = 4e05+7;
const LL N=1e05+10;
const LL mod=1e09+7;
typedef pair<int,int>pl;
priority_queue<LL , vector<LL>, greater<LL> >t;
priority_queue<LL> q;
LL gcd(LL a, LL b){return b > 0 ? gcd(b , a % b) : a;
}LL lcm(LL a , LL b){return a / gcd(a , b) * b;
}
int n , m;
int a[N];
void init(int n){for(int i = 0 ; i <= n ; i ++){a[i] = 0;}
}
void solve() 
{cin >> n >> m;for(int i = 2 ; i <= n ; i ++){cout << i - 1 << " " << i << endl;}int pre = 2;for(int i = 0 ; i < m ; i ++){int x;cin >> x;int len = (n - pre) + 1;if(len == x){cout <<"-1 -1 -1\n";continue;}else{int to = (n - x + 1);cout << 1 << " " << pre << " " << to << endl;pre = to; }}
}            
int main() 
{ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout.precision(10);int t=1;cin>>t;while(t--){solve();}return 0;
}

1899G - Unusual 进入tainment  

        思路:给定一棵树和一个排列p。现有q组询问,每组询问包含了三个数l , r , x。问点x的子树的结点是否在[p_{l} , p_{r}]中出现。

        思路:子树问题,首先想到了用dfs序来解决。对于一颗子树而言,我们可以用set来维护其所有结点在排列p中的位置。然后对于一个询问而言,只需要找到set中大于等于 l 的第一个位置即可,然后判断该位置是否小于等于r。若小于等于r则代表了其子树的结点包含在了[p_{l} , p_{r}]中。共有n个结点,所以我们需要创立n个set,来记录他们的结点在p中的位置。在子树向上合并的过程中,我们可以用启发式合并来实现优化:每一轮虽然是将子树的set合并到父节点的set上,但是可以用swap来交换两个set,确保每次都将小集合合并到大集合上面(swap是O(1)的)。如此总的时间复杂度是O(nlogn + qlogn)的。

        

// Problem: G. Unusual Entertainment
// Contest: Codeforces - Codeforces Round 909 (Div. 3)
// URL: https://codeforces.com/contest/1899/problem/G
// Memory Limit: 256 MB
// Time Limit: 3000 ms
// 
// Powered by CP Editor (https://cpeditor.org)#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define x first
#define y second 
#define endl '\n'
const LL maxn = 4e05+7;
const LL N=2e05+10;
const LL mod=1e09+7;
typedef pair<int,int>pl;
priority_queue<LL , vector<LL>, greater<LL> >t;
priority_queue<LL> q;
LL gcd(LL a, LL b){return b > 0 ? gcd(b , a % b) : a;
}
LL lcm(LL a , LL b){return a / gcd(a , b) * b;
}
int n , m;
int a[N] , pos[N];
vector<int>tr[N + 5];
vector<array< int , 3 > >que[N];
vector<set<int>> num(N);
void init(int n){for(int i = 0 ; i <= n ; i ++){a[i] = 0 , tr[i].clear() , que[i].clear();num[i].clear();}
}
LL ans[N];
void merge(set<int> &a , set<int>&b){if(a.size() < b.size()){swap(a , b);}for(auto it : b){a.insert(it);}b.clear();
}
void dfs(int cur , int f){num[cur].insert(pos[cur]);for(auto it : tr[cur]){if(it == f)continue;dfs(it , cur);merge(num[cur] , num[it]);}for(auto it : que[cur]){auto p = num[cur].lower_bound(it[0]);ans[it[2]] = p != num[cur].end() && *p <= it[1];}
};
void solve() 
{cin >> n >> m;for(int i = 1 ; i < n ; i++){int x , y;cin >> x >> y;tr[x].pb(y);tr[y].pb(x);}for(int i = 1 ; i <= n ; i ++){cin >> a[i];pos[a[i]] = i;}for(int i = 0 ; i < m ; i ++){int l , r , x;cin >> l >> r >> x;que[x].pb({l , r , i});}dfs(1 , 0);for(int i = 0 ; i < m ; i ++){if(ans[i]){cout <<"YES\n";}else{cout <<"NO\n";}}init(n);cout << endl;num.clear();
}            
int main() 
{ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout.precision(10);int t=1;cin>>t;while(t--){solve();}return 0;
}

        另外为何我这个会超时

// Problem: G. Unusual Entertainment
// Contest: Codeforces - Codeforces Round 909 (Div. 3)
// URL: https://codeforces.com/contest/1899/problem/G
// Memory Limit: 256 MB
// Time Limit: 3000 ms
// 
// Powered by CP Editor (https://cpeditor.org)#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define x first
#define y second 
#define endl '\n'
const LL maxn = 4e05+7;
const LL N=1e05+10;
const LL mod=1e09+7;
typedef pair<int,int>pl;
priority_queue<LL , vector<LL>, greater<LL> >t;
priority_queue<LL> q;
LL gcd(LL a, LL b){return b > 0 ? gcd(b , a % b) : a;
}
LL lcm(LL a , LL b){return a / gcd(a , b) * b;
}
int n , m;
int a[N] , pos[N];
vector<int>tr[N + 5];
vector<array< int , 3 > >que[N];
int vis[N];
void init(int n){for(int i = 0 ; i <= n ; i ++){a[i] = 0 , tr[i].clear() , que[i].clear() , vis[i] = 0;}
}
LL l[N] , r[N] , id[N] , sz[N] , hs[N] , tot = 0 , ans[N];
set<int>num;
void dfs(int cur , int f){//重儿子 , 子树大小 l[cur] = ++ tot;id[tot] = cur;//sz[cur] = 1;hs[cur] = -1;for(auto it : tr[cur]){if(it != f){dfs(it , cur);sz[cur] += sz[it];if(hs[cur] == -1 || sz[it] > sz[hs[cur]]){hs[cur] = it;}}		} r[cur] = tot;
}
void dfs2(int cur , int f , int keep){for(auto it : tr[cur]){if(it != f && it != hs[cur]){dfs2(it , cur , 0);//轻儿子的信息无需保留}}if(hs[cur] != -1){dfs2(hs[cur] , cur , 1);}auto add = [&](int x){vis[x] = 1;num.insert(pos[x]);};auto del = [&](int x){vis[x] = 0;num.erase(pos[x]);};for(auto it : tr[cur]){if(it != f && it != hs[cur]){//轻儿子加入到重儿子当中for(int x = l[it] ; x <= r[it] ; x ++){if(!vis[id[x]])add(id[x]);}}}add(cur);for(auto it : que[cur]){//QLOGNauto  p = lower_bound(num.begin() , num.end() , it[0]);int x = *p;//	cout << cur <<" " << it[0] <<" "<< x << endl;if(x > it[1] || x < it[0]){ans[it[2]] = 0;}else{ans[it[2]] = 1;}}if(!keep){for(int x = l[cur] ; x <= r[cur] ; x ++){//NlogNdel(id[x]);}}
}
void solve() 
{cin >> n >> m;for(int i = 1 ; i < n ; i++){int x , y;cin >> x >> y;tr[x].pb(y);tr[y].pb(x);}for(int i = 1 ; i <= n ; i ++){cin >> a[i];pos[a[i]] = i;}for(int i = 0 ; i < m ; i ++){int l , r , x;cin >> l >> r >> x;que[x].pb({l , r , i});}dfs(1 , 0);dfs2(1 , 0 , 0);for(int i = 0 ; i < m ; i ++){if(ans[i]){cout <<"YES\n";}else{cout <<"NO\n";}}init(n);cout << endl;num.clear();
}            
int main() 
{ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout.precision(10);int t=1;cin>>t;while(t--){solve();}return 0;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/188816.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

基于SSM的项目管理系统设计与实现

末尾获取源码 开发语言&#xff1a;Java Java开发工具&#xff1a;JDK1.8 后端框架&#xff1a;SSM 前端&#xff1a;采用JSP技术开发 数据库&#xff1a;MySQL5.7和Navicat管理工具结合 服务器&#xff1a;Tomcat8.5 开发软件&#xff1a;IDEA / Eclipse 是否Maven项目&#x…

C/C++疫情集中隔离 2021年12月电子学会青少年软件编程(C/C++)等级考试一级真题答案解析

目录 C/C疫情集中隔离 一、题目要求 1、编程实现 2、输入输出 二、算法分析 三、程序编写 四、程序说明 五、运行结果 六、考点分析 C/C疫情集中隔离 2021年12月 C/C编程等级考试一级编程题 一、题目要求 1、编程实现 A同学12月初从国外回来&#xff0c;按照防疫要…

SQL Server如何建表

一、数据表的组成 实现完整性的约束有&#xff1a; –6个约束 –非空 not null –主键 primary key –唯一 unique –检查 check –默认 default –主键自增 identity 表约束 主键约束&#xff1a;值不能为null,且不能重复 非空约束&#xff1a;不能为null 默认约束&#xf…

sqlite与mysql的差异

差异点 安装过程&#xff1a;MySQL服务器通常需要单独安装&#xff0c;这涉及下载适用于特定操作系统的MySQL安装程序&#xff0c;运行安装程序并按照指示完成安装过程。SQLite作为嵌入式数据库&#xff0c;可以直接使用其库文件&#xff0c;不需要单独的安装过程。 配置和管理…

SourceTree提示128错误

错误&#xff1a; SourceTree打开报错&#xff1a;git log 失败&#xff0c;错误代码128 错误截图&#xff1a; 解决方法&#xff1a; 第一种&#xff1a; 打开电脑路径 C:\Users\Administrator &#xff0c;删除下面的【.gitconifg】文件 第二种&#xff1a; 如果上述方法…

23111704[含文档+PPT+源码等]计算机毕业设计springboot办公管理系统oa人力人事办公

文章目录 **软件开发环境及开发工具&#xff1a;****功能介绍&#xff1a;****实现&#xff1a;****代码片段&#xff1a;** 编程技术交流、源码分享、模板分享、网课教程 &#x1f427;裙&#xff1a;776871563 软件开发环境及开发工具&#xff1a; 前端技术&#xff1a;jsc…

基于共生生物算法优化概率神经网络PNN的分类预测 - 附代码

基于共生生物算法优化概率神经网络PNN的分类预测 - 附代码 文章目录 基于共生生物算法优化概率神经网络PNN的分类预测 - 附代码1.PNN网络概述2.变压器故障诊街系统相关背景2.1 模型建立 3.基于共生生物优化的PNN网络5.测试结果6.参考文献7.Matlab代码 摘要&#xff1a;针对PNN神…

3.5 Linux 用户管理

1、账号 & 组账号 inux基于用户身份对资源访问进行控制&#xff0c;Linux 属于多用户的操作系统 a. Linux 用户 按建立方式分类&#xff1a; 内建账户: 由系统或程序自行建立的账户自定义账户: 管理员或特权人员手工建立 按权限分类&#xff1a; 特权账户: 有对系统或…

如何编写分层清晰、通用性好的LCD驱动?

网络上配套STM32开发板有很多LCD例程&#xff0c;主要是TFT LCD跟OLED的。从这些例程&#xff0c;大家都能学会如何点亮一个LCD。但这代码都有下面这些问题&#xff1a; 分层不清晰&#xff0c;通俗讲就是模块化太差。 接口乱。只要接口不乱&#xff0c;分层就会好很多了。 可…

〖大前端 - 基础入门三大核心之JS篇㊱〗- JavaScript 的DOM节点操作

说明&#xff1a;该文属于 大前端全栈架构白宝书专栏&#xff0c;目前阶段免费&#xff0c;如需要项目实战或者是体系化资源&#xff0c;文末名片加V&#xff01;作者&#xff1a;不渴望力量的哈士奇(哈哥)&#xff0c;十余年工作经验, 从事过全栈研发、产品经理等工作&#xf…

长距离工业读写器选型要求

RFID在工业领域中的应用有很多&#xff0c;从货物的出入库到库存商品的管理再到产品的生产和销售&#xff0c;都可以看到RFID的身影。具体应用中不同的应用场景下对RFID读写器的要求不同&#xff0c;下面我们就一起来了解一下&#xff0c;长距离工业读写器的应用场景和选型要求…

上机练习 8: DataFrame 综合练习

记录一下做的练习题 目录 1)自定义一个 Series 并命名为 s1&#xff0c;自定义索引值&#xff0c;采用随机数作为其中数据尝试使用 s1.sum(计算其中所有数据的和,使用 s.mean(计算其中所有数据的平均值。 2)创建一个形状为4*6的 DataFrame 并命名为 df1,并指定行索引为[“a”…