習題:農夫約的假期(分析&中位數)
阿新 • • 發佈:2020-11-28
題目
思路
大意了,以為最長的題目是最噁心的題目
原來的魔音值實際上沒有影響,因為不管什麼方案都要包含這些基礎貢獻
仔細觀察,其中行和列是獨立的
考慮行的式子,設選擇的點的行為x
那麼就有\(ans=\sum|x-x_i|\)
然後?這不是個裸的絕對值不等式的經典式子?
取中位數即可
程式碼
#include<iostream> #include<algorithm> #include<cstring> #include<cstdio> #include<queue> using namespace std; struct node { int x,y; long long val; friend bool operator < (const node &a,const node &b) { return a.val>b.val; } }a[100005]; int n,m,z; int x,y; long long ans=0; int f_abs(int x) { return x<0?-x:x; } vector<int> v1,v2; int main() { //ios::sync_with_stdio(false); //freopen("shuru.in","r",stdin); //freopen("shuru.out","w",stdout); cin>>n>>m>>z; for(int i=1;i<=m;i++) { cin>>a[i].x>>a[i].y>>a[i].val; ans=ans+a[i].val; v1.push_back(a[i].x); v2.push_back(a[i].y); } sort(v1.begin(),v1.end());sort(v2.begin(),v2.end()); if(m%2==0) m=m/2-1; else m/=2; for(int i=0;i<v1.size();i++) ans=ans+f_abs(v1[m]-v1[i]); for(int i=0;i<v2.size();i++) ans=ans+f_abs(v2[m]-v2[i]); cout<<ans<<'\n'<<v1[m]<<' '<<v2[m]; return 0; }