Testing Round #12 A,B,C 讨论,贪心,树状数组优化dp
Posted xjhz
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Testing Round #12 A,B,C 讨论,贪心,树状数组优化dp相关的知识,希望对你有一定的参考价值。
题目链接:http://codeforces.com/contest/597
题意:找出[a,b]区间内整除k的数的个数;
思路:小心点特判即可;
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cmath> #include<string> #include<queue> #include<algorithm> #include<stack> #include<cstring> #include<vector> #include<list> #include<set> #include<map> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-14 #define bug(x) cout<<"bug"<<x<<endl; const int N=1e5+10,M=4e6+10,inf=2147483647; const ll INF=1e18+10,mod=1e9+7; /// 数组大小 int main() { ll k,a,b; scanf("%lld%lld%lld",&k,&a,&b); if(a<=0&&b>=0) printf("%lld\n",(b/k)-(a/k)+1); else if(a>=0&&b>=0) printf("%lld\n",(b/k)-(a/k+(a%k?1:0))+1); else printf("%lld\n",(abs(a)/k)-(abs(b)/k+(abs(b)%k?1:0))+1); return 0; }
贪心:按r从小到大排序即可;
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cmath> #include<string> #include<queue> #include<algorithm> #include<stack> #include<cstring> #include<vector> #include<list> #include<set> #include<map> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-14 #define bug(x) cout<<"bug"<<x<<endl; const int N=5e5+10,M=4e6+10,inf=2147483647; const ll INF=1e18+10,mod=1e9+7; struct is { int l,r; bool operator <(const is &c)const { return r<c.r; } }a[N]; /// 数组大小 int main() { int n; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d%d",&a[i].l,&a[i].r); sort(a+1,a+1+n); int s=0,ans=0; for(int i=1;i<=n;i++) { if(a[i].l>s) { ans++; s=a[i].r; } } printf("%d\n",ans); return 0; }
以上是关于Testing Round #12 A,B,C 讨论,贪心,树状数组优化dp的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #538 (Div. 2)
Codeforces Round #527 (Div. 3) 总结 A B C D1 D2 F