【置换,推理】UVa 1315 - Creaz tea party
Dsecription
n participants of «crazy tea party» sit around the table. Each minute one pair of neighbors can change their places. Find the minimum time (in minutes) required for all participants to sit in reverse order (so that left neighbors would become right, and right - left).
Input
The first line is the amount of tests. Each next line contains one integer n (1 <= n <= 32767) - the amount of crazy tea participants.
Output
For each number n of participants to crazy tea party print on the standard output, on a separate line, the minimum time required for all participants to sit in reverse order.
Sample Input
3
4
5
6
Sample Output
2
4
6 题意:有n个人坐成一圈。相邻间可以交换,问至少交换多少次能达到逆序,即左右相邻的数交换。 分析:从中间分开,很明显1~(n/2)号往左交换,(n/2)~n号往右交换。比如:
n = 6时:
1 2 3 | 4 5 6
1 3 2 | 5 4 6
3 1 2 | 5 6 4 (以上3,4分别移至两侧)
3 2 1 | 6 5 4 (2,5移至两侧)
以上完成逆序变换; n = 7时:
1 2 3 4 | 5 6 7
...
4 1 2 3 | 6 7 5
...
4 3 1 2 | 7 6 5
4 3 2 1 | 7 6 5
这样每个数需要交换的次数分别为 (偶)0 1,2,...,(n/2)-1, (n/2)-1,...,2,1,0
(奇)0 1,2,...,(n/2), (n/2)-1,...,2,1,0
求和即可。 【代码】:代码可能略繁琐。自己可以简化一下。
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int T; scanf("%d", &T);
while(T--)
{
int n, ans = ; scanf("%d", &n);
if(n%)
{
int num = n/;
ans = (+(num-)) * (num-);
ans += num;
}
else
{
int num = n/;
ans = (+(num-)) * (num-);
}
cout << ans << endl;
}
return ;
}
【置换,推理】UVa 1315 - Creaz tea party的更多相关文章
- 【uva 10294】 Arif in Dhaka (First Love Part 2) (置换,burnside引理|polya定理)
题目来源:UVa 10294 Arif in Dhaka (First Love Part 2) 题意:n颗珠子t种颜色 求有多少种项链和手镯 项链不可以翻转 手镯可以翻转 [分析] 要开始学置换了. ...
- UVA 11246 - K-Multiple Free set(数论推理)
UVA 11246 - K-Multiple Free set 题目链接 题意:一个{1..n}的集合.求一个子集合.使得元素个数最多,而且不存在有两个元素x1 * k = x2,求出最多的元素个数是 ...
- UVA 10294 项链与手镯 (置换)
Burnside引理:对于一个置换\(f\), 若一个着色方案\(s\)经过置换后不变,称\(s\)为\(f\)的不动点.将\(f\)的不动点数目记为\(C(f)\), 则可以证明等价类数目为\(C( ...
- UVa 11330 (置换 循环的分解) Andy's Shoes
和UVa11077的分析很类似. 我们固定左脚的鞋子不动,然后将右脚的鞋子看做一个置换分解. 对于一个长度为l的循环节,要交换到正确位置至少要交换l-1次. #include <cstdio&g ...
- UVa 11077 Find the Permutations(置换+递推)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35431 [思路] 置换+递推 将一个排列看作一个置换,分解为k个循 ...
- uva 1561 - Cycle Game(推理)
option=com_onlinejudge&Itemid=8&page=show_problem&problem=4336" style=""& ...
- uva 11077 置换
/** 给定一个置换,看能不能存在一个置换A^2 = B 思路; 循环节长度为偶数n的置换只能由循环节长度为长度2*n 的置换A*A 而变得.所以只需求出循环节,看循环节长度为偶数的个数是否为偶数个即 ...
- UVA 1364 - Knights of the Round Table (获得双连接组件 + 二部图推理染色)
尤其是不要谈了些什么,我想A这个问题! FML啊.....! 题意来自 kuangbin: 亚瑟王要在圆桌上召开骑士会议.为了不引发骑士之间的冲突. 而且可以让会议的议题有令人惬意的结果,每次开会前都 ...
- 【UVA 11077】 Find the Permutations (置换+第一类斯特林数)
Find the Permutations Sorting is one of the most used operations in real life, where Computer Scienc ...
随机推荐
- 用C语言实现ipv4地址字符串是否合法
用程序实现ipv4地址字符串是否合法,主要考察的是C字符串的操作. 搜索了下,网上没有特别好的实现,自己实现了下,见笑于大家,请指正. #include <stdio.h> #includ ...
- linux 下终端复用软件推荐——tmux
使用过些linux终端,比如Tilda.Terminator. 之前最经常用的是Terminator,其可以上下左右分屏,比较方便,但其有个缺点是经常无故崩溃. 后来遇到Tmux,根据网上的设置配置了 ...
- Labview中创建属性节点和调用节点的用法
创建属性节点 个人感觉有点像C中的指针 创建调用节点
- 读Qt Demo——Basic Layouts Example
此例程主要展示用代码方式创建控件并用Layout管理类对其进行布局: 例程来自Qt5.2,如过是默认安装,代码位于:C:\Qt\Qt5.2.0\5.2.0\mingw48_32\examples\wi ...
- HDU 3665 Seaside (最短路,Floyd)
题意:给定一个图,你家在0,让你找出到沿海的最短路径. 析:由于这个题最多才10个点,那么就可以用Floyd算法,然后再搜一下哪一个是最短的. 代码如下: #pragma comment(linker ...
- Mysql知识要点总结
1.安装 要点:记得更改字符集 2.数据类型 常用数据类型:INT VARCHAR BLOG 3.操作数据库 SHOW DATABASES; CREATE DATABASE 名称; DROP DATA ...
- Spring Controller参数为空串的处理方式
控制器参数为String类型 Spring框架接收到传入的空串后,此参数被赋值为空串,不为null. 控制器参数为非String类型 Spring框架接收到传入的空串后,此参数被赋值为null.
- Kicad使用经验谈
最近开始学习使用Linux上的开源软件KiCad来绘制电路图和PCB.学习这个还是比较快的,用了两天了,觉得还是蛮方便的. 在这两天的使用以及今后的使用过程中,一定会有很多想要谈的.所以,就写下这篇博 ...
- 【MyLocations】标记位置App开发体会
实现功能: 1.能通过Cora Location获取地址信息 2.用户获取地址信息后能编辑相关信息 3.使用Core Data保存数据 4.使用MapKit,在Map上显示标记的位置,并可以编辑位置信 ...
- Oracle超出最大连接数问题及解决
用过Oracle的应该都熟悉如何查看和设置Oracle数据库的最大连接数.这里就再啰嗦一遍. 查看当前的连接数,可以用select count(*) from v$process;设置的最大连接数(默 ...