您的位置:首页 > 大数据 > 人工智能

贪心练习 again (codeforces 系列)

2015-12-12 20:44 561 查看
codeforces 415B. Mashmokh and Tokens

http://codeforces.com/problemset/problem/415/B

Bimokh is Mashmokh's boss. For the following n days he decided to pay to his workers in a new way. At the beginning of each day he will give each worker a certain amount of tokens. Then at the end of each day each worker
can give some of his tokens back to get a certain amount of money. The worker can save the rest of tokens but he can't use it in any other day to get more money. If a worker gives backw tokens then he'll get


dollars.

Mashmokh likes the tokens however he likes money more. That's why he wants to save as many tokens as possible so that the amount of money he gets is maximal possible each day. He hasn numbers
x1, x2, ..., xn. Numberxi
is the number of tokens given to each worker on thei-th day. Help him calculate for each of
n days the number of tokens he can save.

Input
The first line of input contains three space-separated integers
n, a, b (1 ≤ n ≤ 105; 1 ≤ a, b ≤ 109). The second line of input containsn space-separated integers
x1, x2, ..., xn (1 ≤ xi ≤ 109).

Output
Output n space-separated integers. The
i-th of them is the number of tokens Mashmokh can save on the
i-th day.

Sample test(s)

Input
5 1 4
12 6 11 9 1


Output
02 3 1 1


Input
3 1 2
1 2 3


Output
1 01


Input
1 1 1
1


Output
0


什么都不用说,唯一可能需要的是有道词典。

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
const int N=1e5+10;
LL n,a,b;
LL w
;
int main()
{
while(cin>>n>>a>>b){
for(int i=0;i<n;i++) scanf("%I64d",&w[i]);
for(int i=0;i<n;i++){
LL t1=w[i]*a/b;
LL t2=t1*b;
LL ans=t2/a;
while(ans*a<t2){
ans++;
}
if(i!=n-1) printf("%I64d ",w[i]-ans);
else printf("%I64d\n",w[i]-ans);
}
}
return 0;
}


codeforces 413 C. Jeopardy!

http://codeforces.com/contest/413/problem/C

Jeopardy!' is an intellectual game where players answer questions and earn points. Company Q conducts a simplified 'Jeopardy!' tournament among the best IT companies. By a lucky coincidence, the old rivals made it to the finals: company R1 and company R2.

The finals will have n questions,
m of them are auction questions and n - m of them are regular questions. Each question has a price. The price of thei-th question is
ai points. During the game the players chose the questions. At that, if the question is an auction, then the player who chose it can change the price if the number of his current
points is strictly larger than the price of the question. The new price of the question cannot be less than the original price and cannot be greater than the current number of points of the player who chose the question. The correct answer brings the player
the points equal to the price of the question. The wrong answer to the question reduces the number of the player's points by the value of the question price.

The game will go as follows. First, the R2 company selects a question, then the questions are chosen by the one who answered the previous question correctly. If no one answered the question, then the person who chose last chooses again.

All R2 employees support their team. They want to calculate what maximum possible number of points the R2 team can get if luck is on their side during the whole game (they will always be the first to correctly answer questions). Perhaps you are not going
to be surprised, but this problem was again entrusted for you to solve.

Input
The first line contains two space-separated integers n andm
(1 ≤ n, m ≤ 100; m ≤ min(n, 30)) — the total number of questions and the number of auction questions, correspondingly. The second line containsn
space-separated integers a1, a2, ..., an(1 ≤ ai ≤ 107)
— the prices of the questions. The third line containsm distinct integers
bi
(1 ≤ bi ≤ n) — the numbers of auction questions. Assume that the questions are numbered from1 to
n.

Output
In the single line, print the answer to the problem — the maximum points the R2 company can get if it plays optimally well. It is guaranteed that the answer fits into the integer 64-bit signed type.

Sample test(s)

Input
4 1
1 3 7 5
3


Output
18


Input
3 2
103 8
2 3


Output
40


Input
2 2
100200
1 2


Output
400

有一个特点,按价值排好序后的竞争问题,如果不改变最后(最大价值)的问题prize,能答对的话,新得到的prize和一定大于前面所有的竞争问题
的prize。依据它做出比较。
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N=110;
typedef long long LL;
LL a
,que[35];
int top,b;
int main()
{
//freopen("cin.txt","r",stdin);
int n,m;
while(cin>>n>>m){
top=0;
for(int i=0;i<n;i++){
scanf("%I64d",&a[i]);
}
for(int i=0;i<m;i++){
scanf("%d",&b);
que[top++]=a[b-1];
a[b-1]=0;
}
sort(que,que+m);
int bot=0;
LL sum =0;
for(int i=0;i<n;i++){
if(a[i]) sum+=a[i];
}
LL temp=0,maxm=-1;
for(int i=top-1;i>=0;i--){
temp=sum;
for(int j=top-1;j>=i;j--){
temp=temp+que[j];
}
temp<<=i;
if(maxm<temp){
maxm=temp;
}
}
temp=sum;
for(int i=0;i<top;i++){
if(temp>que[i]){
temp<<=1;
}
else temp+=que[i];
}
maxm=max(maxm,temp);
printf("%I64d\n",maxm);
}
return 0;
}

codeforces 416 C. Booking System
http://codeforces.com/problemset/problem/416/C
(因为粘贴过来出现了各种格式错误,我就不贴了)
经典的贪心,按照钱的多少总排序,然后在钱相等的情况下再按人数从小到大排序,最后对tables人数从小到大排序。即资源消耗最小化,收益最大化
。(它和01背包不同,因为桌子和团队的关系是1对1的)
#include <iostream>//贪心和暴力相比更好写且思路清晰
#include <cstdio>
#include <algorithm>
using namespace std;
int n,m,all;
struct node{
int order,peo,money;
bool vis;
}f[1010];
int cmp(node a,node b){
if(a.money!=b.money){
return a.money>b.money;
}
return a.peo<b.peo;  //贪心要贪的彻底!
}
struct node1{
int order,peo;
}t[1010];
int cmp1(node1 a,node1 b){
return a.peo<b.peo;
}
int ans[1010][2];  //quest number and table number
int main()
{
//freopen("cin.txt","r",stdin);
while(cin>>n){
for(int i=0;i<n;i++){
scanf("%d%d",&f[i].peo,&f[i].money);
f[i].order=i+1;
f[i].vis=false;
}
sort(f,f+n,cmp);
scanf("%d",&m);
for(int i=0;i<m;i++){
scanf("%d",&t[i].peo);
t[i].order=i+1;
}
sort(t,t+m,cmp1);
int top=0,sum=0;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(f[j].peo<=t[i].peo&&f[j].vis==false){
ans[top][0]=f[j].order;
ans[top++][1]=t[i].order;
f[j].vis=1;
sum+=f[j].money;
break;
}
}
}
printf("%d %d\n",top,sum);
for(int i=0;i<top;i++){
printf("%d %d\n",ans[i][0],ans[i][1]);
}
}
return 0;
}

codeforces 416 D. Population Size
http://codeforces.com/problemset/problem/416/D
由前两个非-1的数字可以决定一段等差数列,所以可以直接遍历数组,看是否能够合并到前面的等差数列中。然而我一直卡死在79
个样例中了 T_T 自己写的代码有问题。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: