Codeforces Round #569 (Div. 2)

https://codeforces.com/contest/1180

A. Alex and a Rhombus 

找规律,不难发现 a[i] = a[i-1]+4*(i-1)

或者  a[n] = 2*n*n-2*n+1

#include<bits/stdc++.h>

using namespace std;

int main(){
    int n,t,a[200];
    cin>>n;
    a[1] = 1; a[2] = 5; 
    for(int i = 3; i <= n; i++) a[i] = a[i-1]+4*(i-1);
    cout<< a[n] <<endl;
    return 0;
}
View Code

 

 

B. Nick and Array

给定一个序列,可以对其中的值取负-1, 问乘积最大且操作次数最小的序列是(若多种输出一种)

当a_i > 0时,变为 -a_i-1  |product |会增大,所以当product 为正值时,已经找到答案

当product < 0 时(即n&1),把最大的负数变为正数就可以,证明的话随手写三个负数 纸上乘下一看即知

#include<bits/stdc++.h>

using namespace std;
const int MA = 1e5+100;
//int a[MA],b[MA],c[MA];
vector<int> a;
int MI = 1e6+100;
int MII = -1e6;
int main(){
    int n,t1 = 0, t2 = 0;
    long long sum = 1;
    scanf("%d", &n);
    int cnt, tmp, MII = 0;

    for(int i = 0; i < n; i++) 
        {
            int k;
            scanf("%d", &k);
            a.push_back(k);
            if(a[i] >= 0) a[i] = -a[i]-1;
            if(a[i] <= MII ) tmp = i, MII = a[i];
        }

    if(n&1) a[tmp] = -a[tmp]-1;
    
    
     { for(int i = 0; i < n; i++) cout<<a[i] <<" ";}
    return 0;
}
View Code

 

 

C. Valeriy and Deque

enmmmmm 这道题教我用上了deque,观察以后发现当A变成序列里最大的数以后就不会变化了,也就是A固定 剩下的数成一个序列左移成B

在A不是最大的数之前,扫一遍O(n),将所有情况用vector<pair<int, int> >v记录下来, 当操作数 m_j > n 时,B_j = v[(m_j - maxIndex-1)%(n-1)+maxIndex].second

/*
排首 两个A B, 若A > B, A~~~~B
否     B~~~~A
input
5 3
1 2 3 4 5
1
2
10
output
1 2
2 3
5 2
input
2 0
0 0
output
*/
#include<algorithm>
#include<iostream>
#include<vector>
#include<deque>

using namespace std;

int main(){
    int n,m; cin>>n>>m;
    deque<int> num;
    int maxValue = 0;
    for(int i = 0; i < n; i++) 
    {
        int x;cin>>x;
        num.push_back(x);
        if(x > maxValue) maxValue = x;
    }
    vector<pair<int, int> > results;
    while(num.front() != maxValue) {
        int a = num.front();  num.pop_front();
        int b = num.front();  num.pop_front();
        results.push_back(make_pair(a, b));
        if(a < b) swap(a, b);
        num.push_back(b);
        num.push_front(a);
    }
    
    int len = results.size();
    num.pop_front();
    for(int i = 0; i < n-1; i++)
    {
        int x = num.front();
        results.push_back(make_pair(maxValue, x));
        num.pop_front();
    }
    
    for(int i = 0; i < m; i++){
        long long k; cin>>k;
        if(k <= n) cout<<results[k-1].first <<" "<<results[k-1].second <<endl;
        else{
            int x = (k-len-1)%(n-1) + len;
            cout<<results[x].first <<" " <<results[x].second << endl;
        }
    }
    return 0;
}
View Code

 

 

D. Tolik and His Uncle

输入n,m (1<= n*m <= 1e6),代表一个n*m的网格 左上角坐标1,1 右下角坐标 n,m

enmmmmmm 然后从1,1 开始遍历(可以到任意网格中的任意点),要求每个点都要走过,且两两之间的向量不能相同,

例如 1,1 -> 1,2 以后 1,2 -> 1,3就不行   1,1 -> 2,2 以后 2,2 -> 3,3就不行

First, we are going to describe how to bypass 1m1⋅m strip.

This algorithm is pretty easy — (1,1)(1,1) -> (1,m)(1,m) -> (1,2)(1,2) -> (1,m1)(1,m−1) -> …. Obviously all jumps have different vectors because their lengths are different.

It turns out that the algorithm for nmn⋅m grid is almost the same. Initially, we are going to bypass two uttermost horizontals almost the same way as above — (1,1)(1,1) -> (n,m)(n,m) -> (1,2)(1,2) -> (n,m1)(n,m−1) -> … -> (1,m)(1,m) -> (n,1)(n,1). One can realize that all vectors are different because they have different dydy. Note that all of them have |dx|=n1|dx|=n−1. Then we will jump to (2,1)(2,1) (using ((n2),0)(−(n−2),0) vector). Now we have a smaller task for (n2)m(n−2)⋅m grid. One can see that we used only vectors with |dx|n2|dx|≥n−2, so they don't influence now at all. So the task is fully brought down to a smaller one.

意思是从1,1左上角开始 一个右下角n,m 再左上角 1,2 再右下角n,m-1 这样就可以保证所有的vector都不同,但是要分下n的奇偶,因为若n为奇数,则n/2+1 这一行需要单独讨论,

 n为奇数的话就可以从 x,1->x,m -> x,2 -> x,m-1 .........(x=n/2+1) ,若m为基数则再输出一个 n/2+1,m/2+1

开始没想到,看题解以后 用了双端队列 TLE on test 6,一直TLE 很是不解, 直到想起输入能卡人   输出也能?

enmmmmmm 答案是能, 把endl 改成 \n 就过了

#include<algorithm>
#include<iostream>
#include<deque>

using namespace std;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.precision(20);
    cout << fixed;
    int n,m; cin>>n >>m;
    for(int i = 0; i < n/2; i++)
        for(int j = 0; j < m; j++)
        {
            cout<<i+1 <<" "<<j+1<<"\n";
            cout<<n-i <<" "<<m-j<<"\n";
        }
    if(n & 1) {
        int x = n/2 + 1;
        cout<< x<<" 1\n";
        deque<int > num;
        for(int i = 2; i <= m; i++)  num.push_back(i);

        while(!num.empty()){//!num.empty()
            cout<<x <<" " <<num.back() <<"\n";
            num.pop_back();
            if(!num.empty()) {//!num.empty()
                cout<<x <<" " <<num.front()<<"\n";
                num.pop_front();
            }
        }
    }
    return 0;
}
/*
inputCopy
2 3
outputCopy
1 1
1 3
1 2
2 2
2 3
2 1
inputCopy
1 1
outputCopy
1 1
*/
View Code

直接输出,不用双端队列也行, 而且更节省空间

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,m;
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n/2;i++){
        for(int j=1;j<=m;j++){
            printf("%d %d\n",i,j);
            printf("%d %d\n",n-i+1,m-j+1);//
        }
    }
    if(n&1){
        for(int i=1;i<=m/2;i++){
            printf("%d %d\n",n/2+1,i);
            printf("%d %d\n",n/2+1,m-i+1);
        }
        if(m&1)printf("%d %d\n",n/2+1,m/2+1);
    }
    return 0;
}
View Code

764ms,300KB -> 234ms,0KB

 

转载于:https://www.cnblogs.com/163467wyj/p/11094713.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值