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可以实现以下步骤:

  1. 取出队列的前两个元素,分别是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';
        }
    }
}
posted @ 2019-06-22 19:55  茄子Min  阅读(625)  评论(0编辑  收藏  举报