A. Appleman and Easy Task
Toastman came up with a very easy task. He gives it to Appleman, but Appleman doesn't know how to solve it. Can you help him?
Given a n × n checkerboard. Each cell of the board has either character 'x', or character 'o'. Is it true that each cell of the board has even number of adjacent cells with 'o'? Two cells of the board are adjacent if they share a side.
The first line contains an integer n (1 ≤ n ≤ 100). Then n lines follow containing the description of the checkerboard. Each of them contains n characters (either 'x' or 'o') without spaces.
Print "YES" or "NO" (without the quotes) depending on the answer to the problem.
3 xxo xox oxx
YES
4 xxxo xoxo oxox xxxx
NO
重在审题!不能只看数据。
题意:每个点(共n*n个都算),如果有某个点周围‘o’的个数不是偶数就printf(“NO\n”);
否则输出YES,直接暴力!
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 1000
using namespace std;
int to[4][2]={1,0,0,1,-1,0,0,-1};
char s[N][N];
int main()
{
int i,j,k,n;
int x,y,ans;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%s",s[i]+1);
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
ans=0;
for(k=0;k<4;k++)
{
x=i+to[k][0];
y=j+to[k][1];
if(s[x][y]=='o')ans++;
}
if(ans&1)
{
printf("NO\n");
return 0;
}
}
}
printf("YES\n");
return 0;
}
B. Appleman and Card Gametime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAppleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman's card i you should calculate how much Toastman's cards have the letter equal to letter on ith, then sum up all these quantities, such a number of coins Appleman should give to Toastman.
Given the description of Appleman's cards. What is the maximum number of coins Toastman can get?
InputThe first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n uppercase letters without spaces — the i-th letter describes the i-th card of the Appleman.
OutputPrint a single integer – the answer to the problem.
Sample test(s)Input15 10 DZFDFZDFDDDDDDFOutput82Input6 4 YJSNPIOutput4NoteIn the first test example Toastman can choose nine cards with letter D and one additional card with any letter. For each card with D he will get 9 coins and for the additional card he will get 1 coin.
题意:输入n,m,n个大写字母,取m个,枚举每个取到的字母,看取了多少个,就ans+=个数。
比如:AAAABBBCCD ans=4+4+4+4+3+3+3+2+2+1=30;
题解:数一下每个字母有多少个,然后排序,贪心解决。
<pre class="cpp" name="code">#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #define N 200000 using namespace std; long long n,m,num[50],ans; char t[N]; int main() { long long i,j,k; cin>>n>>m; cin>>t+1; for(i=1;i<=n;i++) { num[t[i]-'A'+1]++; } sort(num+1,num+27); for(i=26;i;i--) { if(m<=num[i]) { ans+=(m*m); break; } ans+=(num[i]*num[i]); m-=num[i]; } cout<<ans; return 0; }
<div class="header"><div class="title">C. Appleman and Toastman</div><div class="time-limit"><div class="property-title">time limit per test</div>2 seconds</div><div class="memory-limit"><div class="property-title">memory limit per test</div>256 megabytes</div><div class="input-file"><div class="property-title">input</div>standard input</div><div class="output-file"><div class="property-title">output</div>standard output</div></div><div><p>Appleman and Toastman play a game. Initially Appleman gives one group of <span class="tex-span"><em>n</em></span> numbers to the Toastman, then they start to complete the following tasks:</p><ul><li> Each time Toastman gets a group of numbers, he sums up all the numbers and adds this sum to the score. Then he gives the group to the Appleman. </li><li> Each time Appleman gets a group consisting of a single number, he throws this group out. Each time Appleman gets a group consisting of more than one number, he splits the group into two non-empty groups (he can do it in any way) and gives each of them to Toastman. </li></ul><p>After guys complete all the tasks they look at the score value. What is the maximum possible value of score they can get?</p></div><div class="input-specification"><div class="section-title">Input</div><p>The first line contains a single integer <span class="tex-span"><em>n</em></span> (<span class="tex-span">1 ≤ <em>n</em> ≤ 3·10<sup class="upper-index"><span style="font-size:12px;">5</span></sup></span>). The second line contains <span class="tex-span"><em>n</em></span> integers <span class="tex-span"><em>a</em><sub class="lower-index"><span style="font-size:12px;">1</span></sub></span>, <span class="tex-span"><em>a</em><sub class="lower-index"><span style="font-size:12px;">2</span></sub></span>, ..., <span class="tex-span"><em>a</em><sub class="lower-index"><em><span style="font-size:12px;">n</span></em></sub></span> (<span class="tex-span">1 ≤ <em>a</em><sub class="lower-index"><em><span style="font-size:12px;">i</span></em></sub> ≤ 10<sup class="upper-index"><span style="font-size:12px;">6</span></sup></span>) — the initial group that is given to Toastman.</p></div><div class="output-specification"><div class="section-title">Output</div><p>Print a single integer — the largest possible score.</p></div><div class="sample-tests"><div class="section-title">Sample test(s)</div><div class="sample-test"><div class="input"><div class="title">Input</div><pre>3
3 1 5
26
1 10
10
Consider the following situation in the first example. Initially Toastman gets group [3, 1, 5] and adds 9 to the score, then he give the group to Appleman. Appleman splits group [3, 1, 5] into two groups: [3, 5] and [1]. Both of them should be given to Toastman. When Toastman receives group [1], he adds 1 to score and gives the group to Appleman (he will throw it out). When Toastman receives group [3, 5], he adds 8 to the score and gives the group to Appleman. Appleman splits [3, 5] in the only possible way: [5] and [3]. Then he gives both groups to Toastman. When Toastman receives [5], he adds 5 to the score and gives the group to Appleman (he will throws it out). When Toastman receives [3], he adds 3 to the score and gives the group to Appleman (he will throws it out). Finally Toastman have added 9 + 1 + 8 + 5 + 3 = 26 to the score. This is the optimal sequence of actions.
题意:给一个集合n个数,每轮随意将它们拆成两个非空集合加和,然后元素个数大于1的集合将延续到下一轮,若干轮后将无法延续,输出加和的权值,
比如样例测点 3 1 5:第一轮{1,3,5}ans=9;第二轮{1},{3,5}ans=18;第三轮{3},{5}ans=26;第四轮停止。
题解:排个序,然后一个个从集合里往外分,可能不正确,但是PreAC了。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define N 500000
using namespace std;
long long n,a[N],ans;
int main()
{
long long i,j,k;
cin>>n;
for(i=1;i<=n;i++)
{
cin>>a[i];
}
sort(a+1,a+n+1);
for(i=1;i<=n;i++)
{
ans+=(a[i]*(i+1));
}
ans-=a[n];
cout<<ans;
return 0;
}
还有一个防hack版:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define N 500000
#define MAIMENG int
#define YIRANMAI long long
#define sjfkkkkk cout
using namespace std;
YIRANMAI n,a[N],ans;MAIMENG main(){YIRANMAI isodooooo,j,k,dsljdskf,dsfdfdsf,sdfsd,sf,sd,dg,fdg,df,gd,f,er,fd,sdf;cin>>n;for(isodooooo=1;isodooooo<=n;isodooooo++){cin>>a[isodooooo];}sort(a+1,a+n+1);for(isodooooo=1;isodooooo<=n;isodooooo++){ans+=(a[isodooooo]*(isodooooo+1));}ans-=a[n];sjfkkkkk<<ans;return 0;}
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 ≤ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
The first line contains an integer n (2 ≤ n ≤ 105) — the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 ≤ pi ≤ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output a single integer — the number of ways to split the tree modulo 1000000007 (109 + 7).
3 0 0 0 1 1
2
6 0 1 1 0 4 1 1 0 0 1 0
1
10 0 1 2 1 4 4 4 0 8 0 0 0 1 0 1 1 0 0 1
27
题意:有一个黑白树,然后把它分成k块,使得每块有且只有一个黑节点,问有多少种方案(mod 1000000007)。
输入:第一行n节点数(0~n-1),第二行第i个数Pi指第i个节点和第Pi个节点连一条边,比如Input1中的0 0 分别指1到0连一条边,2到0连一条边。
分析(刚开始翻墙软件挂了,然后QQ电脑管家又一直警告我codeforces被大量用户举报,浪费了很多时间导致没时间写了)
: 树形DP,递归处理每个节点,初步设想是每个节点=∑(每个黑子点的dp权值*其到当前节点的长度),
当然然后还有一些东西需要处理,没深想了,哪位神犇想了的话给个回复(评论)。
代码没写。
然后我再把E的题目贴一下吧,虽然没看。
E. Appleman and a Sheet of Papertime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAppleman has a very big sheet of paper. This sheet has a form of rectangle with dimensions 1 × n. Your task is help Appleman with folding of such a sheet. Actually, you need to perform q queries. Each query will have one of the following types:
- Fold the sheet of paper at position pi. After this query the leftmost part of the paper with dimensions 1 × pi must be above the rightmost part of the paper with dimensions 1 × ([current width of sheet] - pi).
- Count what is the total width of the paper pieces, if we will make two described later cuts and consider only the pieces between the cuts. We will make one cut at distance li from the left border of the current sheet of paper and the other at distance ri from the left border of the current sheet of paper.
Please look at the explanation of the first test example for better understanding of the problem.
InputThe first line contains two integers: n and q (1 ≤ n ≤ 105; 1 ≤ q ≤ 105) — the width of the paper and the number of queries.
Each of the following q lines contains one of the described queries in the following format:
- "1 pi" (1 ≤ pi < [current width of sheet]) — the first type query.
- "2 li ri" (0 ≤ li < ri ≤ [current width of sheet]) — the second type query.
OutputFor each query of the second type, output the answer.
Sample test(s)Input7 4 1 3 1 2 2 0 1 2 1 2Output4 3Input10 9 2 2 9 1 1 2 0 1 1 8 2 0 8 1 2 2 1 3 1 4 2 2 4Output7 2 10 4 5NoteThe pictures below show the shapes of the paper during the queries of the first example:
After the first fold operation the sheet has width equal to 4, after the second one the width of the sheet equals to 2.