题目大意:一只袋鼠第i秒能够向左或向右跳i步或者不跳,问从0跳到x的最小时间c++
就是1,2,3,4...k总和超过x的最小的k,由于若是超过了x的那部分须要减掉的那步我不跳便可函数
#include <bits/stdc++.h> #define fi first #define se second #define pii pair<int,int> #define mp make_pair #define pb push_back #define space putchar(' ') #define enter putchar('\n') #define eps 1e-10 #define MAXN 200005 //#define ivorysi using namespace std; typedef long long int64; typedef unsigned int u32; typedef double db; template<class T> void read(T &res) { res = 0;T f = 1;char c = getchar(); while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') { res = res * 10 +c - '0'; c = getchar(); } res *= f; } template<class T> void out(T x) { if(x < 0) {x = -x;putchar('-');} if(x >= 10) { out(x / 10); } putchar('0' + x % 10); } int X; void Solve() { read(X); int r = 0; for(int i = 1 ; i <= 100000 ; ++i) { r += i; if(r >= X) { out(i);enter;return; } } } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); }
大意:若一个数在任意总和大于等于K的子集内被删掉后,该子集的和仍然大于等于K,认为这个数没必要要,求没必要要的数的个数spa
发现若是小于这个数的某值是必要的,这个数也必定是必要的,咱们作一个背包,若是存在一个值小于K加上这个数是大于等于K的,那么这个数就是必要的,比最小的必要的数还要小的数都是没必要要的code
#include <bits/stdc++.h> #define fi first #define se second #define pii pair<int,int> #define mp make_pair #define pb push_back #define space putchar(' ') #define enter putchar('\n') #define eps 1e-10 #define MAXN 200005 //#define ivorysi using namespace std; typedef long long int64; typedef unsigned int u32; typedef double db; template<class T> void read(T &res) { res = 0;T f = 1;char c = getchar(); while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') { res = res * 10 +c - '0'; c = getchar(); } res *= f; } template<class T> void out(T x) { if(x < 0) {x = -x;putchar('-');} if(x >= 10) { out(x / 10); } putchar('0' + x % 10); } int N,K,f[MAXN]; int a[MAXN]; void Solve() { read(N);read(K); for(int i = 1 ; i <= N ; ++i) { read(a[i]); } sort(a + 1,a + N + 1); f[0] = 1; int ans = N; for(int i = N ; i >= 1 ; --i) { for(int j = K - 1 ; j >= 0 ; --j) { if(f[j] && a[i] + j >= K) ans = min(i - 1,ans); if(j >= a[i]) f[j] |= f[j - a[i]]; } } out(ans);enter; } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); }
大意:每段长度为1的纵坐标有一个线段,初始坐标\(l[i],r[i]\),线段能够移动,要求相邻两个线段之间有交集,能够在端点,求最小移动距离和get
设\(dp[i][x]\)是第i条线段左边的点在x的最小花费,这个函数是一个凹(数学老师读音:wa)函数,每一个拐点斜率单调递增1,最左的斜率是-i - 1数学
每次至关于一个绝对值函数累加上,上一次的函数变成,左边i个点向左移动\(r[i] - l[i]\),右边i个点向右移动\(r[i - 1] - l[i - 1]\)it
能够用set维护,而后两个新的拐点都是l[i]class
维护函数最小值便可sort
#include <bits/stdc++.h> #define fi first #define se second #define pii pair<int,int> #define mp make_pair #define pb push_back #define space putchar(' ') #define enter putchar('\n') #define eps 1e-10 #define MAXN 200005 //#define ivorysi using namespace std; typedef long long int64; typedef unsigned int u32; typedef double db; template<class T> void read(T &res) { res = 0;T f = 1;char c = getchar(); while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') { res = res * 10 +c - '0'; c = getchar(); } res *= f; } template<class T> void out(T x) { if(x < 0) {x = -x;putchar('-');} if(x >= 10) { out(x / 10); } putchar('0' + x % 10); } int N; int64 l[MAXN],r[MAXN]; multiset<int64> Lb,Rb; int64 c,d[2]; void Solve() { read(N); for(int i = 1 ; i <= N ; ++i) { read(l[i]);read(r[i]); } Lb.insert(l[1]);Rb.insert(l[1]);c = 0; for(int i = 2 ; i <= N ; ++i) { d[0] -= r[i] - l[i];d[1] += r[i - 1] - l[i - 1]; int64 a = *(--Lb.end()) + d[0],b = *(Rb.begin()) + d[1]; if(l[i] < a) { c += abs(a - l[i]); Lb.erase(Lb.find(a - d[0]));Rb.insert(a - d[1]); Lb.insert(l[i] - d[0]);Lb.insert(l[i] - d[0]); } else if(l[i] > b){ c += abs(b - l[i]); Rb.erase(Rb.find(b - d[1]));Lb.insert(b - d[0]); Rb.insert(l[i] - d[1]);Rb.insert(l[i] - d[1]); } else { Lb.insert(l[i] - d[0]);Rb.insert(l[i] - d[1]); } } out(c);enter; } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); }
若是\(A <= B\),从B中选A我的装成互相都是诚实的人,那么不可能分辨出来top
而后若是\(A > B\),若是有一个p认为q是unkind,那么p和q至少有一个unkind,同时忽略p,q,那么剩下的仍然honest大于unkind
用个栈,问栈顶新加的点是否是unkind,若是是unkind,那么两个一块儿删除,最后栈顶剩下的点必定是honest,由于栈中至少有一个honest,两两之间回答都是1,一直指向栈顶都是honest
问那个honest的人全部人的身份便可
#include <bits/stdc++.h> #define fi first #define se second #define pii pair<int,int> #define mp make_pair #define pb push_back #define space putchar(' ') #define enter putchar('\n') #define eps 1e-10 #define MAXN 200005 //#define ivorysi using namespace std; typedef long long int64; typedef unsigned int u32; typedef double db; template<class T> void read(T &res) { res = 0;T f = 1;char c = getchar(); while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') { res = res * 10 +c - '0'; c = getchar(); } res *= f; } template<class T> void out(T x) { if(x < 0) {x = -x;putchar('-');} if(x >= 10) { out(x / 10); } putchar('0' + x % 10); } bool Query(int a,int b) { putchar('?');space;out(a);space;out(b);enter; fflush(stdout); char s[5]; scanf("%s",s + 1); if(s[1] == 'Y') return 1; else return 0; } int sta[MAXN],top,A,B; int ans[MAXN]; void Solve() { read(A);read(B); if(A <= B) {puts("Impossible");} else { for(int i = 0 ; i < A + B ; ++i) { if(!top) sta[++top] = i; else { if(!Query(sta[top],i)) --top; else sta[++top] = i; } } int h = sta[top]; for(int i = 0 ; i < A + B ; ++i) { if(Query(h,i)) ans[i] = 1; else ans[i] = 0; } putchar('!');space; for(int i = 0 ; i < A + B ; ++i) { out(ans[i]); } enter; } } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); }