2017CCPC- 女生专场 第2题(hdu 6024)和第8题(hdu 6030)
来源:互联网 发布:电视连接网络怎么设置 编辑:程序博客网 时间:2024/06/08 04:37
1002 Building Shops http://acm.hdu.edu.cn/showproblem.php?pid=6024
这题题目有点难看懂;
题意: x轴上有n个班级,第i个班级建商店的费用为c[i];不建的话也会有费用,费用就是从i到距离i的左边最近的商店的距离,问总费用最少是多少;
题解:毫无疑问,第1个教室是肯定要建商店的;
然后就dp; dp[I][0]表示第I个教室不建商店,前I个教师的花费;
dp[I][1]表示第I个教室建商店,前I个教师的花费;
状态转移的话 dp[I][1] = min(dp[I-1][0], dp[I-1][1]) + c[I];
dp[I][0] 的转移有点坑我一开始以为是由dp[I-1][0]转来的,然后贡献了一次wa;
是这样的,dp[I][0] = dp[j][0] + x[I]-x[j]; (j=1~I-1);
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;typedef long long ll;const int MAXN = 3008;ll dp[MAXN][2];//int path[MAXN];struct classroom { int x, c;} ritian[MAXN];int cmp(classroom xx, classroom yy) { return xx.x < yy.x;}int main() { int n; //freopen("in.txt", "r", stdin); while (~scanf("%d", &n)) { for(int i = 1; i <= n; i++) scanf("%d%d", &ritian[i].x, &ritian[i].c); sort(ritian+1, ritian+n+1, cmp); dp[1][0] = (ll)1<<60; dp[1][1] = ritian[1].c; for(int i = 2; i <= n; i++) { dp[i][1] = min(dp[i-1][0], dp[i-1][1])+ritian[i].c; ll tmp = 0; dp[i][0] = (ll)1<<60; for(int j = i-1; j > 0; j--) { tmp = (ll)(ritian[j+1].x-ritian[j].x)*(i-j)+tmp;//这里少写了(ll)也贡献了2次wa,强迫症的我表示一个变量的值小于int型,我决不 //会定义成long long 型->.->; dp[i][0] = min(dp[i][0], dp[j][1]+tmp); } } printf("%I64d\n", min(dp[n][0], dp[n][1])); } return 0;}
1008 Happy Necklace http://acm.hdu.edu.cn/showproblem.php?pid=6030
题意: 小A要给他女朋友买一条n颗珠子的项链,他女朋友要求项链的珠子只有红和蓝这两种颜色,而且要保证每连续的奇数个珠子中,红珠子要大于等于蓝珠子;
2 <= n <= 10^18;问这条项链有多少个放珠子的方法;
题解: n > 4时, a[n] = a[n-1] + a[n-3],(需要时间推出来),意思是n相当于在n-1上加一个红珠子和在n-3上加一个红红蓝,这么说你懂我意思了吧, 然后上矩阵快速幂;
代码:
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;const int N=3;typedef long long ll;const ll Mod=1000000000+7;struct Mat{ ll mat[N+1][N+1];};Mat Multiply(Mat a, Mat b){ Mat c; memset(c.mat, 0, sizeof(c.mat)); for(int k = 0; k < N; ++k) for(int i = 0; i < N; ++i) if(a.mat[i][k]) for(int j = 0; j < N; ++j) if(b.mat[k][j]) c.mat[i][j] = (c.mat[i][j] +a.mat[i][k] * b.mat[k][j])%Mod; return c;}Mat QuickPower(Mat a, ll k){ Mat c; memset(c.mat,0,sizeof(c.mat)); for(int i = 0; i < N; ++i) c.mat[i][i]=1; for(; k; k >>= 1) { if(k&1) c = Multiply(c,a); a = Multiply(a,a); } return c;}int main(){ int T; scanf("%d",&T); ll a[]={0,1,3,4,6}; Mat A; A.mat[0][0]=1,A.mat[0][1]=1,A.mat[0][2]=0; A.mat[1][0]=0,A.mat[1][1]=0,A.mat[1][2]=1; A.mat[2][0]=1,A.mat[2][1]=0,A.mat[2][2]=0; while(T--) { ll n; scanf("%I64d",&n); if(n<=4) { printf("%I64d\n",a[n]); continue; } Mat ans=QuickPower(A,n-4); printf("%I64d\n",(a[4]*ans.mat[0][0]+a[3]*ans.mat[1][0]+a[2]*ans.mat[2][0])%Mod); } return 0;}
0 0
- 2017CCPC- 女生专场 第2题(hdu 6024)和第8题(hdu 6030)
- HDU-5703-Desert【2016CCPC女生专场】
- (HDU 6029 女生专场)Graph Theory 思维题
- HDU-5702-Solving Order【2016CCPC女生专场】
- HDU-5706-GirlCat【BFS】【2016CCPC女生专场】
- HDU-5707-Combine String【2016CCPC女生专场】【DP】
- 2017 CCPC 女生专场(重现)
- "巴卡斯杯" 中国大学生程序设计竞赛 - 女生专场(重现) HDU 5702
- "巴卡斯杯" 中国大学生程序设计竞赛 - 女生专场(重现) HDU 5703
- "巴卡斯杯" 中国大学生程序设计竞赛 - 女生专场(重现) HDU 5704
- "巴卡斯杯" 中国大学生程序设计竞赛 - 女生专场(重现) HDU 5705
- "巴卡斯杯" 中国大学生程序设计竞赛 - 女生专场(重现) HDU 5706
- "巴卡斯杯" 中国大学生程序设计竞赛 - 女生专场(重现) HDU 5707
- (HDU 6023 女生专场)Automatic Judge 简单模拟
- (HDU 6025 女生专场)Coprime Sequence 水题
- (HDU 6027 女生专场)Easy Summation 水题 预处理
- 2017CCPC女生赛 hdu 6024 Building Shops
- 2017CCPC女生赛 hdu 6030 Happy Necklace
- wampserver修改www目录为指定目录
- Qt contextMenu的创建和使用
- 需求:公司outlook配置sina邮箱
- 部分云台以及车载摄像机的种类和功能
- 欢迎使用CSDN-markdown编辑器
- 2017CCPC- 女生专场 第2题(hdu 6024)和第8题(hdu 6030)
- HelloJni导入到Eclipse及命令行编译
- elasticsearch 组合查询
- Jvm参数调优初探索(一)
- ’com.android.support:appcompat-v7:version‘不兼容
- python 读取csv文件
- win7 显示器驱动已停止响应并且已恢复
- 决策树算法实现(二)
- QDockWidget嵌套布局详解-实现Visual Studio布局