2 条题解

  • 0
    @ 2026-1-2 0:13:52
    #include<bits/stdc++.h>
    using namespace std;
    const int N=1e5;
    const int M=5e5;
    const int mod=998244353;
    const int INF=0x3f3f3f3f;
    int n;
    long long ans; 
    struct Point{
    	int idx,type,x,y;
    	bool friend operator<(Point a,Point b){
    		if(a.x==b.x)return a.type<b.type;
    		return a.x<b.x;
    	}
    }p[N+10];
    struct Node{
    	int y,idx;
    	bool friend operator<(Node a,Node b){
    		return a.y<b.y;
    	}
    };
    multiset<Node>s;
    multiset<Node>::iterator it;
    int main(){
        ios::sync_with_stdio(0);
    	cin.tie(0);cout.tie(0);
    	cin>>n;
    	for(int i=1;i<=n;i++){
    		cin>>p[i].x>>p[i].y;
    		ans+=p[i].y-p[i].x;
    		p[i].idx=i;p[i].type=0;
    	}
    	for(int i=n+1;i<=n*2;i++){
    		cin>>p[i].x>>p[i].y;
    		ans+=p[i].x-p[i].y;
    		p[i].idx=i-n;p[i].type=1;
    	}
    	sort(p+1,p+1+n*2);
    	cout<<ans<<"\n";
    	for(int i=1;i<=n*2;i++){
    		if(p[i].type){
    			it=s.lower_bound((Node){p[i].y,p[i].idx});
    			cout<<it->idx<<" "<<p[i].idx<<"\n";
    			s.erase(it);
    		}
    		else s.insert((Node){p[i].y,p[i].idx});
    	}
    	return 0;
    }
    
    • 0
      @ 2025-10-8 17:02:56

      因为管道只能向东或向南铺,我们平移管道之后可以发现,管道无论怎么连接,长度都不会变。可以发现总路程 ans=Xj+Yi−(Xi+Yj)。以下2份代码实际上是一样的,但第一份代码避免了爆long long。第二份无法通过洛谷数据。

      #include <bits/stdc++.h>
      using namespace std;
      typedef long long LL;
      int main() {
          int n; scanf("%d", &n);
          LL ans = 0;
          for (LL i = 1; i <= n; i++) {
              LL a, b;
              scanf("%lld%lld", &a, &b);
              ans -= a; ans += b;
          }
          for (LL i = 1; i <= n; i++) {
              LL a, b;
              scanf("%lld%lld", &a, &b);
              ans += a; ans -= b;
          }
          printf("%lld", abs(ans));
          return 0;
      }
      
      • 1

      「POI2007 R3」天然气管道 Gas Pipelines

      信息

      ID
      2761
      时间
      1000ms
      内存
      64MiB
      难度
      6
      标签
      递交数
      19
      已通过
      12
      上传者