【CodeForces - 545 ABCDE套题训练题解】贪心, 构造,模拟,dp,最短路树(Dijkstra+变形)

A:

题干:

Input

The first line contains integer n (1 ≤ n ≤ 100) — the number of cars.

Each of the next n lines contains n space-separated integers that determine matrix A.

It is guaranteed that on the main diagonal there are  - 1, and  - 1 doesn't appear anywhere else in the matrix.

It is guaranteed that the input is correct, that is, if Aij = 1, then Aji = 2, if Aij = 3, then Aji = 3, and if Aij = 0, then Aji = 0.

Output

Print the number of good cars and in the next line print their space-separated indices in the increasing order.

Examples

Input

3
-1 0 0
0 -1 1
0 2 -1

Output

2
1 3 

Input

4
-1 3 3 3
3 -1 3 3
3 3 -1 3
3 3 3 -1

Output

0

题目大意:

  给定一个N*N的矩阵a来描述N辆车的关系,如果a(i,j)=0代表这两辆车都没翻车,=1代表i车翻车了,=2代表j车翻车了,=3代表都翻车了。问你N辆车中有多少车是完好无损的?

解题报告:

   模拟、、

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 2e5 + 5;
int n;
int maze[555][555],qq[MAX];
int main()
{cin>>n;for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) {scanf("%d",&maze[i][j]);}}int ans = 0;for(int i = 1;i<=n; i++) {bool flag = 1;for(int j = 1; j<=n; j++) {if(i == j) continue;if(maze[i][j] == 1 || maze[i][j] == 3) flag = 0;}if(flag) ++ans,qq[ans] = i;}printf("%d\n",ans);for(int i = 1; i<=ans; i++) {printf("%d%c",qq[i],i == ans ? '\n' : ' ');}return 0 ;}

B:

题干:

Little Susie loves strings. Today she calculates distances between them. As Susie is a small girl after all, her strings contain only digits zero and one. She uses the definition of Hamming distance:

We will define the distance between two strings s and t of the same length consisting of digits zero and one as the number of positions i, such that si isn't equal to ti.

As besides everything else Susie loves symmetry, she wants to find for two strings sand t of length n such string p of length n, that the distance from p to s was equal to the distance from p to t.

It's time for Susie to go to bed, help her find such string p or state that it is impossible.

Input

The first line contains string s of length n.

The second line contains string t of length n.

The length of string n is within range from 1 to 105. It is guaranteed that both strings contain only digits zero and one.

Output

Print a string of length n, consisting of digits zero and one, that meets the problem statement. If no such string exist, print on a single line "impossible" (without the quotes).

If there are multiple possible answers, print any of them.

Examples

Input

0001
1011

Output

0011

Input

000
111

Output

impossible

Note

In the first sample different answers are possible, namely — 0010, 0011, 0110, 0111, 1000, 1001, 1100, 1101.

题目大意:

 先给出了汉明距离的定义,然后输入s,t两个字符串。让你构造一个字符串p,st. Hamming(s,p) == Hamming(p.t)。

解题报告:

  贪心就行了、

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 2e5 + 5;
char s[MAX],t[MAX];
int main()
{cin>>(s+1);cin>>(t+1);int qq = 0;int len = strlen(s+1);for(int i = 1; i<=len; i++) {if(s[i] != t[i]) qq++;}	if(qq%2 == 1) puts("impossible");else {int cur = 0;for(int i = 1; i<=len; i++) {if(s[i] != t[i] && cur*2 < qq) {cur++;printf("%c",t[i]);}else printf("%c",s[i]);}}return 0 ;}

C:

题干:

Little Susie listens to fairy tales before bed every day. Today's fairy tale was about wood cutters and the little girl immediately started imagining the choppers cutting wood. She imagined the situation that is described below.

There are n trees located along the road at points with coordinates x1, x2, ..., xn. Each tree has its height hi. Woodcutters can cut down a tree and fell it to the left or to the right. After that it occupies one of the segments [xi - hi, xi] or [xi;xi + hi]. The tree that is not cut down occupies a single point with coordinate xi. Woodcutters can fell a tree if the segment to be occupied by the fallen tree doesn't contain any occupied point. The woodcutters want to process as many trees as possible, so Susie wonders, what is the maximum number of trees to fell.

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of trees.

Next n lines contain pairs of integers xi, hi (1 ≤ xi, hi ≤ 109) — the coordinate and the height of the і-th tree.

The pairs are given in the order of ascending xi. No two trees are located at the point with the same coordinate.

Output

Print a single number — the maximum number of trees that you can cut down by the given rules.

Examples

Input

5
1 2
2 1
5 10
10 9
19 1

Output

3

Input

5
1 2
2 1
5 10
10 9
20 1

Output

4

Note

In the first sample you can fell the trees like that:

  • fell the 1-st tree to the left — now it occupies segment [ - 1;1]
  • fell the 2-nd tree to the right — now it occupies segment [2;3]
  • leave the 3-rd tree — it occupies point 5
  • leave the 4-th tree — it occupies point 10
  • fell the 5-th tree to the right — now it occupies segment [19;20]

In the second sample you can also fell 4-th tree to the right, after that it will occupy segment [10;19].

题目大意:

无限长的数轴上有n个点,每个点的坐标为x[i],种有高度为h[i]的树,现在要把一些树砍到,被砍倒的树要么倒向左边,要么倒向右边,会分别把[xi - hi, xi] 和 [xi,xi + hi]占用,如果某棵树不被砍倒,那么它就只占用x[i]这一个点的位置,现在给定你n个点的x[i],h[i],问最多能砍倒几棵树?(n<=1e5, x[i],h[i]<=1e9)

解题报告:

  多阶段决策问题,显然dp。(不过这题好像贪心也能解决?因为关于第i棵树的摆放,看他能不能往左倒,能倒就倒,否则看他如果往右倒那后面那颗能不能往左倒,,如果同时可以那就可以往右倒,如果左边这个不可以倒,那就看右边,如果左边这个可以倒,那就一定倒。(为什么呢?因为你可以倒,那么就算你不倒,右边那个如果往左倒,答案相同且对后面没有影响,如果直立,答案还不如这种选择,如果往右倒,那更不如这种选择(因为这段空间就空着了啊)),,这样贪心就行了,,但是显然没有dp简单。)

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 2e5 + 5;
int n;
ll x[MAX],h[MAX];
ll dp[MAX][3];//状态表示:0不倒 1左边 2右边 
ll Max(ll a,ll b,ll c) {return max(a,max(b,c));
}
int main()
{cin>>n;for(int i = 1; i<=n; i++) scanf("%lld %lld",x+i,h+i);x[0] = -0x3f3f3f3f;x[n+1] = 0x3f3f3f3f3f3f;ll ans = 0;for(int i = 1; i<=n; i++) {dp[i][0] = Max(dp[i-1][0],dp[i-1][1],dp[i-1][2]);if(x[i+1] - x[i] > h[i]) dp[i][2] = dp[i][0] + 1;else dp[i][2] = 0;dp[i][1] = max(dp[i-1][0],dp[i-1][2]);if(x[i] - x[i-1] > h[i]) dp[i][1] = max(dp[i][1],max(dp[i-1][1],dp[i-1][0])+1);if(x[i] - x[i-1] > h[i] + h[i-1]) dp[i][1] = max(dp[i][1],dp[i-1][2]+1);}printf("%lld\n",Max(dp[n][0],dp[n][1],dp[n][2]));return 0 ;}

D:

题干:

Little girl Susie went shopping with her mom and she wondered how to improve service quality.

There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed.

Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue.

Input

The first line contains integer n (1 ≤ n ≤ 105).

The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces.

Output

Print a single number — the maximum number of not disappointed people in the queue.

Examples

Input

5
15 2 1 5 3

Output

4

Note

Value 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5.

题目大意:

有n个人,每个人都有一个等待时间,如果对于当前的人来说总等待时间超过自己的等待时间,那么这个人就会失望,问换一下顺序,使失望的人最少,问最多有多少个人不失望。

解题报告:

   贪心,首先根据直觉排序,,然后如果他不失望那更好,如果他失望了,那么反正他也得失望,我们还不如把他换到最后去让他失望的更多一些,所以扫一遍就行了。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 2e5 + 5;
int n;
ll a[MAX];
int main()
{cin>>n;for(int i = 1; i<=n; i++) scanf("%lld",a+i);sort(a+1,a+n+1);ll cur = 0,ans = 0;for(int i = 1; i<=n; i++) {if(cur <= a[i]) {ans++;cur += a[i];}}printf("%lld\n",ans);return 0 ;}

 


E:

题干:

Little girl Susie accidentally found her elder brother's notebook. She has many things to do, more important than solving problems, but she found this problem too interesting, so she wanted to know its solution and decided to ask you about it. So, the problem statement is as follows.

Let's assume that we are given a connected weighted undirected graph G = (V, E) (here V is the set of vertices, E is the set of edges). The shortest-path tree from vertex u is such graph G1 = (V, E1) that is a tree with the set of edges E1 that is the subset of the set of edges of the initial graph E, and the lengths of the shortest paths from u to any vertex to G and to G1 are the same.

You are given a connected weighted undirected graph G and vertex u. Your task is to find the shortest-path tree of the given graph from vertex u, the total weight of whose edges is minimum possible.

Input

The first line contains two numbers, n and m (1 ≤ n ≤ 3·105, 0 ≤ m ≤ 3·105) — the number of vertices and edges of the graph, respectively.

Next m lines contain three integers each, representing an edge — ui, vi, wi — the numbers of vertices connected by an edge and the weight of the edge (ui ≠ vi, 1 ≤ wi ≤ 109). It is guaranteed that graph is connected and that there is no more than one edge between any pair of vertices.

The last line of the input contains integer u (1 ≤ u ≤ n) — the number of the start vertex.

Output

In the first line print the minimum total weight of the edges of the tree.

In the next line print the indices of the edges that are included in the tree, separated by spaces. The edges are numbered starting from 1 in the order they follow in the input. You may print the numbers of the edges in any order.

If there are multiple answers, print any of them.

Examples

Input

3 3
1 2 1
2 3 1
1 3 2
3

Output

2
1 2 

Input

4 4
1 2 1
2 3 1
3 4 1
4 1 2
4

Output

4
2 3 4 

Note

In the first sample there are two possible shortest path trees:

  • with edges 1 – 3 and 2 – 3 (the total weight is 3);
  • with edges 1 – 2 and 2 – 3 (the total weight is 2);

And, for example, a tree with edges 1 – 2 and 1 – 3 won't be a shortest path tree for vertex 3, because the distance from vertex 3 to vertex 2 in this tree equals 3, and in the original graph it is 1.

题目大意:

给定一个n个结点m条边的无向图,并给出源点s,让你找出图中权值最小的最短路树,并输出这个权值,和选择的边的编号。

定义最短路树:设最短路的图为图G,最短路树的图为图Q,则源点s到G中任意一个顶点的距离,应该等于s到Q中任意一个顶点的距离,且Q为一棵树。

解题报告:

  首先不难证明,这个其实就是在所有最短路的可能的边中去选择其中的一些,其实可以看成是松弛(将一个点松弛成两个点,也就是将边数变得越多越好,当然,这是在保证最短路的基础之上的)。所以我们可以在Dijkstra的时候同时维护边权的最小值(也就是如果dis[] > dis[] + e[i].w 更新,else if dis[] == dis[] + e[i].w,则取权值最小,并且将该边的编号记录到对应的e[i].to对应的数组上,Dijkstra完了之后就结束了,最后按要求输出就行了)。

  这题我选择了另外一种做法,先跑一边裸的Dijkstra,然后我们得到了最短路数组dis,然后枚举除了起点之外的每一个点,然后枚举他的每一条边,并记录它到起点的那条边的边权最小值,这条边一定选在这棵树内,最后按要求输出。这样写代码更好实现一些。其实两个方法是等价的。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#include<ctime>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 6e5 + 5;
struct Edge {int to,ne,ii;ll w;
} e[MAX];
int head[MAX],tot,n,m;
bool vis[MAX];
int ans[MAX],cnt;
ll dis[MAX];
void add(int u,int v,ll w,int i) {e[++tot].to = v;e[tot].ne = head[u];e[tot].w = w;e[tot].ii = i;head[u] = tot; 
}
struct Point {int id;ll c;Point(){}Point(int id,ll c):id(id),c(c){}bool operator<(const Point & b) const{return c > b.c;}
};
void Dijkstra(int st) {for(int i = 1; i<=n; i++) dis[i] = LLONG_MAX;dis[st] = 0;priority_queue<Point> pq;pq.push(Point(st,0));while(!pq.empty()) {Point cur = pq.top();pq.pop();if(vis[cur.id]) continue;vis[cur.id] = 1;for(int i = head[cur.id]; i!=-1; i = e[i].ne) {int v = e[i].to;if(dis[v] > dis[cur.id] + e[i].w) {dis[v] = dis[cur.id] + e[i].w;pq.push(Point(v,dis[v]));}}}
}
int main()
{int u,v;memset(head,-1,sizeof head);ll w;cin>>n>>m;for(int i = 1; i<=m; i++) {scanf("%d%d%lld",&u,&v,&w);add(u,v,w,i);add(v,u,w,i);}cin>>u;Dijkstra(u);int minid;ll ANS = 0;for(int i = 1; i<=n; i++) {if(i == u) continue;ll minn = LLONG_MAX;for(int j = head[i]; j != -1; j = e[j].ne) {if(dis[i] == dis[e[j].to] + e[j].w && minn > e[j].w) {minn = e[j].w;minid = e[j].ii;}}ANS += minn;ans[++cnt] = minid;}printf("%lld\n",ANS);for(int i = 1; i<=cnt; i++) {printf("%d ",ans[i]);}return 0 ;}

 

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

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

相关文章

linux搜索pdf文件,桌面应用|如何使用 pdfgrep 从终端搜索 PDF 文件

诸如 grep 和 ack-grep 之类的命令行工具对于搜索匹配指定正则表达式的纯文本非常有用。但是你有没有试过使用这些工具在 PDF 中搜索&#xff1f;不要这么做&#xff01;由于这些工具无法读取PDF文件&#xff0c;因此你不会得到任何结果。它们只能读取纯文本文件。顾名思义&…

【CodeForces - 546C 】Soldier and Cards (模拟)

题干&#xff1a; Two bored soldiers are playing card war. Their card deck consists of exactly n cards, numbered from 1 to n, all values are different. They divide cards between them in some manner, its possible that they have different number of cards. Th…

linux wifi 蓝牙冲突,linux 下 无线 wifi 蓝牙 无法启用

linux 下 无线 wifi 蓝牙 无法启用装了Debian squeeze 后发现无线不能打开首先想到的是装驱动于是在wiki.debian.org上查了下以重新装了下驱动#aptitude install firmware-b43-installler#modprobe b43# iwconfiglo no wireless extensions.eth0 no wireless exten…

Linux中wait接口用于延时,linux2.6驱动编写参考

1、 使用新的入口必须包含 module_init(your_init_func);module_exit(your_exit_func);老版本&#xff1a;int init_module(void);void cleanup_module(voi);2.4中两种都可以用&#xff0c;对如后面的入口函数不必要显示包含任何头文件。2、 GPLMODULE_LICENSE("Dual BSD/…

【51nod - 1108】距离之和最小 V2(曼哈顿距离,中位数性质)

题干&#xff1a; 三维空间上有N个点, 求一个点使它到这N个点的曼哈顿距离之和最小&#xff0c;输出这个最小的距离之和。 点(x1,y1,z1)到(x2,y2,z2)的曼哈顿距离就是|x1-x2| |y1-y2| |z1-z2|。即3维坐标差的绝对值之和。 收起 输入 第1行&#xff1a;点的数量N。(2 <…

Linux实验室阿里云证书,开发者云体验实验室

{"data":[{"title":"技术领域","data":[{"title":"全部","key":1,"children":[{"title":"程序语言","key":12,"children":[{"title":&qu…

【OpenJudge - noi - 7624】山区建小学(dp)

题干&#xff1a; 总时间限制: 1000ms 内存限制: 65536kB 描述 政府在某山区修建了一条道路&#xff0c;恰好穿越总共m个村庄的每个村庄一次&#xff0c;没有回路或交叉&#xff0c;任意两个村庄只能通过这条路来往。已知任意两个相邻的村庄之间的距离为di&#xff08;为…

nuc8i7beh安装linux随机重启,【图片】来分享一下我的NUC8I7BEH【intelnuc吧】_百度贴吧...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼测试来了&#xff0c;Linux 脚本&#xff0c;2G测试&#xff0c;操完这次以后休息俩礼拜。ssd随机读4kfio -filename./ran4K.log -direct1 -iodepth 1 -thread -rwrandread -ioenginepsync -bs4k -size2G -numjobs10 -runtime1000 …

【Codeforces - 找不到题号】三元环计数(bitset优化,压位)

题干&#xff1a; 给你一个二维字符矩阵&#xff0c;如果 ( i , j ) 为 表明 两点之间有一条有向边&#xff0c;为-表示没有边&#xff0c;那么你要找出所有的三元环的个数。顶点数N<1500。 解题报告&#xff1a; 考虑最暴力的方法&#xff0c;开个二维数组来存每两个顶点之…

自定义函数删除字母C语言,[编程入门]自定义函数之字符提取-题解(C语言代码)...

解题思路:输入一个字符串&#xff0c;调用函数&#xff0c;遍历字符串中每一个字符&#xff0c;看是否含有aeiou字符&#xff0c;若有&#xff0c;将其保存到另一个字符型数组中&#xff0c;在主函数中对得到的字符型数组进行排序&#xff0c;输出。注意事项:题目要求顺序输出元…

【Hihocoder - offer编程练习赛39 - D】前缀后缀查询(后缀字典树,哈希,思维)

题干&#xff1a; 时间限制:10000ms 单点时限:1000ms 内存限制:512MB 描述 给定一个包含N个单词的字典:{W1, W2, W3, ... WN}&#xff0c;其中第i个单词Wi有具有一个权值Vi。 现在小Hi要进行M次查询&#xff0c;每次查询包含一个前缀字符串Pi和一个后缀字符串Si。他希望…

c 语言 while break,26 C 语言中的break和continue - C 语言基础教程

循环语句很好用&#xff0c;但是如果循环进行到一般想要跳出循环或者结束循环怎么办&#xff1f;那么那你需要 break 和 continue 语句。1. break 和 continue 的使用语法1.1 or 循环中使用 break 和 continuebreakfor (控制循环的变量; 循环判断条件; 循环变量增减变化){语句1…

【牛客 - 21302】被3整除的子序列(线性dp)

题干&#xff1a; 给你一个长度为50的数字串,问你有多少个子序列构成的数字可以被3整除 答案对1e97取模 输入描述: 输入一个字符串&#xff0c;由数字构成&#xff0c;长度小于等于50 输出描述: 输出一个整数 示例1 输入 复制 132 输出 复制 3 示例2 输入 复制 …

c语言链表实现数组逆置,数组与链表等顺序表逆置

一)数组的逆置(1)算法#indclude#define N 8main(){int array[N] {100,90,80,70,60,50,50,40};int i,j,t;for(i0,jN-1;i{t array[i];array[i] array[j];array[j] t;}for(i0,iprintf("%d",qlist.data[i])}(2)时间复杂度由于只需循环N/2即可完成逆置,所以时…

linux系统get命令详解,Ubuntu Linux系统下apt-get命令详解

整理了Ubuntu Linux操作系统下apt-get命令的详细说明,分享给大家。常用的APT命令参数&#xff1a;apt-cache search package 搜索包apt-cache show package 获取包的相关信息&#xff0c;如说明、大小、版本等sudo apt-get install package 安装包sudo apt-get install package…

*【Hihocoder - offer编程练习赛94 - A】最短管道距离(中位数)

题干&#xff1a; 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在一张2D地图上有N座城市&#xff0c;坐标依次是(X1, Y1), (X2, Y2), ... (XN, YN)。 现在H国要修建一条平行于X轴的天然气主管道。这条管道非常长&#xff0c;可以认为是一条平行于X轴的直线。…

android开发百度地图坐标偏差,利用百度地图Android sdk高仿微信发送位置功能及遇到的问题...

接触了百度地图开发平台半个月了&#xff0c;这2天试着模仿了微信给好友发送位置功能&#xff0c;对百度地图的操作能力又上了一个台阶我在实现这个功能的时候&#xff0c;遇到一些困难&#xff0c;可能也是别人将会遇到的困难&#xff0c;特在此列出1、在微信发送功能中&#…

*【牛客 1 - A】矩阵(字符串hash)

题干&#xff1a; 给出一个n * m的矩阵。让你从中发现一个最大的正方形。使得这样子的正方形在矩阵中出现了至少两次。输出最大正方形的边长。 输入描述: 第一行两个整数n, m代表矩阵的长和宽&#xff1b; 接下来n行&#xff0c;每行m个字符&#xff08;小写字母&#xff…

*【洛谷 - P1025】数的划分(dfs 或 dp 或 母函数,第二类斯特林数Stirling)

题干&#xff1a; 题目描述 将整数n分成k份&#xff0c;且每份不能为空&#xff0c;任意两个方案不相同(不考虑顺序)。 例如&#xff1a;n7&#xff0c;k3&#xff0c;下面三种分法被认为是相同的。 1,1,5 1,5,1 5,1,1 问有多少种不同的分法。 输入输出格式 输入格式&am…

kali linux 截图 软件,Kali-Linux-Tools-Interface:针对Kali Linux的图形化Web接口

Kali-Linux-Tools-Interface在当今这个信息时代&#xff0c;数据是最有价值的资产&#xff0c;因此&#xff0c;广大用户和企业已成为网络攻击的主要目标。众所周知&#xff0c;信息安全专业人员都会使用一系列技术工具来协助他们的活动。但是设置环境&#xff0c;安装这些工具…