版本比较
【CSYZ OJ】动态数点 题解
正文
增加 16 行删除 16 行
10 行未改动1112131415161718197 行未改动27282930313233343536373839404142434445464748495051525354555657585960616263646529 行未改动95969798991001011021037 行未改动11111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415510 行未改动#define int long longusing namespace std;void read(int& x) {char c;while(c=getchar(),c<'!');x=c-48;while(c=getchar(),c>'!')x=x*10+c-48;7 行未改动freopen("point.in","r",stdin);freopen("point.out","w",stdout);ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);read(n);for(int i=1; i<=n; i++) read(a[i]);for(int i=1; i<=n; i++) {int L = i, R = i;while(L > 1 && a[L-1] % a[i] == 0) L--;while(R < n && a[R+1] % a[i] == 0) R++;int val = R - L;if(val > mv) {mv = val;v.clear();v.push_back(L);} else if(val == mv) {v.push_back(L);}}sort(v.begin(), v.end());v.erase(unique(v.begin(), v.end()), v.end());cout << v.size() << " " << mv << "\n";for(size_t i=0; i<v.size(); i++) {cout << v[i] << " ";}cout << "\n";return 0;}```但是这份代码你交上去会发现有 [$\color{orange}\text{95pts } \color{red}\text{Time Exceeded}$](http://csyzoj.cc/d/CSPS_2025/record/68bbb2ce78a423e5284b91fc),不是因为数据太水,待会解释。考虑优化。显然的,当一个答案区间里存在有两个相同的元素时,这两个元素的答案区间和答案一定是相同的。29 行未改动#define int long longusing namespace std;void read(int& x) {char c;while(c=getchar(),c<'!');x=c-48;while(c=getchar(),c>'!')x=x*10+c-48;7 行未改动freopen("point.in","r",stdin);freopen("point.out","w",stdout);ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);read(n);for(int i=1; i<=n; i++) read(a[i]);for(int i=1; i<=n; i++) {int L = i, R = i;bool flag = 1;while(L > 1 && a[L-1] % a[i] == 0) {L--;if (a[L] == a[i]){flag = 0;break;}}if(!flag) continue;while(R < n && a[R+1] % a[i] == 0) R++;int val = R - L;if(val > mv) {mv = val;v.clear();v.push_back(L);} else if(val == mv) {v.push_back(L);}}sort(v.begin(), v.end());v.erase(unique(v.begin(), v.end()), v.end());cout << v.size() << " " << mv << "\n";for(size_t i=0; i<v.size(); i++) {cout << v[i] << " ";}cout << "\n";return 0;}```## 完结撒花,欢迎 hack10 行未改动1112131415161718197 行未改动27282930313233343536373839404142434445464748495051525354555657585960616263646529 行未改动95969798991001011021037 行未改动11111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415510 行未改动#define int long longusing namespace std;void read(int& x) {char c;while(c=getchar(),c<'!');x=c-48;while(c=getchar(),c>'!')x=x*10+c-48;7 行未改动freopen("point.in","r",stdin);freopen("point.out","w",stdout);ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);read(n);for(int i=1; i<=n; i++) read(a[i]);for(int i=1; i<=n; i++) {int L = i, R = i;while(L > 1 && a[L-1] % a[i] == 0) L--;while(R < n && a[R+1] % a[i] == 0) R++;int val = R - L;if(val > mv) {mv = val;v.clear();v.push_back(L);} else if(val == mv) {v.push_back(L);}}sort(v.begin(), v.end());v.erase(unique(v.begin(), v.end()), v.end());cout << v.size() << " " << mv << "\n";for(size_t i=0; i<v.size(); i++) {cout << v[i] << " ";}cout << "\n";return 0;}```但是这份代码你交上去会发现有 [$\color{orange}\text{95pts } \color{red}\text{Time Exceeded}$](http://csyzoj.cc/d/CSPS_2025/record/68bbb2ce78a423e5284b91fc),不是因为数据太水,待会解释。考虑优化。显然的,当一个答案区间里存在有两个相同的元素时,这两个元素的答案区间和答案一定是相同的。29 行未改动#define int long longusing namespace std;void read(int& x) {char c;while(c=getchar(),c<'!');x=c-48;while(c=getchar(),c>'!')x=x*10+c-48;7 行未改动freopen("point.in","r",stdin);freopen("point.out","w",stdout);ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);read(n);for(int i=1; i<=n; i++) read(a[i]);for(int i=1; i<=n; i++) {int L = i, R = i;bool flag = 1;while(L > 1 && a[L-1] % a[i] == 0) {L--;if (a[L] == a[i]){flag = 0;break;}}if(!flag) continue;while(R < n && a[R+1] % a[i] == 0) R++;int val = R - L;if(val > mv) {mv = val;v.clear();v.push_back(L);} else if(val == mv) {v.push_back(L);}}sort(v.begin(), v.end());v.erase(unique(v.begin(), v.end()), v.end());cout << v.size() << " " << mv << "\n";for(size_t i=0; i<v.size(); i++) {cout << v[i] << " ";}cout << "\n";return 0;}```## 完结撒花,欢迎 hack