Codeforces Round 997 (Div. 2) A~C

D题和BC题的难度差距太大了,手速场,但偏偏手速场没了手速还爆了罚时 本文章只做思路分享,并非完全正确 如果有什么不对的 请指出

A. Shape Perimeter

链接:Problem - A - Codeforces

题意:给定一个小方格 移动到右上角 求它所移动的每一段联合图形的边长

算法:无

思路:如图 将所有对应边移动到箭头后的位置,最终边长实际为第一次移动的左下端点,和最后一次移动的右上端点所组成的矩形的边长。

代码:

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int Q = 1e5 + 9;
const ll MOD = 1e9 + 7;
ll a[Q];
void solve(){
    ll n,m;cin>>n>>m;
    ll x,y;cin>>x>>y;
    ll x_1=x,y_1=y;
    for (ll i = 1; i < n; i++)
    {
        ll o,p;cin>>o>>p;
        x+=o;
        y+=p;
    }
    x+=m;y+=m;
    cout<<(x-x_1+y-y_1)*2<<"\n";
}
int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    ll _ = 1;cin>>_;
    while(_--){
        solve();
    }
    return 0;
}

B. Find the Permutation

链接:Problem - B - Codeforces

题意:构造一个包含1到n的排列,当i<j 并且a[i]<a[j]的时候 i和j链接一条边 给定邻接矩阵 求排列

算法:构造

思路:1 ~ n 从后往前跑每个位置能到达的最前的位置

代码:

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int Q = 1000 + 9;
const ll MOD = 1e9 + 7;
string s[Q];
ll ans[Q];
void solve(){
    ll n;cin>>n;
    for (ll i = 1; i <= n; i++)
    {
        cin>>s[i];s[i]=" "+s[i];
        ans[i]=i;
    }
    for (ll i = n; i >= 1; i--)
    {
        ll l=n-1,r=n;
        while(l>=1){
            if(ans[l]<ans[r] and s[ans[l]][ans[r]]!='1') {
                swap(ans[l],ans[r]);
            }
            l--,r--;
        }
    }
    
    for (ll i = 1; i <= n; i++)
    {
        cout<<ans[i]<<" ";
    }cout<<"\n";
}
int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    ll _ = 1;cin>>_;
    while(_--){
        solve();
    }
    return 0;
}

C. Palindromic Subsequences

链接:Problem - C - Codeforces

题意:构造一个长度为n的数组 每个数在1~n之间 使得这个数组的最大回文长度为x 且有超过n个回文长度为x的回文

算法:构造

思路:

构造诸如下列数组即可

1    1~n-3     1 2   三部分 左中右

这样可以使得 最大回文长度永远为3 且回文长度为3的个数为(n-3)+(n-4)+(n-4)

(n-3)为左右第一个1 加中间n-3个元素
(n-4)为中间的1 和右边1 加中间 n-4个元素

(n-4)为种右第一个2 加中间n-5个元素 加右边第一个1

因为n是大于6的 所以即使最小的n为6  则6*3 -11 =7 且7>6 符合条件

代码:

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int Q = 1000 + 9;
const ll MOD = 1e9 + 7;
void solve(){
    ll n;cin>>n;
    for (ll i = 1; i <= 1; i++)
    {
        cout<<i<<" ";
    }
    for (ll i = 1; i <= n-3; i++)
    {
        cout<<i<<" ";
    }
    for (ll i = 1; i <= 2; i++)
    {
        cout<<i<<" ";
    }
    
    cout<<"\n";
}
int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    ll _ = 1;cin>>_;
    while(_--){
        solve();
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值