可化繁为简的思维题
1.Not Sitting
描述:
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a
As the class president, Tina has access to exactly
- First, Tina chooses exactly
k ">kk seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. - After Tina has painted
k ">kk seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. - After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul.
Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!
Now, Rahul wonders for
输入:
The input consists of multiple test cases. The first line contains an integer
The first line of each test case contains two integers
The sum of
输出:
For each test case, output
样例输入:
2 4 3 1 2
样例输出:
3 3 4 4 4 4 4 4 5 5 5 5 1 1
大意就是:在以个n*m的教室内,每一个桌子是一个単位格,有两个人(R,T)选座位,R想坐在离T更可能近的地方,T则想坐在离R更远的地方,T有0--n*m-1瓶油漆,如果被涂有油漆的位子,R是不能坐的,输出
T使用0--n*m-1瓶油漆R与T能够坐到最近距离;
一开始我是顺着这道题的思路去一个一个画图推理,弄了一个小时。关键是最后代码写出来还是错的;
其实后来发现没这么麻烦;
T想要离R更远,所以T只能选择4个角落
R想要与T离得更近,所以R最优的选择是选中间
因为如果R选某个靠边上的话,T就可以选到相对R的地方,会离得更远
所以为了防止R与T离得更近, T会优先去用油漆涂中间的地方。既然双方均用最优解法,我只要枚举每个格子离4个顶点最远的距离,然后再从小到大,排列一下就是答案了
#include#include #include using namespace std; int maxm[100005]; int main() { int n; cin>>n; while (n--) { int r,c,cnt=0; cin>>r>>c; memset(maxm,0,sizeof(maxm)); for (int i=1;i<=r;i++) { for (int j=1;j<=c;j++) { int d1=abs(1-i)+abs(1-j); int d2=abs(1-i)+abs(c-j); int d3=abs(r-i)+abs(1-j); int d4=abs(r-i)+abs(c-j); maxm[cnt++]=max( max(d1,d2), max(d3,d4) ); } } sort(maxm,maxm+cnt); for (int i=0;i<=r*c-1;i++) { cout< " "; } cout<<endl; } return 0; }
GCD Arrays
2000ms 262144K
描述:
Consider the array
Given
- Choose
2 ">22 numbers froma ">aa. - Permanently remove one occurrence of each of them from the array.
- Insert their product back into
a ">aa.
输入:
The first line of the input contains a single integer
The input for each test case consists of a single line containing
输出:
For each test case, print "YES" if it is possible to have the GCD of the corresponding array greater than
样例输入:
9 1 1 0 3 5 1 13 13 0 4 4 0 3 7 4 4 10 3 2 4 0 1 7 3 1 5 3
样例输出:
NO NO YES YES YES YES NO NO YES
注释:
For the first test case,
For the second test case the array is
For the third test case,
For the fourth test case,
这道题看起来很复杂
但其实首先知道唯一分解定理:算术基本定理可表述为:任何一个大于1的自然数 N,如果N不为质数,那么N可以唯一分解成有限个质数的乘积N=P1a1P2a2P3a3......Pnan,这里P1
则我们就知道,每一个偶数它都有一个2作为质因子,可以说2是在一个集合中最多的质因子,所以我们就可以选择让偶数与奇数相乘。这样就可以以后的集合中都有2这个质因子
可能你会在想有没有更好的方法,就是不是2作为质因子,其实是不可能的,假设二不作为最后集合的质因子,假设K是,但想一想K绝对没有2在原来集合中那么多,他花的次数一定会比偶数与奇数相乘的次数多。
#includeusing namespace std; int main() { int n; cin >> n; while (n--) { int l, r, k; cin >> l >> r >> k; int len = r - l + 1; int num = len >> 1; if ( (len&1==1) && (l&1==1) ) num++; if ( len==1) { if (l==1) cout<<"NO"< =num) cout<<"YES"<