B. Most socially-distanced subsequence(思维+模拟)

B. Most socially-distanced subsequence

题意:
给出一个数组 a a a,找到数组中的一个子序列 s s s(长为 x x x),满足“ ∑ i = 1 x ∣ s [ i ] − s [ i − 1 ] ∣ \sum_{i=1}^x|s[i]-s[i-1]| i=1xs[i]s[i1]最大”。如果有多个答案就出 x x x较小的子序列

思路:
我们观察一个递增的序列 s = { 1 , 2 , 3 , 4 , 6 } s = \{1,2,3,4,6\} s={12346},则:
∑ i = 1 x ∣ s [ i ] − s [ i − 1 ] ∣ = 5 \sum_{i=1}^x|s[i]-s[i-1]| = 5 i=1xs[i]s[i1]=5,因为 s [ i ] > s [ i − 1 ] s[i] > s[i-1] s[i]>s[i1],所以当我们拆开公式,发现只剩下 s [ 5 ] − s [ 1 ] = 6 − 1 = 5 s[5] - s[1] = 6-1 = 5 s[5]s[1]=61=5,同理当序列递减也有同样的结论。(解决了最短的问题)。
在这里插入图片描述
序列 { x , z , y } \{x,z,y\} {x,z,y},如果在数轴上 z z z x , y x,y xy中间(如 z 1 z_1 z1), ∣ y − z ∣ + ∣ z − x ∣ = y − x |y-z|+|z-x| = y-x yz+zx=yx,如果像 z 2 z_2 z2一样,我们可以发现 ∣ y − z ∣ + ∣ z − x ∣ = z − y + z − x |y-z|+|z-x| = z-y+z-x yz+zx=zy+zx,这个找到 ∑ i = 1 x ∣ s [ i ] − s [ i − 1 ] ∣ \sum_{i=1}^x|s[i]-s[i-1]| i=1xs[i]s[i1]最大和 x x x最小用到的方法都是一样的。

剩下的就是我们的模拟了,找出数组a中连续的递增或递减的序列,除去中间的元素,不影响结果。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+10;
const int inf = 0x7ffffff;
const ll INF  = 0x7fffffffffff;
ll f[][2] = {1, 0, 0, 1, -1, 0, 0, -1}, n, m;
ll s[N];
int read() {
    int x = 0; int f = 1; char s = getchar();
    while(s < '0' || s > '9') {if(s == '-') f = -1; s = getchar();}
    while(s >= '0' && s <= '9') {x = (x << 3) + (x << 1) + s - 48; s = getchar();}
    return x * f;
}
int main() {
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    int t;
    cin >> t;
    while(t--) {
        n = read();
        m = n;
        for(int i=1; i<=n; i++) s[i] = read();
        if(n == 2) {
            cout << 2 << endl;
            for(int i=1; i<=n; i++) cout << s[i] << ' ';
            cout << endl; 
            continue;
        }
        int i = 1, j = 2, k = 3;//从数组下标1,2,3开始扫描,每三个三个比较。
        while(k <= n) {
        	//判断是否连续递增或
            if(s[j]>s[i] && s[k]>s[j] || s[j]<s[i] && s[j]>s[k]) {
                m--;
                s[j] = -1;/做标记。
                j++, k++;
            }
            else i = j, j++, k++;
        }
        cout << m << endl;
        for(int i=1; i<=n; i++) {
            if(s[i] != -1) cout << s[i] << ' ';
        }
        cout << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
I choose Germany to talk about its attitude and measures towards sustainable energy use. Germany is a leading country in the world when it comes to sustainable energy use. The country has committed to phase out nuclear power and shift towards renewable energy sources. The German government has set a target of generating 65 percent of its electricity from renewable energy by 2030. One of the main measures taken by Germany to achieve this target is the Renewable Energy Sources Act, which was introduced in 2000. This act provides financial incentives for individuals and companies to invest in renewable energy sources such as wind, solar, and biomass. The act guarantees a fixed price for renewable energy generated by private individuals and businesses, making it easier for them to recover their investment costs. Germany has also invested heavily in research and development of renewable energy technologies. The country has a number of research institutions and universities that are dedicated to developing new and innovative technologies to support sustainable energy. Another notable measure taken by Germany is the introduction of the Energiewende program. This program is a comprehensive strategy aimed at transforming the country’s energy system to one that is based on renewable energy sources. The program seeks to create a sustainable and affordable energy system that is also socially responsible. The German government has also introduced a number of policies and regulations to promote energy efficiency. For instance, all new buildings in Germany are required to meet strict energy efficiency standards. The country has also implemented a number of policies to promote the use of electric vehicles and public transportation. In conclusion, Germany has shown a strong commitment towards sustainable energy use. The country has taken a number of measures to promote the use of renewable energy sources and to phase out nuclear power. With the Renewable Energy Sources Act, Energiewende program, and other policies and regulations, Germany is well on its way to achieving its target of generating 65 percent of its electricity from renewable energy by 2030.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值