2019 南京网络赛
F、Greedy Sequence
You're given a permutation aa of length nn (1 \le n \le 10^51≤n≤105).
For each i \in [1,n]i∈[1,n], construct a sequence s_isi by the following rules:
- s_i[1]=isi[1]=i;
- The length of s_isi is nn, and for each j \in [2, n]j∈[2,n], s_i[j] \le s_i[j-1]si[j]≤si[j−1];
- First, we must choose all the possible elements of s_isi from permutation aa. If the index of s_i[j]si[j] in permutation aa is pos[j]pos[j], for each j \ge 2j≥2, |pos[j]-pos[j-1]|\le k∣pos[j]−pos[j−1]∣≤k (1 \le k \le 10^51≤k≤105). And for each s_isi, every element of s_isi must occur in aa at most once.
- After we choose all possible elements for s_isi, if the length of s_isi is smaller than nn, the value of every undetermined element of s_isi is 00;
- For each s_isi, we must make its weight high enough.
Consider two sequences C = [c_1, c_2, ... c_n]C=[c1,c2,...cn] and D=[d_1, d_2, ..., d_n]D=[d1,d2,...,dn], we say the weight of CC is higher thanthat of DD if and only if there exists an integer kk such that 1 \le k \le n1≤k≤n, c_i=d_ici=di for all 1 \le i < k1≤i<k, and c_k > d_kck>dk.
If for each i \in [1,n]i∈[1,n], c_i=d_ici=di, the weight of CC is equal to the weight of DD.
For each i \in [1,n]i∈[1,n], print the number of non-zero elements of s_isi separated by a space.
It's guaranteed that there is only one possible answer.
Input
There are multiple test cases.
The first line contains one integer T(1 \le T \le 20)T(1≤T≤20), denoting the number of test cases.
Each test case contains two lines, the first line contains two integers nn and kk (1 \le n,k \le 10^51≤n,k≤105), the second line contains nn distinct integers a_1, a_2, ..., a_na1,a2,...,an (1 \le a_i \le n1≤ai≤n) separated by a space, which is the permutation aa.
Output
For each test case, print one line consists of nn integers |s_1|, |s_2|, ..., |s_n|∣s1∣,∣s2∣,...,∣sn∣ separated by a space.
|s_i|∣si∣ is the number of non-zero elements of sequence s_isi.
There is no space at the end of the line.
样例输入
2 3 1 3 2 1 7 2 3 1 4 6 2 5 7
样例输出
1 2 3 1 1 2 3 2 3 3
题意:给n个数和一个数k,找n个符合条件的si序列,问si序列中非0元素的值的个数
条件:si序列的第一个值是 i;从i开始找小于i的数x, 且位置满足 i_pos-k<=x_pos<=i_pos+k;保证字典序最大,依次往下找直到结束
题解:暴力+排序
#include<iostream> #include<string.h> #include<string> #include<algorithm> #include<math.h> #include<string> #include<string.h> #include<vector> #include<utility> #include<map> #include<queue> #include<set> #define mx 0x3f3f3f3f #define ll long long #define MAXN 100 using namespace std; string s,ss; int a[100005],b[100005]; int n,k,t; struct node { int val; int pos; }p[100005]; bool cmp(node x,node y) { return x.val<y.val; } int find(int x) { int ans=1,len=1; while(1) { if(x<=1) break; else if(x-len>=1&&abs(a[p[x-len].val]-a[p[x].val])<=k) { if(b[x-len]!=0)//直接使用前面处理过的数据,避免超时 { ans=ans+b[x-len]; return ans; } ans++; x=x-len; len=1; continue; } else if(x-len<1) break; len++; } return ans; } int main() { cin>>t; while(t--) { cin>>n>>k; for(int i=1;i<=n;i++) { cin>>p[i].val; p[i].pos=i; a[p[i].val]=i; } sort(p+1,p+n+1,cmp); for(int i=1;i<=n;i++) b[i]=find(i); for(int i=1;b[i];i++) { if(i==1) cout<<b[i]; else cout<<' '<<b[i]; } cout<<endl; } return 0; }
H、Holy Grail
As the current heir of a wizarding family with a long history,unfortunately, you find yourself forced to participate in the cruel Holy Grail War which has a reincarnation of sixty years.However,fortunately,you summoned a Caster Servant with a powerful Noble Phantasm.When your servant launch her Noble Phantasm,it will construct a magic field,which is actually a directed graph consisting of n vertices and m edges.More specifically,the graph satisfies the following restrictions :
- Does not have multiple edges(for each pair of vertices x and y, there is at most one edge between this pair of vertices in the graph) and does not have self-loops(edges connecting the vertex with itself).
- May have negative-weighted edges.
- Does not have a negative-weighted loop.
- n<=300 , m<=500.
Currently,as your servant's Master,as long as you add extra 6 edges to the graph,you will beat the other 6 masters to win the Holy Grail.
However,you are subject to the following restrictions when you add the edges to the graph:
- Each time you add an edge whose cost is c,it will cost you c units of Magic Value.Therefore,you need to add an edge which has the lowest weight(it's probably that you need to add an edge which has a negative weight).
- Each time you add an edge to the graph,the graph must not have negative loops,otherwise you will be engulfed by the Holy Grail you summon.
Input
Input data contains multiple test cases. The first line of input contains integer t — the number of testcases (1 \le t \le 51≤t≤5).
For each test case,the first line contains two integers n,m,the number of vertices in the graph, the initial number of edges in the graph.
Then m lines follow, each line contains three integers x, y and w (0 \le x,y<n0≤x,y<n,-10^9−109≤w≤10^9109, x \not = yx=y) denoting an edge from vertices x to y (0-indexed) of weight w.
Then 6 lines follow, each line contains two integers s,t denoting the starting vertex and the ending vertex of the edge you need to add to the graph.
It is guaranteed that there is not an edge starting from s to t before you add any edges and there must exists such an edge which has the lowest weight and satisfies the above restrictions, meaning the solution absolutely exists for each query.
Output
For each test case,output 66 lines.
Each line contains the weight of the edge you add to the graph.
样例输入
1 10 15 4 7 10 7 6 3 5 3 3 1 4 11 0 6 20 9 8 25 3 0 9 1 2 15 9 0 27 5 2 0 7 3 -5 1 7 21 5 0 1 9 3 16 1 8 4 4 1 0 3 6 9 2 1 8 7 0 4
样例输出
-11 -9 -45 -15 17 7
题意:直接求y到x的最短路,如果为正输出负,为负输出正,然后再加上x到y的边
#include<iostream> #include<string.h> #include<string> #include<algorithm> #include<queue> #define ll long long #define mx 0x3f3f3f3f using namespace std; int way[520][520],dis[520],vis[520],cnt[520]; //way记录路径关系,dis[i]记录起点到点j的最近距离,vis[i]标记点是否在队列中,cnt[i]记录点i进入队列的次数 int n,m; void init() { for(int i=0;i<n;i++)//先初始化way { for(int j=0;j<n;j++) { if(i==j) way[i][j]=0; else way[i][j]=mx; } } } void spfa(int st) { for(int i=0;i<n;i++)//这里点编号是从0开始的 dis[i]=mx; memset(vis,0,sizeof(vis)); memset(cnt,0,sizeof(cnt)); vis[st]=1; cnt[st]=1; dis[st]=0; queue<int>p; p.push(st); while(!p.empty()) { int now=p.front(); p.pop(); vis[now]=0; for(int i=0;i<n;i++) { if(dis[now]+way[now][i]<dis[i]) { dis[i]=dis[now]+way[now][i]; if(vis[i]==0)//如果点不在队列里面 { p.push(i); vis[i]=1; cnt[i]++; if(cnt[i]>n)//如果这个点加入超过n次,说明存在负圈,直接返回 return ; } } } } } int main() { int t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); init(); for(int i=0;i<m;i++) { int x,y,z; scanf("%d%d%d",&x,&y,&z); way[x][y]=z; } for(int i=0;i<6;i++) { int x,y; scanf("%d%d",&x,&y); spfa(y);//求y到x的最短路 printf("%d\n",-1*dis[x]); way[x][y]=-1*dis[x];//加边 } } return 0; }
原文地址:https://www.cnblogs.com/-citywall123/p/11453164.html
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 基于thinkphp6.0的success、error实现方法
- Yii框架模拟组件调用注入示例
- 解决laravel 表单提交-POST 异常的问题
- laravel5.0在linux下解决.htaccess无效和去除index.php的问题
- laravel返回统一格式错误码问题
- 使用 PHP Masked Package 屏蔽敏感数据的实现方法
- PHP简单实现图片格式转换(jpg转png,gif转png等)
- 在thinkphp5.0路径中实现去除index.php的方式
- Laravel5.5 手动分页和自定义分页样式的简单实现
- laravel自定义分页的实现案例offset()和limit()
- Laravel6.0.4中将添加计划任务事件的方法步骤
- Laravel 不同生产环境服务器的判断实践
- 解决thinkPHP 5 nginx 部署时,只跳转首页的问题
- Laravel 类和接口注入相关的代码
- laravel unique验证、确认密码confirmed验证以及密码修改验证的方法