6879-Mine Sweeper
Theme and analysis
It is a game similar to minesweeper. In a large rectangle, the position of each square with mines is marked with'X', and the position without mines is marked with'.', and the position without mines can represent a number (0-8) , The size of the number meets the rules of minesweeping.
A number n is required, which is the sum of the numbers in each square in the large rectangle. If there is a rectangular pattern that meets the requirements, the number of rows and columns and the rectangular pattern are output; otherwise, -1 is output.
The number of rows and columns is required to be less than or equal to 25.
Code (AC)
#include<bits/stdc++.h>
using namespace std;
const int N = 5005;
char grid[30][30];
int main() {
int n,t;
scanf("%d",&t);
while(t--) {
scanf("%d",&n);
for(int i = 0; i < 26;i++)
fill(grid[i],grid[i]+30,'X');
int r = 0, c = 0;
if(n <= 24) {
c = n + 1;
r = 1;
for(int i = 0; i < c; i += 2)
grid[0][i] = '.';
printf("%d %d\n",r,c);
for(int i = 0; i < c; i++)
cout<<grid[0][i];
cout<<endl;
}
else {
int n1, n2;
for(int i = 0; i <= 8; i++) {
if((n - i*3) % 8 == 0) {
n1 = (n - i*3)/8;
n2 = i;
r = (n1 + 11) / 12 ;
if(n1 % 12)
c = n1 % 12 * 2 + 1;
else
c = 25;
break;
}
}
for(int i = 1; i < 2*r-1; i += 2) {
for(int j = 1; j < 25; j += 2) {
grid[i][j] = '.';
}
}
for(int j = 1; j < c ; j += 2)
grid[2*r-1][j] = '.';
for(int i = 24; i > 24-n2; i--)
grid[24][i] = '.';
printf("%d %d\n",25,25);
for(int i = 0; i < 25; i++) {
for(int j = 0; j < 25; j++)
cout<<grid[i][j];
cout<<endl;
}
}
}
return 0;
}
6887-Task Scheduler
Theme and analysis
This question is a probabilistic analysis question, that is, a mathematical question. Given the number of servers required for each task, and the total number of bad servers, they are taken out without putting them back. The order of execution of the tasks is such that each time it is selected The bad server re-selects the least number of such conflicts.
Code (AC)
#include<bits/stdc++.h>
using namespace std;
vector<pair<int, int> >p;
//记住不要忘了按字典序输出呀,因为有所需服务器数量大小相同的任务!
bool cmp(pair<int,int> a, pair<int,int> b) {
if(a.second == b.second) return a.first < b.first;
else return a.second > b.second;
}
int main() {
//freopen("./1.in","r",stdin);
int t,n,m,k;
scanf("%d",&t);
while(t--) {
p.clear();
scanf("%d %d %d",&n, &m, &k);
for(int i = 0; i < n; i++) {
int temp;
scanf("%d",&temp);
p.push_back(make_pair(i+1, temp));
}
sort(p.begin(), p.end(), cmp);
if( !k ) {
for(int i = 1; i < n; i++)
cout<<i<<" ";
cout<<n<<endl;
}
else {
for(int i = 0; i < n-1; i++)
cout<<p[i].first<<" ";
cout<<p[n-1].first<<endl;
}
}
return 0;
}