C. Pie Rules 逆向思维+DP

22 篇文章 0 订阅

C. Pie Rules
C. Pie Rules
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You may have heard of the pie rule before. It states that if two people wish to fairly share a slice of pie, one person should cut the slice in half, and the other person should choose who gets which slice. Alice and Bob have many slices of pie, and rather than cutting the slices in half, each individual slice will be eaten by just one person.

The way Alice and Bob decide who eats each slice is as follows. First, the order in which the pies are to be handed out is decided. There is a special token called the “decider” token, initially held by Bob. Until all the pie is handed out, whoever has the decider token will give the next slice of pie to one of the participants, and the decider token to the other participant. They continue until no slices of pie are left.

All of the slices are of excellent quality, so each participant obviously wants to maximize the total amount of pie they get to eat. Assuming both players make their decisions optimally, how much pie will each participant receive?
Input

Input will begin with an integer N (1 ≤ N ≤ 50), the number of slices of pie.

Following this is a line with N integers indicating the sizes of the slices (each between 1 and 100000, inclusive), in the order in which they must be handed out.
Output

Print two integers. First, the sum of the sizes of slices eaten by Alice, then the sum of the sizes of the slices eaten by Bob, assuming both players make their decisions optimally.


题意:A,B两个人玩一个游戏,开始时决定权属于B,每一轮拥有决定权的人,可以决定这一轮的面包给A还是给B ,没有得到面包的人有下一轮的决定权,每个人都希望自己得到的面包尽可能的多。


题解:考虑第i轮拥有决定权的人肯定会希望自己在i-n轮内获得的面包尽量 多,
设dp[i]表示第i轮拥有决定权的人在i-n轮内获得的最多面包量,这个人可以选择放弃这一轮的面包,拥有下一轮的决定权那么就是dp[i+1] ,或者选择这一轮的面包放弃权力也就是s[i+1]-dp[i+1]+c[i] --他得到的面包量就是 剩下的i+1-n的面包量减去 i+1轮拥有决定权的人在i+1-n轮可以拿到的面包量+第i轮得到的面包量。
dp[1]就表示B在第一轮有决定权,他在1-n中可以得到的最大面包量~~
QAQ 代码量少,但是没想法;;;


    #include<bits/stdc++.h>
    using namespace std;
    int c[100];
    int dp[100],s[100];
    int main()
    {
    	int n;cin>>n;s[n+1]=0;
    	for(int i=1;i<=n;i++)
    	{
    		cin>>c[i];
    	}
    	for(int i=n;i>=1;i--)
    	{
    		s[i]=s[i+1]+c[i];
    		dp[i]=max(dp[i+1],s[i+1]-dp[i+1]+c[i]);
    	}
    	cout<<s[1]-dp[1]<<" "<<dp[1]<<endl;
    }
package com.blog.dragview; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.os.Bundle; import android.util.Log; import com.blog.R; import org.json.JSONArray; import org.json.JSONException; import java.util.List; public class MainActivity extends Activity{ private DrawingView _view; @Override protected void onCreate(Bundle savedInstanceState) { _view = new DrawingView(this); super.onCreate(savedInstanceState); setContentView(_view); Bitmap bmp1 = BitmapFactory.decodeResource(getResources(), R.drawable.hengzhegou); Bitmap bmp2 = BitmapFactory.decodeResource(getResources(),R.drawable.pie); CustomBitmap customBitmap1 = new CustomBitmap(bmp1); CustomBitmap customBitmap2 = new CustomBitmap(bmp2); customBitmap1.setId(1); customBitmap2.setId(2); if (getSavedMatrix(1) != null){ Log.e("tag", "matrix 1 is not null"); customBitmap1.setMatrix(getSavedMatrix(1)); } if (getSavedMatrix(2) != null){ Log.e("tag", "matrix 2 is not null"); customBitmap2.setMatrix(getSavedMatrix(2)); } _view.addBitmap(customBitmap1); _view.addBitmap(customBitmap2); } //����matrix private void saveMatrix(CustomBitmap customBitmap){ Log.e("tag", "save matrix" + customBitmap.getId()); SharedPreferences.Editor editor = getSharedPreferences("matrix", Context.MODE_PRIVATE).edit(); Matrix matrix = customBitmap.matrix; float[] values = new float[9]; matrix.getValues(values); JSONArray array = new JSONArray(); for (float value:values){ try { array.put(value); } catch (JSONException e) { e.printStackTrace(); } } editor.putString(String.valueOf(customBitmap.getId()), array.toString()); editor.commit(); Log.e("tag", "save matrix id:" + customBitmap.getId() + "---------"+values[Matrix.MPERSP_0] + " , " + values[Matrix.MPERSP_1] + " , " + values[Matrix.MPERSP_2] + " , " + values[Matrix.MSCALE_X] + " , " + values[Matrix.MSCALE_Y] + " , " + values[Matrix.MSKEW_X] + " , " + values[Matrix.MSKEW_Y] + " , " +values[Matrix.MTRANS_X] + " , " + values[Matrix.MTRANS_Y]); } //��ȡmatrix private Matrix getSavedMatrix(int id){ SharedPreferences sp = getSharedPreferences("matrix", Context.MODE_PRIVATE); String result = sp.getString(String.valueOf(id), null); if (result != null){ float[] values = new float[9]; Matrix matrix = new Matrix(); try { JSONArray array = new JSONArray(result); for (int i = 0; i < array.length(); i++) { values[i] = Float.valueOf(String.valueOf(array.getDouble(i))); } matrix.setValues(values); } catch (JSONException e) { e.printStackTrace(); } Log.e("tag", "get matrix id:" + id + "---------"+values[Matrix.MPERSP_0] + " , " + values[Matrix.MPERSP_1] + " , " + values[Matrix.MPERSP_2] + " , " + values[Matrix.MSCALE_X] + " , " + values[Matrix.MSCALE_Y] + " , " + values[Matrix.MSKEW_X] + " , " + values[Matrix.MSKEW_Y] + " , " +values[Matrix.MTRANS_X] + " , " + values[Matrix.MTRANS_Y]); return matrix ; } return null; } @Override public void finish() { List<CustomBitmap> list = _view.getViews(); for (CustomBitmap customBitmap:list){ saveMatrix(customBitmap); } super.finish(); } }
最新发布
06-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值