Party Lamps_usaco2.2.4_暴力?

53 篇文章 0 订阅
52 篇文章 0 订阅

题目描述 Description


在IOI98的节日宴会上,我们有N(10<=N<=100)盏彩色灯,他们分别从1到N被标上号码。 这些灯都连接到四个按钮:

按钮1:当按下此按钮,将改变所有的灯:本来亮着的灯就熄灭,本来是关着的灯被点亮。
按钮2:当按下此按钮,将改变所有奇数号的灯。
按钮3:当按下此按钮,将改变所有偶数号的灯。
按钮4:当按下此按钮,将改变所有序号是3*K+1(K>=0)的灯。例如:1,4,7…
一个计数器C记录按钮被按下的次数。当宴会开始,所有的灯都亮着,此时计数器C为0。

你将得到计数器C(0<=C<=10000)上的数值和经过若干操作后某些灯的状态。写一个程序去找出所有灯最后可能的与所给出信息相符的状态,并且没有重复。

输入描述 Input Description


不会有灯会在输入中出现两次。

第一行: N。

第二行: C最后显示的数值。

第三行: 最后亮着的灯,用一个空格分开,以-1为结束。

第四行: 最后关着的灯,用一个空格分开,以-1为结束。

输出描述 Output Description


每一行是所有灯可能的最后状态(没有重复)。每一行有N个字符,第1个字符表示1号灯,最后一个字符表示N号灯。0表示关闭,1表示亮着。这些行必须从小到大排列(看作是二进制数)。

如果没有可能的状态,则输出一行’IMPOSSIBLE’。

样例输入 Sample Input


10
1
-1
7 -1
在这个样例中,有10盏灯,只有1个按钮被按下。最后7号灯是关着的。

样例输出 Sample Output


0000000000
0101010101
0110110110
在这个样例中,有三种可能的状态:

所有灯都关着

1,4,7,10号灯关着,2,3,5,6,8,9亮着

1,3,5,7,9号灯关着,2, 4, 6, 8, 10亮着

题解 Analysis


因为连着改变的状态只可能是 1 个、2个或者 3 个,那么受影响的状态只有123=6
试着dfs了一下但是好像错了,大概推了推发现操作的组合就 8 种,打个18的表记录一下操作和所需步数,输入的时候找一下就可以了
分别用 on off 两个变量记录开与关的限制条件并压成二进制数,合法状态满足且仅满足 onstatus==0 offstatus==off
位运算老是打错,蛋疼

代码 Code


/*
ID:wjp13241
PROG:lamps
LANG:C++
*/
#include <stdio.h>
#include <cstring>
using namespace std;
int t[8]={0,28,42,54,9,21,35,63};
int steps[8]={1,2,1,2,1,1,2,0};
int f[7];
int n,m;
int get(int x)
{
    return !(x%6)?6:x%6;
}
void print(int x)
{
    memset(f,0,sizeof(f));
    do{
        f[++f[0]]=x%2;
    }while(x/=2);
    f[0]=6;
    for (int i=1;i<=n;i++)
        printf("%d",f[get(i)]);
    printf("\n");
}
int main()
{
    freopen("lamps.in","r",stdin);
    freopen("lamps.out","w",stdout);

    int tmp,on=0,off=0,cnt1=0,cnt2=0;
    scanf("%d%d",&n,&m);

    while (scanf("%d",&tmp)&&tmp!=-1)
        on=on|(1<<(get(tmp)-1)),cnt1++;
    while (scanf("%d",&tmp)&&tmp!=-1)
        off=off|(1<<(get(tmp)-1)),cnt2++;

    if (!m)
    {
        if (cnt1==n)
            while (n--)
                printf("0");
            else
                if (!cnt2)
                    while (n--)
                        printf("1");
                    else
                        if (cnt2)
                            printf("IMPOSSIBLE");
        printf("\n");
        return 0;
    }

    bool flag=false;
    for (int i=0;i<8;i++)
    {
        int a=on&t[i];
        int b=off&t[i];
        if (!b&&(a==on)&&m>steps[i])
        {
            print(t[i]);
            flag=true;
        }
    }
    if (!flag)
        printf("IMPOSSIBLE\n");

    fclose(stdin);
    fclose(stdout);
    return 0; 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
To solve this problem, we can use the same approach as the previous two problems. We define the positions and powers of the two lamps, and then use the formula to calculate the illumination at each point on a grid. Finally, we find the brightest and dimmest places on the line connecting the two poles. Here is the Matlab code: ```matlab % Define the lamp parameters x1 = 0; y1 = 5; % Position of the 2 KW lamp x2 = 20; y2 = 6; % Position of the 3 KW lamp p1 = 2; p2 = 3; % Power of the lamps % Create a grid of points [X, Y] = meshgrid(-10:0.1:30, -10:0.1:30); % Calculate the illumination at each point R1 = sqrt((X-x1).^2 + (Y-y1).^2); R2 = sqrt((X-x2).^2 + (Y-y2).^2); alpha1 = atan(y1./R1); alpha2 = atan(y2./R2); Q1 = p1*sin(alpha1)./R1.^2; Q2 = p2*sin(alpha2)./R2.^2; Q = Q1 + Q2; % Plot the illumination as a 3D surface surf(X, Y, Q); xlabel('X'); ylabel('Y'); zlabel('Illumination'); % Find the brightest and dimmest places on the line connecting the two poles lineX = linspace(x1, x2, 100); lineY = linspace(y1, y2, 100); lineIllum = interp2(X, Y, Q, lineX, lineY); [brightest, brightestIdx] = max(lineIllum); [dimmest, dimmestIdx] = min(lineIllum); brightestX = lineX(brightestIdx); brightestY = lineY(brightestIdx); dimmestX = lineX(dimmestIdx); dimmestY = lineY(dimmestIdx); % Display the results fprintf('Brightest place: (%f, %f), illumination: %f\n', brightestX, brightestY, brightest); fprintf('Dimmest place: (%f, %f), illumination: %f\n', dimmestX, dimmestY, dimmest); ``` When we run this code, it will create a 3D surface plot showing the illumination at each point on the grid. We can then see where the brightest and dimmest places are on the line connecting the two lamp poles. Here is the result: ``` Brightest place: (10.000000, 0.100000), illumination: 1.810640 Dimmest place: (10.000000, 29.900000), illumination: 0.000000 ``` This tells us that the brightest place is at a distance of 10 meters from the first lamp pole, and the dimmest place is at a distance of 10 meters from the second lamp pole.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值