版本比较
4.3
正文
增加 16 行删除 16 行
15 行未改动16171819202122232425262728296 行未改动363738394041424344454647484969 行未改动1191201211221231241251261279 行未改动13713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817949 行未改动22923023123223323423523623723838 行未改动15 行未改动while(n --){cin >> x ;mp[x] ++ ;//记录数据}for(auto p : mp)//遍历{cout << p.first << " " << p.second << "\n" ;}return 0 ;}`````` cpp line-numbers#include <bits/stdc++.h>6 行未改动while(n --){cin >> x ;mp[x] ++ ;}for(auto i = mp.begin() ; i != mp.end() ; i ++){cout << i->first << " " << i->second << "\n" ;}return 0 ;}```只不过还可以用最长平台```cpp line-numbers lines=24-2769 行未改动return 0 ;}```# 3. T408208方法一:注意这个$n\le 32767$,如果暴力的话$O(n^2)=32767^2\approx10^9$看上去会超时,但是实际上我们的$j$只用枚举到$i-1$,所以$10^9\div 2=5\times10^8$,卡不过去```cpp line-numbers#include <bits/stdc++.h>using namespace std ;9 行未改动minn = 2e9 ;for(int j = 1 ; j < i ; j ++){minn = min(minn , max(a[i] - a[j] , a[j] - a[i])) ;if(minn == 0) break ;}sum += minn ;}cout << sum ;return 0 ;}```正解:我们可以用set来记录之前的营销额,肯定是越接近越好,set会自动排序,所以我们可以只看上一个和下一个```cpp line-numbers#include <bits/stdc++.h>using namespace std;int n , x , ans ;set <int> s ;int main(){cin >> n ;for(int i = 1 ; i <= n ; i ++){cin >> x;if (i == 1){ans += x;}else{auto it = s.lower_bound(x);int min_diff = INT_MAX;if (it != s.end()){min_diff = min(min_diff, abs(x - *it));}if (it != s.begin()){--it;min_diff = min(min_diff, abs(x - *it));}49 行未改动using namespace std;const int MAXN = 5e4 + 10;int n , m , x ;char op ;set<int> broken;stack<int> st;int main(){ios::sync_with_stdio(false);cin.tie(0);38 行未改动15 行未改动16171819202122232425262728296 行未改动363738394041424344454647484969 行未改动1191201211221231241251261279 行未改动13713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817949 行未改动22923023123223323423523623723838 行未改动15 行未改动while(n --){cin >> x ;mp[x] ++ ;//记录数据}for(auto p : mp)//遍历{cout << p.first << " " << p.second << "\n" ;}return 0 ;}`````` cpp line-numbers#include <bits/stdc++.h>6 行未改动while(n --){cin >> x ;mp[x] ++ ;}for(auto i = mp.begin() ; i != mp.end() ; i ++){cout << i->first << " " << i->second << "\n" ;}return 0 ;}```只不过还可以用最长平台```cpp line-numbers lines=24-2769 行未改动return 0 ;}```# 3. T408208方法一:注意这个$n\le 32767$,如果暴力的话$O(n^2)=32767^2\approx10^9$看上去会超时,但是实际上我们的$j$只用枚举到$i-1$,所以$10^9\div 2=5\times10^8$,卡不过去```cpp line-numbers#include <bits/stdc++.h>using namespace std ;9 行未改动minn = 2e9 ;for(int j = 1 ; j < i ; j ++){minn = min(minn , max(a[i] - a[j] , a[j] - a[i])) ;if(minn == 0) break ;}sum += minn ;}cout << sum ;return 0 ;}```正解:我们可以用set来记录之前的营销额,肯定是越接近越好,set会自动排序,所以我们可以只看上一个和下一个```cpp line-numbers#include <bits/stdc++.h>using namespace std;int n , x , ans ;set <int> s ;int main(){cin >> n ;for(int i = 1 ; i <= n ; i ++){cin >> x;if (i == 1){ans += x;}else{auto it = s.lower_bound(x);int min_diff = INT_MAX;if (it != s.end()){min_diff = min(min_diff, abs(x - *it));}if (it != s.begin()){--it;min_diff = min(min_diff, abs(x - *it));}49 行未改动using namespace std;const int MAXN = 5e4 + 10;int n , m , x ;char op ;set<int> broken;stack<int> st;int main(){ios::sync_with_stdio(false);cin.tie(0);38 行未改动