上代码
普通代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100000+10;
struct node{
int l,r;
}s[maxn];
bool cmp(node a,node b){
if(a.l-a.r != b.l-b.r)
return a.l-a.r > b.l-b.r;
return a.l > b.l;
}
int main ()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n;
scanf("%d ",&n);
for(int i=0;i<n;i++)
scanf("%d %d",&s[i].l,&s[i].r);
sort(s,s+n,cmp);
//for(int i=0;i<n;i++){
// printf("%d %d\n",s[i].l,s[i].r);
//}
// printf("\n");
int sum =0 ,res = 0;
for(int i=0;i<n;i++)
{
if(sum < s[i].l)
{
res += s[i].l -sum;//总空间要扩大
sum = s[i].l;
}
sum = sum - s[i].r;
}
printf("%d\n",res);
}
效率高代码:
#include <iostream>
using namespace std;
const int INF = 1e9 + 7;
namespace IO {
const int MT = 10 * 1024 * 1024; /// 10MB 请注意输入数据的大小!!!
char IO_BUF[MT];
int IO_PTR, IO_SZ;
/// 要记得把这一行添加到main函数第一行!!!
void begin() {
IO_PTR = 0;
IO_SZ = fread (IO_BUF, 1, MT, stdin);
}
template<typename T>
inline bool scan_d (T & t) {
while (IO_PTR < IO_SZ && IO_BUF[IO_PTR] != '-' && (IO_BUF[IO_PTR] < '0' || IO_BUF[IO_PTR] > '9'))
IO_PTR ++;
if (IO_PTR >= IO_SZ) return false;
bool sgn = false;
if (IO_BUF[IO_PTR] == '-') sgn = true, IO_PTR ++;
for (t = 0; IO_PTR < IO_SZ && '0' <= IO_BUF[IO_PTR] && IO_BUF[IO_PTR] <= '9'; IO_PTR ++)
t = t * 10 + IO_BUF[IO_PTR] - '0';
if (sgn) t = -t;
return true;
}
inline bool scan_s (char s[]) {
while (IO_PTR < IO_SZ && (IO_BUF[IO_PTR] == ' ' || IO_BUF[IO_PTR] == '\n') ) IO_PTR ++;
if (IO_PTR >= IO_SZ) return false;
int len = 0;
while (IO_PTR < IO_SZ && IO_BUF[IO_PTR] != ' ' && IO_BUF[IO_PTR] != '\n')
s[len ++] = IO_BUF[IO_PTR], IO_PTR ++;
s[len] = '\0';
return true;
}
template<typename T>
void print(T x) {
static char s[33], *s1; s1 = s;
if (!x) *s1++ = '0';
if (x < 0) putchar('-'), x = -x;
while(x) *s1++ = (x % 10 + '0'), x /= 10;
while(s1-- != s) putchar(*s1);
}
template<typename T>
void println(T x) {
print(x); putchar('\n');
}
};
int main()
{
IO::begin();
int n,a,b,min=INF,sum=0;
IO::scan_d(n);
while(n--)
{
IO::scan_d(a);
IO::scan_d(b);
sum+=b;
if(a-b<min) min=a-b;
}
IO::println(sum+min);
return 0;
}
求赞 求关注 求评论