ACM 第七天
水题
B - Minimum’s Revenge
Mr. Frog is wondering about the total weight of the minimum spanning tree. Can you help him?
For each test case, the first line contains only one integer n (2≤n≤109), indicating the number of vertices.
OutputFor each test case, output one line "Case #x:y",where x is
the case number (starting from 1) and y is the total weight of the
minimum spanning tree.
Sample Input
2
2
3
Sample Output
Case #1: 2
Case #2: 5
Hint
In the second sample, the graph contains 3 edges which are (1, 2, 2), (1, 3, 3) and (2, 3, 6). Thus the answer is 5.
#include<stdio.h>
int main()
{
int t;
long long ans;
scanf("%d",&t);
int v=;
while(t--)
{
long long n;
scanf("%lld",&n); ans=(n+)*(n-)/; printf("Case #%d: %lld\n",++v,ans); }
return ;
}
C - Mr. Frog’s Problem
One day, you, a clever boy, feel bored in your math class, and then fall asleep without your control. In your dream, you meet Mr. Frog, an elder man. He has a problem for you.
He gives you two positive integers A and B, and your task is to find all pairs of integers (C, D), such that A≤C≤B,A≤D≤B and AB+BA≤CD+DC
Inputfirst line contains only one integer T (T≤125), which indicates the number of test cases. Each test case contains two integers A and B (1≤A≤B≤1018).OutputFor each test case, first output one line "Case #x:", where x is the case number (starting from 1).
Then in a new line, print an integer s indicating the number of pairs you find.
In each of the following s lines, print a pair of integers C and D.
pairs should be sorted by C, and then by D in ascending order.
Sample Input
2
10 10
9 27
Sample Output
Case #1:
1
10 10
Case #2:
2
9 27
27 9
#include<stdio.h>
int main()
{
int t;
scanf("%d",&t);
long long a,b;
long long cas=;
while(t--)
{
scanf("%lld%lld",&a,&b);
printf("Case #%lld:\n",cas++);
if(a==b)
{
printf("1\n",a,b);
printf("%lld %lld\n",a,b);
}
else
{
printf("2\n",a,b);
printf("%lld %lld\n",a,b);
printf("%lld %lld\n",b,a);
} }
}
D - Mr. Frog’s Game
In this game, if you can draw at most three horizontal or vertical
head-and-tail-connected lines over the empty grids(the lines can be out
of the whole board) to connect two non-empty grids with the same symbol
or the two non-empty grids with the same symbol are adjacent, then you
can change these two grids into empty and get several more seconds to
continue the game.
Now, Mr. Frog starts a new game (that means there is no empty grid
in the board). If there are no pair of grids that can be removed
together,Mr. Frog will say ”I’m angry” and criticize you.
Mr. Frog is battle-scarred and has seen many things, so he can
check the board in a very short time, maybe one second. As a Hong Kong
Journalist, what you should do is to check the board more quickly than
him, and then you can get out of the room before Mr. Frog being angry.
For each test case, the first line contains two integers n and m (1≤n,m≤30).
In the next n lines, each line contains m integers, j-th number in
the i-th line means the symbol on the grid(the same number means the
same symbol on the grid).
OutputFor each test case, there should be one line in the output.
You should output “Case #x: y”,where x is the case number(starting
from 1), and y is a string representing the answer of the question. If
there are at least one pair of grids that can be removed together, the y
is “Yes”(without quote), else y is “No”.Sample Input
2
3 3
1 2 1
2 1 2
1 2 1
3 3
1 2 3
2 1 2
3 2 1
Sample Output
Case #1: Yes
Case #2: No
Hint
first sample can be explained as below.
#include<stdio.h>
#include<string.h>
using namespace std;
int n,m,g[][];
int f[][]= {{,},{,},{-,},{,-}};
bool dfs()
{
for(int i=; i<=n; i++)
for(int j=i+; j<=n; j++)
if(g[i][]==g[j][])
return true;
for(int i=; i<=n; i++)
for(int j=i+; j<=n; j++)
if(g[i][m]==g[j][m])
return true;
for(int i=; i<=m; i++)
for(int j=i+; j<=m; j++)
if(g[][i]==g[][j])
return true;
for(int i=; i<=m; i++)
for(int j=i+; j<=m; j++)
if(g[n][i]==g[n][j])
return true;
for(int i=; i<n; i++)
{
for(int j=; j<m; j++)
{
for(int k=; k<; k++)
{
int x=f[k][]+i,y=f[k][]+j;
if(g[i][j]==g[x][y])
return true;
}
}
}
return false;
}
int main()
{
int T;
scanf("%d",&T);
int t=;
while(T--)
//for(int t=1; t<=T; t++)
{
scanf("%d %d",&n,&m);
for(int i=; i<=n; i++)
for(int j=; j<=m; j++)
scanf("%d",&g[i][j]);
printf("Case #%d: ",t++);
if(dfs())
printf("Yes\n");
else
printf("No\n");
}
return ;
}
G - Left-handers, Right-handers and Ambidexters
You are at a water bowling training. There are l people who play with their left hand, r people, who play with their right hand, and a ambidexters, who can play with left or right hand.
The coach decided to form a team of even number of players, exactly half of the players should play with their right hand, and exactly half of the players should play with their left hand. One player should use only on of his hands.
Ambidexters play as well with their right hand as with their left hand. In the team, an ambidexter can play with their left hand, or with their right hand.
Please find the maximum possible size of the team, where equal number of players use their left and right hands, respectively.
The only line contains three integers l, r and a (0 ≤ l, r, a ≤ 100) — the number of left-handers, the number of right-handers and the number of ambidexters at the training.
Output
Print a single even integer — the maximum number of
players in the team. It is possible that the team can only have zero
number of players.
Examples
1 4 2
6
5 5 5
14
0 2 0
0
Note
In the first example you can form a team of 6 players. You should take the only left-hander and two ambidexters to play with left hand, and three right-handers to play with right hand. The only person left can't be taken into the team.
In the second example you can form a team of 14 people. You have to take all five left-handers, all five right-handers, two ambidexters to play with left hand and two ambidexters to play with right hand.
#include<stdio.h>
int main()
{
int l,r,m;
scanf("%d%d%d",&l,&r,&m);
if(l==r)
{
if(m%==) printf("%d\n",l+r+m);
else printf("%d\n",l+r+m-);
}
else
{
if(l>r)
{
int temp=l;
l=r;
r=temp;
}
if(m<=r-l)
{
printf("%d\n",(m+l)*);
}
else
{
m=m-(r-l);
if(m&)
printf("%d\n",*r+m-);
else printf("%d\n",*r+m);
}
return ;
} }
I - Zebras
Oleg writes down the history of the days he lived. For each day he decides if it was good or bad. Oleg calls a non-empty sequence of days a zebra, if it starts with a bad day, ends with a bad day, and good and bad days are alternating in it. Let us denote bad days as 0 and good days as 1. Then, for example, sequences of days 0, 010, 01010 are zebras, while sequences 1, 0110, 0101 are not.
Oleg tells you the story of days he lived in chronological order in form of string consisting of 0 and 1. Now you are interested if it is possible to divide Oleg's life history into several subsequences, each of which is a zebra, and the way it can be done. Each day must belong to exactly one of the subsequences. For each of the subsequences, days forming it must be ordered chronologically. Note that subsequence does not have to be a group of consecutive days.
In the only line of input data there is a non-empty string s consisting of characters 0 and 1, which describes the history of Oleg's life. Its length (denoted as |s|) does not exceed 200 000 characters.
Output
If there is a way to divide history into zebra subsequences, in the first line of output you should print an integer k (1 ≤ k ≤ |s|), the resulting number of subsequences. In the i-th of following k lines first print the integer li (1 ≤ li ≤ |s|), which is the length of the i-th subsequence, and then li
indices of days forming the subsequence. Indices must follow in
ascending order. Days are numbered starting from 1. Each index from 1 to n must belong to exactly one subsequence. If there is no way to divide day history into zebra subsequences, print -1.
Subsequences may be printed in any order. If there are several
solutions, you may print any of them. You do not have to minimize nor
maximize the value of k.
Examples
0010100
3
3 1 3 4
3 2 5 6
1 7
111
-1
#include<stdio.h>
#include<vector>
#include<string.h>
using namespace std; #define N 200100
int a[N];
char str[N];
int cnt;
int n,k;
vector <int> ans[N];
int main()
{
scanf("%s",str);
n=strlen(str);
for(int i=;i<n;i++)
{
a[i+]=str[i]-'';
}
cnt=;
for(int i=;i<=n;i++)
{
if(a[i]==) ans[cnt++].push_back(i);
else if(a[i]==)
{
if(cnt==)
{printf("-1\n");
return ;}
ans[--cnt].push_back(i);
}
k=max(k,cnt);
}
if(k!=cnt)printf("-1\n");
else
{
printf("%d\n",cnt);
for(int i=;i<cnt;i++)
{
printf("%d",ans[i].size());
for(int j=;j<ans[i].size();j++)
{
printf(" %d",ans[i][j]);
}
printf("\n");
}
}
return ;
}
多校练习赛3
Problem D. Euler Function
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1340 Accepted Submission(s): 1008
For example, φ(9)=6 because 1,2,4,5,7 and 8 are coprime with 9. As another example, φ(1)=1 since for n=1 the only integer in the range from 1 to n is 1 itself, and gcd(1,1)=1.
A
composite number is a positive integer that can be formed by
multiplying together two smaller positive integers. Equivalently, it is a
positive integer that has at least one divisor other than 1 and itself. So obviously 1 and all prime numbers are not composite number.
In this problem, given integer k, your task is to find the k-th smallest positive integer n, that φ(n) is a composite number.
In each test case, there is only one integer k(1≤k≤109).
1
2
7
#include<stdio.h>
int main()
{
int t;
long long k;
scanf("%d",&t);
while(t--)
{
scanf("%lld",&k);
if(k==) printf("5\n");
else
{
printf("%lld\n",k+);
}
}
return ;
}
Problem F. Grab The Tree
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1581 Accepted Submission(s): 762
In
this game, Little Q needs to grab some vertices on the tree. He can
select any number of vertices to grab, but he is not allowed to grab
both vertices that are adjacent on the tree. That is, if there is an
edge between x and y, he can't grab both x and y.
After Q's move, Little T will grab all of the rest vertices. So when
the game finishes, every vertex will be occupied by either Q or T.
The
final score of each player is the bitwise XOR sum of his choosen
vertices' value. The one who has the higher score will win the game. It
is also possible for the game to end in a draw. Assume they all will
play optimally, please write a program to predict the result.
In each test case, there is one integer n(1≤n≤100000) in the first line, denoting the number of vertices.
In the next line, there are n integers w1,w2,...,wn(1≤wi≤109), denoting the value of each vertex.
For the next n−1 lines, each line contains two integers u and v, denoting a bidirectional edge between vertex u and v.
each test case, print a single line containing a word, denoting the
result. If Q wins, please print Q. If T wins, please print T. And if the
game ends in a draw, please print D.
3
2 2 2
1 2
1 3
#include<stdio.h>
int main()
{
int t,n,a,b;
int aq[];
scanf("%d",&t); while(t--)
{
int ans=;
scanf("%d",&n); for(int i=; i<n; i++)
{
scanf("%d",&aq[i]);
ans=ans^aq[i]; }
for(int i=; i<n-; i++)
{
scanf("%d %d",&a,&b);
}
if(ans==)
printf("D\n");
else printf("Q\n"); }
return ;
}
预定义
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <string>
#include <iostream> using namespace std;
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define pd(n) printf("%d\n", (n))
#define pdd(n,m) printf("%d %d", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n,m) printf("%lld %lld\n", n, m)
#define sld(n) scanf("%lld",&n)
#define sldd(n,m) scanf("%lld%lld",&n,&m)
#define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k)
#define sf(n) scanf("%lf",&n)
#define sff(n,m) scanf("%lf%lf",&n,&m)
#define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k)
#define ss(str) scanf("%s",str)
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define mm(a,n) memset(a, n, sizeof(a))
#define debug(x) cout<<#x<<": "<<x<<endl
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll mod = ;
const double eps = 1e-;
const int inf = 0x3f3f3f3f; //head
Hacker Zhorik wants to decipher two secret messages he intercepted yesterday. Yeah message is a sequence of encrypted blocks, each of them consists of several bytes of information.
Zhorik knows that each of the messages is an archive containing one or more files. Zhorik knows how each of these archives was transferred through the network: if an archive consists of k files of sizes l1, l2, ..., lk bytes, then the i-th file is split to one or more blocks bi, 1, bi, 2, ..., bi, mi (here the total length of the blocks bi, 1 + bi, 2 + ... + bi, mi is equal to the length of the file li), and after that all blocks are transferred through the network, maintaining the order of files in the archive.
Zhorik thinks that the two messages contain the same archive, because their total lengths are equal. However, each file can be split in blocks in different ways in the two messages.
You are given the lengths of blocks in each of the two messages. Help Zhorik to determine what is the maximum number of files could be in the archive, if the Zhorik's assumption is correct.
The first line contains two integers n, m (1 ≤ n, m ≤ 105) — the number of blocks in the first and in the second messages.
The second line contains n integers x1, x2, ..., xn (1 ≤ xi ≤ 106) — the length of the blocks that form the first message.
The third line contains m integers y1, y2, ..., ym (1 ≤ yi ≤ 106) — the length of the blocks that form the second message.
It is guaranteed that x1 + ... + xn = y1 + ... + ym. Also, it is guaranteed that x1 + ... + xn ≤ 106.
Output
Print the maximum number of files the intercepted array could consist of.
Examples
7 6
2 5 3 1 11 4 4
7 8 2 4 1 8
3
3 3
1 10 100
1 100 10
2
1 4
4
1 1 1 1
1
Note
In the first example the maximum number of files in the archive is 3. For example, it is possible that in the archive are three files of sizes 2 + 5 = 7, 15 = 3 + 1 + 11 = 8 + 2 + 4 + 1 and 4 + 4 = 8.
In the second example it is possible that the archive contains two files of sizes 1 and 110 = 10 + 100 = 100 + 10. Note that the order of files is kept while transferring archives through the network, so we can't say that there are three files of sizes 1, 10 and 100.
In the third example the only possibility is that the archive contains a single file of size 4.
#include<stdio.h>
int a[];
int b[];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=; i<=n; i++)
{
scanf("%d",&a[i]);
}
for(int i=; i<=m; i++)
{
scanf("%d",&b[i]);
}
int i=,j=;
int sum=;
int sum1=a[];
int sum2=b[];
while(i<=n &&j<=m)
{
if(sum1>sum2)
{
j++;
sum2+=b[j];
}
else if(sum1<sum2)
{
i++;
sum1+=a[i];
}
else
{
sum++;
i++;
j++;
if(i<=n &&j<=m)
{
sum1=a[i];
sum2=b[j];
} } }
printf("%d\n",sum);
}
ACM 第七天的更多相关文章
- 【优质blog、网址】置顶
一.大公司等技术blog: blog1: http://blog.csdn.net/mfcing/article/details/51577173 blog2: http://blog.csdn. ...
- 山东省第七届ACM省赛------Memory Leak
Memory Leak Time Limit: 2000MS Memory limit: 131072K 题目描述 Memory Leak is a well-known kind of bug in ...
- 山东省第七届ACM省赛------Reversed Words
Reversed Words Time Limit: 2000MS Memory limit: 131072K 题目描述 Some aliens are learning English. They ...
- 山东省第七届ACM省赛------Triple Nim
Triple Nim Time Limit: 2000MS Memory limit: 65536K 题目描述 Alice and Bob are always playing all kinds o ...
- 山东省第七届ACM省赛------The Binding of Isaac
The Binding of Isaac Time Limit: 2000MS Memory limit: 65536K 题目描述 Ok, now I will introduce this game ...
- 山东省第七届ACM省赛------Fibonacci
Fibonacci Time Limit: 2000MS Memory limit: 131072K 题目描述 Fibonacci numbers are well-known as follow: ...
- 山东省第七届ACM省赛------Julyed
Julyed Time Limit: 2000MS Memory limit: 65536K 题目描述 Julyed is preparing for her CET-6. She has N wor ...
- ZZUOJ-1195-OS Job Scheduling(郑州大学第七届ACM大学生程序设计竞赛E题)
1195: OS Job Scheduling Time Limit: 2 Sec Memory Limit: 128 MB Submit: 106 Solved: 35 [id=1195&quo ...
- 第七届山东省ACM省赛
激动人心的省赛终于结束了…平静下来再回头看真的感觉一波三折…先是赛前毫无预兆的查出突发性耳聋…伴随而来的就是左耳听力下降.轻微耳鸣.极个别情况下的头晕…不过这都还好,毕竟药物可以恢复…热身赛只过了一道 ...
随机推荐
- MVC Controller 基类 BaseController 中的 Request
今天修复mvc中的一个bug,需求是每个页面要获取当前URL链接中 host首是否正确,我把获取url的方法写到了Controller的基类BaseController(BaseController继 ...
- PHP获取当月天数,获取当月的每天的开始和结束的时间戳,获取当月每号
由于经常要写导单和数据分析功能,所以要获取什么时间的数据,想什么当天,周,年,月之类的时间格式都很好获取.我今天在这里为大家提供的是当月每天的开始和结束的时间格式. 希望能帮到大家!!! # 获取当月 ...
- angular2配置使用ng2-bootstrap
第一步,安装.进入项目目录 npm install ng2-bootstrap bootstrap --save 第二步,angular-cli 配置 ng2-bootstrap src/.ang ...
- 大数据学习--day06(Eclipse、数组)
Eclipse.数组 Eclipse 的基本设置 调节控制条字体大小. Window -> Preferences -> General -> Appearance -> ...
- Zookeeper -- 关于Zookeeper
Zookeeper是什么? 分布式协调框架 Zookeeper中文件呈树形结构,树形结构下包含多个节点,称为Znode:zk中节点存储数据不超过1M,指得是Znode中存储数据不超过1M Zookee ...
- 树莓派安装samba
(1) sudo apt-get install samba samba-common (2)mkdir /home/lin/share #(文件路径自己添加) (3)sudo chmod 777 / ...
- 20154327 EXP8 Web基础
基础问题回答 (1)什么是表单? 表单:表单在网页中主要负责数据采集功能.一个表单有三个基本组成部分: 表单标签:这里面包含了处理表单数据所用CGI程序的URL以及数据提交到服务器的方法. 表单域:包 ...
- CF 570 D. Tree Requests
D. Tree Requests http://codeforces.com/problemset/problem/570/D 题意: 一个以1为根的树,每个点上有一个字母(a-z),每次询问一个子树 ...
- ORB-SLAM(十)LoopClosing Sim3求解
主要参考这篇论文 Horn B K P. Closed-form solution of absolute orientation using unit quaternions[J]. JOSA A, ...
- MongoDB-Ubuntu环境下安装
1.在官网下载安装包,下载后为 mongodb-linux-x86_64-ubuntu1604-3.4.6.tgz 解压:tar -zxvf mongodb-linux-x86_64-ubuntu16 ...