【题解】CF1215C Swap Letters】的更多相关文章

题面传送门 解决思路 首先容易得知,两个字符串中 \(b\)(或 \(a\)) 的个数为偶数时,一定有解.为奇数则一定无解. 其次考虑怎么交换.对照样例三: in: 8 babbaabb abababaa out: 3 2 6 1 3 7 8 发现,每一对交换的字符有共同点: 要不是串一都为 \(a\),串二都为 \(b\) 的一对 要不是串一都为 \(b\),串二都为 \(a\) 的一对 简单思考后发现这样成对交换就是最优的.(换一次就可以匹配上两位) 于是,考虑先统计出 串一为 \(a\),…
C. Swap Letters time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Monocarp has got two strings ss and tt having equal length. Both strings consist of lowercase Latin letters "a" and &qu…
前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Yo…
链接: https://codeforces.com/contest/1215/problem/C 题意: Monocarp has got two strings s and t having equal length. Both strings consist of lowercase Latin letters "a" and "b". Monocarp wants to make these two strings s and t equal to each…
problem: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the…
1.题目描述 2.问题分析 对两个节点进行交换操作 3.代码 ListNode* swapPairs(ListNode* head) { if( !head || head->next == NULL ){ return head; } ListNode dummy(); ListNode* d = &dummy; ListNode* p = head; while( p && p->next != NULL ){ ListNode* tmp = p->next-…
因为不太熟悉链表操作,所以解决方法烦了点,空间时间多有冗余. 代码中l,r分别是每一组的需要交换的左右指针,temp是下一组的头指针,用于交换后链接:res是交换后的l指针,用于本组交换后尾指针在下一组交换后链接上交换后的头指针. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} *…
传送门 好像是个挺显然的贪心 首先每次交换当然要尽量一次交换就多两个相同的位置 即 优先把 $\begin{bmatrix}a\\ b\end{bmatrix}$ 和 $\begin{bmatrix}a\\ b\end{bmatrix}$ 交换 优先把 $\begin{bmatrix}b\\ a\end{bmatrix}$ 和 $\begin{bmatrix}b\\ a\end{bmatrix}$ 交换 最后如果剩下$\begin{bmatrix}a\\ b\end{bmatrix}$,$\be…
此题评测机出了点问题,数据全部AC,却显示UKE 下面是数据全部AC,却显示UKE的代码 思路:b[i]减去每个宿舍的房间总数,如果b[i]小于了某个宿舍的房间总数则为答案. #include<bits/stdc++.h> using namespace std; int main(){ long long n,m,a[20010],b[20010],i,j; cin>>n>>m; for(i=1;i<=n;i++)cin>>a[i];//输入 for…
题目传送门 题目大意 给出一个长度为 \(n\) 的排列 \(a_{1,2,...,n}\) 以及常数 \(k\),每次可以交换两个数 \(a_i,a_j\) 当且仅当 \(j-i\ge k \text{ and } |a_i-a_j|=1\) ,问最小能变成的最小字典序的 \(a\) 序列. \(n\le 5\times 10^5\) 思路 首先很重要的一点是,我们肯定得把题目转换一下,不然 \(j-i\ge k\) 这个条件太难用了.我们其实可以设 \(p_i\) 表示元素 \(i\) 所在…