Find the number of k-divisible numbers on the segment [a, b]. In other words you need to find the number of such integer values x that a ≤ x ≤ b and x is divisible by k.
The only line contains three space-separated integers k, a and b (1 ≤ k ≤ 1018; - 1018 ≤ a ≤ b ≤ 1018).
Print the required number.
1 1 10
10
2 -4 4
5 题解: 简单水题 AC代码:/* *********************************************** Author :xdlove Created Time :2015年11月12日 星期四 10时28分42秒 File Name :xdlove/codeforces/Testing_Round_#12/A/A.cpp ************************************************ */ #pragma comment(linker, "/STACK:1024000000,1024000000") #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <memory.h> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <time.h> using namespace std; #define REP_ab(i,a,b) for(int i = a; i <= b; i++) #define REP(i, n) for(int i = 0; i < n; i++) #define REP_1(i,n) for(int i = 1; i <= n; i++) #define DEP(i,n) for(int i = n - 1; i >= 0; i--) #define DEP_N(i,n) for(int i = n; i >= 1; i--) #define CPY(A,B) memcpy(A,B,sizeof(B)) #define MEM(A) memset(A,0,sizeof(A)) #define MEM_1(A) memset(A,-1,sizeof(A)) #define MEM_INF(A) memset(A,0x3f,sizeof(A)) #define MEM_INFLL(A) memset(A,0x3f3f,sizeof(A)) #define mid (((l + r) >> 1)) #define lson l, mid, u << 1 #define rson mid + 1, r, u << 1 | 1 #define ls (u << 1) #define rs (u << 1 | 1) typedef long long ll; typedef unsigned long long ull; const int INF = 0x3f3f3f3f; const ll INFLL = 0x3f3f3f3f3f3f3f3f; const int MAXN = 1e5 + 5; const int MAXM = MAXN; const int mod = 1e9 + 7; int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); ll k,a,b,ans = 0; cin>>k>>a>>b; if(a == b && a == 0) ans = 1; else { if(b >= 0) { ans += b / k; if(a > 0) ans -= (a - 1) / k; else ans += -a / k + 1; } else ans += -a / k - (-b - 1) / k; } cout<<ans<<endl; return 0; }
B. Restaurant
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values — the start time li and the finish time ri (li ≤ ri).
Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?
No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
InputThe first line contains integer number n (1 ≤ n ≤ 5·105) — number of orders. The following n lines contain integer values li and ri each (1 ≤ li ≤ ri ≤ 109).
OutputPrint the maximal number of orders that can be accepted.
Sample test(s)Input2 7 11 4 7Output1Input5 1 2 2 3 3 4 4 5 5 6Output3Input6 4 8 1 5 4 7 2 5 1 3 6 8Output2 题解: 简单水题 AC代码:/* *********************************************** Author :xdlove Created Time :2015年11月12日 星期四 10时39分50秒 File Name :xdlove/codeforces/Testing_Round_#12/B/B.cpp ************************************************ */ #pragma comment(linker, "/STACK:1024000000,1024000000") #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <memory.h> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <time.h> using namespace std; #define REP_ab(i,a,b) for(int i = a; i <= b; i++) #define REP(i, n) for(int i = 0; i < n; i++) #define REP_1(i,n) for(int i = 1; i <= n; i++) #define DEP(i,n) for(int i = n - 1; i >= 0; i--) #define DEP_N(i,n) for(int i = n; i >= 1; i--) #define CPY(A,B) memcpy(A,B,sizeof(B)) #define MEM(A) memset(A,0,sizeof(A)) #define MEM_1(A) memset(A,-1,sizeof(A)) #define MEM_INF(A) memset(A,0x3f,sizeof(A)) #define MEM_INFLL(A) memset(A,0x3f3f,sizeof(A)) #define mid (((l + r) >> 1)) #define lson l, mid, u << 1 #define rson mid + 1, r, u << 1 | 1 #define ls (u << 1) #define rs (u << 1 | 1) typedef long long ll; typedef unsigned long long ull; const int INF = 0x3f3f3f3f; const ll INFLL = 0x3f3f3f3f3f3f3f3f; const int MAXN = 5e5 + 5; const int MAXM = MAXN; const int mod = 1e9 + 7; struct RE { int l,r; void in() { scanf("%d %d",&l,&r); } bool operator < (const RE &a) const { return r < a.r; } }p[MAXN]; int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int n; cin>>n; REP(i,n) p[i].in(); sort(p,p + n); int num = 1,pos = p[0].r; REP_1(i,n - 1) { if(p[i].l > pos) { num++; pos = p[i].r; } } cout<<num<<endl; return 0; }
C. Subsequences
For the given sequence with n different elements find the number of increasing subsequences with k + 1 elements. It is guaranteed that the answer is not greater than 8·1018.
InputFirst line contain two integer values n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 10) — the length of sequence and the number of elements in increasing subsequences.
Next n lines contains one integer ai (1 ≤ ai ≤ n) each — elements of sequence. All values ai are different.
OutputPrint one integer — the answer to the problem.
Sample test(s)Input5 2 1 2 3 5 4Output7 题解: 很好的一道用数据结构来优化DP的一道题,首先很容易想到DP的状态转移方程,以dp[i][j]表示前i个数以a[i]结尾的递增序列长度为j的方案数,则状态的转移方程就可以写为 dp[i][j] += dp[x][j - 1],其中满足a[x]<a[i];这部分的处理如果不用数据结构优化,时间复杂度是n^2的,因为注意到我们是统计所有小于a[i]的DP值的和,所以可以利用树状数组或者 线段树优化; AC代码:/* *********************************************** Author :xdlove Created Time :2015年11月12日 星期四 10时49分32秒 File Name :xdlove/codeforces/Testing_Round_#12/C/c.cpp ************************************************ */ #pragma comment(linker, "/STACK:1024000000,1024000000") #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <memory.h> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <time.h> using namespace std; #define REP_ab(i,a,b) for(int i = a; i <= b; i++) #define REP(i, n) for(int i = 0; i < n; i++) #define REP_1(i,n) for(int i = 1; i <= n; i++) #define DEP(i,n) for(int i = n - 1; i >= 0; i--) #define DEP_N(i,n) for(int i = n; i >= 1; i--) #define CPY(A,B) memcpy(A,B,sizeof(B)) #define MEM(A) memset(A,0,sizeof(A)) #define MEM_1(A) memset(A,-1,sizeof(A)) #define MEM_INF(A) memset(A,0x3f,sizeof(A)) #define MEM_INFLL(A) memset(A,0x3f3f,sizeof(A)) #define mid (((l + r) >> 1)) #define lson l, mid, u << 1 #define rson mid + 1, r, u << 1 | 1 #define ls (u << 1) #define rs (u << 1 | 1) typedef long long ll; typedef unsigned long long ull; const int INF = 0x3f3f3f3f; const ll INFLL = 0x3f3f3f3f3f3f3f3f; const int MAXN = 1e5 + 5; const int MAXM = MAXN; const int mod = 1e9 + 7; int n,k; ll dp[MAXN][20],val[20][MAXN]; int lowbit(int x) { return x & -x; } void add(ll c,ll b[],int v) { while(v <= n) { b[v] += c; v += lowbit(v); } } ll sum(ll b[],int v) { ll s = 0; while(v > 0) { s += b[v]; v -= lowbit(v); } return s; } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); cin>>n>>k; k++; REP_1(i,n) { int x; scanf("%d",&x); dp[i][1] = 1; REP_1(j,k) { if(j > i) break; ll tp = sum(val[j - 1],x - 1); dp[i][j] += tp; add(dp[i][j],val[j],x); } } ll ans = 0; REP_1(i,n) ans += dp[i][k]; cout<<ans<<endl; return 0; }