A
A monster is attacking the Cyberland!
Master Yang, a braver, is going to beat the monster. Yang and the monster each have 3 attributes: hitpoints (HP), offensive power (ATK) and defensive power (DEF).
During the battle, every second the monster's HP decrease by max(0, ATKY - DEFM), while Yang's HP decreases bymax(0, ATKM - DEFY), where index Y denotes Master Yang and index M denotes monster. Both decreases happen simultaneously Once monster's HP ≤ 0 and the same time Master Yang's HP > 0, Master Yang wins.
Master Yang can buy attributes from the magic shop of Cyberland: h bitcoins per HP, a bitcoins per ATK, and d bitcoins per DEF.
Now Master Yang wants to know the minimum number of bitcoins he can spend in order to win.
The first line contains three integers HPY, ATKY, DEFY, separated by a space, denoting the initial HP, ATK and DEF of Master Yang.
The second line contains three integers HPM, ATKM, DEFM, separated by a space, denoting the HP, ATK and DEF of the monster.
The third line contains three integers h, a, d, separated by a space, denoting the price of 1 HP, 1 ATK and 1 DEF.
All numbers in input are integer and lie between 1 and 100 inclusively.
The only output line should contain an integer, denoting the minimum bitcoins Master Yang should spend in order to win.
暴力攻防
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
const int maxa = ;
int dp[maxa][maxa];
int main(){
int x, y, z;
int x1, y1, z1;
int a, b, c;
cin>>x>>y>>z>>x1>>y1>>z1>>a>>b>>c;
int guanwujianxue = y - z1;
int uu = ; //钱
if(guanwujianxue <= ){
uu = b * (-guanwujianxue + );
guanwujianxue = ;
}
int yingxiongjianxue = max(, y1 - z);
int mina = ;
for(int i =guanwujianxue; i < maxa; i++){
for(int k= yingxiongjianxue; k >= ; k--){
int sum = (i - guanwujianxue)*b + (yingxiongjianxue-k)*c;
int n = x1/i;
if(x1 % i != )n++;
if(k * n < x)
mina = min(mina, sum);
else{
mina = min(mina, sum + (k*n+-x)*a);
}
}
}
cout<<mina+uu<<endl;
}
Alexandra has a paper strip with n numbers on it. Let's call them ai from left to right.
Now Alexandra wants to split it into some pieces (possibly 1). For each piece of strip, it must satisfy:
- Each piece should contain at least l numbers.
- The difference between the maximal and the minimal number on the piece should be at most s.
Please help Alexandra to find the minimal number of pieces meeting the condition above.
The first line contains three space-separated integers n, s, l (1 ≤ n ≤ 105, 0 ≤ s ≤ 109, 1 ≤ l ≤ 105).
The second line contains n integers ai separated by spaces ( - 109 ≤ ai ≤ 109).
Output the minimal number of strip pieces.
If there are no ways to split the strip, output -1.
思路就是线性的,看到个牛逼的解法
#include<stdio.h> #include<string.h>
#include<iostream>
#include<set>
using namespace std;
const int maxa = ;
int dp[maxa];
int n, s, l;
multiset<int>st, rt;
int a[maxa];
int main(){
scanf("%d%d%d", &n, &s, &l);
for(int i = ; i < n; i++){
scanf("%d", &a[i]);
}
for(int i = , j = ; i < n; i++){
st.insert(a[i]);
while(*st.rbegin() - *st.begin() > s){
st.erase(st.find(a[j]));
if(i - j >= l)
rt.erase(rt.find(dp[j-]));
j++;
}
if(i - j+ >=l)rt.insert(dp[i-l]);
if(rt.begin() == rt.end())dp[i] = maxa;
else dp[i] = *rt.begin()+;
}
if(dp[n-] >= maxa)dp[n-] = -;
cout<<dp[n-]<<endl;
}