Temporarily unavailable
\ [Time Limit: 1 s \
quad Memory Limit: 256 MB \] calculated directly \ ([cr, c + r ] \) in \ ([a, b] \ ) range much, then it is subtracted it.
view
#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 1e5 + 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std;
int n, m;
int cas, tol, T;
int main() {
scanf("%d", &T);
while(T--) {
ll a, b, c, r, ans;
scanf("%lld%lld%lld%lld", &a, &b, &c, &r);
if(a>b) swap(a, b);
ans = b-a;
ll over = max(a, min(c+r, b)) - min(b, max(c-r, a));
printf("%lld\n", ans-over);
}
return 0;
}
K for the Price of One (Hard Version)
\ [Time Limit: 2 s \
quad Memory Limit: 256 MB \] of course, is to buy the cheapest, because of \ (k \) one can only buy \ (k \) months, so there are two strategies can be seen as buy \ (1 \) th and buy \ (K \) a, then \ (dp [i] \) represents bought before \ (I \) takes a minimum number of items, then look give money to buy how many.
view
#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 2e5 + 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std;
int n, m, k;
int cas, tol, T;
ll dp[maxn];
int a[maxn];
int main() {
scanf("%d", &T);
while(T--) {
scanf("%d%d%d", &n, &m, &k);
for(int i=1; i<=n; i++) scanf("%d", &a[i]);
for(int i=1; i<=n; i++) dp[i] = INF;
dp[0] = 0;
sort(a+1, a+1+n);
for(int i=1; i<=n; i++) {
if(i-k>=0)
dp[i] = min(dp[i-1], dp[i-k])+a[i];
else
dp[i] = dp[i-1]+a[i];
}
ll ans = 0;
for(int i=n; i>=1; i--) {
if(dp[i] <= m) {
ans = i;
break;
}
}
printf("%lld\n", ans);
}
return 0;
}
Petya and Exam
\ [Time Limit: 2 s \
quad Memory Limit: 256 MB \] Because there is a time limit \ (t_i \) , then I can press the time limit \ (t_i \) ordering to complete each job.
So \ (p [i] \) indicates completion \ (i \) time job required, then I want to do this \ (i \) job, I used the time to be in \ (t_ {i + 1} \) or less, which are necessary to complete the job.
Then there are still some time left, I can use the remaining time to complete the job greed has not reached the time limit, each time to complete a simple, completing difficult.
view
#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 2e5 + 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std;
ll n, m;
int cas, tol, T;
struct Node {
ll a, b, p;
bool operator < (Node c) const {
return b<c.b;
}
} node[maxn];
ll p[maxn], a[2];
ll solve(ll time, ll x, ll y) {
ll cnt = 0;
ll oka = min(x, time/a[0]);
cnt += oka;
time -= oka*a[0];
ll okb = min(y, time/a[1]);
cnt += okb;
return cnt;
}
int main() {
scanf("%d", &T);
while(T--) {
scanf("%lld%lld%lld%lld", &n, &m, &a[0], &a[1]);
for(int i=1, x; i<=n; i++) {
scanf("%lld", &node[i].p);
node[i].a = a[node[i].p];
}
for(int i=1; i<=n; i++) {
scanf("%lld", &node[i].b);
}
p[0] = 0;
sort(node+1, node+1+n);
node[n+1].b = m+1;
for(int i=1; i<=n; i++) p[i] = p[i-1]+node[i].a;
ll ans = 0, cnt[2] = {0};
// for(int i=1; i<=n; i++) printf("%d%c", node[i].a, i==n ? '\n':' ');
// for(int i=1; i<=n; i++) printf("%d%c", node[i].b, i==n ? '\n':' ');
// for(int i=1; i<=n; i++) printf("%d%c", p[i], i==n ? '\n':' ');
// cout << "=====" << endl;
for(int i=n; i>=0; i--) {
if(p[i] < node[i+1].b)
ans = max(ans, i+solve(node[i+1].b-p[i]-1, cnt[0], cnt[1]));
cnt[node[i].p]++;
}
printf("%lld\n", ans);
}
return 0;
}