第三周 周结

训练内容:

SDAU19训练-贪心&DP3
题目链接:https://vjudge.net/contest/361142#overview

本周训练感悟:

本周烦心事可不少,不过还是写了做了几个训练的题。
感悟就是做题可不能烦躁越烦越想不出思路,手写一下思路还是很重要的。
懒得说有的没的,就整理几个题叭。

题目1:A. Business trip

What joy! Petya’s parents went on a business trip for the whole year and the playful kid is left all by himself. Petya got absolutely happy. He jumped on the bed and threw pillows all day long, until…

Today Petya opened the cupboard and found a scary note there. His parents had left him with duties: he should water their favourite flower all year, each day, in the morning, in the afternoon and in the evening. “Wait a second!” — thought Petya. He know for a fact that if he fulfills the parents’ task in the i-th (1 ≤ i ≤ 12) month of the year, then the flower will grow by ai centimeters, and if he doesn’t water the flower in the i-th month, then the flower won’t grow this month. Petya also knows that try as he might, his parents won’t believe that he has been watering the flower if it grows strictly less than by k centimeters.

Help Petya choose the minimum number of months when he will water the flower, given that the flower should grow no less than by k centimeters.

Input
The first line contains exactly one integer k (0 ≤ k ≤ 100). The next line contains twelve space-separated integers: the i-th (1 ≤ i ≤ 12) number in the line represents ai (0 ≤ ai ≤ 100).

Output
Print the only integer — the minimum number of months when Petya has to water the flower so that the flower grows no less than by k centimeters. If the flower can’t grow by k centimeters in a year, print -1.
题目理解:
就是一个懒惰的调皮小孩的父母抛下他去“度蜜月”,还要他浇花的令人悲伤故事。小孩预知了12个月如果浇花花会长多高,让你替他挑最少的月份数浇花,使得长高不低于他爹地和妈咪给他的严格标准k米。
(这可真让人开心,题这么简单)就是给他预知的12个月排序呗,再加个简单循环就完事了,(ac代码附下)

#include<iostream>
#include<cmath>
#include<algorithm>
#define ll long long
using namespace std;
int n,a[15],ans=0;
int main()
{
    cin>>n;
    for(int i=0;i<12;i++)
        cin>>a[i];
    sort(a,a+12);
    for(int i=11;i>=0;i--)
    {
        if(n<=0)break;
        n=n-a[i];
        ans++;
    }
    if(n<=0)cout<<ans<<endl;
    else cout<<-1<<endl;
}

题目2:B - Dima and a Bad XOR

Student Dima from Kremland has a matrix a of size n×m filled with non-negative integers.

He wants to select exactly one integer from each row of the matrix so that the bitwise exclusive OR of the selected integers is strictly greater than zero. Help him!

Formally, he wants to choose an integers sequence c1,c2,…,cn (1≤cj≤m) so that the inequality a1,c1⊕a2,c2⊕…⊕an,cn>0 holds, where ai,j is the matrix element from the i-th row and the j-th column.

Here x⊕y denotes the bitwise XOR operation of integers x and y.

Input
The first line contains two integers n and m (1≤n,m≤500) — the number of rows and the number of columns in the matrix a.

Each of the next n lines contains m integers: the j-th integer in the i-th line is the j-th element of the i-th row of the matrix a, i.e. ai,j (0≤ai,j≤1023).

Output
If there is no way to choose one integer from each row so that their bitwise exclusive OR is strictly greater than zero, print “NIE”.

Otherwise print “TAK” in the first line, in the next line print n integers c1,c2,…cn (1≤cj≤m), so that the inequality a1,c1⊕a2,c2⊕…⊕an,cn>0 holds.

If there is more than one possible answer, you may output any.

内心独白:
这题可真要整死我,心思半天没整出来,没本事的我还是看了题解,突然发现有个小符号我没见过,“^”就是这个小位运算符,然后就搜啊搜,然后就把小符号引起的学习给整理一下吧;
位运算符
位运算符作用于位,并逐位执行操作。&、 | 和 ^ 的真值表如下所示:
0&0=0;0|0=0;0^0=0;
1&0=0;1|0=1;1^0=1;
1&1=1;1|1=1;1^1=0;
0&1=0;0|1=1;0^1=1;
假设如果 A = 60,且 B = 13,现在以二进制格式表示,它们如下所示:
A = 0011 1100
B = 0000 1101

A&B = 0000 1100
A | B = 0011 1101
A^B = 0011 0001
~A = 1100 0011

下表显示了 C++ 支持的位运算符。假设变量 A 的值为 60,变量 B 的值为 13,则:

运算符 描述 实例
& 如果同时存在于两个操作数中,二进制 AND 运算符复制一位到结果中。 (A & B) 将得到 12,即为 0000 1100
^ 如果存在于其中一个操作数中但不同时存在于两个操作数中,二进制异或运算符复制一位到结果中。 (A ^ B) 将得到 49,即为 0011 0001
~ 二进制补码运算符是一元运算符,具有"翻转"位效果,即0变成1,1变成0。 (~A ) 将得到 -61,即为 1100 0011,一个有符号二进制数的补码形式。
<< 二进制左移运算符。左操作数的值向左移动右操作数指定的位数。 A << 2 将得到 240,即为 1111 0000
>> 二进制右移运算符。左操作数的值向右移动右操作数指定的位数。 A >> 2 将得到 15,即为 0000 1111

(好不容易会整表格又卡“|”这了,就写在这把实在整不了)
| 如果存在于任一操作数中,二进制 OR 运算符复制一位到结果中。 (A | B) 将得到 61,即为 0011 1101
当然忘不掉题目的ac代码啦:

#include<iostream>
#include<cstdio>
using namespace std;
const int MAXN = 510;
int map[MAXN][MAXN] = {0};
int N,M;
int main()
{

    scanf("%d%d",&N,&M);
    int ans = 0;
    for(int i=1; i<=N; i++)
    {
        for(int j=1; j<=M; j++)
        {
            scanf("%d",&map[i][j]);
        }
        ans^=map[i][1];
    }
    if(ans>0)
    {
        cout<<"TAK\n";
        for(int i=1; i<=N; i++)
        {
            if (i==1)
                cout<<1;
            else
                cout<<" "<<1;
        }
        return 0;
    }
    else
    {
        for(int i=1; i<=N; i++)
        {
            for(int j=2; j<=M; j++)
            {
                int ii,ij;
                if(map[i][j]!=map[i][1])
                {
                    ii = i;
                    ij = j;
                    cout<<"TAK\n";
                    for(int i=1; i<=N; i++)
                    {
                        if(i==ii)
                            cout<<ij;
                        else
                            cout<<1;
                        if(i<N)
                            cout<<" ";
                    }
                    cout<<" ";
                    return 0;
                }
            }
        }
    }
    cout<<"NIE"<<endl;
    return 0;
}

嘿嘿,电脑前面的我要转战另一份学习总结啦,去那边整理学习资料,溜了溜了。

Gods determine what you‘re going to be.
上一篇:【转】TabHost详解


下一篇:购物车案例(JavaScript动态效果)