Codeforces Round #647 (Div. 2) - Thanks, Algo Muse! D. Johnny and Contribution (贪心,模拟)

题意:有\(n\)个点,\(m\)条边,现在要给这些点赋值,,每次只能赋给某一点的四周(所连边)的最小没出现过的值.如果不能按照所给的数赋值,输出\(-1\),否则输出赋值顺序.
题解:我们用\(pair\)记录最后一行所给的数和位置,不难想,每次肯定是赋最小的数,所以我们对其排序,然后遍历取位置,看这个位置周围能赋的值是否满足条件即可.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define me memset
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
using namespace std;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL; int n,m;
int a,b;
int num[N];
vector<PII> mp;
vector<int> v[N];
vector<int> ans; int check(int pos){
set<int> tmp;
for(auto w:v[pos]){
tmp.insert(num[w]);
}
num[pos]=1;
while(tmp.count(num[pos]))
num[pos]++;
return num[pos];
} int main() {
ios::sync_with_stdio(false);cin.tie(0);
cin>>n>>m;
for(int i=1;i<=m;++i){
cin>>a>>b;
v[a].pb(b);
v[b].pb(a);
}
for(int i=1;i<=n;++i){
int x;
cin>>x;
mp.pb({x,i});
}
sort(mp.begin(),mp.end());
for(auto w:mp){
if(check(w.se)!=w.fi){
puts("-1");
return 0;
}
else ans.pb(w.se);
}
for(auto w:ans) printf("%d ",w); return 0;
}
Codeforces Round #647 (Div. 2) - Thanks, Algo Muse! D. Johnny and Contribution (贪心,模拟)的更多相关文章
- Codeforces Round #647 (Div. 2) - Thanks, Algo Muse! C. Johnny and Another Rating Drop (规律,二进制)
题意:有一个正整数\(n\),要求写出所有\(1\)~\(n\)的二进制数,统计相邻的两个二进制同位置上不同数的个数. 题解:打表找规律,不难发现: \(00000\) \(00001\) ...
- Codeforces Round #647 (Div. 2) - Thanks, Algo Muse! A、Johnny and Ancient Computer B、Johnny and His Hobbies C、Johnny and Another Rating Drop
题目链接:A.Johnny and Ancient Computer 题意: 给你两个数a,b.问你可不可以通过左移位运算或者右移位运算使得它们两个相等.可以的话输出操作次数,不可以输出-1 一次操作 ...
- Codeforces Round #547 (Div. 3) F1/2. Same Sum Blocks (Easy/Hard) (贪心,模拟)
题意:有一长度为\(n\)的数组,求最多的区间和相同的不相交的区间的个数. 题解:我们可以先求一个前缀和,然后第一层循环遍历区间的右端点,第二层循环枚举左端点,用前缀和来\(O(1)\)求出区间和,\ ...
- Codeforces Round #647 (Div. 2) A. Johnny and Ancient Computer
题目链接:https://codeforces.com/contest/1362/problem/A 题意 有一个正整数 $a$,可选择的操作如下: $a \times 2$ $a \times 4$ ...
- Codeforces Round #647 (Div. 2)
Problem A https://codeforces.com/contest/1362/problem/A 判断x/y是不是2的k次方, 如果是 k/3 + (k%3)/2 + (k%3%2)即为 ...
- Codeforces Round #647 (Div. 2) D. Johnny and Contribution(BFS)
题目链接:https://codeforces.com/contest/1362/problem/D 题意 有一个 $n$ 点 $m$ 边的图,每个结点有一个从 $1 \sim n$ 的指定数字,每个 ...
- Codeforces Round #647 (Div. 2) C. Johnny and Another Rating Drop(数学)
题目链接:https://codeforces.com/contest/1362/problem/C 题意 计算从 $0$ 到 $n$ 相邻的数二进制下共有多少位不同,考虑二进制下的前导 $0$ .( ...
- Codeforces Round #647 (Div. 2) B. Johnny and His Hobbies(枚举)
题目链接:https://codeforces.com/contest/1362/problem/B 题意 有一个大小及元素值均不超过 $1024$ 的正整数集合,求最小正整数 $k$,使得集合中的每 ...
- Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] B. "Or" Game 线段树贪心
B. "Or" Game Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/578 ...
随机推荐
- LR参数
一.LR函数 : lr_start_transaction: 为性能分析标记事务的开始 lr_end_transaction: 为性能分析标记事务的结束:事务名称与事务开始时保持一致 lr_ren ...
- ctfshow——web_AK赛
签到_观己 从题目描述中没发现什么有用的信息 发现文件包含 尝试使用PHP伪协议执行命令,发现无法执行 尝试使用远程文件包含,发现也未开启 尝试使用日志注入 记录了UA值,抓包写入一句话木马 使用蚁剑 ...
- Java优先队列PriorityQueue的各种打开方式以及一些你不知道的细节
目录 Java优先队列PriorityQueue的各种打开方式以及一些你不知道的细节 优先队列的默认用法-从小到大排序 对String类用优先队列从大到小排序 通过自定义比较器对自定义的类进行从小到大 ...
- 阿里云RDS物理备份恢复到本地
一:业务场景 验证阿里云备份文件可用性 二:恢复到本地过程中遇到的问题 1.修改密码报错 2.自定义函数不可用 三:恢复步骤 1.xtrabackup安装使用 请参考:https://www.cnbl ...
- Vue 3自定义指令开发
本文由葡萄城技术团队原创并首发 转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 什么是指令(directive) 在Angular和Vue中都有Direct ...
- Swagger2配置与使用
Swagger2配置与使用 Swagger2介绍 前后端分离开发模式中,api文档是最好的沟通方式. Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 We ...
- (011)每日SQL学习:SQL开窗函数
开窗函数:在开窗函数出现之前存在着很多用 SQL 语句很难解决的问题,很多都要通过复杂的相关子查询或者存储过程来完成.为了解决这些问题,在 2003 年 ISO SQL 标准加入了开窗函数,开窗函数的 ...
- Group by 优化
一个标准的 Group by 语句包含排序.分组.聚合函数,比如 select a,count(*) from t group by a ; 这个语句默认使用 a 进行排序.如果 a 列没有索引,那 ...
- SpringMVC听课笔记(六:视图和试图解析器)
1.spring mvc解析视图 2. 视图和视图解析器 3. 视图 4.常用的视图类 5.视图解析器 1) 2) 3) 4)JSTL 需要注意的是,配置了mvc:view-controller,为 ...
- Java进阶专题(二十二) 从零开始搭建一个微服务架构系统 (上)
前言 "微服务"一词源于 Martin Fowler的名为 Microservices的,博文,可以在他的官方博客上找到http:/ /martinfowler . com/art ...