CF# 334 Alternative Thinking

A. Alternative Thinking
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.

However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as anot-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and{1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.

Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.

Input

The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).

The following line contains a binary string of length n representing Kevin's results on the USAICO.

Output

Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.

Sample test(s)
input
8
10000011
output
5
input
2
01
output
2
Note

In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.

In the second sample, Kevin can flip the entire string and still have the same score.

 

题意:给出1个01串,必须将某一段取反,问选择一个1、0间隔的子序列,最长有多长?

分析:

  我真是傻逼。

  我想了个dp,dp[i]表示前i个中,取反的那个串最后一个数是第i个,的答案。

  预处理两个数组,front[i]表示不修改的前i个的答案,back[i]表示不修改的i~n的答案。

  那么转移显然

dp[i] = max(dp[i - 1] + (arr[i] ^ arr[i - 1]),  front[i - 1] + (arr[i] == arr[i - 1]));

前一个表示修改的是一段的情况,后一个代表只修改第i个的情况。

答案为
ans = max(ans, dp[i] + back[i + 1]);

 

 1 /**
 2 Create By yzx - stupidboy
 3 */
 4 #include <cstdio>
 5 #include <cstring>
 6 #include <cstdlib>
 7 #include <cmath>
 8 #include <deque>
 9 #include <vector>
10 #include <queue>
11 #include <iostream>
12 #include <algorithm>
13 #include <map>
14 #include <set>
15 #include <ctime>
16 #include <iomanip>
17 using namespace std;
18 typedef long long LL;
19 typedef double DB;
20 #define MIT (2147483647)
21 #define INF (1000000001)
22 #define MLL (1000000000000000001LL)
23 #define sz(x) ((int) (x).size())
24 #define clr(x, y) memset(x, y, sizeof(x))
25 #define puf push_front
26 #define pub push_back
27 #define pof pop_front
28 #define pob pop_back
29 #define mk make_pair
30 
31 inline int Getint()
32 {
33     int Ret = 0;
34     char Ch = ' ';
35     bool Flag = 0;
36     while(!(Ch >= '0' && Ch <= '9'))
37     {
38         if(Ch == '-') Flag ^= 1;
39         Ch = getchar();
40     }
41     while(Ch >= '0' && Ch <= '9')
42     {
43         Ret = Ret * 10 + Ch - '0';
44         Ch = getchar();
45     }
46     return Flag ? -Ret : Ret;
47 }
48 
49 const int N = 100010;
50 int n;
51 string data;
52 bool arr[N];
53 int front[N], back[N], dp[N], ans;
54 
55 inline void Input()
56 {
57     scanf("%d", &n);
58     cin >> data;
59 }
60 
61 inline void Solve()
62 {
63     for(int i = 0; i < n; i++) arr[i + 1] = data[i] == '1';
64     int cnt[2] = {0};
65     arr[n + 1] = arr[n] ^ 1;
66     for(int i = n; i > 1; i--)
67     {
68         if(arr[i + 1] ^ arr[i]) cnt[arr[i]] = cnt[arr[i] ^ 1] + 1;
69         back[i] = cnt[arr[i - 1]];
70     }
71     cnt[0] = cnt[1] = 0, arr[0] = arr[1] ^ 1;
72     for(int i = 1; i < n; i++)
73     {
74         if(arr[i] != arr[i - 1]) cnt[arr[i]] = cnt[arr[i] ^ 1] + 1;
75         front[i] = cnt[arr[i + 1]];
76     }
77     
78     for(int i = 1; i <= n; i++)
79     {
80         dp[i] = max(dp[i - 1] + (arr[i] ^ arr[i - 1]), 
81                     front[i - 1] + (arr[i] == arr[i - 1]));
82         ans = max(ans, dp[i] + back[i + 1]);
83     }
84     printf("%d\n", ans);
85 }
86 
87 int main()
88 {
89     freopen("a.in", "r", stdin);
90     Input();
91     Solve();
92     return 0;
93 }
View Code

 

 

可是正解简单到令人发指。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 int N, res = 1;
 5 string S;
 6 
 7 int main(){
 8   cin >> N >> S;
 9   for(int i = 1; i < N; i++){
10     res += (S[i] != S[i - 1]);
11   }
12   cout << min(res + 2, N) << '\n';
13 }
View Code

 

其实很容易理解,修改比不修改的答案最多增加2,举几个例子就知道了。。。

转载于:https://www.cnblogs.com/StupidBoy/p/5091230.html

内容概要:本文介绍了ENVI Deep Learning V1.0的操作教程,重点讲解了如何利用ENVI软件进行深度学习模型的训练与应用,以实现遥感图像中特定目标(如集装箱)的自动提取。教程涵盖了从数据准备、标签图像创建、模型初始化与训练,到执行分类及结果优化的完整流程,并介绍了精度评价与通过ENVI Modeler实现一键化建模的方法。系统基于TensorFlow框架,采用ENVINet5(U-Net变体)架构,支持通过点、线、面ROI或分类图生成标签数据,适用于多/高光谱影像的单一类别特征提取。; 适合人群:具备遥感图像处理基础,熟悉ENVI软件操作,从事地理信息、测绘、环境监测等相关领域的技术人员或研究人员,尤其是希望将深度学习技术应用于遥感目标识别的初学者与实践者。; 使用场景及目标:①在遥感影像中自动识别和提取特定地物目标(如车辆、建筑、道路、集装箱等);②掌握ENVI环境下深度学习模型的训练流程与关键参数设置(如Patch Size、Epochs、Class Weight等);③通过模型调优与结果反馈提升分类精度,实现高效自动化信息提取。; 阅读建议:建议结合实际遥感项目边学边练,重点关注标签数据制作、模型参数配置与结果后处理环节,充分利用ENVI Modeler进行自动化建模与参数优化,同时注意软硬件环境(特别是NVIDIA GPU)的配置要求以保障训练效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值