Codeforces Round #669 (Div. 2)A-C题解
A. Ahahahahahahahaha
题目:http://codeforces.com/contest/1407/problem/A
题解:最多进行n/2的操作次数,我们统计这n个数中1的个数,是否过半,如果没有代表0过半,因此输出剩余的0的个数即可
如果1的个数过半,则考虑1的个数是奇数还是偶数,若为奇数,则再减去一个输出;若为偶数个,则直接输出。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll; int main()
{
int t,n,i,j;
cin>>t;
while(t--)
{
cin>>n;
int num=;
for(i=;i<=n;i++)
{
int x;
cin>>x;
if(x==)
num++;
}
if(num<=n/)
{
cout<<n/<<endl;
for(int j=;j<=n/;j++)
cout<<""<<" ";
cout<<endl;
}
else
{
if(num%==)
{
cout<<num<<endl;
for(int j=;j<=num;j++)
cout<<""<<" ";
cout<<endl;
}
else
{
cout<<num-<<endl;
for(int j=;j<=num-;j++)
cout<<""<<" ";
cout<<endl;
}
}
}
return ;
}
B. Big Vova
题目:http://codeforces.com/contest/1407/problem/B
题解:首先挑选出最大的作为第一个,将其与第一个数互换位置,然后开始从第二位一直到第n位,遍历求出当前最大的gcd,并记录相应的下标,然后和第二到第n位交换位置,最多执行n-1次。
最后输出数组即可。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll; int t,n,a[];
int gcd(int a,int b)
{
return b==?a:gcd(b,a%b);
} int main()
{
int i,j,maxn;
cin>>t;
while(t--)
{
cin>>n;
int f;
int maxn=-;
for(i=;i<n;i++)
{
cin>>a[i];
if(a[i]>maxn)
{
maxn=a[i];
f=i;
}
}
swap(a[f],a[]);
int cnt=;
while(cnt<n)
{
int maxgcd=-;
int p;
for(i=cnt;i<n;i++)
{
if(gcd(maxn,a[i])>maxgcd)
{
maxgcd=gcd(maxn,a[i]);
p=i;
}
}
swap(a[cnt],a[p]);
maxn=maxgcd;
cnt++;
}
for(i=;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
}
return ;
}
C. Chocolate Bunny
题目:http://codeforces.com/contest/1407/problem/C
题解:这是一道交互题,以前没碰到过,也好理解。
可以询问 2×n2×n 次,每次给出两个不同的索引 i 和 j,会返回 ai%aj的值。
询问i和j,假设ai>aj,那结果为ai%aj=x<aj
再询问j和i,结果为aj%ai=y=aj
因此aj=y;并将y这个值用map标记,mp[y]=1
反之,若ai<aj,询问i和j,结果为ai%aj=x=ai
再询问j和i,结果为aj%ai=y<ai
因此ai=x,并将x用map标记,mp[x]=1;
最后遍历完整个数组后,还剩下一个,遍历数组找到mp还未标记的那个数,即为最终的a[f]的值
最后输出数组即可。
代码:
#include<bits/stdc++.h>
using namespace std; const int N=;
int x,y,n;
int a[N];
map<int,int>mp;
int main()
{
int i,j;
cin>>n;
int f=;
for(i=;i<=n;i++)
{
cout<<"? "<<f<<" "<<i<<endl;
cin>>x;
cout<<"? "<<i<<" "<<f<<endl;
cin>>y;
if(x>y)
{
a[f]=x;
mp[x]=;
f=i;
}
else
{
a[i]=y;
mp[y]=;
}
}
for(i=;i<=n;i++)
{
if(mp[i]==)
continue;
a[f]=i;
break;
}
cout<<"!";
for(i=;i<=n;i++)
cout<<" "<<a[i];
return ;
}
Codeforces Round #669 (Div. 2)A-C题解的更多相关文章
- Codeforces Round #612 (Div. 2) 前四题题解
这场比赛的出题人挺有意思,全部magic成了青色. 还有题目中的图片特别有趣. 晚上没打,开virtual contest打的,就会前三道,我太菜了. 最后看着题解补了第四道. 比赛传送门 A. An ...
- Codeforces Round #198 (Div. 2)A,B题解
Codeforces Round #198 (Div. 2) 昨天看到奋斗群的群赛,好奇的去做了一下, 大概花了3个小时Ak,我大概可以退役了吧 那下面来稍微总结一下 A. The Wall Iahu ...
- Codeforces Round #672 (Div. 2) A - C1题解
[Codeforces Round #672 (Div. 2) A - C1 ] 题目链接# A. Cubes Sorting 思路: " If Wheatley needs more th ...
- Codeforces Round #669 (Div. 2) C. Chocolate Bunny 题解(交互)
题目链接 题目大意 有一个长度为n的全排列,你可以询问2n次,要你经过这个2n次的询问后,求出这个全排列 询问定义为:输入"? i j"输出\(p_{i} mod p_{j}\) ...
- Codeforces Round #614 (Div. 2) A-E简要题解
链接:https://codeforces.com/contest/1293 A. ConneR and the A.R.C. Markland-N 题意:略 思路:上下枚举1000次扫一遍,比较一下 ...
- Codeforces Round #610 (Div. 2) A-E简要题解
contest链接: https://codeforces.com/contest/1282 A. Temporarily unavailable 题意: 给一个区间L,R通有网络,有个点x,在x+r ...
- Codeforces Round #611 (Div. 3) A-F简要题解
contest链接:https://codeforces.com/contest/1283 A. Minutes Before the New Year 题意:给一个当前时间,输出离第二天差多少分钟 ...
- Codeforces Round #499 (Div. 2) D. Rocket题解
题目: http://codeforces.com/contest/1011/problem/D This is an interactive problem. Natasha is going to ...
- Codeforces Round #499 (Div. 2) C Fly题解
题目 http://codeforces.com/contest/1011/problem/C Natasha is going to fly on a rocket to Mars and retu ...
随机推荐
- C#LeetCode刷题之#203-删除链表中的节点(Remove Linked List Elements)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3826 访问. 删除链表中等于给定值 val 的所有节点. 输入: ...
- PostgreSQL在没有备份情况下误删除Clog恢复
创建实验表postgres# create table t (n_id int primary key,c_name varchar(300));CREATE TABLEpostgres# inser ...
- 极简 Node.js 入门 - 2.3 process
极简 Node.js 入门系列教程:https://www.yuque.com/sunluyong/node 本文更佳阅读体验:https://www.yuque.com/sunluyong/node ...
- JS实例—DOM的增删改
<!DOCTYPE html><html lang="zh"><head> <meta charset="UTF-8" ...
- angular中a标签带请求头下载excel
<!DOCTYPE html> <html lang="en" ng-app="app"> <head> <meta ...
- YAML简要入门
这是一篇简单的YAML入门教程,目的是让你知晓什么YAML,以及YAML的基础语法.方便接下来学习如何使用Golang解析YAML.如果想获得更多YAML的知识,请查看http://yaml.org ...
- L1-006 连续因子 天梯
思路: 素数只有1和本身 合数 暴力枚举 把连续因子最大的记录下来 注意: AC代码 //思路: //素数只有1和本身 //合数 暴力枚举 把连续因子最大的记录下来 #include<iostr ...
- 【SP2916】Can you answer these queries V - 线段树
题面 You are given a sequence \(a_1,a_2,...,a_n\). (\(|A[i]| \leq 10000 , 1 \leq N \leq 10000\)). A qu ...
- ES6中的变量的解构赋值, 解放我们的双手,实现变量的批量赋值
ES6--变量的解构赋值 引言 正文 一.数组的解构赋值 解构失败 不完全解构 默认值 二.对象的解构赋值 三.字符串的解构赋值 结束语 引言 变量的解构赋值, 听起来很复杂, 简单点说可以理解成批量 ...
- 【MySQL】记一次线上重大事故:二狗子竟然把线上数据库删了!!
写在前面 估计二狗子这几天是大姨夫来了,心情很郁闷,情绪也很低落,工作的时候也有点心不在焉.让他发个版本,结果,一行命令下去把线上的数据库删了!你没听错:是删掉了线上的数据库!运营那边顿时炸了锅:怎么 ...