Codeforces 584E - Anton and Ira - [贪心]】的更多相关文章

题目链接:https://codeforces.com/contest/584/problem/E 题意: 给两个 $1 \sim n$ 的排列 $p,s$,交换 $p_i,p_j$ 两个元素需要花费 $|i-j|$,要求你用最少的钱交换 $p$ 中的元素使得其变成 $s$. 题解: 思路很简单,给个例子如下: $p$:$5,1,2,3,4,6$ $s$:$3,4,1,6,2,5$ 我们不难发现,像: $p$:$5,1,\underline{2},\underline{3},4,6$ $s$:$…
Anton and Ira 我们把点分为三类, 向左走的, 向右走的, 不动的. 最完美的情况就是每个点没有走反方向. 每次我们挑选最右边的向右走的去把向左走的交换过来,这样能保证最优. #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #define PLI pair<LL,…
题意简述: 给定一个$1$到$n(n<=2000)$的初始排列以及最终排列 我们每次可以选取位置为$i$和$j$的 并交换它们的位置 花费为$ |i-j| $ 求从初始状态变换到末状态所需最小花费 ----------------------------------------------------------------------------------------------------------- 比赛时这题留了$40min$ 然而自己贪心策略还是有漏洞 结束后看了首页的官方题解 感…
E. Anton and Ira Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/584/problem/E Description Anton loves transforming one permutation into another one by swapping elements for money, and Ira doesn't like paying for stupid game…
题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include<bits/stdc++.h> using namespace std; typedef unsigned long long ull; ; ull base[maxbit], n, k; void preDeal() { ] = ; ; i < maxbit; i++){ *]; } } voi…
[Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的货币越优.如有1,2,5,10,20,50,那么我们尽量用面值为1的.如果我们把原始货币换成面值为x的货币,设汇率为d,那么需要的原始货币为dx的倍数.显然dx越小,剩下的钱,即n取模dx会尽量小. 然后就可以枚举换某一种货币的数量,时间复杂度\(O(\frac{n}{d})\) 代码 #inclu…
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output Anton loves transforming one permutation into another one by swapping elements for money, and Ira doesn't like paying for stupid games. Help them…
B. Anton and Lines   The teacher gave Anton a large geometry homework, but he didn't do it (as usual) as he participated in a regular round on Codeforces. In the task he was given a set of n lines defined by the equations y = ki·x + bi. It was necess…
E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard input output: standard output Anton is growing a tree in his garden. In case you forgot, the tree is a connected acyclic undirected graph. There are …
1.codeforces 349B    Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1-9每个字母分别要ai升油漆,问最多可画多大的数字. 贪心,也有点考思维. #include<bits/stdc++.h> using namespace std; #define LL long long #define INF 0x3f3f3f3f int main() { ]; while(…
LINK time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output The teacher gave Anton a large geometry homework, but he didn't do it (as usual) as he participated in a regular round on Codeforces. In…
题目链接:http://codeforces.com/gym/100269/attachments 题意: 有长度为n个格子,你有两种操作,1是放一个长度为1的东西上去,2是放一个长度为2的东西上去 每个东西在每秒钟都会产生1的能力. 然后问你怎么放才能使得最后能力最大,输出出来 解法: 贪心,最后肯定1越多越好 所以我们放1的时候,注意一下,如果放不下的话,就把其中一个2扔掉,然后放1就好了 //CF gym 100269E #include <bits/stdc++.h> using na…
题目链接:http://codeforces.com/problemset/problem/797/C 题意: 给你一个非空字符串s,空字符串t和u.有两种操作:(1)把s的首字符取出并添加到t的末尾.(2)把t的尾字符取出并添加到u的末尾. 问你当经过一系列操作后,s和t均为空时,字典序最小的u. 题解: 操作的本质: s为队列,t为栈. 贪心思路: (1)找到s中的最小字符c,不断出队并加入t,直至有一次出队的字符等于c,停止出队. (2)当t的尾字符小于等于s中的最小字符时,优先弹出t的尾…
Magazine Ad 题目链接:http://codeforces.com/contest/803/problem/D ——每天在线,欢迎留言谈论. 题目大意: 给你一个数字k,和一行字符 例: garage for sa-le 其中这行字符串能够在 ' '与'-'的后面分割.例如分割为:(点代表空格) garage. for. sa- le 求:分割成不超过k行的情况下的最小宽度.(宽度:最大行的字符个数) 思路: 答案一定在 到 所给字符串长度 之间. ①通过二分宽度 来逼近最小宽度. 判…
原文链接https://www.cnblogs.com/zhouzhendong/p/9074226.html 题目传送门 - Codeforces 980E 题意 $\rm Codeforces$ 真是个令人伤心的地方. 伤心的 $zzd$ 给你一个有 $n$ 个节点的树,编号为 $i$ 的节点权值为 $2^i$. 让你砍掉其中 $k$ 个节点,使得剩余的所有节点都连通,并最大化剩余节点的权值和.输出方案. $n\leq 10^6$ 题解 伤心的 $zzd$ 再一次来到了令人伤心的 $\rm…
Mike has always been thinking about the harshness of social inequality. He's so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers A = [a1, a2, ..., an] and B = [b1, …
题目链接:http://codeforces.com/problemset/problem/402/D 题意: 给你一个长度为n的数列a[i],又给出了m个“坏质数”b[i]. 定义函数f(s),其中p是s的最小质因子: f(1) = 0 f(s) = f(s/p) + 1 (p不是坏质数) f(s) = f(s/p) - 1 (p是坏质数) 你可以任意次数地进行操作:给a[1 to i]的每个数都除以gcd(a[1 to i]). 问你 ∑ f(a[i)最大为多少. 题解: 函数f(s)的实际…
题目链接:http://codeforces.com/contest/233/problem/C 题意:在一个无相图中有N个长度为3 的回路,输出符合条件的图.注意此图的节点数不得超过100 题解:贪心即可具体怎么贪心看代码. #include <iostream> #include <cstring> using namespace std; bool vis[120][120]; int main() { int k , n; cin >> k; memset(vi…
Anton is playing a very interesting computer game, but now he is stuck at one of the levels. To pass to the next level he has to prepare npotions. Anton has a special kettle, that can prepare one potions in x seconds. Also, he knows spells of two typ…
Codeforces Global Round 2 题目链接: E. Pavel and Triangles Pavel has several sticks with lengths equal to powers of two. He has \(a_0\) sticks of length \(2^0=1\), \(a1\) sticks of length \(2^1=2\), ..., \(a_{n−1}\) sticks of length \(2^{n−1}\). Pavel wa…
B - Sorted Adjacent Differences(CodeForces - 1339B) 题目链接 算法 思维+贪心 时间复杂度O(nlogn) 1.这道题的题意主要就是让你对一个数组进行一种特殊的排序,使得数组中相邻的两个数的差的绝对值成非递减趋势: 2.刚开始对这道题总是执拗于两个相等的数在不同位置,如何把它们放到前面这个问题,因为路走歪了,最终无果,没有思路.后来看了一些关于这道题的解题博客,豁然开朗. 3.使得数组中相邻的两个数的差的绝对值成非递减趋势,怎么想呢.单纯想怎么…
Codeforces 题面传送门 & 洛谷题面传送门 一道(绝对)偏简单的 D1E,但是我怕自己过若干年(大雾)忘了自己的解法了,所以过来水篇题解( 首先考虑怎么暴力地解决这个问题,不难发现我们每一步肯定会贪心,贪心地跳到所有经过当前点的公交线路中另一端最浅的位置,直到到达两点的 \(\text{LCA}\) 为止.不难发现上述过程可以倍增优化,具体来说我们记 \(nxt_{i,j}\) 表示从 \(i\) 开始走 \(2^j\) 步最浅能够到达哪里,那么我们可以一面树剖求出经过每个点能够到达深…
题目链接:http://codeforces.com/problemset/problem/719/C 题目大意: 留坑...…
题意:给出城市(0,0),给出n个坐标,起始人数s,每个坐标k个人, 每个坐标可以覆盖到半径为r的区域,r=sqrt(x*x+y*y)的区域,问最小的半径是多少,使得城市的总人数大于等于1000000 最开始是排序,贪心来做的,发现sqrt的精度老达不到要求,于是翻了代码 于是发现用map就可以解决了 map<int,int>,it->first是第一个int的内容,it->second是第二个int的内容 话说本来是按照标签来找的,想做二分查找的题目的= = #include&l…
BerSU Ball 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/E Description The Berland State University is hosting a ballroom dance in celebration of its 100500-th anniversary! n boys and m girls are already busy rehearsing waltz, minuet, po…
E - bits-EqualizerTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87794#problem/K Description You are given two non-empty strings S and T of equal lengths. S contains the characters 0, 1 and ?, where…
[题目链接] http://codeforces.com/problemset/problem/734/F [题目大意] 给出数列b和数列c,求数列a,如果不存在则输出-1 [题解] 我们发现: bi+ci=2n*ai-(所有ai为1且aj为0的数位)+(ai为0且aj为1的数位)= n*ai+Σak 记为(1)式 同时又有(ai为1且aj为0的数位)+(aj为1且ai为0的数位)=ai xor aj 那么Σ(bi+ci)=Σ(2n*ai)-Σ(ai xor aj)+Σ(ai xor aj)=Σ…
做不出题目,只能怪自己不认真 题目: Click here 题意: 给你3个数m,t,r分别表示鬼的数量,每只蜡烛持续燃烧的时间,每个鬼来时要至少亮着的蜡烛数量,接下来m个数分别表示每个鬼来的时间点(増序).输出至少要点多少只蜡烛,不能完成输出-1.注意t时刻点蜡烛,t+1时刻才管用.并且一个时间点只能点一支蜡烛 分析: 很明显的贪心,就尽可能晚的点蜡烛,能少点就少点. 代码: #include <iostream> #include <cstdio> #include <c…
这道题目有人用DFS.有人用DP 我觉得还是最简单的贪心解决也是不错的选择. Ok,不废话了,这道题目的意思就是 原先存在一个严格递增的Arrary_A,然后Array_A[i] 的每位之和为Array_B[i] 现在给你一个Array_B, 让你在条件: Array_A[len] Minimize 下求出次数组 (当然我们很容易得出,如果Array_A[len] 不是最小化的,那么答案有无穷多,随意暴力一下都可以) 所以这题没有那么暴力= = 解题思路: 首先求出Array_B[i] 和 Ar…
题意:有一个小朋友,即喜欢下象棋,还喜欢编程,于是他打算上这两种课的兴趣班,这两种课有着不同的上课时间,他想让两堂课之间的休息时间最多,问最大时间是多少 思路:看到这道题的第一反应就是贪心,于是用结构体排个序然后再暴力枚举....果断超时了,其实只要找两个区间差最远的就可以了 代码: #include <bits/stdc++.h> #define inf 1e9 using namespace std; int main() { int l1,r1,l2,r2; l1=;r1=inf; l2…