比赛链接:click here~~
2 4 5
0 2
【思路】:分为n-2,和n-n/2
代码:
/* * Problem: 搬砖 * Running time: 0MS * Complier: G++ * Author: herongwei * Create Time: 2015/11/29 */ #include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #include <time.h> #include <algorithm> using namespace std; const int maxn=1e5+10; const int MOD=1e9+7; typedef long long LL; char str[maxn],T[maxn]; typedef long long LL; LL n,m,a,b,ans; LL get(LL n) { if(n<3) return 0; LL ans=n%2; return ans+get(n/2)+get(n-n/2); } int main() { //freopen("1.txt","r",stdin); int t;scanf("%d",&t); while(t--) { scanf("%lld",&n); LL ans=0; printf("%lld\n",get(n)); } return 0; }
3 2 4 6 2 2 1
4
代码:
/* * Problem: 投币洗衣机 * Running time: 0MS * Complier: G++ * Author: herongwei * Create Time: 2015/11/29 */ #include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #include <time.h> #include <algorithm> using namespace std; const int maxn=1e5+10; const int MOD=1e9+7; typedef long long LL; char str[maxn],T[maxn]; typedef long long LL; int n,m,a,b,c,ans; int arr[maxn]; int main() { //freopen("1.txt","r",stdin); while(~scanf("%d%d%d%d",&n,&a,&b,&c)) { int sum=0; for(int i=1; i<=n; ++i) { scanf("%d",&arr[i]); } int value=0; for(int i=1; i<=n; ++i) { sum+=arr[i]; if(sum>=a&&sum<b) {value+=2,sum=0;} else if(sum>=b&&sum<c) {value+=3,sum=0;} else if(sum>=c) {value+=4,sum=0;} } printf("%d\n",value); } return 0; }
2 1 10
4 9
代码:
/* * Problem: 质方数 * Running time: 0MS * Complier: G++ * Author: herongwei * Create Time: 2015/11/29 */ #include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #include <time.h> #include <algorithm> using namespace std; const int maxn=2*1e5+100; const int MOD=1e9+7; typedef long long LL; char str[maxn],T[maxn]; typedef long long LL; int n,m,a,b,c,ans; int prime[1000000]; bool Prime[1000000]; int tot = 0; void getPrime() { int i,j; long long temp; for(i = 2; i < 20000; i++) { if(!Prime[i]) prime[tot++] = i; for(j = 0; j < tot && (temp = (long long)i*prime[j]) < 20000; j++) { Prime[temp] = true; if(i%prime[j] == 0) break; } } } int num[maxn]; int Binary_search(int val) { int l = 1,r =2333; while(l<=r) { int mid = (l+r)/2; if(num[mid]>val) r = mid-1; else l = mid+1; } return l; } int main() { //freopen("1.txt","r",stdin); getPrime(); int sum = 1; for(int i = 0 ; i< tot; ++i) { num[sum++] = prime[i]*prime[i]; } int t; scanf("%d",&t); while(t--) { int n; scanf("%d",&n); if(n<4){ puts("4"); continue; } int val = Binary_search(n); if(abs(n-num[val-1])<abs(num[val]-n)){ printf("%d\n",num[val-1]); } else{ printf("%d\n",num[val]); } } return 0; }
1 2 3 4 5 0
1 2 5 14 46
代码:
/* * Problem: ACM组队安排 * Running time: 0MS * Complier: G++ * Author: herongwei * Create Time: 2015/11/29 */ #include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #include <time.h> #include <algorithm> using namespace std; const int maxn=1e5+100; const int MOD=1e9+7; const int inf=0x3f3f3f3f; typedef long long LL; char str[maxn],T[maxn]; typedef long long LL; int n,m; LL a[maxn],b[maxn]; void init() { // a(n) = G(n,3) with G(0,i) = 1, G(n,i) = 0 for n>0 and i<1, otherwise G(n,i) = Sum_{j=0..floor(n/i)} G(n-i*j,i-1) * n!/(i!^j*(n-i*j)!*j!) a[1]=1; a[2]=2; a[3]=5; for(int i=4; i<=20; ++i) a[i]=a[i-1]+a[i]+a[i-2]*(i-1)+a[i]+a[i-3]*(i-1)*(i-2)/2; } int main() { //freopen("1.txt","r",stdin); init(); while(~scanf("%d",&n)&&n) { printf("%lld\n",a[n]); } return 0; }
2 1 1 4 1 2
1
【思路】并查集判断是否属于一个家族,然后是斐波那契的数判断即可,累加,最后排序即可。
代码:
/* * Problem: 油菜花王国 * Running time: 0MS * Complier: G++ * Author: herongwei * Create Time: 2015/11/29 */ #pragma comment(linker,"/STACK:102400000,102400000") #include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #include <time.h> #include <algorithm> using namespace std; const int maxn=1e5+100; const int MOD=1e9+7; const int inf=0x3f3f3f3f; typedef long long LL; char str[maxn],T[maxn]; typedef long long LL; int n,m; LL st[maxn]; LL fib[maxn]; LL vis[maxn]; LL father[maxn]; LL Find(int x) { if(x==father[x]) return x; return father[x]=Find(father[x]); } LL Union(int x,int y) { LL fx=Find(x); LL fy=Find(y); if(fx!=fy) father[fy]=fx; } LL get(LL x) { for(int i=1; i<=50; ++i) { if(fib[i]==x) return 1; } return 0; } int main() { // freopen("1.txt","r",stdin); while(~scanf("%d%d",&n,&m)) { for(int i=1; i<=n; ++i) father[i]=i; for(int i=1; i<=n; ++i) { scanf("%lld",&st[i]); } for(int i=1; i<=m; i++) { LL x,y; scanf("%lld%lld",&x,&y); LL fx=Find(x); LL fy=Find(y); if(fx!=fy) Union(fx,fy); } LL sum[maxn]; memset(sum,0,sizeof(sum)); for(int i=1; i<=n; i++) { if(get(st[i])) sum[Find(i)]++; } sort(sum+1,sum+1+n); printf("%lld\n",sum[n]); } return 0; }
2 5 2 10 4 3 8 1 12 1 2 5 2 10 4 3 8 1 12 1 3
3 -1
【思路】模拟,注意如果朋友的推荐项目可以浏览,则之后要求最多的必须先减去推荐的在判断。
代码:
/* * Problem: 游乐场 * Running time: 0MS * Complier: G++ * Author: herongwei * Create Time: 2015/11/29 */ #include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #include <time.h> #include <algorithm> using namespace std; const int maxn=1e5+100; const int MOD=1e9+7; const int inf=0x3f3f3f3f; typedef long long LL; char str[maxn],T[maxn]; typedef long long LL; int a[maxn],b[maxn]; int tot = 0; struct node { int id,val; bool operator < (const node &t)const { return val<t.val; } } st[maxn]; int main() { //freopen("1.txt","r",stdin); int t; scanf("%d",&t); while(t--) { int n,m,k; scanf("%d%d%d",&n,&m,&k); for(int i=1; i<=n; ++i) { scanf("%lld",&st[i].val); st[i].id=i; } bool ok=0; LL sum_val=0; for(int i=1; i<=m; ++i) { scanf("%lld",&b[i]); sum_val+=st[b[i]].val; } if(sum_val>k) { puts("-1"); } else { for(int i=1; i<=m; ++i) st[b[i]].val=inf; sort(st+1,st+n+1); LL s=m; k-=sum_val; for(int i=1; i<=n-m; ++i) { k-=st[i].val; if(k>=0) s++; else break; } printf("%lld\n",s); } } return 0; }