To celebrate that WF-2018 will be held in PKU, Alice, Bob, and Cate are asked to make N cakes.
Every cake i needs to go through 3 steps in restrict order:
Alice mixes flour and water for ai minutes;
Bob carefully bakes it for bi minutes;
Cate makes cream decoration for ci minutes.
Since Cate wants to have different kinds of cakes, the third step of any cake i is always not less time-consuming than the second step of any cake j. Also, it is reasonable that once anyone starts to process a cake, the procedure cannot be stopped then be resumed.
To have these cakes done as soon as possible, they need your help.
Input
There are several cases (less than 15 cases).
The first line of every case contains an integer N (1 ≤ N ≤ 105)—the number of cakes to prepare.
After that, N lines follow, each of them contains three integers ai, bi and ci (1 ≤ i ≤ N; 0
Output
For every case, print in a single line the least possible time to make all cakes.
Sample Input
3
5 3 4
3 2 9
3 4 8
0
Sample Output
26
题意:
做蛋糕需要三个步骤,必须依次完成。现在一共要做n个蛋糕,分别给出三个步骤的时间。求做完所有蛋糕需要的最短时间。
思路:
算是一个带贪心思想的题目。当知道任意两个蛋糕的a,b,c和顺序时,很容易可以求得制作它们的总时间sum。
图中灰色区域为休息的时间。当123的顺序改变,也可以以此求出改变后的sum。
既然如此,为了使最后的总时间最小,只需要以每两个按不同的顺序进行比较,进行排序,最后求出的就是最短时间。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <iterator>
#include <functional>
#include <stdlib.h>
#include <time.h>
#include <bitset>
using namespace std;
#define pi acos(-1)
#define s_1(x) scanf("%d",&x)
#define s_2(x,y) scanf("%d%d",&x,&y)
#define s_3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define s_4(x,y,z,X) scanf("%d%d%d%d",&x,&y,&z,&X)
#define s_5(x,y,z,X,Y) scanf("%d%d%d%d%d",&x,&y,&z,&X,&Y)
#define S_1(x) scan_d(x)
#define S_2(x,y) scan_d(x),scan_d(y)
#define S_3(x,y,z) scan_d(x),scan_d(y),scan_d(z)
#define PI acos(-1)
#define endl '\n'
#define srand() srand(time(0));
#define me(x,y) memset(x,y,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0); cin.tie(0);
#define FOR(x,n,i) for(int i=x;i<=n;i++)
#define FOr(x,n,i) for(int i=x;i<n;i++)
#define fOR(n,x,i) for(int i=n;i>=x;i--)
#define fOr(n,x,i) for(int i=n;i>x;i--)
#define W while
#define sgn(x) ((x) < 0 ? -1 : (x) > 0)
#define bug printf("***********\n");
#define db double
#define ll long long
#define mp make_pair
#define pb push_back
#define retrun return
#define ture true
typedef long long LL;
typedef pair <int, int> ii;
const int INF = 0x3f3f3f3f;
const LL LINF = 0x3f3f3f3f3f3f3f3fLL;
const int dx[] = {-1, 0, 1, 0, 1, -1, -1, 1};
const int dy[] = {0, 1, 0, -1, -1, 1, -1, 1};
const int maxn = (1 << 17) + 100;
const int maxx = 1e12 + 10;
const double EPS = 1e-8;
const double eps = 1e-10;
const int mod = 1e9 + 7;
const int MOD = 1e3;
template <class T> inline T min(T a, T b, T c) {return min(min(a, b), c);}
template <class T> inline T max(T a, T b, T c) {return max(max(a, b), c);}
template <class T> inline T min(T a, T b, T c, T d) {return min(min(a, b), min(c, d));}
template <class T> inline T max(T a, T b, T c, T d) {return max(max(a, b), max(c, d));}
template <class T> inline bool scan_d(T &ret) {
char c;
int sgn;
if (c = getchar(), c == EOF) return 0;
while (c != '-' && (c < '0' || c > '9')) c = getchar();
sgn = (c == '-') ? -1 : 1;ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
inline bool scan_lf(double &num) {
char in;
double Dec = 0.1;
bool IsN = false, IsD = false;
in = getchar();
if (in == EOF) return false;
while (in != '-' && in != '.' && (in < '0' || in > '9')) in=getchar();
if (in == '-') {IsN = true; num = 0;}
else if (in == '.') {IsD = true; num = 0;}
else num = in - '0';
if (!IsD) {
while (in = getchar(), in >= '0' && in <= '9') {num *= 10; num += in - '0';}
}
if (in != '.') {
if (IsN) num = -num;
return true;
}
else{
while (in = getchar(), in >= '0' && in <= '9') {
num += Dec * (in - '0');
Dec *= 0.1;
}
}
if (IsN) num = num;
return true;
}
void Out(LL a) {if(a < 0) {putchar('-'); a = -a;}if(a >= 10) Out(a / 10); putchar(a % 10 + '0');}
void print(LL a) {Out(a), puts("");}
/**********************************************WHITE_YASHA**********************************************/
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );
//cerr << "run time is " << clock() << endl;
struct cake {
int a, b, c;
} nd[maxn];
int cal(cake x, cake y) {
int A = x.a, B = A + x.b, C = B + x.c;
A += y.a;
B = max(A, B) + y.b;
C = max(B, C) + y.c;
return C;
}
bool cmp(cake x, cake y) {return cal(x, y) < cal(y, x);}
int n;
ll a[maxn], b[maxn], c[maxn];
int main() {
W (s_1(n), n) {
me(a, 0); me(b, 0); me(c, 0);
FOr (0, n, i) s_3(nd[i].a, nd[i].b, nd[i].c);
sort(nd, nd + n, cmp);
FOr (0, n, i) a[i] = !i ? nd[i].a : nd[i].a + a[i - 1];
FOr (0, n, i) b[i] = !i ? a[i] + nd[i].b : nd[i].b + max(b[i - 1], a[i]);
FOr (0, n, i) c[i] = !i ? b[i] + nd[i].c : nd[i].c + max(c[i - 1], b[i]);
printf("%lld\n", c[n - 1]);
}
}