Sicily2012每周一赛第14场题解
来源:互联网 发布:数据挖掘 编辑:程序博客网 时间:2024/06/05 02:41
比赛链接:
http://soj.me/contest_detail.php?cid=775
Problem1000
It may feel warm now, but in a few months, the city will be full of snow. Luckily, many of the buildings on campus are connected by bridges and tunnels, so you do not need to go outside very much. The network of buildings can be confusing, and it is hard to know the best way to get from one building to another. A computer program could help.
The first line of input contains three integers 0 < n <= 4000, 0 < m <= 40000,0 < p <= 30, the number of buildings on campus, the number of (indoor or outdoor) paths between the buildings, and the number of trips that you would like to make. Buildings are numbered sequentially from 0 ton-1. Each of the nextm lines describes a path between buildings with three integers and a letter. The first two integers specify the two buildings connected by the path. The path can be taken in either direction. The third integer specifies the number of seconds required to take the path from one building to the other. The number of seconds is at least 0 and at most one million. Finally, the letter isI if the path is indoors, orO if the path is outdoors. Each of the nextp lines describes a trip from one building to another using two integers, the numbers of the two buildings.
For each trip, find the optimal route between the specified two buildings. The optimal route minimizes the amount of time spent outside. Among routes that require spending the same amount of time outside, the optimal route minimizes the total time spent. For each trip, output a single line containing two integers, the time spent outside and the total time spent on the optimal route. If there is no route connecting the two specified buildings, output instead a line containing the wordIMPOSSIBLE.
2 1 10 1 30 I0 1
0 30
Problem Source: 2012年每周一赛第十四场
MYCode:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
#define MAX 4010
#define inf 200000000
struct edge
{
int v;
int w;
bool flag;
int next;
}E[2*40010];
struct node
{
int id;
int dt;
int tot;
bool operator<(node p)const//note
{
if(dt!=p.dt)
return dt>p.dt;
return tot>p.tot;
}
};
priority_queue<node> q;
int head[MAX];
bool vis[MAX];
int dist[MAX];
int total[MAX];
//int ans[MAX][MAX][2];
int num;
node bg,ed;
int n;
void init()
{
memset(head,-1,sizeof(head));
num=0;
//mesmet(ans,-1,sizeof(-1));
}
void add(int s,int t,int w,bool flag)
{
E[num].v=t;
E[num].w=w;
E[num].flag=flag;
E[num].next=head[s];
head[s]=num++;
}
void dijkstra()
{
memset(vis,0,sizeof(vis));
int i;
for(i=0;i<n;i++)
{
dist[i]=inf;
total[i]=inf;
}
dist[bg.id]=0;
total[bg.id]=0;
bg.dt=0;
bg.tot=0;
q.push(bg);
while(!q.empty())
{
node p=q.top();
q.pop();
int id=p.id;
if(vis[id])
continue;
vis[id]=true;
for(i=head[id];i!=-1;i=E[i].next)
{
int v=E[i].v;
if((dist[v]>dist[id]+(E[i].flag==1?E[i].w:0))
||(dist[v]==dist[id]+(E[i].flag==1?E[i].w:0)
&&total[v]>total[id]+E[i].w))
{
dist[v]=dist[id]+(E[i].flag==1?E[i].w:0);
total[v]=total[id]+E[i].w;
node tp;
tp.id=v;
tp.dt=dist[v];
tp.tot=total[v];
q.push(tp);
}
}
}
}
int main()
{
int m,p;
while(scanf("%d%d%d",&n,&m,&p)!= EOF)
{
init();
int i;
int s,t,w;
char ch;
bool flag;
for(i=1;i<=m;i++)
{
//scanf("%d%d%d%c",&s,&t,&w,&ch);
cin>>s>>t>>w>>ch;
flag= (ch=='I'?0:1);
add(s,t,w,flag);
add(t,s,w,flag);
}
//cout<<"show"<<endl;
for(i=1;i<=p;i++)
{
scanf("%d%d",&s,&t);
bg.id=s;
dijkstra();
printf("%d %d\n",dist[t],total[t]);
}
}
}
//
在最短路的基础上加了一层费用,经典的最短路变形
Joe's landlord has allowed him to paint his room any colour that he wants, even multiple colours. Joe has come up with a very colourful design. Now he needs to buy the paint. Being a struggling student, Joe does not want to waste any money, so he has calculated the exact amount that he needs of each colour down to the microlitre. To his surprise, however, the local paint shop is unwilling to sell him a can of exactly 3.141592 litres of red paint. No, the shop has a set of specific paint can sizes. Joe has no choice but to buy a little bit more paint than he really needs. Still, he would like to minimize the amount of paint wasted. In addition, he does not want to buy more than one can of any given colour.
The first line of input contains two integers 0 < n <= 100000 and 0 < m <= 100000, the number of paint can sizes offered by the paint shop, and the number of colours that Joe needs. Each of the nextn lines contains the size of a can offered by the paint shop, in microlitres. Each can contains no more than 1000 litres. Each of the nextm lines contains the number of microlitres that Joe needs of a given colour. It is guaranteed that for each colour, the paint shop sells a can large enough to satisfy Joe's needs.
Output a single line, the total number of microlitres of paint wasted if Joe buys, for each colour, the smallest can that satisfies his needs.
3 257968
2
Problem Source: 2012年每周一赛第十四场
MYCode:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
#define MAX 100010
int v1[MAX];
int v2[MAX];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
int i;
for(i=0;i<n;i++)
{
scanf("%d",&v1[i]);
}
for(i=0;i<m;i++)
{
scanf("%d",&v2[i]);
}
sort(v1,v1+n);
sort(v2,v2+m);
int j=0;
int sum=0;
for(i=0;i<m;i++)
{
for(;v1[j]<v2[i];j++);
sum+=v1[j]-v2[i];
}
printf("%d\n",sum);
}
}
//
贪心,排序后,为每个数选择第一个大于它的数就可以了
It is a difficult job to schedule all of the courses in a university to satisfy students' choices with a minimum of conflicts. The task is made all the more difficult when some students don't pre-enroll, or pre-enroll multiple times because they forget that they already did it.
The first line of input contains an integer 0 < n <= 100000, the number of student course requests. Each of the nextn lines contains three strings separated by spaces: a student's first and last name, and the course that the student wishes to take. You may assume that each name is a string of at least one and at most 20 upper-case letters, and that a course is a string of at least one and at most 10 upper-case letters and digits. If a student requests a given course more than once, only the first such request should be considered. You may assume that no two students have both their first and last names the same.
For each requested course, output a line containing the course, a space, and the number of students who requested the course. Output the courses sorted in lexicographical order (with digits sorted before letters).
1PINK TIE CS241
CS241 1
Problem Source: 2012年每周一赛第十四场
MYCode:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
#define MAX 1000000
vector<string> mat[MAX];
vector<string> course;
map<string,int> table;
int ans[MAX];
bool cmp(string str1,string str2)
{
return str1<str2;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
//mat.clear();
int i;
for(i=1;i<=n;i++)
{
mat[i].clear();
}
course.clear();
table.clear();
string str1,str2,str3;
string str;
int num=0;
memset(ans,0,sizeof(ans));
for(i=1;i<=n;i++)
{
cin>>str1>>str2>>str3;
str=str1+str2;
if(/*course.find(str3)!=course.end()*/find(course.begin(),course.end(),str3)!=course.end())
{
int id=table[str3];
if(/*mat[id].find(str)==mat[id].end()*/find(mat[id].begin(),mat[id].end(),str)==mat[id].end())
{
mat[id].push_back(str);
}
}
else
{
course.push_back(str3);
table[str3]=num;
mat[num].push_back(str);
num++;
}
}
for(i=0;i<num;i++)
{
ans[i]=mat[i].size();
}
sort(course.begin(),course.end(),cmp);
vector<string>::iterator it;
for(it=course.begin();it!=course.end();it++)
{
cout<<(*it)<<" "<<ans[table[*it]]<<endl;
}
}
}
//
C++ STL的使用.基础要扎实.
You may have encountered IQ tests with inane questions such as the following: find the next number in the sequence 1, 2, 3, __. Obviously the correct answer is 16, since the sequence lists the values f(1), f(2), f(3), f(4), ..., where f(x) = 2x3 - 12x2 + 23x - 12. More generally, given some information about the values of a polynomial, can you find the polynomial? We will restrict our attention to polynomials whose coefficients are all non-negative integers.
The first line of input contains an integer 0 < n <= 10000, the number of polynomials to be identified. Each of the nextn lines contains two integers, the values f(1) and f(f(1)), where f is the polynomial to be found. Each of these values fits within the range of a signed two's complement 32-bit integer.
For each polynomial to be found, output a single line listing its coefficients separated by spaces. Assuming the degree of the polynomial is d, list the d+1 coefficients in descending order of power (i.e. starting with the coefficient of xd and finishing with the coefficient of x0). If the polynomial is the zero polynomial, just output 0. If no polynomial f has the desired values of f(1) and f(f(1)), instead output a line containing the wordIMPOSSIBLE. If multiple polynomials f have the desired values of f(1) and f(f(1)), instead output a line containing the wordAMBIGUOUS.
13 5
1 2
Problem Source: 2012年每周一赛第十四场
没做出来.
可以猜测:如果多项式存在且唯一,必定是一次多项式.
如果在一次多项式范围内无解,可能是IMPOSSIBLE,也可能是AMBIGUOUS.
这时要怎么判断呢?
MYCode(WA)
#include<iostream>
using namespace std;
#include<cstring>
#include<cstdio>
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int k1,k2;
scanf("%d%d",&k1,&k2);
if(k1==0||k2==0)
{
if(k1==0&&k2==0)
printf("0\n");
else
printf("IMPOSSIBLE\n");
continue;
}
if(k1==1)
{
if(k2==1)
{
printf("AMBIGUOUS\n");
}
else
printf("IMPOSSIBLE\n");
}
else
{
int a,b;
if(k1==k2)
{
if(k1!=1)
printf("IMPOSSIBLE\n");
else
printf("AMBIGUOUS\n");
continue;
}
if(k2<k1)//note
{
printf("IMPOSSIBLE\n");
continue;
}
if((k2-k1)%(k1-1)==0)
{
a=(k2-k1)/(k1-1);
b=k1-a;
if(b>=0)
printf("%d %d\n",a,b);
else
printf("AMBIGUOUS\n");
}
else
printf("AMBIGUOUS\n");
}
}
}
//WRONG ANSWER
- Sicily2012每周一赛第14场题解
- 每周训练 题解
- Sicily 2014年每周一赛第一场
- 2015年每周一赛第六场1001. Cow Baseball
- 排位赛第二场E题以及排位赛第三场题解
- 湖南工业大学个人选拔赛第一场 题解
- 校省选赛第一场A题Cinema题解
- 校省选赛第一场C题解Practice
- 校省选赛第一场D题TwoDecks题解
- SCNU省选校赛第二场B题题解
- SCNU省选校赛第二场B题题解
- 异常的校省选赛第三场题解
- 2014 多校第一场官方题解。
- codeforce 259C 最新题解 第二场
- codeforce 259A 最新题解 第二场
- 2015 NJUPT 春训第二场题解报告
- 多校第九场Too Simple题解
- 多校第九场Arithmetic Sequence题解
- 面试题[百度]--二叉排序树中查找和为某一值的所有路径
- devel包的由来
- 迭代器模式——Iterator
- out,ref,params的区别
- BSP_WINCE_ARM_A8_User_Guide
- Sicily2012每周一赛第14场题解
- 黑马程序员_Java Socket 的通信
- 为RichTextBox控件添加行号
- linux 命令杂记
- getBytes()方法详解
- netlink通信机制分析
- 黑马程序员_Java 多线程
- 组合模式——Composition
- 关于不同进制的标志