title: WOJ1022 Competition of Programming 贪心

date: 2020-03-19 13:43:00

categories: acm

tags: [acm,woj,贪心]

贪心。

1 描述

There are many good reasons for participating in programming contests. You can have fun while you improve both your programming

skills and job prospects in the bargain. From somewhere in this vein arises TopCoder, a company which uses programming contests as a

tool to identify promising potential employees and provides this information to its clients.

The format of TopCoder contests is evolving quickly as they hunt for the most appropriate business model. Preliminary rounds are

held over the web, with the final rounds of big tournaments held on site. Each of these rounds shares the same basic format. The coders

are divided into ?rooms? where they compete against the other coders. Each round starts with a coding phase of 75?80 minutes where the

contestants do their main programming. The score for each problem is a decreasing function of the time from when it was first opened to

when it was submitted. There is then a 15-minute challenge phase where coders get to view the submissions from other contestants in their

room and try to find bugs. Coders earn additional points by submitting a test case that breaks another competitor?s program. There is no

partial credit for incorrect solutions. Is it very interesting and exciting for participating in TopCoder and you just want to participate

it right now?

Now, WOJ (Wuhan University Online Judge) development group will introduce the TopCoder contest format into WOJ. Of course, there

will be something different from TopCoder and we will set some new regulations into WOJ?s TopCoder. Here is one of these regulations: in

the coding phase, we will set the finish time limit T and penalty C for each problem. If you don?t solve the i-th problem in the finish time

limit Ti, your penalty will be added by Ci. Note that the penalty here is completely different from the penalty in ACM. The penalty in WOJ?s

TopCoder is a value set by us and is be independent of the problem solving time.

Assume that you are a genius in programming and it takes only one time unit for you to solve one problem whatever the problem is.

Now, there are N problems in WOJ?s TopCoder contest and the finish time limit and penalty of each problem is given, please write a program

to calculate minimum penalty you will get.

输入格式

Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 50) which is the number of test cases.

The first line of each test case contains only one integer N (1 <= N <= 3000) which specify the total problem numbers. The next N lines each contains the description for one problem, the first integer T (1 <= T <= 3000) specifying the finish time limit and the second integer C (1 <= C <= 50000) specifying the penalty.

输出格式

Results should be directed to standard output. Start each case with "Case #:" on a single line, where # is the case number starting from 1. Two consecutive cases should be separated by a single blank line. No blank line should be produced after the last test case.

For each test case, print one integer, the minimum penalty described as above.

样例输入

2

3

1 5

3 2

3 4

3

1 5

2 2

2 4

样例输出

Case 1:

0

Case 2:

2

2 分析

// 贪心。和上学期算法期末考试一道题很像。

//这里读题的时候想了一会儿才发现每道题没有说完成耗时,那么就是说一个时间点就完成了

//期末那道题还考虑到占用时间,这里占用时间就是1

//总之就算每道题尽量拖到允许时间的最后再执行,这样可以给其他题留下更多时间。(像一条线段上窗口越靠后越好)

//因为这里还有罚时,贪心策略是罚时越高越先优执行

//所以先根据罚时排序,然后从高优先级的problem开始,尽量靠后的找到时间点;

3 code

#include<iostream>
#include<algorithm>
#include<cstring> using namespace std; struct problem{
int time,penalty;
bool operator <(const problem &tmp)const{
return penalty>tmp.penalty;
}
}; /*
if(a.grade != b.grade)
return a.grade < b.grade;
else if(t != 0)
return t < 0;
else
return a.age < b.age;
*/ problem probs[3005];
int T=0,casee=0,n; bool vis[3005]; // 50000*3000=150000000=1.5e8. [-2^31,2^31),即-2147483648~2147483647约等于2e9,没超
int solve()
{
int res = 0;
memset(vis, 0, sizeof(vis));
for(int i = 0; i < n; i++){
bool ok = false;
for(int j= probs[i].time; j >= 1; j--) { //time 从 1开始所以>=1
if(!vis[j]) { //还没被占用,就在这里完成
vis[j] = true;
ok = true;
break;
}
}
if(!ok) res += probs[i].penalty;
}
return res;
} int main(){ cin>>T;
while(T--){
cin>>n;
if(casee!=0)
cout<<endl;
casee++;
printf("Case %d:\n",casee);
for(int i=0;i<n;i++)
cin>>probs[i].time>>probs[i].penalty;
sort(probs,probs+n);
printf("%d\n",solve());
}
system("pause");
return 0;
}

title: WOJ1023 Division dp

date: 2020-03-19 13:43:00

categories: acm

tags: [acm,woj,dp]

动态规划

1 描述

输入格式

Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 50) which is the number of test cases.

Each test case begins with a line containing two integers n (1 <= n <= 100) and p (1 <= p <= n), where n represents the size of the set and p represents the least number of elements each cluster must contain. The second line contains positive integers (you are ensured that the value of each integer is less than 2^16), which are separated by a white space denoting the elements of the set.

输出格式

Results should be directed to standard output. Start each case with "Case #:" on a single line, where # is the case number starting from 1. Two consecutive cases should be separated by a single blank line. No blank line should be produced after the last test case.

For each test case, you should output one line containing a floating number, which is the minimum sum of the squared deviation of the numbers from their cluster mean. The answer should be accurate to two fractional digits.

样例输入

2

2 1

1 2

2 2

1 2

样例输出

Case 1:

0.00

Case 2:

0.50

2 分析

给出一个集合大小为n,要把内部的元素分成clusters(簇),每个簇至少大小为p

每个簇S均值S'为 ai求和/|S| |S|为簇S的size

每个簇的值为内部元素 (ai-S'^2)之和

求各个簇如何分,值之和最小

Each cluster contains consecutive number cluster内的数字是连续的

前面的分簇结果可以被后面利用

dp 状态转移方程: f[i]=min(f[i],f[j]+valuecluser[j+1][i])(i-j>=p) //j+1...(注意分界点)

dp[0]=0.i in range p-1-N (编号为1-n)

//dp注意初始状态结束状态预处理。f[0]=0,f[i]初始为maxd

预处理:计算valuecluster[i][j]

3 code

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath> //pow
#include<algorithm> //min
using namespace std; int T=0,casee=0,n,p,i,j,k;
const double maxd=1e15; //2^16*2^16*100 2E11
double summ[105][105]; //存连续数字和
int nums[105];
double clustervalue[105][105];
double f[105]; //dp double dp(){
for(i=p;i<=n;i++)
for(j=i-p;j>=0;j--){ //一开始写成i-p+1了...
f[i]=min(f[i],f[j]+clustervalue[j+1-1][i-1]); //f[i]中元素编号+1,计算的时候注意减1
}
return f[n];
} int main(){ cin>>T;
while(T--){
cin>>n>>p;
for(i=0;i<n;i++)
cin>>nums[i];
for(i=0;i<n;i++)
for(j=i;j<n;j++)
{
if(i==j)
summ[i][j]=nums[i];
else
summ[i][j]=summ[i][j-1]+nums[j];
}
for(i=0;i<n;i++)
for(j=i;j<n;j++)
{
clustervalue[i][j]=0;
}
for(i=0;i<n;i++)
for(j=i+p-1;j<n;j++)
{
double avg=summ[i][j]*1.0/(j-i+1);
for(k=i;k<=j;k++)
clustervalue[i][j]+=pow(nums[k]-avg*1.0,2);
}
for(i=1;i<=n;i++)
f[i]=maxd;
f[0]=0; //初始状态
if(casee!=0)
cout<<endl;
casee++;
printf("Case %d:\n",casee);
printf("%.2lf\n",dp()); }
system("pause");
return 0;
}

WOJ1022 Competition of Programming 贪心 WOJ1023 Division dp的更多相关文章

  1. 0-1背包的动态规划算法,部分背包的贪心算法和DP算法------算法导论

    一.问题描述 0-1背包问题,部分背包问题.分别实现0-1背包的DP算法,部分背包的贪心算法和DP算法. 二.算法原理 (1)0-1背包的DP算法 0-1背包问题:有n件物品和一个容量为W的背包.第i ...

  2. 求树的最大独立集,最小点覆盖,最小支配集 贪心and树形dp

    目录 求树的最大独立集,最小点覆盖,最小支配集 三个定义 贪心解法 树形DP解法 (有任何问题欢迎留言或私聊&&欢迎交流讨论哦 求树的最大独立集,最小点覆盖,最小支配集 三个定义 最大 ...

  3. 2017多校第10场 HDU 6178 Monkeys 贪心,或者DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6178 题意:给出一棵有n个节点的树,现在需要你把k只猴子放在节点上,每个节点最多放一只猴子,且要求每只 ...

  4. 洛谷P1084 疫情控制(NOIP2012)(二分答案,贪心,树形DP)

    洛谷题目传送门 费了几个小时杠掉此题,如果不是那水水的数据的话,跟列队的难度真的是有得一比... 话说蒟蒻仔细翻了所有的题解,发现巨佬写的都是倍增,复杂度是\(O(n\log n\log nw)\)的 ...

  5. CodeForces165E 位运算 贪心 + 状压dp

    http://codeforces.com/problemset/problem/165/E 题意 两个整数 x 和 y 是 兼容的,如果它们的位运算 "AND" 结果等于 0,亦 ...

  6. P2279 [HNOI2003]消防局的设立 贪心or树形dp

    题目描述 2020年,人类在火星上建立了一个庞大的基地群,总共有n个基地.起初为了节约材料,人类只修建了n-1条道路来连接这些基地,并且每两个基地都能够通过道路到达,所以所有的基地形成了一个巨大的树状 ...

  7. 洛谷 P4269 / loj 2041 [SHOI2015] 聚变反应炉 题解【贪心】【DP】

    树上游戏..二合一? 题目描述 曾经发明了零件组装机的发明家 SHTSC 又公开了他的新发明:聚变反应炉--一种可以产生大量清洁能量的神秘装置. 众所周知,利用核聚变产生的能量有两个难点:一是控制核聚 ...

  8. 【P2016】战略游戏(贪心||树状DP)

    这个题真是...看了一会之后,发现有一丝丝的熟悉,再仔细看了看,R,这不是那个将军令么...然后果断调出来那个题,还真是,而且貌似还是简化版的...于是就直接改了改建树和输入输出直接交了..阿勒,就2 ...

  9. POJ 1795 DNA Laboratory (贪心+状压DP)

    题意:给定 n 个 字符串,让你构造出一个最短,字典序最小的字符串,包括这 n 个字符串. 析:首先使用状压DP,是很容易看出来的,dp[s][i] 表示已经满足 s 集合的字符串以 第 i 个字符串 ...

随机推荐

  1. 浅谈JavaScript代码性能优化

    可以通过https://jsbench.me/测试网站完成性能测试. 一.慎用全局变量 1.全局变量定义在全局执行上下文,是所有作用域链的顶端,在局部作用域中没找到的变量都会到全局变量中去查找,所以说 ...

  2. 一句话木马拿下webshell

    1.我们先建立一个简单的一句话木马文件,我们这里就命名为shell2吧. 2.因为提交的文件可能是有过滤的,我们这个靶场的这个题目就是禁止上传危险的文件类型,如jsp jar war等,所以就需要绕过 ...

  3. Linux下安装配置rocketmq (单个Master、双Master)

    一.环境: centos7(2台虚拟机):192.168.64.123:192.168.64.125 apache-maven-3.2.5(官网要求maven版本是3.2.x,版本不同,编译rocke ...

  4. jQ实现图片无缝轮播

    在铺页面的过程中,总是会遇到轮播图需要处理,一般我是会用swiper来制作,但总会有哪个几个个例需要我自己来写功能,这里制作了一个jq用来实现图片无缝轮播的dome,分享给大家ヽ( ̄▽ ̄)ノ. dom ...

  5. Elasticsearch从入门到放弃:浅谈算分

    今天来聊一个 Elasticsearch 的另一个关键概念--相关性算分.在查询 API 的结果中,我们经常会看到 _score 这个字段,它就是用来表示相关性算分的字段,而相关性就是描述一个文档和查 ...

  6. 大数据谢列3:Hdfs的HA实现

    在之前的文章:大数据系列:一文初识Hdfs , 大数据系列2:Hdfs的读写操作 中Hdfs的组成.读写有简单的介绍. 在里面介绍Secondary NameNode和Hdfs读写的流程. 并且在文章 ...

  7. Xamarin.Forms: 无限滚动的ListView(懒加载方式)

    说明 在本博客中,学习如何在Xamarin.Forms应用程序中设计一个可扩展的无限滚动的ListView.这个无限滚动函数在默认的Xamarin.Forms不存在,因此我们需要为此添加插件.在这里我 ...

  8. TCP连接的超时时间

    无论你用任何语言或者是网络库,你都可以设置网络操作的超时时间,特别是connect.read.write的超时时间. 你可以在代码中把超时时间设置任意大小值,但是connect方法会有一点特殊. co ...

  9. pthon之变量

    1.变量由三部分组成: 变量名  =   值 如:name = 'xiaohan'     sex='男'   age = 20 2.变量名的规范 2.1 变量名只能是字母,数字或下划线的任意组合 2 ...

  10. 题解 P1248 【加工生产调度】

    题目 某工厂收到了 n 个产品的订单,这 n 个产品分别在 A.B 两个车间加工,并且必须先在 A 车间加工后才可以到 B 车间加工. 某个产品 i 在 A.B 两车间加工的时间分别为 Ai,Bi 怎 ...