比赛地址:Codeforces Round #443 (Div. 2)
A. Borya’s Diagnosis
题意:
有n个诊所,在特定的时间开放,问看完所有的诊所的时间。
题解:
略。
代码:
#include<iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <stack>
#include <string>
#include <map>
using namespace std;
int main() {
int N;
long long pre = 0;
cin >> N;
for (int i = 0; i < N; i++) {
long long temp1, temp2;
cin >> temp1 >> temp2;
if (temp1 > pre) {
pre = temp1;
}
else if (temp1 == pre) {
pre = temp1 + temp2;
}else {
long long t = (pre - temp1) / temp2;
pre = temp1 + (t + 1) * temp2;
}
}
cout << pre << endl;
return 0;
}
B. Table Tennis
题意:
每次队伍头两个人比赛,输的拍到队尾,问哪个人最先赢k次。
题解:
当k>=N-1的时候直接求最大,否则模拟下。
代码:
#include<iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <stack>
#include <string>
#include <map>
#include <queue>
using namespace std;
int main() {
queue<long long> q;
long long N, K;
long long ans = 0;
cin >> N >> K;
for (int i = 0; i < N; i++) {
int temp;
cin >> temp;
q.push(temp);
}
if (K >= N || N <= 2) {
while (!q.empty()) {
ans = max(ans, q.front());
q.pop();
}
cout << ans << endl;
}
else {
int cnt = 1;
int now;
int temp1, temp2;
temp1 = q.front();
q.pop();
temp2 = q.front();
q.pop();
now = max(temp1, temp2);
q.push(min(temp1, temp2));
while (cnt < K) {
temp1 = q.front();
q.pop();
if( temp1 > now ){
q.push(now);
now = temp1;
cnt = 1;
}
else {
q.push(temp1);
cnt++;
}
}
cout << now << endl;
}
return 0;
}
C. Short Program
题意:
给定n个操作,这些操作包含&、|、^。用五个以内的操作来代替这些操作。
题解:
直接观察每一位的最后变化,
如果1变0,0变1,则该位^
如果都变1,则|
如果都变0,则&
否则不变
代码:
#include<iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <stack>
#include <string>
#include <map>
#include <queue>
using namespace std;
int main() {
int N;
int t1 = (1 << 10) - 1;
int t2 = 0;
cin >> N;
for (int i = 0; i < N; i++) {
string s;
int temp;
cin >> s >> temp;
if (s == "|") {
t1 |= temp;
t2 |= temp;
}
else if( s == "&" ) {
t1 &= temp;
t2 &= temp;
}
else {
t1 ^= temp;
t2 ^= temp;
}
}
int ans1, ans2, ans3;
ans1 = (1 << 10) - 1;
ans2 = ans3 = 0;
for (int i = 0; i < 10; i++) {
if ((t1&(1 << i)) && (t2&(1 << i))) {
ans2 |= (1 << i);
}
else if (!(t1&(1 << i)) && !(t2&(1 << i))) {
ans1 ^= (1 << i);
}
else if(!(t1&(1 << i)) && (t2&(1 << i))){
ans3 |= (1 << i);
}
}
cout << 3 << endl;
cout << "& " << ans1 << endl;
cout << "| " << ans2 << endl;
cout << "^ " << ans3 << endl;
return 0;
}
D. Teams Formation
题意:
m个长度为n的数组排成一列,当每有k个相同的数的时候消掉,求问最后剩余几个数
题解:
首先预处理数组,该数组不能再消了,然后从l、r考虑能不能继续消除,然后一直消除到不能再消除了。
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#define For(i,a) for(int i=1;i<=a;++i)
#define F(i,a,b) for(int i=a;i<=b;++i)
#define dFor(i,a) for(int i=a;i>=1;--i)
#define dF(i,a,b) for(int i=a;i>=b;--i)
#define F2(i,a,b) for(int i=a;i<b;++i)
#define eF(h,i,u) for(int i=h[u];i;i=nxt[i])
using namespace std;
int n,m,k,s[100009];
int a[100009],b[100009],t=0;
long long ans=0;
void init(){
scanf("%d%d%d",&n,&k,&m);
For(i,n) scanf("%d",s+i);
For(i,n){
if(s[i]!=a[t])
a[++t]=s[i], b[t]=1;
else{
++b[t];
if(b[t]==k) ans+=k, --t;
}
} ans*=m;
}
int main(){
init();
// For(i,t) printf("%d %d, ",a[i],b[i]); puts("");
// printf("%I64d\n",ans);
int l=1, r=t;
while(l<=r){
if(a[l]!=a[r]) break;
if(l==r) {ans+=1ll*b[l]*m/k*k; break;}
if(b[l]+b[r]>=k){
ans+=1ll*k*(m-1);
if(b[l]+b[r]==k) ++l, --r;
else break;
}else break;
}
// if(l==r&&1ll*b[l]*m%k==0) ans=1ll*n*m;
if(l==r && (b[1] + b[t])%k == 0 &&1ll*b[l]*m%k==0 )ans = 1ll*n*m;
printf("%I64d",1ll*n*m-ans);
}