Codeforces Round #334 (Div. 2) B C D
来源:互联网 发布:淘宝上的宝贝怎么上架 编辑:程序博客网 时间:2024/06/05 15:42
B. More Cowbell
题意:给定N表示铃铛的个数,和k表示箱子的个数,牛铃铛要放在箱子里运输,要求每个箱子放1个或者2个牛铃铛,且每个箱子都要放铃铛,箱子的大小相等,老板为了省钱,希望箱子越小越好( smallest ),求满足条件的箱子的尺寸。给定的n个铃铛的尺寸依次从小到大。
样例:
6 4
1 2 3 101 102 103
{1 101} {2 3} {102} {103}
Answer:103
思路:设有x个箱子放2个铃铛,(k-x)个箱子放一个铃铛。2*x+(k-x) == n --> x == n-k
由于给出的N个铃铛有序,所以前2*x个铃铛折中取 {1,2*x} {2,2*x-1}....同时跟新最大的箱子的尺寸max
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<vector>#include <ctime>#include<queue>#include<set>#include<map>#include<stack>#include<cmath>#define mst(ss,b) memset((ss),(b),sizeof(ss))#define maxn 0x3f3f3f3f///#pragma comment(linker, "/STACK:102400000,102400000")typedef long long ll;#define INF (1ll<<60)-1using namespace std;ll a[1001000];int main(){ ll n,k; cin>>n>>k; for(ll i=1;i<=n;i++) cin>>a[i]; ll x=2*(n-k); ll ans=a[n]; for(ll i=1;i<=x/2;i++){ ans=max(ans,a[i]+a[x-i+1]); } cout<<ans<<endl; return 0;}
C. Alternative Thinking
题意:简直不能忍 contiguous 这个单词,被坑了!!!给定一串01字符串的长度n以及字符串。定义不连续的子串为任意相邻两个元素不相等也就是0101010之类的。给定一个操作可以将一段区间内的字符翻转 比如区间1101翻转为0010 。求执行一次操作后的最长不连续的子串长度。
样例:
8
10110101
Answer:8
解释:
1 2 3 4 5 6 7 8
1 0 1 1 0 1 0 1
将区间4~8翻转就变成10101010
思路:随手画画就出来 只要出现三个或者三个以上的比如 ...111... anw+2 或者出现两次以上的两个连续的比如 ..11...11... anw+2 出现一次两个连续的比如 ...11... anw+1 恩就是这样
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<vector>#include <ctime>#include<queue>#include<set>#include<map>#include<stack>#include<cmath>#define mst(ss,b) memset((ss),(b),sizeof(ss))#define maxn 0x3f3f3f3f///#pragma comment(linker, "/STACK:102400000,102400000")typedef long long ll;#define INF (1ll<<60)-1using namespace std;string s;int a[100100];int n;int main(){ cin>>n; cin>>s; for(int i=0;i<n;i++) a[i+1]=s[i]-'0'; int t=a[1],ans=1; for(int i=2;i<=n;i++){ if(a[i]!=a[t]){ ans++; t=i; } } int flag1=0,flag2=0; for(int i=2;i<=n;i++){ if(a[i]==a[i-1]) flag1++; } for(int i=2;i<n;i++){ if(a[i]==a[i-1] && a[i]==a[i+1]) flag2=1; } if(flag2 || flag1>1) { cout<<ans+2<<endl; return 0; } if(flag1){ cout<<ans+1<<endl; return 0; } cout<<ans<<endl; return 0;}
D. Moodular Arithmetic
题意:给定函数 f ( k * x % p) = k * f ( x ) % p ‘=’ 意思是同余 下面出现的也是 问在集合A->B上不同的映射函数 f 有几种,其中A=B={0,1,2..p-1},p为素数(除了2),k为小于p的一个常数。
思路:当 k==0 f(0)%p=k*f(x)%p=0 只有 f(0)==0的时候才是唯一 其他的都是1~p-1任意的数 所以Answer==p^(p-1)。当k==1 时 f(x)==f(x) 显然f(x) 取 0~p-1 任意值都可以 所以Answer==p^(p) 当 k > 1 时 P是奇素数 函数可以写成 f ( k^m * x % p ) = k ^ m * f ( x ) % p ==> x = ( k^(m-1)*x , k^(m-2)*x....k*x , x ) m为最小的 k^m%p == 1 (由于m个 x%p 值要严格两两不相等 相等了就无法满足题目的映射要求 ) 找到m的值也就知道了循环节的长度 Answer==p^((p-1)/m) 为什么是p-1因为0是不能取的
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<vector>#include <ctime>#include<queue>#include<set>#include<map>#include<stack>#include<cmath>#define mst(ss,b) memset((ss),(b),sizeof(ss))#define mod 1000000007///#pragma comment(linker, "/STACK:102400000,102400000")typedef long long ll;#define INF (1ll<<60)-1using namespace std;ll Qpow(ll a,ll b){ ll ans=1; while(b){ if(b%2==1) ans=ans*a%mod; a=a*a%mod; b=b>>1; } return ans%mod;}int main(){ ll p,k; cin>>p>>k; if(k==0) cout<<Qpow(p,p-1)<<endl; else if(k==1) cout<<Qpow(p,p)<<endl; else{ ll m,ans=k; for(m=1;ans!=1;m++){ ans=ans*k%p; } /// cout<<m<<endl; cout<<Qpow(p,(p-1)/m)<<endl; } return 0;}
- Codeforces Round #334 (Div. 2) B C D
- Codeforces Round #179 (Div. 2)A、B、C、D
- Codeforces Round #186 (Div. 2)A、B、C、D、E
- Codeforces Round #202 (Div. 2) (A、B、C、D)
- Codeforces Round #211 (Div. 2)(A,B,C,D)
- Codeforces Round #220 (Div. 2)(A,B,C,D)
- Codeforces Round #231 (Div. 2)A, B, C, D
- Codeforces Round #258 (Div. 2)-(A,B,C,D,E)
- Codeforces Round #246 (Div. 2) A,B,C,D
- Codeforces Round #261 (Div. 2) A,B,C,D,E
- Codeforces Round #259 (Div. 2) A/B/C/D
- Codeforces Round #257 (Div. 2) A/B/C/D
- Codeforces Round #256 (Div. 2) A/B/C/D
- Codeforces Round #262 (Div. 2)-A,B,C,D
- Codeforces Round #263 (Div. 2) A,B,C,D,E
- Codeforces Round #264 (Div. 2) A,B,C,D,E
- Codeforces Round #276 (Div. 2) A B C D
- Codeforces Round #277 (Div. 2) A,B,C,D,E
- qt Qcompleter自动补全类
- 贪吃——1000 移动桌子
- ajax_之get请求方式(ajax验证用户名)
- Java学习笔记-对象与类
- 蓝桥杯常用算法知识点:【递归】m个A与n个B的全排列个数
- Codeforces Round #334 (Div. 2) B C D
- hdu ACMSteps 5.3.8 Rotate
- 三色排序(荷兰国旗问题)
- Dijkstra单源最短路径
- CodeForces 148BEscape(数学)
- scrollview的相关问题
- java7新特性之Diamond syntax
- 蓝桥杯 算法训练 大小写转换
- Wu-Manber字符串多模式匹配