drink(链表)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lvmaooi/article/details/80341534

drink

先看看题吧~

 
 
 
 
 
 
 

解:

观察数据范围,可能标算是个N2的,诶,数字在0~9之间。一定有什么性质!!!

然而并没有。

唉,普及组题目都做不来了,我可能会一个N4的。其实这道题还挺好的,我们观察性质,在旋转一个正方形的时候,实质操作是什么?
发现性质:
除矩形边界上的点,其它点上下左右的点不变。
除正方形边界上的点,其它点周围点的相对位置不变。
什么叫相对位置不变,就是说顺时针旋转的话,虽然上面的点变成右边的,右边的变成下面的,但是这四个点只是旋转了,我知道某一个点在x的上方,那么我就可以知道x其它方向的点是什么位置。
于是我们修改变成改一波正方形边界就行了,这样时间复杂度就是O(NQ)的。
然后就A了。

但是这题的代码不是很好打,我们只有知道一个点才能向前走,不好记录位置。蒟蒻的方法是把边界上点暴力打到数组里,在遍历一遍正方形边界,虽然常数大一些,但是好写了许多!

每次找正方形边界都要从大矩形边界往里走。

贴波代码:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
struct lxy{
    int to[4];
}b[10000005];

int n,m,k;
int data[4005][4005];
int l[20005],aim[20005];
int x,y,c,id,t,cnt;

void upup()
{
    int i;
    for(i=0;i<=3;i++)
      if(b[b[id].to[t]].to[i]==id)
        break;
    id=b[id].to[t];
    t=(i+2)%4;
}

void add()
{
    l[++cnt]=b[id].to[(t+1)%4];
    for(aim[cnt]=0;aim[cnt]<=3;aim[cnt]++)
      if(b[l[cnt]].to[aim[cnt]]==id)
        break;
}

void relink()
{
    cnt++;
    b[id].to[(t+1)%4]=l[cnt];
    b[l[cnt]].to[aim[cnt]]=id;
}

void moveit()
{
    memset(l,0,sizeof(l));
    memset(aim,0,sizeof(aim));
    cnt=0;id=y+c;int tix=4;
    for(t=0;t<=3;t++) if(b[id].to[t]!=0) break;
    for(register int i=1;i<=x;i++)  upup();
    while(tix--){
    for(register int i=1;i<c;i++)
    {
        add(),upup();
        if(i==c-1) add(),t=(t+3)%4;
    }}
    tix=4;id=x*(m+1)+1;cnt=0;
    for(t=0;t<=3;t++) if(b[id].to[t]!=0) break;
    for(register int i=1;i<=y;i++) upup();
    while(tix--){
    for(register int i=1;i<c;i++)
    {
        relink(),upup();
        if(i==c-1) relink(),t=(t+3)%4;
    }}
}

int main()
{
    scanf("%d%d%d",&n,&m,&k);
    for(register int i=1;i<=n;i++)
      for(register int j=1;j<=m;j++)
        scanf("%d",&data[i][j]);
    for(register int i=0;i<=n;i++)
      for(register int j=0;j<=m;j++)
      {
        int id=i*(m+1)+j+1;
        if(i>=1&&j!=0) b[id].to[0]=(i-1)*(m+1)+j+1;
        if(i<n&&j!=0) b[id].to[2]=(i+1)*(m+1)+j+1;
        if(j>=1&&i!=0) b[id].to[1]=i*(m+1)+j;
        if(j<m&&i!=0) b[id].to[3]=i*(m+1)+j+2;
      }
    while(k--)
    {
        scanf("%d%d%d",&x,&y,&c);
        if(c<=1) continue;
        moveit();
    }
    for(register int i=1;i<=n;i++)
    {
        id=i*(m+1)+1;
        for(t=0;t<=3;t++) if(b[id].to[t]!=0) break;
        for(register int j=1;j<=m;j++)
        {
            upup();
            int retx=(id-1)/(m+1);
            int rety=(id-1)%(m+1);
            printf("%d ",data[retx][rety]);
        }
        printf("\n");
    }
}

总结一下,这是一个四向链表,操作感觉有点窒息。但是仔细思考一下,这还是很好想到的。开始我们的暴力做法没有抓住“旋转”这个性质。在数据结构的题中往往是要抓住不变的东西,把它们不变的作用发挥到极致,才可以优化复杂度。

Drink, on Ice

12-20

DescriptionnnA good drink is always served on ice. That said, the amount of ice is what makes the difference. If it is too much, the drink will be well cooled, however, this is a bit of fraud as there could be less ice (and more Vodka for example). On the other hand, if there is too little ice the drink is warm which is unacceptable. You are to help the bartender, of course neither with mixing nor drinking, but with calculating the expected outcome of such mixtures. nTo make things easier, we assume that pure water is mixed with ice in a closed system, i.e., there is no problem with the outside temperature or the warming of the bottle, etc. Therefore, after a some time has passed, the system may be regarded as balanced (there is no further change in temperature and no more melting or freezing). Your job is to calculate the final temperature of this balanced system and the amount of ice and water in this equilibrium state. nAs you know from physics, it takes 4.19 Joule to heat one gram of water one Kelvin, whereas it takes 2.09 Joule if it is ice. We define the capacities cw = 4.19 J/(g*K) and ci = 2.09 J/(g*K). Melting one gram of ice takes 335 Joule, where the temperature remains constant at zero. We define the constant em = 335 J/g. The total thermal energy of the ice and the water before the experiment is equal to the thermal energy of the final mixture. nThe figure below shows the energy of one gram of ice, ice-water-mixture, or water, where the temperature is measured relative to -30 degrees Celsius. The jump at 0 degrees represents the melting of ice to water. The amount of energy gained is proportional to the amount of ice already melted.nInputnnThe input contains several test cases. Each test case consists of four real numbers mw, mi, tw, ti. The mass of water mw and the mass of ice mi are both non-negative, given in grams, and mw + mi > 0. The water temperature tw and the ice temperature ti follow, both given in degrees Celsius, and you may assume that -30 < ti <= 0 <= tw < 100. The last test case is followed by four zeroes.nOutputnnFor each test case output the amount of ice and water in grams and the final temperature of the mixture in degrees Celsius. All numbers must be rounded to one digit. Adhere to the sample output for the exact format to use.nSample Inputnn100 20 50 -10n100 22 0 0n100 35 25 -10.5n10 90 25 -28n0 0 0 0nSample Outputnn0.0 g of ice and 120.0 g of water at 27.5 Cn22.0 g of ice and 100.0 g of water at 0.0 Cn6.0 g of ice and 129.0 g of water at 0.0 Cn100.0 g of ice and 0.0 g of water at -4.2 C

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭