A题:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
void init(){
int n,m,ans;
scanf("%d%d",&n,&m);
if(n==2){
if(m==1)
printf("4\n");
else printf("5\n");
return;
}
if(m <= 5) ans = 5;
else if(m == 7) ans = 6;
else{
if(n == 1 || n == 3 || n == 5|| n == 7||n == 8||n ==10||n==12) ans = 6;
else ans = 5;
}
printf("%d\n",ans);
}
int main(){
init();
return 0;
}
B题:
二分
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
ll n,m,k;
bool check(ll x){
ll ans = 0;
if(x <= n-k){
ans += x*(x-1)/2+n-k-(x-1);
}
else ans += (n-k)*(2*x-1-n+k)/2;
if(x >= k) ans += (x-k+1+x)*k/2;
else ans += (1+k)*k/2+k-x;
if(ans <= m) return true;
else return false;
}
void init(){
scanf("%I64d%I64d%I64d",&n,&m,&k);
ll l = 0,r = m,ans = 0;
while(l <= r){
long long mid = (l+r)/2;
if(check(mid)){
l = mid+1;
ans = max(ans,mid);
}
else r = mid-1;
}
printf("%I64d\n",ans);
}
int main(){
init();
return 0;
}
C题:
算有多少个环,每个环出一个换一下,就可以把他们全连在一起了=。=
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int n;
const int maxn = 2e5+10;
int book[maxn],a[maxn],p[maxn];
void init(){
int cnt = 0,flag1 = 0,flag0= 0 ;
scanf("%d",&n);
for(int i = 1; i <=n ;i++){
scanf("%d",&a[i]);
}
for(int i = 1; i <= n ;i++){
if(book[i]==1) continue;
int j = a[i];
book[i]=1;
while(book[j]==0){
// printf("j = %d\n",j);
book[j] =1;
j = a[j];
}
// printf("oo\n");
cnt++;
}
if(cnt ==1)cnt=0;
for(int i = 1; i <= n ;i++){
scanf("%d",&p[i]);
if(p[i]==1) flag1++;
else flag0++;
}
if(n%2==0){
//printf("n = %d\n",n);
if(flag1%2==0||flag0%2==0)cnt++;
}
else {
// printf("flag = %d\n",flag1);
if(flag1%2==0) cnt++;
}
printf("%d\n",cnt);
}
int main(){
init();
return 0;
}
D题:
三种方式取min 先假设花20块。如果用到50,upperbound求一个能和他公用90分钟的,120元,求公用1440分钟的,然后最后求min
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1e5+10;
int n,t[maxn],sum[maxn];
void init(){
scanf("%d",&n);
for(int i = 1; i <= n ; i++){
scanf("%d",&t[i]);
sum[i]=sum[i-1]+20;
int pos = upper_bound(t,t+i,t[i]-90)-t;
int tot = 50-(sum[i-1]-sum[pos-1]);
sum[i] = min(sum[i],sum[i-1]+tot);
pos = upper_bound(t,t+i,t[i]-1440)-t;
tot = 120-(sum[i-1]-sum[pos-1]);
sum[i] = min(sum[i],sum[i-1]+tot);
printf("%d\n",sum[i]-sum[i-1]);
}
}
int main(){
init();
}