6 条题解
-
2
第一部分 分析
把题目转换得:一堆竹竿被固定在一个点上,旋转竹竿求覆盖最大区间。 我们可以把竹竿从标记处分成两半,将两端长度扔到一个数组里去,对这个数组排序,最大的两个就是能放到标记点左边和右边能达到的最长长度,加起来就是答案。
第二部分 代码
#include <iostream> #include <algorithm> using namespace std; long long n,a[100010],b[100010],c[100010],cnt; int main() { cin>>n; for(int i=1; i<=n; i++) { cin>>a[i]>>b[i]; c[++cnt]=a[i]-b[i]; c[++cnt]=b[i];//记录两端长度 } sort(c+1,c+cnt+1);//排序 cout<<c[cnt]+c[cnt-1];//输出答案 return 0; }
-
1
标题
你说得对,但是我AC了
思路
找到右最长和左最长的最后合并就可以了
解题方法
模拟
Code
#include <bits/stdc++.h> #define ll long long using namespace std; ll n, maxx1 = -1, maxx2 = -1, s1, d1, i, ans; struct lin { int a, b; } arr[100010]; int main() { cin >> n; for (i = 1; i <= n; i++) { cin >> arr[i].a >> arr[i].b; if (arr[i].a > ans) { ans = arr[i].a; } //ans = max(ans, arr[i].a); ll line1 = arr[i].a - arr[i].b, line2 = arr[i].b; if (maxx1 < line1) { s1 = i, d1 = 1; maxx1 = line1; } if (maxx1 < line2) { s1 = i, d1 = 2; maxx1 = line2; } } for (i = 1; i <= n; i++) { ll line1 = arr[i].a - arr[i].b, line2 = arr[i].b; if (maxx2 < line1) { if (s1 == i && d1 == 1) continue; maxx2 = line1; } if (maxx2 < line2) { if (s1 == i && d1 == 2) continue; maxx2 = line2; } } cout << max(maxx1 + maxx2, ans) << endl; return 0; }
-
0
J8A. 竹竿
思路
分析题目,可以看出就是求出 个竹竿标记的两边的最大值和第二大值,然后将两个值相加。
也可以理解为,将 个竹竿从标记处切开,将得到的竹竿中长度最长的两个相加。
Code
#include <bits/stdc++.h> using namespace std; long long n, a, b, s[10000010], cnt; int main() { scanf("%lld", &n); for (int i = 0; i < n; i ++ ) { scanf("%lld %lld", &a, &b); s[cnt ++] = b; s[cnt ++] = a - b; } sort(s, s + cnt); cout << s[cnt - 1] + s[cnt - 2] << '\n'; return 0; }
- 1
信息
- ID
- 81
- 时间
- 3000ms
- 内存
- 512MiB
- 难度
- 2
- 标签
- 递交数
- 1036
- 已通过
- 253
- 上传者