B. New Year Permutation time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permutation as pretty…
B. New Year Permutation time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permutation as pretty…
题目链接:http://codeforces.com/problemset/problem/500/B 题目意思:给出一个含有 n 个数的排列:p1, p2, ..., pn-1, pn.紧接着是一个 n * n 的矩阵A,当且仅当 Aij = 1 时,pi 与 pj 可以交换数值.现在问如何交换数值,使得最后得到的排列字典序最小. 比赛的时候不会做,看了Tutorial 1 的解法,觉得别人做得太巧妙了,出题者也出得很好 ^_^ 可以看这个:http://codeforces.com/blog…
刷水题做几道入门贪心题预热... 找联通块里字典序最小的放到最前面即可.记得写传递闭包 #include <iostream> #include <cstdio> #include <cstring> using namespace std; const int N=350; int n,a[N],pos[N],g[N][N]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) sca…
题目连接:Codeforces 437C  The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; const int MAX_N = 1000 + 10; int G[MAX_N][MAX_N]; struct…
https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则,假如u在v相邻前面,那么u和v可以交换位置,问你是队列最后一个人的时候你最前可以换到前面哪里 题解 因为相邻才能换,所以最后一个换到前面一定是一步一步向前走,所以不存在还要向后走的情况 设最后一个为u,假设前面有一个能和u换位置的集合,那么需要将这些点尽量往后移动去接u 假设前面有一个不能和u换位置的集合S,…
题意:给定一个字符串,问你能不能通过重排,使得任意一个素数p <= 字符串长度n,并且 任意的 i <= 长度n/素数p,满足s[p] == s[p*i]. 析:很容易能够看出来,只要是某个素数的小于等于该素数的倍数都是一样的,然后如果他和其他素数也有倍数,那么这些位置也是一样的, 所以我们只要找到任意一个小于等于 n 的素数与该素数相乘都大于 n的,然后用把数目最少的字符种给它,然后剩下的给那些. 所以能够看出lcm(2, i) (i是素数) <= n的那么这些位置包括小于等于该素数的…
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n grid. In this game a ships are placed on the grid. Each of the ships consists of bconsecutive cells. No cell can be part of two ships, however, the shi…
C. Load Balancing 题目连接: http://www.codeforces.com/contest/609/problem/C Description In the school computer room there are n servers which are responsible for processing several computing tasks. You know the number of scheduled tasks for each server:…
C. Make Palindrome Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/problem/C Description A string is called palindrome if it reads the same from left to right and from right to left. For example "kazak", "oo&q…
B. Restaurant Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/problem/B Description A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order…
题目链接:Codeforces 437D The Child and Zoo 题目大意:小孩子去參观动物园,动物园分非常多个区,每一个区有若干种动物,拥有的动物种数作为该区的权值.然后有m条路,每条路的权值为该条路连接的两个区中权值较小的一个.假设两个区没有直接连接,那么f值即为从一个区走到还有一个区中所经过的路中权值最小的值做为权值.问,平均两个区之间移动的权值为多少. 解题思路:并查集+贪心.将全部的边依照权值排序,从最大的開始连接,每次连接时计算的次数为连接两块的节点数的积(乘法原理).…
Time Limit:5000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 691D Description You are given a permutation of the numbers 1, 2, ..., n and m pairs of positions (aj, bj). At each step you can choose a…
Description Ivan has an array consisting of n elements. Each of the elements is an integer from 1 to n. Recently Ivan learned about permutations and their lexicographical order. Now he wants to change (replace) minimum number of elements in his array…
题目链接: http://codeforces.com/problemset/problem/777/B 题目大意: A, B玩游戏,每人一串数字,数字不大于1000,要求每人从第一位开始报出数字,并且比较,如果等于则没事,A>B则B被打一下,反之A被打一下,A很老实不会耍计谋,老老实实从第一个开始报,但是B很狡猾,会改变数字的顺序.问2个次数,一个是B最小被打的次数,二是A最多被打的次数. 思路: 贪心,模拟一下就行了.这里用到了cmp的技巧,直接用字符比较,这样就省去了转换为字符串的步骤.…
https://codeforces.com/contest/1141/problem/F2 题意 一个大小为n的数组a[],问最多有多少个不相交的区间和相等 题解 离散化用值来做,贪心选择较前的区间 代码 #include<bits/stdc++.h> #define M 5000005 #define ll long long #define pb push_back using namespace std; struct N{int l,r;N(int l=0,int r=0):l(l)…
You are given a directed acyclic graph with n vertices and m edges. There are no self-loops or multiple edges between any pair of vertices. Graph can be disconnected. You should assign labels to all vertices in such a way that: Labels form a valid pe…
A. Table time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Simon has a rectangular table consisting of n rows and m columns. Simon numbered the rows of the table from top to bottom starting f…
C. Simple Strings 题目连接: http://www.codeforces.com/contest/665/problem/C Description zscoder loves simple strings! A string t is called simple if every pair of adjacent characters are distinct. For example ab, aba, zscoder are simple whereas aa, add a…
题目链接: F. PolandBall and Gifts time limit per test 1.5 seconds memory limit per test 256 megabytes input standard input output standard output It's Christmas time! PolandBall and his friends will be giving themselves gifts. There are n Balls overall.…
B. Inventory time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Companies always have a lot of equipment, furniture and other things. All of them should be tracked. To do this, there is an inv…
Codeforces 762B 题目大意: 有a台只有USB接口的电脑,b台PS/2接口的电脑,c台两种接口都有的电脑.每台电脑只用装一个鼠标.给出n个鼠标及其费用,每个鼠标只能使用一遍.在最大化有鼠标的电脑数目的情况下最小化费用. \((0 \le a,b,c \leq 10^5 , n \leq 3*10^5 )\) 题解: 这道题我们很容易抽象出来一个网络流模型 我们设超级源汇为S,T,每台电脑和鼠标都抽象成一个节点 我们可以这么建图 S -> 每台电脑 (flow = 1,cost =…
题目链接:http://codeforces.com/contest/600/problem/C C. Make Palindrome time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output A string is called palindrome if it reads the same from left to right an…
一道用STL的贪心,正好可以用来学习使用STL库 题目大意:给出n条可以内含,相交,分离的线段,如果重叠条数超过k次则为坏点,n,k<2e5 所以我们贪心的想我们从左往右遍历,如果重合部分条数超过了k,就必须去除线段,(此时从左边看去除线段后不会出现冲突,右边还有剩余很多线段未知)所以我们选择去除这些重合线段里右端最右的部分 实现: #include<bits/stdc++.h>using namespace std;typedef pair<int,int> pii;typ…
https://codeforces.com/contest/1152/problem/D 题意 给你一个n代表合法括号序列的长度一半,一颗有所有合法括号序列构成的字典树上,选择最大的边集,边集的边没有公共点,问边集大小 题解 对于一颗字典树,从低向上贪心,最底层的边全拿,定义好状态,记忆化搜索计数 定义dp[i][j]为左括号数量为i,右括号数量为j的最大边集 \(i<n\),\(dp[i][j]->dfs(i+1,j)\) \(j<i\),\(dp[i][j]->dfs(i,j…
题目描述: time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standard output Dima worked all day and wrote down on a long paper strip his favorite number n consisting of l digits. Unfortunately, the strip turned…
题目链接: https://codeforces.com/gym/101987/my 题意: 顶点数为$n$,边数为$m$ 求出每个点对$(a,b)$,$a$到$b$的最小路径的最大值 数据范围: $1\leq n \leq 500$ $1\leq m \leq 1250000$ 分析: 直接floyd处理 当时想的是用边来放缩S值,既然可以用边放缩,那么就可以用点放缩 AC代码: #include<bits/stdc++.h> #define ll long long #define pii…
Codeforces Round #228 (Div. 1) 题目链接:C. Fox and Card Game Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card. The players take turns and Ciel takes the first…
Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] 题目链接:B. "Or" Game You are given \(n\) numbers \(a_1, a_2, ..., a_n\). You can perform at most \(k\) operations. For each operation you can multiply one of the numbers by \(x\). We want to make…
传送门 题目大意 给你一个数列,再给你一个矩阵,矩阵的(i,j)如果为1就表示可以将i,j位置上的数交换,问任意交换之后使原数列字典序最小并输出. 解题思路 因为如果i与j能交换,j与k能交换,那么i与k相当于能直接交换,所以我们先使用传递闭包求出所有可以交换的情况.之后从第一个位置开始贪心,看它能跟后面哪个小于它的数交换. 代码 #include<iostream> #include<cstdio> #include<cstring> using namespace…