A.一开始想用暴力来解决,但是应该要用数学来打败数学,计算每一种车最接近但是不小于t的时间然后取最小值
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a[120],b[120],c[120];
int n,t,p,q;
cin >> n >> t;
for(int i = 0;i < n;++ i){
cin >> a[i] >> b[i];
if(t < a[i]) c[i] = a[i];
else c[i] = a[i] + b[i]*ceil((t-a[i])*1.0/b[i]);
}
for(int i = 0;i < n;++ i){
if(i == 0){p = c[i];q = 1;}
if(c[i] < p){
p = c[i];
q = i+1;
}
}
cout << q << endl;
}
B.感觉B题比A题更简单,题目说只要满足任意一种情况即可,那么对x【i】【j】= min(right【j】,left【i】)即可
#include <bits/stdc++.h>
using namespace std;
int main()
{
int right[120],left[120],top[120][120];
int n,m,h;
cin >> n >> m >> h;
for(int i = 1;i <= m;++ i) cin >> right[i];
for(int i = 1;i <= n;++ i) cin >> left[i];
for(int i = 1;i <= n;++ i){
for(int j = 1;j <= m;++ j)
cin >> top[i][j];
}
for(int i = 1;i <= n;++ i){
for(int j = 1;j <= m;++ j){
if(top[i][j])
cout << min(right[j],left[i]) << ' ';
else
cout << 0 << ' ';
}
cout << endl;
}
}
c.读错题意,怪不得我之前一直纠结哪里有问题。
贪心加模拟:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int flag = 1;
int len;
string s;
cin >> len >> s;
int n1,n2;
if(len%2) flag = 0;
n1 = n2 = 0;
for(int i = 0;i < len;++ i){
if(s[i] == '(') n1++;
if(s[i] == ')') n2++;
}
n1 = len/2-n1;
n2 = len/2-n2;
if(n1 < 0 || n2 < 0) flag = 0;
else{
for(int i = 0;i < len;++ i){
if(s[i] == '?'){
if(n1) {s[i] = '(';n1--;}
else s[i] = ')';
}
}
int sum = 0;
for(int i = 0;i < len-1;++ i){
if(s[i] == '(') sum ++;
else sum --;
if(sum <= 0){
flag = 0;
break;
}
}
}
if(flag == 0) cout << ":(\n";
else cout << s << endl;
}