NC25136. [USACO 2006 Ope B]Cows on a Leash
描述
输入描述
Line 1: A single integer, N(2 <= N <= 32000)
Lines 2..N+1: Each line contains two space-separated positive integers that describe a leash. The first is the location of the leash's stake; the second is the length of the leash.(1 <= length <= 1e7)
输出描述
Line 1: A single integer that is the minimum number of cuts so that each leash is cut at least once.
示例1
输入:
7 2 4 4 7 3 3 5 3 9 4 1 5 7 3
输出:
2
C++(g++ 7.5.0) 解法, 执行用时: 30ms, 内存消耗: 648K, 提交时间: 2023-01-10 21:29:49
#include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; int main() { int n; cin >> n; vector<pii> a(n); for (auto &[x, y]: a) { cin >> x >> y; y += x; } sort(a.begin(), a.end(), [](auto &l, auto &r) { return l.second < r.second; }); int e = 0, ans = 0; for (auto &[x, y] : a) { if (x >= e) ans++, e = y; } cout << ans << "\n"; }
C++14(g++5.4) 解法, 执行用时: 33ms, 内存消耗: 632K, 提交时间: 2020-05-18 09:54:19
#include "bits/stdc++.h" using namespace std; class A{ public: int x,y; //x起点,y终点 }; bool cmp(A a,A b) { return a.y<b.y; } int main() { int N,i,t,s=0; A a[32005]; cin>>N; for(i=0;i<N;i++) { cin>>a[i].x>>a[i].y; a[i].y+=a[i].x; } sort(a,a+N,cmp); i=0; while(i<N) { s++; t=a[i].y; while(i<N&&a[i].x<t) i++; } cout<<s<<endl; return 0; }
C++ 解法, 执行用时: 22ms, 内存消耗: 632K, 提交时间: 2021-07-18 20:05:40
#include<bits/stdc++.h> using namespace std; #define MAXN 32010 pair<int,int>p[MAXN]; int main(){ int n; cin>>n; int a,b; for(int i=0;i<n;i++){ cin>>a>>b; p[i].second=a; p[i].first=a+b;//结束的时间 } sort(p,p+n); int ans=0,temp=0; for(int i=0;i<n;i++){ if(p[i].second>=temp){ ans++; temp=p[i].first; } } cout<<ans<<endl; return 0; }
pypy3 解法, 执行用时: 209ms, 内存消耗: 26472K, 提交时间: 2023-05-30 16:20:59
N = int(input()) s = [] for o in range(N): a,b=map(int,input().split(" ")) s.append([a,a+b]) s.sort() rt = s[0][1] ans = 0 for i in range(1,N): rt = min(rt,s[i][1]) if rt <= s[i][0]: rt = s[i][1] ans+=1 print(ans+1)