Codeforces Round #569 (Div. 2) 题解A - Alex and a Rhombus+B - Nick and Array+C - Valeriy and Dequ+D - Tolik and His Uncle
A. Alex and a Rhombus
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
While playing with geometric figures Alex has accidentally invented a concept of a n-th order rhombus in a cell grid.
A 1-st order rhombus is just a square 1×1 (i.e just a cell).
A n-th order rhombus for all n≥2 one obtains from a n−1-th order rhombus adding all cells which have a common side with it to it (look at the picture to understand it better).
Alex asks you to compute the number of cells in a n-th order rhombus.
Input
The first and only input line contains integer n (1≤n≤100) — order of a rhombus whose numbers of cells should be computed.
Output
Print exactly one integer — the number of cells in a n-th order rhombus.
Examples
inputCopy
1
outputCopy
1
inputCopy
2
outputCopy
5
inputCopy
3
outputCopy
13
Note
Images of rhombus corresponding to the examples are given in the statement.
题意:
思路:
就用一个等差数列的前列项和的公式即可求解
细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int main()
{
//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
//freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
ll n;
cin>>n;
ll ans=n+(n-1)*n;
ans=ans+(ans-(n-1)*2-1);
cout<<ans<<endl;
return 0;
}
inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}
B. Nick and Array
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Nick had received an awesome array of integers a=[a1,a2,…,an] as a gift for his 5 birthday from his mother. He was already going to explore its various properties but after unpacking he was disappointed a lot because the product a1⋅a2⋅…an of its elements seemed to him not large enough.
He was ready to throw out the array, but his mother reassured him. She told him, that array would not be spoiled after the following operation: choose any index i (1≤i≤n) and do ai:=−ai−1.
For example, he can change array [3,−1,−4,1] to an array [−4,−1,3,1] after applying this operation to elements with indices i=1 and i=3.
Kolya had immediately understood that sometimes it's possible to increase the product of integers of the array a lot. Now he has decided that he wants to get an array with the maximal possible product of integers using only this operation with its elements (possibly zero, one or more times, as many as he wants), it is not forbidden to do this operation several times for the same index.
Help Kolya and print the array with the maximal possible product of elements a1⋅a2⋅…an which can be received using only this operation in some order.
If there are multiple answers, print any of them.
Input
The first line contains integer n (1≤n≤105) — number of integers in the array.
The second line contains n integers a1,a2,…,an (−106≤ai≤106) — elements of the array
Output
Print n numbers — elements of the array with the maximal possible product of elements which can be received using only this operation in some order from the given array.
If there are multiple answers, print any of them.
Examples
inputCopy
4
2 2 2 2
outputCopy
-3 -3 -3 -3
inputCopy
1
0
outputCopy
0
inputCopy
3
-3 -3 2
outputCopy
-3 -3 2
题意:
给你一个数组,每一个数都可以通过给定的公式变成另外一个值,让你求所有可能的数组中,数组的所有值乘积最大的那个数组
思路:
我们通过分析可以知道,每一个数可以变成一个负数和一个非负数,而且那个负数的绝对值是大于非负数的绝对值,那么通过这个性质我们可以贪心心的来搞,通过排序,我们让尽可能多的偶数个数变成负数,然后如果还剩余 一个数,让他变成非负数,这样得到的就是答案数组。
细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int f(int x)
{
return -1*x-1;
}
pii a[maxn];
int n;
int ans[maxn];
int main()
{
//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
//freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
gbtb;
cin>>n;
repd(i,1,n)
{
cin>>a[i].fi;
if(a[i].fi<0)
{
a[i].fi=f(a[i].fi);
}
a[i].se=i;
}
sort(a+1,a+1+n);
int jian;
if(n&1)
{
jian=1;
}else
{
jian=0;
}
repd(i,1,(n-jian))
{
a[i].fi=f(a[i].fi);
}
repd(i,1,n)
{
ans[a[i].se]=a[i].fi;
}
repd(i,1,n)
{
cout<<ans[i]<<" ";
}
cout<<endl;
return 0;
}
inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}
C. Valeriy and Deque
time limit per test6 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is ai (i = 1,2,…,n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A>B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation.
For example, if deque was [2,3,4,5,1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3,4,5,1,2].
The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number mj (j=1,2,…,q). It is required for each query to answer which two elements he will pull out on the mj-th operation.
Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque.
Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides.
Input
The first line contains two integers n and q (2≤n≤105, 0≤q≤3⋅105) — the number of elements in the deque and the number of queries. The second line contains n integers a1, a2, ..., an, where ai (0≤ai≤109) — the deque element in i-th position. The next q lines contain one number each, meaning mj (1≤mj≤1018).
Output
For each teacher's query, output two numbers A and B — the numbers that Valeriy pulls out of the deque for the mj-th operation.
Examples
inputCopy
5 3
1 2 3 4 5
1
2
10
outputCopy
1 2
2 3
5 2
inputCopy
2 0
0 0
outputCopy
Note
Consider all 10 steps for the first test in detail:
[1,2,3,4,5] — on the first operation, A and B are 1 and 2, respectively.
So, 2 we write to the beginning of the deque, and 1 — to the end.
We get the following status of the deque: [2,3,4,5,1].
[2,3,4,5,1]⇒A=2,B=3.
[3,4,5,1,2]
[4,5,1,2,3]
[5,1,2,3,4]
[5,2,3,4,1]
[5,3,4,1,2]
[5,4,1,2,3]
[5,1,2,3,4]
[5,2,3,4,1]⇒A=5,B=2.
题意:
题目给定一个双端队列,然后给定一个operation,每一个operation可以实现以下步骤:
- 取出队列的前两个元素,分别是A,B。
2、如果A>B,那么A加入到这个队列的front,B加入到back,否则A加入back,B加入front。,然后给你q个询问,每一个询问是问你从原始数组开始执行第x次operation时,A和B分别是多少?
思路:
通过分析我们可以这样的性质:
当数组中的最大的数被移动到第一个的位置的时候,接下来的操作是有循环节的,每一个循环里就是数组a[1] 之后的元素,滚动一遍。
那么我们可以这样来写:,首先读入时候得到数组的最大值,然后进行operation,每一个operation的A和B都记录下来,当front元素是最大值的时候,结束操作。然后记录一下a[2]~a[n] 分别都是哪些书。
对于每一个询问,如果x小于执行到最大值的次数,就直接输出记录的A和B,否则直接取模后从循环节里输出答案。
细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n;
int w;
int a[maxn];
pii ans[maxn];
int b[maxn];
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
gbtb;
cin>>n>>w;
int num=-2;
int id;
repd(i,1,n)
{
cin>>a[i];
if (a[i]>num)
{
num=a[i];
id=i;
/* code */
}
}
// 5 4 3 2 1
// 5 3 2 1 4
// 5 2 1 4 3
// 5 1 4 3 2
// 5 4 3 2 1
// 5 3 2 1 4
// 5 2 1 4 3
// 5 1 4 3 2
//
//
//
// 1 5 4 3 2
// 5 4 3 2 1
// 5 3 2 1 4
// 5 2 1 4 3
// 5 1 4 3 2
// 5 4 3 2 1
//
//
int mod=n-1;
ll x;
deque<int> q;
while(!q.empty())
q.pop_front();
repd(i,1,n)
{
q.push_back(a[i]);
}
x=1;
while(q.front()!=num)
{
int A;
int B;
A=q.front();
q.pop_front();
B=q.front();
q.pop_front();
ans[x].fi=A;
ans[x].se=B;
x++;
if(A>B)
{
q.push_front(A);
q.push_back(B);
}else
{
swap(A,B);
q.push_front(A);
q.push_back(B);
}
}
x=1;
while(!q.empty())
{
b[x++]=q.front();
q.pop_front();
}
while(w--)
{
cin>>x;
if(x<id)
{
cout<<ans[x].fi<<" "<<ans[x].se<<endl;
}else
{
x-=id;
int c=x%(n-1);
cout<<b[1]<<" "<<b[2+c]<<endl;
}
}
return 0;
}
inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}
D. Tolik and His Uncle
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
This morning Tolik has understood that while he was sleeping he had invented an incredible problem which will be a perfect fit for Codeforces! But, as a "Discuss tasks" project hasn't been born yet (in English, well), he decides to test a problem and asks his uncle.
After a long time thinking, Tolik's uncle hasn't any ideas on how to solve it. But, he doesn't want to tell Tolik about his inability to solve it, so he hasn't found anything better than asking you how to solve this task.
In this task you are given a cell field n⋅m, consisting of n rows and m columns, where point's coordinates (x,y) mean it is situated in the x-th row and y-th column, considering numeration from one (1≤x≤n,1≤y≤m). Initially, you stand in the cell (1,1). Every move you can jump from cell (x,y), which you stand in, by any non-zero vector (dx,dy), thus you will stand in the (x+dx,y+dy) cell. Obviously, you can't leave the field, but also there is one more important condition — you're not allowed to use one vector twice. Your task is to visit each cell of the field exactly once (the initial cell is considered as already visited).
Tolik's uncle is a very respectful person. Help him to solve this task!
Input
The first and only line contains two positive integers n,m (1≤n⋅m≤106) — the number of rows and columns of the field respectively.
Output
Print "-1" (without quotes) if it is impossible to visit every cell exactly once.
Else print n⋅m pairs of integers, i-th from them should contain two integers xi,yi (1≤xi≤n,1≤yi≤m) — cells of the field in order of visiting, so that all of them are distinct and vectors of jumps between them are distinct too.
Notice that the first cell should have (1,1) coordinates, according to the statement.
Examples
inputCopy
2 3
outputCopy
1 1
1 3
1 2
2 2
2 3
2 1
inputCopy
1 1
outputCopy
1 1
Note
The vectors from the first example in the order of making jumps are (0,2),(0,−1),(1,0),(0,1),(0,−2).
题意:
给你一个n*m的网格,每一次从x1,y1走到x2,y2 移动值是dx=x2-x1.dy=y2-y1,现在要求不能有相同的 (dx,dy) 走遍所有网格,初始时你在1,1位置。
思路: 构造如下的走法,即可满足条件。编号是走的顺序。

细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
int n,m;
gbtb;
cin>>n>>m;
int l=1;
int r=m;
while(l<=r)
{
if(l<r)
{
repd(i,1,n)
{
cout<<i<<" "<<l<<'\n';
cout<<n-i+1<<" "<<r<<'\n';
}
l++;
r--;
}else
{
int u=1;
int d=n;
repd(i,1,n)
{
if(i&1)
{
cout<<u<<" "<<l<<'\n';
u++;
}else
{
cout<<d<<" "<<l<<'\n';
d--;
}
}
l++;
r--;
}
}
cout<<endl;
return 0;
}
inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}
Codeforces Round #569 (Div. 2) 题解A - Alex and a Rhombus+B - Nick and Array+C - Valeriy and Dequ+D - Tolik and His Uncle的更多相关文章
- Codeforces Round #182 (Div. 1)题解【ABCD】
Codeforces Round #182 (Div. 1)题解 A题:Yaroslav and Sequence1 题意: 给你\(2*n+1\)个元素,你每次可以进行无数种操作,每次操作必须选择其 ...
- Codeforces Round #608 (Div. 2) 题解
目录 Codeforces Round #608 (Div. 2) 题解 前言 A. Suits 题意 做法 程序 B. Blocks 题意 做法 程序 C. Shawarma Tent 题意 做法 ...
- Codeforces Round #525 (Div. 2)题解
Codeforces Round #525 (Div. 2)题解 题解 CF1088A [Ehab and another construction problem] 依据题意枚举即可 # inclu ...
- Codeforces Round #528 (Div. 2)题解
Codeforces Round #528 (Div. 2)题解 A. Right-Left Cipher 很明显这道题按题意逆序解码即可 Code: # include <bits/stdc+ ...
- Codeforces Round #466 (Div. 2) 题解940A 940B 940C 940D 940E 940F
Codeforces Round #466 (Div. 2) 题解 A.Points on the line 题目大意: 给你一个数列,定义数列的权值为最大值减去最小值,问最少删除几个数,使得数列的权 ...
- Codeforces Round #677 (Div. 3) 题解
Codeforces Round #677 (Div. 3) 题解 A. Boring Apartments 题目 题解 简单签到题,直接数,小于这个数的\(+10\). 代码 #include &l ...
- Codeforces Round #665 (Div. 2) 题解
Codeforces Round #665 (Div. 2) 题解 写得有点晚了,估计都官方题解看完切掉了,没人看我的了qaq. 目录 Codeforces Round #665 (Div. 2) 题 ...
- Codeforces Round #160 (Div. 1) 题解【ABCD】
Codeforces Round #160 (Div. 1) A - Maxim and Discounts 题意 给你n个折扣,m个物品,每个折扣都可以使用无限次,每次你使用第i个折扣的时候,你必须 ...
- Codeforces Round #383 (Div. 2) 题解【ABCDE】
Codeforces Round #383 (Div. 2) A. Arpa's hard exam and Mehrdad's naive cheat 题意 求1378^n mod 10 题解 直接 ...
随机推荐
- git强行覆盖master分支
目录 我遇到的场景 需要注意的预备操作 操作步骤 我遇到的场景 1.master分支只为护较早的版本 2.由于业务不稳定,新业务和功能都在dev 分支上,dev的开发周期很长,一直变更迭代 3.从de ...
- HTTP头详解:
GET/mycode/2.gifHTTP/1.1 [表示发送的是GET请求,请求资源是/mycode/2.gif,协议HTTP/1.1] Host:localhost [主机] Connectio ...
- 【python3】 抓取异常信息try/except
注意:老版本的Python,except语句写作"except Exception, e",Python 2.6后应写作"except Exception as e&qu ...
- 测开之路一百四十四:ORM之SQLAlchemy查询
在上一篇的基础上,插入数据 查询 Department.query.all() # 用表对象查db.session.query(Department).all() # 用db对象查 查询前两条,直接p ...
- python学习笔记:(七)dict(字典)常用的方法
字典是通过名称来引用值的数据结构,这种类型的数据结构称为:映射. 字典是python中唯一的内建映射类型. 注意: 1.字典中键必须是唯一的,如果同一个键被赋值两次,会使用后一个值: 2.键必须不可变 ...
- 系统分析与设计HW6
1. 使用 UML State Model建模 建模对象: 参考 Asg_RH 文档, 对 Reservation/Order 对象建模. 建模要求: 参考练习不能提供足够信息帮助你对订单对象建模,请 ...
- 【js】什么是函数节流与函数去抖
函数节流 意思:节省流量,不会一直访问. | 指定时间内不执行,指定时间后执行. | 一段时间内只执行一次 场景: 比如控制游戏人物攻击,时间内就算按得很快,也只能砍一刀,过后才能砍第二刀. 搜索引擎 ...
- Go语言入门篇-环境准备
一.GO语言特点 静态类型:首先要明确变量类型,如上所示. 编译型:指GO语言要被编译成机器能识别机器代码. GO语言开源. 编程范式:支持“函数式”和“面向对象” GO语言原生的支持并发编程:即GO ...
- RPM包或源码包
安装RPM包或源码包 点击vmware右下角光驱连接. 安装rpm包 -i:表示安装 -v:表示可视化 -h:表示显示安装进度 (同时使用) --force:表示强制安装,即使覆盖属于其他包的文件也要 ...
- Linux yum 命令篇
yum( Yellow dog Updater, Modified)是一个在Fedora和RedHat以及SUSE中的Shell前端软件包管理器. 基於RPM包管理,能够从指定的服务器自动下载RPM包 ...