A. Minimizing the String
time limit per test

1 second

memory limit per test

256 megabytes

Description:

You are given a string ss consisting of nn lowercase Latin letters.

You have to remove at most one (i.e. zero or one) character of this string in such a way that the string you obtain will be lexicographically smallest among all strings that can be obtained using this operation.

String s=s1s2…sns=s1s2…sn is lexicographically smaller than string t=t1t2…tmt=t1t2…tm if n<mn<m and s1=t1,s2=t2,…,sn=tns1=t1,s2=t2,…,sn=tn or there exists a number pp such that p≤min(n,m)p≤min(n,m) and s1=t1,s2=t2,…,sp−1=tp−1s1=t1,s2=t2,…,sp−1=tp−1 and sp<tpsp<tp .

For example, "aaa" is smaller than "aaaa", "abb" is smaller than "abc", "pqr" is smaller than "z".

Input

The first line of the input contains one integer nn (2≤n≤2⋅1052≤n≤2⋅105 ) — the length of ss .

The second line of the input contains exactly nn lowercase Latin letters — the string ss .

Output

Print one string — the smallest possible lexicographically string that can be obtained by removing at most one character from the string ss .

Examples
Input
3 aaa
 
Output
aa
 
Input

5
abcda
 
Output
abca
 
Note

In the first example you can remove any character of ss to obtain the string "aa".

In the second example "abca" < "abcd" < "abcda" < "abda" < "acda" < "bcda".

题意:

在序列中至多删去一个数,使得操作后得序列最小(与执行相同操作的其它序列相比)

题解:

通过模拟一下这个过程可以发现我们要找 i 这个位置,满足si>si+1&&i<n 或者直接i=n。

代码如下:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. const int N = 2e5+;
  8. char s[N];
  9. int n;
  10.  
  11. int main(){
  12. scanf("%d",&n);getchar();
  13. for(int i=;i<=n;i++) scanf("%c",&s[i]);
  14. int i,pos=n;
  15. for(i=;i<n;i++){
  16. int j=i+;
  17. if(s[j]<s[i]){
  18. pos=i;
  19. break ;
  20. }
  21. }
  22. for(int i=;i<=n;i++){
  23. if(i==pos) continue ;
  24. printf("%c",s[i]);
  25. }
  26. return ;
  27. }
B. Divisor Subtraction
time limit per test

2 seconds

memory limit per test

256 megabytes

Description:

You are given an integer number nn. The following algorithm is applied to it:

  1. if n=0, then end algorithm;
  2. find the smallest prime divisor d of n;
  3. subtract d from n and go to step 1.

Determine the number of subtrations the algorithm will make.

Input

The only line contains a single integer nn (2≤n≤10102≤n≤1010).

Output

Print a single integer — the number of subtractions the algorithm will make.

Examples
Input
5
Output
1
 
Input
4
Output
2
 
Note

In the first example 5 is the smallest prime divisor, thus it gets subtracted right away to make a 0.

In the second example 2 is the smallest prime divisor at both steps.

题意:

找n最小的质因子d,然后减去d,不断 重复这一过程直到n=0。

题解:

n为偶数很容易。当n为奇数时,质因子必为奇数,减去后也为偶数。所以问题的关键就是当n为奇数的情况。

最后发现只要找到一个最小的d,使得n%d==0就可以了,不管d是否为质数。

我当时没考虑到这一点,所以代码有点丑陋。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <cmath>
  6. using namespace std;
  7.  
  8. long long n;
  9.  
  10. inline int prim(int x){
  11. int flag = ;
  12. for(int i=;i<=sqrt(x+0.5)+;i++){
  13. if(x%i==){
  14. flag=;break ;
  15. }
  16. }
  17. return flag;
  18. }
  19.  
  20. int main(){
  21. scanf("%lld",&n);
  22. if(n%==){
  23. printf("%lld",n/);return ;
  24. }
  25. if(prim(n)){
  26. printf("");return ;
  27. }
  28. for(int i=;i<=sqrt(n+0.5)+;i++){
  29. if(n%i== && prim(i)){
  30. printf("%lld",+(n-i)/);
  31. return ;
  32. }
  33. }
  34. return ;
  35. }
C. Meme Problem
time limit per test

1 second

memory limit per test

256 megabytes

Try guessing the statement from this picture:

You are given a non-negative integer dd . You have to find two non-negative real numbers aa and bb such that a+b=d and a⋅b=d .

Input

The first line contains tt (1≤t≤1031≤t≤103 ) — the number of test cases.

Each test case contains one integer d (0≤d≤103) .

Output

For each test print one line.

If there is an answer for the i -th test, print "Y", and then the numbers a and b .

If there is no answer for the i -th test, print "N".

Your answer will be considered correct if |(a+b)−a⋅b|≤10−6|(a+b)−a⋅b|≤10−6 and |(a+b)−d|≤10−6|(a+b)−d|≤10−6 .

 
Example
Input
  1. 7
  2. 69
  3. 0
  4. 1
  5. 4
  6. 5
  7. 999
  8. 1000
Output
  1. Y 67.985071301 1.014928699
  2. Y 0.000000000 0.000000000
  3. N
  4. Y 2.000000000 2.000000000
  5. Y 3.618033989 1.381966011
  6. Y 997.998996990 1.001003010
  7. Y 998.998997995 1.00100200
题意:
给出一个数d,找两个数x,y,满足x+y=d 并且x*y=d。
 
题解:
我用的是二分,当时好像证明了二分的正确性。但实际上有更简单的数学方法,就是利用x+y=d,x*y=d这两个等式(韦达定理),变换一下可以解一个方程。
 
我还是给出我的二分代码吧。。
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <cmath>
  6. using namespace std;
  7.  
  8. int t,n;
  9. double eps = 1e-;
  10.  
  11. int main(){
  12. scanf("%d",&t);
  13. while(t--){
  14. scanf("%d",&n);
  15. if(n==){
  16. printf("Y 0.000000000 0.000000000\n");
  17. continue ;
  18. }else if(n== || n== ||n==){
  19. printf("N\n");continue;
  20. }else if(n==){
  21. printf("Y 2.000000000 2.000000000\n");
  22. continue ;
  23. }else{
  24. double l = ,r=,mid,Ans,tmp;
  25. while(l<=r){
  26. mid=(l+r)/;
  27. tmp = n-mid;
  28. if(abs(tmp*mid-tmp-mid)<eps || abs(tmp*mid-n)<eps){
  29. Ans=mid;break;
  30. }
  31. if(tmp*mid-tmp-mid<) l=mid+0.0000000001;
  32. else r=mid-0.0000000001;
  33. }
  34. printf("Y %.9lf %.9lf\n",n-Ans,Ans);
  35. }
  36. }
  37.  
  38. return ;
  39. }
D. Edge Deletion
time limit per test

2.5 seconds

memory limit per test

256 megabytes

Description:

You are given an undirected connected weighted graph consisting of nn vertices and mm edges. Let's denote the length of the shortest path from vertex 11 to vertex ii as didi .

You have to erase some edges of the graph so that at most kk edges remain. Let's call a vertex ii good if there still exists a path from 11 to ii with length didi after erasing the edges.

Your goal is to erase the edges in such a way that the number of good vertices is maximized.

Input

The first line contains three integers nn , mm and kk (2≤n≤3⋅1052≤n≤3⋅105 , 1≤m≤3⋅1051≤m≤3⋅105 , n−1≤mn−1≤m , 0≤k≤m0≤k≤m ) — the number of vertices and edges in the graph, and the maximum number of edges that can be retained in the graph, respectively.

Then mm lines follow, each containing three integers xx , yy , ww (1≤x,y≤n1≤x,y≤n , x≠yx≠y , 1≤w≤1091≤w≤109 ), denoting an edge connecting vertices xx and yy and having weight ww .

The given graph is connected (any vertex can be reached from any other vertex) and simple (there are no self-loops, and for each unordered pair of vertices there exists at most one edge connecting these vertices).

Output

In the first line print ee — the number of edges that should remain in the graph (0≤e≤k0≤e≤k ).

In the second line print ee distinct integers from 11 to mm — the indices of edges that should remain in the graph. Edges are numbered in the same order they are given in the input. The number of good vertices should be as large as possible.

Examples
Input

3 3 2
1 2 1
3 2 1
1 3 3
Output
2
1 2
 
Input

4 5 2
4 1 8
2 4 1
2 1 3
3 4 9
3 1 5
Output
2
3 2
 
题意:
这个复制粘贴过来好像有点问题...将就看吧。
题目的要求就是可以留下最多k条边,使得最后的图中所有的点的最小距离都跟未删边一样。
 
题解:
题解还是好像,但是代码实现起来坑了我好久...
这题spfa会被卡,所以用了dijsktra。
我们可以发现,最短的最短路径树就是答案,然后记录结果的时候就当点从优先队列里面取出来的时候记录就可以了,因为Dijsktra中,点取出来的时候就是当前最小距离。
 
代码如下:
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <queue>
  6. #include <vector>
  7. #define INF 1e18
  8. using namespace std;
  9.  
  10. typedef long long LL;
  11. typedef pair<LL,int> pli;
  12. typedef pair<int,int> pii;
  13.  
  14. const int N = 3e5+;
  15. int n,m,k;
  16. int vis[N]={};
  17. LL d[N];
  18. pli pre[N];
  19. vector<pair<int,pii> > vec[N];
  20. vector<int> ans ;
  21. void Dij(int x){
  22. fill(d,d+n+,INF);d[x]=0ll;
  23. priority_queue<pli,vector<pli>,greater<pli> > q;
  24. q.push(make_pair(d[x],x));
  25. while(!q.empty()){
  26. pli now = q.top();q.pop();
  27. int u = now.second;
  28. if(vis[u]) continue ;
  29. vis[u]=;
  30. for(int i=;i<vec[u].size();i++){
  31. int v = vec[u][i].second.first;
  32. if(d[v]>d[u]+vec[u][i].second.second &&!vis[v]){
  33. d[v]=d[u]+vec[u][i].second.second;
  34. pre[v]=make_pair(u,vec[u][i].first);
  35. q.push(make_pair(d[v],v));
  36. }
  37. }
  38. }
  39. }
  40. queue <int> que ;
  41. void bfs(int x,int K){
  42. que.push(x);
  43. while(!que.empty() && K){
  44. int u = que.front();que.pop();
  45. for(int i=;i<vec[u].size();i++){
  46. int v = vec[u][i].second.first ;
  47. if(d[v]==d[u]+vec[u][i].second.second){
  48. que.push(v);
  49. ans.push_back(vec[u][i].first);
  50. K--;
  51. }
  52. if(!K) break ;
  53. }
  54. }
  55. }
  56. int main(){
  57. scanf("%d%d%d",&n,&m,&k);
  58. for(int i=,u,v,c;i<=m;i++){
  59. scanf("%d%d%d",&u,&v,&c);
  60. vec[u].push_back(make_pair(i,make_pair(v,c)));
  61. vec[v].push_back(make_pair(i,make_pair(u,c)));
  62. }
  63. Dij();
  64. bfs(,k);
  65. printf("%d\n",ans.size());
  66. for(int i=;i<ans.size();i++) printf("%d ",ans[i]);
  67. return ;
  68. }
 

Educational Codeforces Round 54 (Rated for Div. 2) ABCD的更多相关文章

  1. Educational Codeforces Round 54 (Rated for Div. 2) D:Edge Deletion

    题目链接:http://codeforces.com/contest/1076/problem/D 题意:给一个n个点,m条边的无向图.要求保留最多k条边,使得其他点到1点的最短路剩余最多. 思路:当 ...

  2. Educational Codeforces Round 54 [Rated for Div. 2] (CF1076)

    第一次在宿舍打CF 把同宿舍的妹子吵得不行... 特此抱歉QAQ A 题意:给定一个字符串, 最多删掉一个字符,使得剩余字符串字典序最小 n<=2e5 当然"最多"是假的 删 ...

  3. Educational Codeforces Round 54 (Rated for Div. 2) Solution

    A - Minimizing the String solved 题意:给出一个字符串,可以移掉最多一个字符,在所有可能性中选取一个字典序最小的. 思路:显然,一定可以移掉一个字符,如果移掉的字符的后 ...

  4. Educational Codeforces Round 54 (Rated for Div. 2) DE

    D 给出一个无向图,需要删去一些边,想知道最后能有多少个点到1的距离还是过去那么短 如果求一个最短路,然后从删边的角度看,看起来很难做,但是如果从零开始加边就会有做法,如同prim那样,先加入和1直接 ...

  5. Educational Codeforces Round 54 (Rated for Div. 2) D Edge Deletion (SPFA + bfs)

    题目大意:给定你一个包含n个点m条边的无向图,现在最多在图中保留k条边,问怎么删除多的边,使得图中良好的节点数最多,求出保留在图中的边的数量和编号. 良好的节点定义为:删除某条边后该点到点1的最短距离 ...

  6. Educational Codeforces Round 56 (Rated for Div. 2) ABCD

    题目链接:https://codeforces.com/contest/1093 A. Dice Rolling 题意: 有一个号数为2-7的骰子,现在有一个人他想扔到几就能扔到几,现在问需要扔多少次 ...

  7. Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序

    Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序 [Problem Description] ​ 给你 ...

  8. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  9. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

随机推荐

  1. Matplotlib 图表的样式参数

    1. import numpy as np import pandas as pd import matplotlib.pyplot as plt % matplotlib inline # 导入相关 ...

  2. MyBatis的笔记

    1.#{}和${}的区别是什么? #{}是预编译处理,${}是字符串替换. #{}是sql的参数占位符,${}是Properties文件中的变量占位符,它可以用于标签属性值和sql内部,属于静态文本替 ...

  3. 详解jQuery中 .bind() vs .live() vs .delegate() vs .on() 的区别

    转载自:http://zhuzhichao.com/2013/12/differences-between-jquery-bind-vs-live/ 我见过很多开发者很困惑关于jQuery中的.bin ...

  4. HashMap源码注释翻译

    HashMap.java(JDK1.8) 如有错误翻译的地方,欢迎评论指出. 介绍:对于HashMap及其子类而言,它们采用Hash算法来决定集合中元素的存储位置.当系统开始初始化HashMap时,系 ...

  5. babel配置

      首页 首页 首页 博客园 博客园 博客园 联系我 联系我 联系我 demo demo demo GitHub GitHub GitHub 管理 管理 管理 魔魔魔芋芋芋铃铃铃 [02]websto ...

  6. 使用PSSH批量操作Linux服务器

    简介 服务器多了,有一个问题就是如何批量快速操作多台服务器,在网上搜到了PSSH工具,试用了一下发现挺好用,推荐给大家. pssh是一个python编写的可以在多台服务器上执行命令的轻量级管理工具,同 ...

  7. Elasticsearch中的DocValues

    Elasticsearch最近一段时间非常火,以致于背后的公司都改名为Elastic了,因为Elasticsearch已经不仅限于搜索,反而更多的用在大数据分析场景,所以在公司品牌上开始“去Searc ...

  8. Visual Studio 2015安装包

    点击下载

  9. java设计模式之观察者模式以及在java中作用

    观察者模式是对象的行为模式,又叫发布-订阅(Publish/Subscribe)模式.模型-视图(Model/View)模式.源-监听器(Source/Listener)模式或从属者(Dependen ...

  10. mingw编译opencv2.4.13问题记录

    为了在程序中用regex,升级了我的mingw,结果官网上的GCC版本都到6.3了,之前一直用4.8.换了编译器以后,对opencv2.4.10的引用就出了问题:undefined reference ...