普及练习场 贪心EX 皇后游戏
2018-01-16 23:11
260 查看
题目链接
记i−1位大臣拿的钱为ci−1 ,第i 位大臣和第i+1位大臣拿的钱可以分别表示出来。此时顺序是,i−1,i,i+1 ①
然后交换第i 位大臣和第i+1位大臣。此时顺序是i−1,i+1,i②
此时我们希望第i+1位大臣排在后面拿的钱要少与第i 位大臣排在后面的前,此时就有不等式了
题意理解
显然,第i+1 位大臣拿的钱比第i 位大臣多记i−1位大臣拿的钱为ci−1 ,第i 位大臣和第i+1位大臣拿的钱可以分别表示出来。此时顺序是,i−1,i,i+1 ①
然后交换第i 位大臣和第i+1位大臣。此时顺序是i−1,i+1,i②
此时我们希望第i+1位大臣排在后面拿的钱要少与第i 位大臣排在后面的前,此时就有不等式了
代码
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; class People implements Comparable{ int l; int r; public People(int l, int r) { this.l = l; this.r = r; } @Override public int compareTo(Object o) { People people = (People)o; return Math.min(this.l, people.r) - Math.min(this.r, people.l); } } public class Main { static int T; static int n; static People[] peoples; public static void main(String[] args) { FastScanner fs = new FastScanner(); T = fs.nextInt(); while(T-->0) { long sum = 0; long[] dp = new long[50050]; dp[0] = 0; n = fs.nextInt(); peoples = new People ; for(int i = 0; i < n; i++) { int x = fs.nextInt(); int y = fs.nextInt(); peoples[i] = new People(x, y); } Arrays.sort(peoples); for(int i = 0; i < n; i++) { sum += peoples[i].l; dp[i] = Math.max(dp[Math.max(0, i - 1)], sum) + peoples[i].r; } System.out.println(dp[n-1]); } } public static class FastScanner { private BufferedReader br; private StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } public String nextToken() { while(st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { // TODO: handle exception } } return st.nextToken(); } public int nextInt() { return Integer.valueOf(nextToken()); } } }
相关文章推荐
- 普及练习场 贪心 国王游戏
- 普及练习场 贪心EX 木棍加工
- 普及练习场之排序Ex
- 普及练习场 排序Ex 谁拿了最多奖学金
- 普及练习场 贪心 排队接水
- 普及练习场 排序Ex 奖学金
- 普及练习场 贪心 纪念品分组
- 普及练习场 贪心 合并果子
- 普及练习场 深度优先搜索 八皇后
- 【贪心】【noip模拟】皇后游戏
- 普及练习场 贪心 凌乱的yyy
- 普及练习场 排序Ex 瑞士轮
- 【2016普及组模拟考试】01 贪心 game(接数游戏)
- 普及练习场 排序Ex 魔法照片
- 普及练习场 贪心 数列分段
- 普及练习场 贪心 [USACO1.3]混合牛奶 Mixing Milk
- 国王游戏 2012年NOIP全国联赛提高组(贪心+高精)
- 1093 花生采摘 2004年NOIP全国联赛普及组【貌似贪心】
- 【noip2008普及】传球游戏
- 数学游戏(mathgame)解题方法 - N皇后位运算解法,14皇后亦在1秒中