IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> Codeforces Global Round 15 -> 正文阅读

[数据结构与算法]Codeforces Global Round 15

A - Subsequence Permutation

题意

问要最少选择多少个字符在选择的区域内排序使得排完序之后成为字典序

题解

我们可以对所有的字符进行排序,在于原来的进行比较,那么原本就成为答案的位置就不需要动了,只需要选择原来不在答案上的就可以

Code

// Problem: A. Subsequence Permutation
// Contest: Codeforces - Codeforces Global Round 15
// URL: https://codeforces.com/contest/1552/problem/A
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// Code by: ING__
// 
// Edited on 2021-08-02 17:54:16

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <sstream>
#define ll long long
#define re return
#define Endl "\n"
#define endl "\n"

using namespace std;

typedef pair<int, int> PII;

int dx[4] = {-1,0,1,0};
int dy[4] = {0,1,0,-1};

int T;
int n;
char s[50];
char ss[50];

int main(){
	cin >> T;
	while(T--){
		cin >> n;
		scanf("%s", s + 1);
		
		
		for(int i = 1; i <= n; i++){
			ss[i] = s[i];
			// cout << ss[i];
		}
		// cout << Endl;
		
		sort(ss + 1, ss + 1 + n);
		int ans = 0;
		for(int i = 1; i <= n; i++){
			if(ss[i] != s[i]){
				ans ++;
			}
		}
		
		cout << ans << endl;
	}
	return 0;
}

B - Running for Gold

题意

题意很长,简单来说就找一个“最强”的来当冠军

题解

如果存在,就跟我们在学语法的时候储存最大值就是一个做法

当出现矛盾的时候呢?即答案要是-1呢?

就如样例给的那个-1的那个样例

构成了一个三角克制的关系

我们如果先确定好一个基准,如果根据这个基准得到的答案是正确的答案,那么显然无论从以哪个为基准,正解一定就是刚才求出那个答案,不存在顺序的问题。

如果存在有方向的克制关系,就以样例为举例来说,以1位基准,从2到n,第二个比第一个差,第三个比第一个好,那么得到的答案是3,但是2比3好这显然也是不合理的;以n为基准,从n-1到1,那么按照刚才那个做法来计算答案,答案应该是1。所以说,只要存在环装的矛盾结构,以不同的基准来得到答案结果应该是不一样的

Code

// Problem: B. Running for Gold
// Contest: Codeforces - Codeforces Global Round 15
// URL: https://codeforces.com/contest/1552/problem/B
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// Code by: ING__
// 
// Edited on 2021-08-02 17:54:20

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <sstream>
#define ll long long
#define re return
#define Endl "\n"
#define endl "\n"

using namespace std;

int T;
int n;
int r[50010][10];

int main(){
	cin >> T;
	while(T--){
		
		cin >> n;
		for(int i = 1; i <= n; i++){
			for(int j = 1; j <= 5; j++){
				cin >> r[i][j];
			}
		}
		
		if(n == 1){
			cout << 1 << endl;
			continue;
		}
		
		int ans = 1;
		
		for(int i = 2; i <= n; i++){
			int cnt = 0;
			for(int j = 1; j <= 5; j++){
				if(r[i][j] < r[ans][j]){
					cnt++;
				}
			}
			if(cnt >= 3){
				ans = i;
			}
		}
		
		int backup = ans;
		ans = n;
		
		for(int i = n - 1; i >= 1; i--){
			int cnt = 0;
			for(int j = 1; j <= 5; j++){
				if(r[i][j] < r[ans][j]){
					cnt++;
				}
			}
			if(cnt >= 3){
				ans = i;
			}
		}
		
		if(ans != backup){
			cout << -1 << endl;
		}
		else cout << ans << Endl;
	}
}

C - Maximize the Intersections

题意

在圆上给定2n个点,k条已经连好的边;每个点只能用一次,也就是只能作为一条边的顶点,问剩下的 2*(n - k) 个点怎么连使得最后交点个数最多,最多是多少

思路

大佬都说这是结论题,愣是看了三份题解才看懂

我们首先考虑怎么才能判断两个线是不是相交呢?

这个也没有什么可以证的,如果遇到这种问题应该首先多画图找找规律,我这里直接放代码理解了,建议看完画图验证一下

int xj(PII x, PII y){
	if(x.x > y.x) swap(x, y);
	return x.y > y.x && x.y < y.y;
}

那么剩下的没有用过的点,我们考虑他们的相对位置,也就是剩下的点本质上我们让他交点个数最大就可以保证最后的交点个数最多,也就是成为一个星形的结果是最大的,证明星形可以去看官方的题解,我不是很能看懂但我觉得星型是对的。(不用考虑原来已经给出的,因为从给出的线 中的交点数已经固定好了。)

那么怎么构造星型呢?画图可以得出来,当有2n个点的时候,构成的点对是 (i, i + n) 的时候是星形的。那么还有2(n - k)个点,就是(i, i + n - k)

Code

// Problem: C. Maximize the Intersections
// Contest: Codeforces - Codeforces Global Round 15
// URL: https://codeforces.com/contest/1552/problem/C
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// Code by: ING__
// 
// Edited on 2021-08-02 17:54:23

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <sstream>
#define ll long long
#define re return
#define Endl "\n"
#define endl "\n"
#define x first
#define y second
#define pb push_back

using namespace std;

typedef pair<int, int> PII;

int dx[4] = {-1,0,1,0};
int dy[4] = {0,1,0,-1};

int T;
int n, k;
bool vis[510];
vector<PII> save;
vector<int> unsave;

int xj(PII x, PII y){
	if(x.x > y.x) swap(x, y);
	return x.y > y.x && x.y < y.y;
}

int main(){
	cin >> T;
	while(T--){
		cin >> n >> k;
		for(int i = 1; i <= k; i++){
			int a, b;
			cin >> a >> b;
			
			if(a > b) swap(a, b); // 统一格式,方便处理
			
			vis[a] = 1;
			vis[b] = 1;
			
			save.pb({a, b});
		}
		
		for(int i = 1; i <= 2 * n; i++){ // 找出没有用过的点
			if(!vis[i]) unsave.pb(i);
		}
		
		for(int i = 0; i < n - k; i++){ //  把新的要连线的点连起来
			save.pb(make_pair(unsave[i], unsave[i + n - k]));
		}
		
		int ans = 0;
		
		for(int i = 0; i < n; i++){ // 统计答案
			for(int j = i + 1; j < n; j++){
				ans += xj(save[i], save[j]);
			}
		}
		
		cout << ans << Endl;
		
		save.clear();
		unsave.clear();
		memset(vis, 0, sizeof(vis));
	}
}

D 待补

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-08-04 11:27:49  更:2021-08-04 11:27:55 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 18:16:46-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码