UVA - 410(贪心.)、HDU - 1576(快速幂算法、简单题)、UVA - 11624(bfs.)

Time limit 3000 ms
OS Linux

题目:
The International Space Station contains many centrifuges in its labs. Each centrifuge will have some
number © of chambers each of which can contain 0, 1, or 2 specimens. You are to write a program
which assigns all S specimens to the chambers such that no chamber contains more than 2 specimens
and the following expression for IMBALANCE is minimized.
IMBALANCE =

C
i=1
| CMi − AM |
where:
CMi
is the Chamber Mass of chamber i and is computed by summing the masses of the specimens
assigned to chamber i.
AM is the Average Mass of the chambers and is computed by dividing the sum of the masses of all
specimens by the number of chambers ©.
Input
Input to this program will be a file with multiple sets of input. The first line of each set will contain
two numbers. The first number (1 ≤ C ≤ 5) defines the number of chambers in the centrifuge and the
second number (1 ≤ S ≤ 2C) defines the number of specimens in the input set. The second line of
input will contain S integers representing the masses of the specimens in the set. Each specimen mass
will be between 1 and 1000 and will be delimited by the beginning or end of the line and/or one or
more blanks.
Output
For each input set, you are to print a line specifying the set number (starting with 1) in the format
‘Set #X’ where X is the set number.
The next C lines will contain the chamber number in column 1, a colon in column number 2, and
then the masses of the specimens your program has assigned to that chamber starting in column 4.
The masses in your output should be separated by exactly one blank.
Your program should then print ‘IMBALANCE = X’ on a line by itself where X is the computed
imbalance of your specimen assignments printed to 5 digits of precision to the right of the decimal.
The final line of output for each set should be a blank line. (Follow the sample output format.)
Sample Input
2 3
6 3 8
3 5
51 19 27 14 33
5 9
1 2 3 5 7 11 13 17 19
Sample Output
Set #1
0: 6 3
1: 8
IMBALANCE = 1.00000
Set #2
0: 51
1: 19 27
2: 14 33
IMBALANCE = 6.00000
Set #3
0: 1 17
1: 2 13
2: 3 11
3: 5 7
4: 19
IMBALANCE = 11.60000

思路是贪心,先排序,如果S<C 那么直接填,否则的话,先处理大的,将前2*C-S个大数占一个格子,剩下的几个,按两头取,例如:1 2 3 4 取 1,4和2,3分别填到格子里!

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
vector<vector<int> > vsp;
int c,s;
int num[20];
double chm[20];
void init(){
    vsp.clear();
    vsp.resize(c);
}
int main(){
    int T = 1;
    while(~scanf("%d%d",&c,&s)){
            init();
            double aver = 0;
            for(int i = 0; i < s; i++){
                scanf("%d",&num[i]);
                aver += num[i]*1.0;
            }
            memset(chm,0.0,sizeof chm);
            aver /= c;
            sort(num,num+s);
             int ind = 0;
            if(s <= c){
                for(; ind < s; ind++){
                    vsp[ind].push_back(num[ind]);
                    chm[ind] = num[ind]*1.0;
                }
            }else{
                int ed = s-1;
                for(; ind < 2*c-s; ind++){
                    vsp[ind].push_back(num[ed]);
                    chm[ind] = num[ed]*1.0;
                    ed--;
                }
                int sta = 0;
                while(sta < ed){
                    vsp[ind].push_back(num[sta]);
                    vsp[ind].push_back(num[ed]);
                    chm[ind] = (num[ed]+num[sta])*1.0;
                    ind++;
                    sta++;
                    ed--;
                }
            }
            double ans = 0;
            for(int i = 0; i < vsp.size(); i++){
                ans += fabs(aver-chm[i]);
            }
       //     cout<<aver<<endl;
            printf("Set #%d\n",T++);
            for(int i = 0; i < vsp.size(); i++){
                printf(" %d:",i);
                for(int j = 0; j < vsp[i].size(); j++){
                    cout<<" "<<vsp[i][j];
                }
                cout<<endl;
            }
            printf("IMBALANCE = %.5lf\n\n",ans);
    }
    return 0;
}

Time limit1000 ms
Memory limit32768 kB
OSWindows

题目:
要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1)。
Input
数据的第一行是一个T,表示有T组数据。
每组数据有两个数n(0 <= n < 9973)和B(1 <= B <= 10^9)。
Output
对应每组数据输出(A/B)%9973。
Sample Input
2
1000 53
87 123456789
Sample Output
7922
6060

代码:

#include<iostream>
using namespace std;
int main()
{
	
	long long int n, b,a,t;
	cin >> t;
	for(int i=0;i<t;i++)
	{
		cin >> n >> b;
		for(int k=1;;k++)
		{
			a = b * k;
			if ((a-n)%9973 == 0) break;
		}
		cout << (a / b) % 9973 << endl;
	}
}

Time limit 1000 ms
OS Linux

题目:
Joe works in a maze. Unfortunately, portions of the maze have
caught on fire, and the owner of the maze neglected to create a fire
escape plan. Help Joe escape the maze.
Given Joe’s location in the maze and which squares of the maze
are on fire, you must determine whether Joe can exit the maze before
the fire reaches him, and how fast he can do it.
Joe and the fire each move one square per minute, vertically or
horizontally (not diagonally). The fire spreads all four directions
from each square that is on fire. Joe may exit the maze from any
square that borders the edge of the maze. Neither Joe nor the fire
may enter a square that is occupied by a wall.
Input
The first line of input contains a single integer, the number of test
cases to follow. The first line of each test case contains the two
integers R and C, separated by spaces, with 1 ≤ R, C ≤ 1000. The
following R lines of the test case each contain one row of the maze. Each of these lines contains exactly
C characters, and each of these characters is one of:
• #, a wall
• ., a passable square
• J, Joe’s initial position in the maze, which is a passable square
• F, a square that is on fire
There will be exactly one J in each test case.
Output
For each test case, output a single line containing ‘IMPOSSIBLE’ if Joe cannot exit the maze before the
fire reaches him, or an integer giving the earliest time Joe can safely exit the maze, in minutes.
Sample Input
2
4 4

#JF#
#…#
#…#
3 3

#J.
#.F
Sample Output
3
IMPOSSIBLE

代码:

#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
#define MAX 1010
using namespace std;
 
char Map[MAX][MAX];
int Time[MAX][MAX];
bool vis[MAX][MAX];
int r,c;
int dr[]={-1,1,0,0};
int dc[]={0,0,-1,1};
 
struct P
{
    int r,c,t;
    P(int r_,int c_,int t_):r(r_),c(c_),t(t_){}
};
queue<P> F;
queue<P> J;
 
 
/**************Debug*************/
 
void show_Time()
{
    for(int i=0;i<r;i++)
    {
        for(int j=0;j<c;j++)
            printf("%d ",Time[i][j]);
        printf("\n");
    }
}
 
 
/*******************************/
 
 
 
void clear()
{
    memset(vis,false,sizeof(vis));
    while(!F.empty())
        F.pop();
    while(!J.empty())
        J.pop();
}
 
bool judge(int nr,int nc)
{
    return (nr>=0&&nr<r&&nc>=0&&nc<c);
}
 
bool ok(int nr,int nc)
{
    return (nr==0||nr==r-1||nc==0||nc==c-1);
}
 
void bfs_f()
{
    int nr,nc,nt;
    while(!F.empty())
    {
        P f=F.front();
        F.pop();
        for(int i=0;i<4;i++)
        {
            nr=f.r+dr[i];
            nc=f.c+dc[i];
            nt=f.t+1;
            if(judge(nr,nc))
            {
                if((Map[nr][nc]=='.'||Map[nr][nc]=='J')&&nt<Time[nr][nc])
                {
                    F.push(P(nr,nc,nt));
                    Time[nr][nc]=nt;
                }
            }
        }
    }
}
 
int bfs_j()
{
    int nr,nc,nt;
    while(!J.empty())
    {
        P j=J.front();
        J.pop();
        if(ok(j.r,j.c))
            return j.t+1;
 
        for(int i=0;i<4;i++)
        {
            nr=j.r+dr[i];
            nc=j.c+dc[i];
            nt=j.t+1;
            if(judge(nr,nc))
            {
                if(vis[nr][nc]==false&&Map[nr][nc]=='.'&&nt<Time[nr][nc])
                {
                    J.push(P(nr,nc,nt));
                    vis[nr][nc]=true;
                }
            }
        }
    }
    return 0;
}
 
int main()
{
    int T,t;
    scanf("%d",&T);
    while(T--)
    {
        clear();
        scanf("%d%d",&r,&c);
 
        for(int i=0;i<r;i++)
        {
 
            scanf("%s",Map[i]);
            for(int j=0;j<c;j++)
            {
                Time[i][j]=1000000007;
                if(Map[i][j]=='J')
                {
                    J.push(P(i,j,0));
                    vis[i][j]=true;
                }
 
                else if(Map[i][j]=='F')
                    F.push(P(i,j,0));
            }
        }
        bfs_f();
        //show_Time();
        if(t=bfs_j())
            printf("%d\n",t);
        else
            printf("IMPOSSIBLE\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43967023/article/details/86684308